学习路线指引(点击解锁) | 知识定位 | 人群定位 |
---|---|---|
🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
💛Python量化交易实战💛 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
1.更新应用的环境变量
可通过命令行的方式亦可以通过读取配置文件的方式,这里主要来看命令行的方式
[root@kn-server-master01-13 knative]# kn service update --help来查看帮助
[root@kn-server-master01-13 knative]# kn service update hello \ # 更新命名空间default下的服务hello-example;
> --env TARGET=Second
Updating Service 'hello' in namespace 'default':
0.045s The Configuration is still working to reflect the latest desired specification.
2.077s Traffic is not yet migrated to the latest revision.
2.096s Ingress has not yet been reconciled.
2.123s Waiting for load balancer to be ready
2.338s Ready to serve.
服务hello-example已经更新到最新修订版本hello-00002,并且URL是http://hello.default.example.com
Service 'hello' updated to latest revision 'hello-00002' is available at URL:
http://hello.default.example.com
00002会取代00001吗?是的,这取决于访问的修订版本,从客户端来看,HTTP请求将全部发送到新版本的URL上,即版本已经进行了替换,从开发角度来看,两个修订版本仍然存在;
[root@kn-server-master01-13 ~]# kn revision list
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-00002 hello 100% 2 118m 3 OK / 4 True
hello-00001 hello 1 126m 3 OK / 4 True
[root@kn-server-master01-13 ~]# kn revision describe hello-00002
Name: hello-00002 # 名称
Namespace: default # 所在的名称空间
Age: 2h # 运行时间
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b) # 镜像来自哪儿
Env: TARGET=Second # 环境变量是什么
Service: hello # Srevise的名称
Conditions:
OK TYPE AGE REASON
++ Ready 2h
++ ContainerHealthy 2h
++ ResourcesAvailable 2h
I Active 2h NoTraffic
OK表示服务是不是健康的,符号"++"表示一切正常
符号"I"表示服务还好,但它表示的信息没有符号"++"那么正向。如果服务出现的问题十分严重,那么会出现符号"!!"。如果服务出现的问题不是很严重,那么会出现符号"w"。如果knative不知道当前服务出现了什么问题,那么符号会变为"??";
TYPE: 这一列数据是唯一描述状态的,例如Ready表示kubernetes就绪探针探测的结果是正常的。
AGE: 这一列数据表示当前状态的最后修改时间,这个时间是会变化的。
REASON: 这列数据提供了许多排查问题的线索,例如Active状态在REASON这一栏显示的是NoTraffic状态。
Active表示什么?
当Active状态显示为NoTraffic时,表示修订版本当前没有活跃的实例在运行。假如我们对它执行curl;
可以看到更新后读取的是更新后的环境变量;
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
这里显示的是"++Active",而不是NoTraffic,knative表达的意思是一个运行的进程被创建并且处于活跃状态,如果几分钟不访问的话,那么这个进程会再次被关闭,并且Active状态会再次回到缺少流量的状态(NoTraffic)
[root@kn-server-master01-13 ~]# kn revision describe hello-00002
Name: hello-00002
Namespace: default
Age: 2h
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
Replicas: 1/1
Env: TARGET=Second
Service: hello
Conditions:
OK TYPE AGE REASON
++ Ready 2h
++ ContainerHealthy 2h
++ ResourcesAvailable 2h
++ Active 32s
[root@kn-server-master01-13 ~]# kn service update hello --image gcr.io/knative-samples/helloworld-rust
Updating Service 'hello' in namespace 'default':
0.019s The Configuration is still working to reflect the latest desired specification.
132.577s Traffic is not yet migrated to the latest revision.
132.633s Ingress has not yet been reconciled.
132.665s Waiting for load balancer to be ready
132.850s Ready to serve.
这里说的是最新的revision叫hello-00004访问的URL是http://hello.default.example.com
Service 'hello' updated to latest revision 'hello-00004' is available at URL:
http://hello.default.example.com
镜像确实已经被更新;而且是NoTraffic状态,因为目前没有流量;
[root@kn-server-master01-13 ~]# kn revision describe hello-00004
Name: hello-00004
Namespace: default
Age: 9d
Image: gcr.io/knative-samples/helloworld-rust (pinned to 33fe75)
Env: TARGET=Second
Service: hello
Conditions:
OK TYPE AGE REASON
++ Ready 9d
++ ContainerHealthy 9d
++ ResourcesAvailable 9d
I Active 9d NoTraffic
访问测试是没有问题的;
sh-3.2# curl -H "Host:hello.default.example.com" xx.xx.xx.xx
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" xx.xx.xx.xx
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" xx.xx.xx.xx
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" xx.xx.xx.xx
随着有流量打进来,Pod是会被启动的;
[root@kn-server-master01-13 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-00004-deployment-864974d9b6-jjh8w 2/2 Running 0 28s
traffic允许在两个修订版本之间按照百分百分流。注意,关键是所有的流量比例加起来必须是100。如果流量比例是50和60,那么knative会返回"given traffic percents sum to 110,want 100"。 同理,如果流量比例是50和40,那么knative会返回"given traffic percents sum to 90, want 100"。我们必须保证流量比例是正确的。并且其和是100。
[root@kn-server-master01-13 ~]# kn service update hello \
> --traffic hello-00004=50 \
> --traffic hello-00002=50
Updating Service 'hello' in namespace 'default':
0.022s The Route is still working to reflect the latest desired specification.
0.049s Ingress has not yet been reconciled.
0.094s Waiting for load balancer to be ready
0.293s Ready to serve.
Service 'hello' with latest revision 'hello-00004' (unchanged) is available at URL:
http://hello.default.example.com
可以看到的是流量比例各百分之50
[root@kn-server-master01-13 ~]# kn revision list
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-00004 hello 50% 4 9d 3 OK / 4 True
hello-00002 hello 50% 2 10d 3 OK / 4 True
可以看到流量差不多是均分的;
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2#
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
过几分钟没有访问的话,Pod会处Terminating状态
[root@kn-server-master01-13 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-00004-deployment-864974d9b6-m6vbr 2/2 Terminating 0 3m12s
三分流量,流量在各个revision之间分发
[root@kn-server-master01-13 ~]# kn service update hello \
> --traffic hello-00004=50 \
> --traffic hello-00002=25 \
> --traffic hello-00001=25
Updating Service 'hello' in namespace 'default':
0.022s The Route is still working to reflect the latest desired specification.
0.056s Ingress has not yet been reconciled.
0.093s Waiting for load balancer to be ready
0.297s Ready to serve.
Service 'hello' with latest revision 'hello-00004' (unchanged) is available at URL:
http://hello.default.example.com
可以看到的是04占50%,1和2各占流量百分之25%
[root@kn-server-master01-13 ~]# kn revision list
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-00004 hello 50% 4 9d 3 OK / 4 True
hello-00002 hello 25% 2 10d 3 OK / 4 True
hello-00001 hello 25% 1 10d 3 OK / 4 True
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello First!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello First!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello First!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello First!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello First!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello world: Secondsh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello Second!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello First!
sh-3.2# curl -H "Host:hello.default.example.com" 10.0.0.15
Hello First!
[root@kn-server-master01-13 ~]# kn service describe hello
Name: hello
Namespace: default
Age: 10d
URL: http://hello.default.example.com
Revisions:
50% hello-00004 (current @latest) [4] (9d)
Image: gcr.io/knative-samples/helloworld-rust (pinned to 33fe75)
Replicas: 0/0
25% hello-00002 [2] (10d)
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
Replicas: 0/0
25% hello-00001 [1] (10d)
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
Replicas: 0/0
Conditions:
OK TYPE AGE REASON
++ Ready 4m
++ ConfigurationsReady 9d
++ RoutesReady 4m
可以发现3个Pod同时被拉起,等几分钟没有流量的时候会再次处于Terminating状态
[root@kn-server-master01-13 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-00001-deployment-84d5ff6489-dszfb 2/2 Running 0 39s
hello-00002-deployment-655986d86d-vgkqj 2/2 Running 0 23s
hello-00004-deployment-864974d9b6-4dhl5 2/2 Running 0 37s
[root@kn-server-master01-13 ~]# kn revision describe hello-00001
Name: hello-00001
Namespace: default
Age: 10d
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
Replicas: 0/0
Env: TARGET=First
Service: hello
Conditions:
OK TYPE AGE REASON
++ Ready 10d
++ ContainerHealthy 10d
++ ResourcesAvailable 10d
I Active 4m NoTraffic 这里显示的是no traffic没有流量
已经没有了Pod,已经缩容至0
[root@kn-server-master01-13 ~]# kubectl get pods
No resources found in default namespace.