1. 程式人生 > 其它 >⑤.kubernetes Deployment

⑤.kubernetes Deployment

POD --> RC --> RS --> Deployment

什麼是Deployment資源

Deployment 表示使用者對k8s叢集的一次更新操作 Deployment是一個比RS應用模式更廣的API物件 用於保證Pod的副本數量

Deployment資源的作用

可以建立一個新的服務 更新一個新的服務 滾動升級一個新的服務 實際是建立一個新的RS 把新的RS中副本數增加到理想狀態 將舊的RS中的副本數減小到0的複合操作

Deployment需要解決的問題

RC、RS和Deployment只是保證了支撐服務的POD數量,但是沒有解決如何訪問這些服務的問題。一個POD只是一個執行服務的例項,隨時可以能在一個節點上停止,在另一個節點以一個新的IP啟動一個新的POD,因此不能以確定的IP和埠號提供服務。
要穩定地提供服務需要服務發現和負載均衡能力。服務發現完成的工作,是針對客戶端訪問的服務,找到對應的後端服務例項。
在K8S的集中當中,客戶端需要訪問的服務就是Service物件。每個Service會對應一個叢集內部有效的虛擬IP,叢集內部通過虛擬IP訪問一個服務。

1.建立第一個k8s的應用
[root@rstx-203 ~]# kubectl run net-test --image=alpine --replicas=2 sleep 36000 
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/net-test created
[root@rstx-203 ~]# kubectl get pod -o wide
NAME                                READY   STATUS             RESTARTS   AGE   IP            NODE                 NOMINATED NODE   READINESS GATES
net-test-7dc6fd954d-fwjgc           0/1     Error              8          19m   172.7.214.2   rstx4-214.host.com   <none>           <none>
net-test-7dc6fd954d-xnq26           0/1     CrashLoopBackOff   8          19m   172.7.241.2   rstx4-241.host.com   <none>           <none>
[root@rstx-203 ~]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
net-test           0/2     2            0           19m
nginx-deployment   3/3     1            3           15h
**kubectl get deployment命令可以檢視net-test的狀態,輸出顯示兩個副本正常執行。還可以在建立的過程中,通過kubectl describe deployment net-test瞭解詳細的資訊。**
[root@rstx-203 ~]# kubectl describe deployment net-test
Name:                   net-test
Namespace:              default
CreationTimestamp:      Tue, 20 Jul 2021 11:00:26 +0800
Labels:                 run=net-test
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=net-test
Replicas:               2 desired | 2 updated | 2 total | 0 available | 2 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=net-test
  Containers:
   net-test:
    Image:      alpine
    Port:       <none>
    Host Port:  <none>
    Args:
      sleep
      36000 
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      False   MinimumReplicasUnavailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  <none>
NewReplicaSet:   net-test-7dc6fd954d (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  3m26s  deployment-controller  Scaled up replica set net-test-7dc6fd954d to 2

RS來控制pod的數量 deployment控制rs

[root@rstx-203 ~]# kubectl get rs    #獲取副本集的資訊
NAME                          DESIRED   CURRENT   READY   AGE
net-test-7dc6fd954d           2         2         0       34m
nginx-deployment-5455f758b9   1         1         0       16h
nginx-deployment-6cbc65fd69   0         0         0       16h
nginx-deployment-d75f79f4d    3         3         3       16h
[root@rstx-203 ~]# kubectl describe rs net-test-7dc6fd954d   #檢視副本集的詳細資訊
Name:           net-test-7dc6fd954d
Namespace:      default
Selector:       pod-template-hash=7dc6fd954d,run=net-test
Labels:         pod-template-hash=7dc6fd954d
                run=net-test
Annotations:    deployment.kubernetes.io/desired-replicas: 2
                deployment.kubernetes.io/max-replicas: 3
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/net-test                       #指明 rs是由deployment名字是net-test的控制器控制
Replicas:       2 current / 2 desired
Pods Status:    2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  pod-template-hash=7dc6fd954d
           run=net-test
  Containers:
   net-test:
    Image:      alpine
    Port:       <none>
    Host Port:  <none>
    Args:
      sleep
      36000 
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  35m   replicaset-controller  Created pod: net-test-7dc6fd954d-fwjgc
  Normal  SuccessfulCreate  35m   replicaset-controller  Created pod: net-test-7dc6fd954d-xnq26
[root@rstx-203 ~]# kubectl get pod  #檢視pod資訊 可以看到兩個pod都處於奔潰狀態
NAME                                READY   STATUS             RESTARTS   AGE
net-test-7dc6fd954d-fwjgc           0/1     CrashLoopBackOff   11         40m
net-test-7dc6fd954d-xnq26           0/1     CrashLoopBackOff   11         40m
[root@rstx-203 ~]# kubectl describe pod net-test-7dc6fd954d-xnq26        #檢視pod的詳細資訊
Name:           net-test-7dc6fd954d-xnq26
Namespace:      default
Priority:       0
Node:           rstx4-241.host.com/192.168.1.241
Start Time:     Tue, 20 Jul 2021 10:56:37 +0800
Labels:         pod-template-hash=7dc6fd954d
                run=net-test
Annotations:    <none>
Status:         Running
IP:             172.7.241.2
Controlled By:  ReplicaSet/net-test-7dc6fd954d
Containers:
  net-test:
    Container ID:  docker://c9fc1b937b66d932bd4da376d1855c4cdac7b1c50fc04bd2e93ae66e604f4e57
    Image:         alpine
    Image ID:      docker-pullable://alpine@sha256:234cb88d3020898631af0ccbbcca9a66ae7306ecd30c9720690858c1b007d2a0
    Port:          <none>
    Host Port:     <none>
    Args:
      sleep
      36000 
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Tue, 20 Jul 2021 11:37:12 +0800
      Finished:     Tue, 20 Jul 2021 11:37:12 +0800
    Ready:          False
    Restart Count:  12
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-clf9q (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-clf9q:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-clf9q
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                    From                         Message
  ----     ------     ----                   ----                         -------
  Normal   Pulling    44m (x4 over 46m)      kubelet, rstx4-241.host.com  Pulling image "alpine"
  Normal   Created    44m (x4 over 46m)      kubelet, rstx4-241.host.com  Created container net-test
  Normal   Started    44m (x4 over 46m)      kubelet, rstx4-241.host.com  Started container net-test
  Normal   Scheduled  42m                    default-scheduler            Successfully assigned default/net-test-7dc6fd954d-xnq26 to rstx4-241.host.com
  Normal   Pulled     11m (x12 over 46m)     kubelet, rstx4-241.host.com  Successfully pulled image "alpine"
  Warning  BackOff    6m44s (x171 over 45m)  kubelet, rstx4-241.host.com  Back-off restarting failed container   #崩潰原因是沒有程式在前臺執行

Controlled By 指明此 Pod 是由 Controlled By: ReplicaSet/net-test-7dc6fd954d 建立。Events 記錄了 Pod 的啟動過程。如果操作失敗(比如 image 不存在),也能在這裡檢視到原因。