Knative部署應用以及應用的更新、應用的分流(二)
1. 應用的更新
1.1 更新hello-example應用
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是//hello.default.example.com
Service 'hello' updated to latest revision 'hello-00002' is available at URL:
//hello.default.example.com
1.2 查看revision;
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
1.3 kn describe查看詳情;
[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;
1.4 訪問測試;
可以看到更新後讀取的是更新後的環境變量;
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!
1.4.1 再次describe查看
這裡顯示的是”++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
2. 應用的鏡像修改
2.1 修改實例的鏡像
- 使用update修改具體可使用kn service update –help來查看幫助
- 修改環境變量會創建新的修訂版本,修改鏡像也會創建新的修訂版本。實際上,由於沒有修改環境變量,所以第三個修訂版本依賴回復”Hello World:Seconds”。實際上,幾乎所有對服務的更新都會創建新的修訂版本;幾乎所有? 有沒有例外,當然有。當修改路由配置時,即更新服務的路由配置不會創建新的修訂版本。
[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是//hello.default.example.com
Service 'hello' updated to latest revision 'hello-00004' is available at URL:
//hello.default.example.com
2.2 查看鏡像是否更新成功;
鏡像確實已經被更新;而且是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
2.3 訪問測試;
訪問測試是沒有問題的;
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
2.3.1 查看Pod是否被啟動;
隨着有流量打進來,Pod是會被啟動的;
[root@kn-server-master01-13 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-00004-deployment-864974d9b6-jjh8w 2/2 Running 0 28s
3. 應用的分流
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。
3.1 50/50分流
[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:
//hello.default.example.com
3.1.1 查看revision
可以看到的是流量比例各百分之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
3.1.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 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!
3.1.3 查看Pod的狀態;
過幾分鐘沒有訪問的話,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
3.2 多路分流
三分流量,流量在各個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:
//hello.default.example.com
3.2.1 查看revision
可以看到的是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
3.2.2 測試訪問;
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!
3.2.3 describe查看狀態
[root@kn-server-master01-13 ~]# kn service describe hello
Name: hello
Namespace: default
Age: 10d
URL: //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.2.4 查看Pod
可以發現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
3.2.5 查看revision
[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沒有流量
3.2.6 再次查看Pod
已經沒有了Pod,已經縮容至0
[root@kn-server-master01-13 ~]# kubectl get pods
No resources found in default namespace.