流量拆分¶
一个 Revision 是应用程序代码和配置的快照。每次更改 Knative 服务的配置时,都会创建一个新的 Revision。在拆分流量时,Knative 会在 Knative 服务的不同 Revision 之间拆分流量。
创建新的 Revision¶
将环境变量 TARGET 从 TARGET=World 更新为向 "Knative" 打招呼。
通过运行以下命令部署更新后的 Knative Service 版本
kn service update hello \
--env TARGET=Knative
与之前一样,kn 会向 CLI 打印一些有用的信息。
预期输出
Service 'hello' created to latest revision 'hello-00002' is available at URL:
http://hello.default.${LOADBALANCER_IP}.sslip.io
- 编辑现有的
hello.yaml文件以包含以下内容apiVersion: serving.knative.dev/v1 kind: Service metadata: name: hello spec: template: spec: containers: - image: ghcr.io/knative/helloworld-go:latest ports: - containerPort: 8080 env: - name: TARGET value: "Knative" -
通过运行以下命令部署更新后的 Knative Service 版本
kubectl apply -f hello.yaml预期输出
service.serving.knative.dev/hello configured
由于您正在更新现有的 Knative Service,URL 不会改变,但新的 Revision 具有新名称 hello-00002。
访问新的 Revision¶
要查看更改,请再次在浏览器中访问 Knative Service 或在终端中使用 curl
echo "Accessing URL $(kn service describe hello -o url)"
curl "$(kn service describe hello -o url)"
预期输出
Hello Knative!
查看现有 Revision¶
您可以使用 Knative (kn) 或 kubectl CLI 查看现有 Revision 列表
通过运行以下命令查看 Revision 列表
kn revisions list
预期输出
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-00002 hello 100% 2 30s 3 OK / 4 True
hello-00001 hello 1 5m 3 OK / 4 True
通过运行以下命令查看 Revision 列表
kubectl get revisions
预期输出
NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON ACTUAL REPLICAS DESIRED REPLICAS
hello-00001 hello 1 True 0 0
hello-00002 hello 2 True 0 0
运行 kn 命令时,相关的列是 TRAFFIC。您可以看到 100% 的流量流向最新的 Revision hello-00002,它位于具有最高 GENERATION 的行上。0% 的流量流向 Revision hello-00001。
当您创建 Knative Service 的新 Revision 时,Knative 默认将 100% 的流量导向此最新 Revision。您可以通过指定希望每个 Revision 接收的流量百分比来更改此默认行为。
在 Revision 之间拆分流量¶
在两个 Revision 之间拆分流量
运行命令
kn service update hello \
--traffic hello-00001=50 \
--traffic @latest=50
- 将
traffic部分添加到现有hello.yaml文件的底部apiVersion: serving.knative.dev/v1 kind: Service metadata: name: hello spec: template: spec: containers: - image: ghcr.io/knative/helloworld-go:latest ports: - containerPort: 8080 env: - name: TARGET value: "Knative" traffic: - latestRevision: true percent: 50 - latestRevision: false percent: 50 revisionName: hello-00001 - 运行以下命令应用 YAML
kubectl apply -f hello.yaml
信息
@latest 始终指向“最新”的 Revision,在本例中为 hello-00002。
验证流量拆分¶
要验证流量拆分是否已正确配置,请再次运行以下命令查看 Revision 列表
kn revisions list
预期输出
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-00002 hello 50% 2 10m 3 OK / 4 True
hello-00001 hello 50% 1 36m 3 OK / 4 True
在浏览器中多次访问您的 Knative Service,以查看每个 Revision 正在服务的不同输出。
同样,您可以多次从终端访问服务 URL,以查看流量正在 Revision 之间拆分。
echo "Accessing URL $(kn service describe hello -o url)"
curl "$(kn service describe hello -o url)"
预期输出
Hello Knative!
Hello World!
Hello Knative!
Hello World!