Knative部署應用以及應用的更新、應用的分流(二)

2022-09-05 12:04:35

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是http://hello.default.example.com
  Service 'hello' updated to latest revision 'hello-00002' is available at URL:
  http://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 修改範例的映象

  1. 使用update修改具體可使用kn service update --help來檢視幫助
  2. 修改環境變數會建立新的修訂版本,修改映象也會建立新的修訂版本。實際上,由於沒有修改環境變數,所以第三個修訂版本依賴回覆"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是http://hello.default.example.com
Service 'hello' updated to latest revision 'hello-00004' is available at URL:
http://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:
http://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:
http://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:        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.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.