修改Kservice流量策略的方法
kn service update <service-name> --traffic revisionRef=percent
命令行配置示例
创建kservice/hello, 有2个revision: revision-001: hello world, revision-002: hello knative
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
spec:
template:
spec:
containers:
- image: ikubernetes/helloworld-go
ports:
- containerPort: 8080
env:
- name: TARGET
value: "World"
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
spec:
template:
spec:
containers:
- image: ikubernetes/helloworld-go
ports:
- containerPort: 8080
env:
- name: TARGET
value: "knative"
目前有2个revision,默认100%流量会到最新的那个revision
将流量完全发送给hello-00001这个Revision(rollback)
kn service update hello --traffic hello-00001=100
可以发现立即生效,所有的流量立马切换到Hello-00001的Revision:
将流量切分至不同的Reviision
kn service update hello --traffic hello-00001=10 --traffic hello-00002=90
验证:
将流量完全发送至最新的版本Revision
kn service update hello --traffic '@latest'=100
验证:将全部流量发送给最新的Revision
资源配置清单
将流量发送给最新版本10%, revision-2 70%,revision-1 20%
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
spec:
template:
spec:
containers:
- image: ikubernetes/helloworld-go
ports:
- containerPort: 8080
env:
- name: TARGET
value: "Little boy"
traffic:
- latestRevision: true
percent: 10
- revisionName: hello-00002
percent: 70
- revisionName: hello-00001
percent: 20
验证:基本7-2-1比例
场景:可以为特定的Revision打上tag,然后可以通过特定的URL格式来访问
<tag-name>-<ksvc-name>.<namespace>.<domain>
的歌格式访问;<ksvc-name>.<namespace>.<domain>
的格式访问管理标签的方法
在Traffic Target上直接配置
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
spec:
template:
spec:
containers:
- image: ikubernetes/helloworld-go
ports:
- containerPort: 8080
env:
- name: TARGET
value: "Cloud-Native"
traffic:
- latestRevision: true
percent: 0
tag: staging
- revisionName: hello-00002
percent: 90
- revisionName: hello-00001
percent: 10
命令
kn service update <ksvc> --tag revisionRef=tagNmae
kn service update <ksvc> --untag tagName
示例
先给hello-00001打上tag名字为staging的标签:
kn service update hello --tag hello-00001=staging
kubectl get ksvc hello -o yaml
访问带tag的revision 00001
curl -H "Host: staging-hello.default.icloud2native.com" 192.168.58.100
Configuration Target
ConfigurationName也可以作为路由项中的流量目标,意味着相关的流量部分由该Configurate下最新就绪的Revision承载
存在问题
解决方式
在KService或Route上使用“serving.knative.dev/rollout-duration”注解来指定流量迁移过程的时长;新的Revision上线后,它会先从Configuration Target迁出1%的流量;随后再等分迁出余下的流量部分
配置案例
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
annotations:
serving.knative.dev/rollout-duration: "380s"
spec:
...
traffic:
- percent: 55
configurationName: hello # Pinned to latest ready Revision
- percent: 45
revisionName: hello-00001 # Pinned to a specific Revision.