1. 程式人生 > >Kubernetes Controller 之 Deployment 介紹

Kubernetes Controller 之 Deployment 介紹

一、Deployment

我們接著前面的文章說,如果不清楚的請檢視之前的博文:http://blog.51cto.com/wzlinux/2322616

前面我們已經瞭解到,Kubernetes 通過各種 Controller 來管理 Pod 的生命週期。為了滿足不同業務場景,Kubernetes 開發了 Deployment、ReplicaSet、DaemonSet、StatefuleSet、Job 等多種 Controller。我們首先學習最常用的 Deployment。

1、執行 Deployment

先從例子開始,執行一個 Deployment:

kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2

上面的命令將部署包含兩個副本的 Deployment nginx-deployment,容器的 image 為 nginx:1.7.9

2、檢視 Deployment(deploy)

檢視剛剛建立的 deployment,其可以簡寫為deploy。

[[email protected] ~]# kubectl get deploy
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2         2         2            2           4m56s

使用命令kubectl describe deploy檢視內部內容。

kubectl describe deploy nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Thu, 29 Nov 2018 17:47:16 +0800
Labels:                 run=nginx-deployment
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx-deployment
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx-deployment
  Containers:
   nginx-deployment:
    Image:        nginx:1.7.9
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-5fd98dbf5f (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  6m11s  deployment-controller  Scaled up replica set nginx-deployment-5fd98dbf5f to 2

展示的內容大部分都是描述資訊,我們看最後一行,這裡告訴我們建立了一個 ReplicaSet nginx-deployment-5fd98dbf5f,Events 是 Deployment 的日誌,記錄了 ReplicaSet 的啟動過程。

通過上面的分析,也驗證了 Deployment 通過 ReplicaSet 來管理 Pod 的事實。

3、檢視 ReplicaSet(rs)

檢視我們有哪些 rs。

[[email protected] ~]# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-5fd98dbf5f   2         2         2       12m

使用命令kubectl describe rs檢視其詳細資訊。

kubectl describe rs nginx-deployment-5fd98dbf5f
Name:           nginx-deployment-5fd98dbf5f
Namespace:      default
Selector:       pod-template-hash=5fd98dbf5f,run=nginx-deployment
Labels:         pod-template-hash=5fd98dbf5f
                run=nginx-deployment
Annotations:    deployment.kubernetes.io/desired-replicas: 2
                deployment.kubernetes.io/max-replicas: 3
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/nginx-deployment
Replicas:       2 current / 2 desired
Pods Status:    2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  pod-template-hash=5fd98dbf5f
           run=nginx-deployment
  Containers:
   nginx-deployment:
    Image:        nginx:1.7.9
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  13m   replicaset-controller  Created pod: nginx-deployment-5fd98dbf5f-8g7nm
  Normal  SuccessfulCreate  13m   replicaset-controller  Created pod: nginx-deployment-5fd98dbf5f-58c4z

我們可以看到Controlled By: Deployment/nginx-deployment,說明此 ReplicaSet 由 Deployment nginx-deployment
Events記錄了兩個副本 Pod 的建立,那我們檢視一下 Pod。

4、檢視 Pod

檢視目前的 Pod。

[[email protected] ~]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-5fd98dbf5f-58c4z   1/1     Running   0          19m
nginx-deployment-5fd98dbf5f-8g7nm   1/1     Running   0          19m

隨便選擇一個 Pod,檢視其詳細資訊。

kubectl describe pod nginx-deployment-5fd98dbf5f-58c4z
Name:               nginx-deployment-5fd98dbf5f-58c4z
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               node02.wzlinux.com/172.18.8.202
Start Time:         Thu, 29 Nov 2018 17:47:16 +0800
Labels:             pod-template-hash=5fd98dbf5f
                    run=nginx-deployment
Annotations:        <none>
Status:             Running
IP:                 10.244.2.3
Controlled By:      ReplicaSet/nginx-deployment-5fd98dbf5f
Containers:
  nginx-deployment:
    Container ID:   docker://69fa73ed16d634627b69b8968915d9a5704f159206ac0d3b2f1179fa99acd56f
    Image:          nginx:1.7.9
    Image ID:       docker-pullable://[email protected]:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Thu, 29 Nov 2018 17:47:28 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-sm664 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-sm664:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-sm664
    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  Scheduled  20m   default-scheduler            Successfully assigned default/nginx-deployment-5fd98dbf5f-58c4z to node02.wzlinux.com
  Normal  Pulling    20m   kubelet, node02.wzlinux.com  pulling image "nginx:1.7.9"
  Normal  Pulled     20m   kubelet, node02.wzlinux.com  Successfully pulled image "nginx:1.7.9"
  Normal  Created    20m   kubelet, node02.wzlinux.com  Created container
  Normal  Started    20m   kubelet, node02.wzlinux.com  Started container

我們可以看到Controlled By: ReplicaSet/nginx-deployment-5fd98dbf5f,說明此 Pod 是由 ReplicaSet nginx-deployment-5fd98dbf5f 建立的。
Events記錄了 Pod 的啟動過程。

5、總結

  1. 使用者通過 kubectl 建立 Deployment。
  2. Deployment 建立 ReplicaSet。
  3. ReplicaSet 建立 Pod。

Kubernetes Controller 之 Deployment 介紹

從上圖也可以看出,物件的命名方式是:子物件的名字 = 父物件名字 + 隨機字串或數字。