Kubernetes控制器之Deployment介紹(六)

一、Deployment、ReplicaSet、Pod之間的關(guān)系

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

10年積累的成都網(wǎng)站建設(shè)、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有滎經(jīng)免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

前面我們已經(jīng)了解到,Kubernetes 通過各種 Controller 來管理 Pod 的生命周期。為了滿足不同業(yè)務(wù)場景,Kubernetes 開發(fā)了 Deployment、ReplicaSet、DaemonSet、StatefuleSet、Job 等多種 Controller。我們首先學(xué)習(xí)最常用的 Deployment。

1、運(yùn)行 Deployment

先從例子開始,運(yùn)行一個(gè) Deployment:

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

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

2、查看 Deployment(deploy)

查看剛剛創(chuàng)建的 deployment,其可以簡寫為deploy。

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

使用命令kubectl describe deploy查看內(nèi)部內(nèi)容。

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

展示的內(nèi)容大部分都是描述信息,我們看最后一行,這里告訴我們創(chuàng)建了一個(gè) ReplicaSet nginx-deployment-5fd98dbf5f,Events 是 Deployment 的日志,記錄了 ReplicaSet 的啟動過程。

通過上面的分析,也驗(yàn)證了 Deployment 通過 ReplicaSet 來管理 Pod 的事實(shí)。

3、查看 ReplicaSet(rs)

查看我們有哪些 rs。

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

使用命令kubectl describe rs查看其詳細(xì)信息。

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記錄了兩個(gè)副本 Pod 的創(chuàng)建,那我們查看一下 Pod。

4、查看 Pod

查看目前的 Pod。

[root@master ~]# 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

隨便選擇一個(gè) Pod,查看其詳細(xì)信息。

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://nginx@sha256: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 創(chuàng)建的。
Events記錄了 Pod 的啟動過程。

5、總結(jié)

  1. 用戶通過 kubectl 創(chuàng)建 Deployment。
  2. Deployment 創(chuàng)建 ReplicaSet。
  3. ReplicaSet 創(chuàng)建 Pod。

Kubernetes 控制器之 Deployment 介紹(六)

從上圖也可以看出,對象的命名方式是:子對象的名字 = 父對象名字 + 隨機(jī)字符串或數(shù)字。

二、伸縮

伸縮(Scale Up/Down)是指在線增加或減少 Pod 的副本數(shù)。

我們重新創(chuàng)建一下。

[root@master ~]# kubectl run nginx --image=nginx:1.7.9 --replicas=2
deployment.apps/nginx created
[root@master ~]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP            NODE                 NOMINATED NODE
nginx-699ff78c9-2xxnj   1/1     Running   0          51s   10.244.1.11   node01.wzlinux.com   <none>
nginx-699ff78c9-j5w6c   1/1     Running   0          51s   10.244.3.6    node02.wzlinux.com   <none>

我們把副本數(shù)量修改為5個(gè),查看一下。

[root@master ~]# kubectl scale --replicas=5 deploy/nginx
deployment.extensions/nginx scaled
[root@master ~]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP            NODE                 NOMINATED NODE
nginx-699ff78c9-2xxnj   1/1     Running   0          2m21s   10.244.1.11   node01.wzlinux.com   <none>
nginx-699ff78c9-4qq9h   1/1     Running   0          18s     10.244.1.12   node01.wzlinux.com   <none>
nginx-699ff78c9-b6dt4   1/1     Running   0          18s     10.244.3.7    node02.wzlinux.com   <none>
nginx-699ff78c9-j5w6c   1/1     Running   0          2m21s   10.244.3.6    node02.wzlinux.com   <none>
nginx-699ff78c9-zhwsz   1/1     Running   0          18s     10.244.3.8    node02.wzlinux.com   <none>

三個(gè)新副本被創(chuàng)建并調(diào)度到 node01 和 node02 上,出于安全考慮,默認(rèn)配置下 Kubernetes 不會將 Pod 調(diào)度到 Master 節(jié)點(diǎn)。如果希望將 master 也當(dāng)作 Node 使用,可以執(zhí)行如下命令:

kubectl taint node master node-role.kubernetes.io/master-

如果要恢復(fù) Master Only 狀態(tài),執(zhí)行如下命令:

kubectl taint node master node-role.kubernetes.io/master="":NoSchedule

想要減少副本數(shù)量也是同樣的方法,指定數(shù)量即可,那我們減少到3個(gè)副本。

[root@master ~]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP            NODE                 NOMINATED NODE
nginx-699ff78c9-2xxnj   1/1     Running   0          2m55s   10.244.1.11   node01.wzlinux.com   <none>
nginx-699ff78c9-4qq9h   1/1     Running   0          52s     10.244.1.12   node01.wzlinux.com   <none>
nginx-699ff78c9-j5w6c   1/1     Running   0          2m55s   10.244.3.6    node02.wzlinux.com   <none>

三、故障轉(zhuǎn)移

目前是五個(gè)應(yīng)用分別運(yùn)行在兩臺機(jī)器上面,我們把 node02 關(guān)閉,造成 node02 出現(xiàn)問題,然后再查看一下 Pod 的情況。

[root@master ~]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP            NODE                 NOMINATED NODE
nginx-699ff78c9-2xxnj   1/1     Running   0          8m49s   10.244.1.11   node01.wzlinux.com   <none>
nginx-699ff78c9-4qq9h   1/1     Running   0          6m46s   10.244.1.12   node01.wzlinux.com   <none>
nginx-699ff78c9-j5w6c   1/1     Unknown   0          8m49s   10.244.3.6    node02.wzlinux.com   <none>
nginx-699ff78c9-wqd5k   1/1     Running   0          32s     10.244.1.13   node01.wzlinux.com   <none>

等待一段時(shí)間之后,我們看到 node02 上的 Pod 標(biāo)記為 Unknown 狀態(tài),并在 node01 上面新建三個(gè) Pod,維持總副本數(shù)為3。

那我們重啟把服務(wù)器啟動起來,正常情況下,如果配置沒有問題,服務(wù)會自動添加到集群中,我們啟動查看狀態(tài)。

[root@master ~]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE     IP            NODE                 NOMINATED NODE
nginx-699ff78c9-2xxnj   1/1     Running   0          14m     10.244.1.11   node01.wzlinux.com   <none>
nginx-699ff78c9-4qq9h   1/1     Running   0          12m     10.244.1.12   node01.wzlinux.com   <none>
nginx-699ff78c9-wqd5k   1/1     Running   0          6m37s   10.244.1.13   node01.wzlinux.com   <none>

當(dāng) node02 恢復(fù)后,Unknown 的 Pod 會被刪除,不過已經(jīng)運(yùn)行的 Pod 不會重新調(diào)度回 node02。

四、標(biāo)簽

默認(rèn)配置下,Scheduler 會將 Pod 調(diào)度到所有可用的 Node。不過有些情況我們希望將 Pod 部署到指定的 Node,比如將有大量磁盤 I/O 的 Pod 部署到配置了 SSD 的 Node;或者 Pod 需要 GPU,需要運(yùn)行在配置了 GPU 的節(jié)點(diǎn)上。

我們使用mytest.yaml文件創(chuàng)建一個(gè)Deployment,內(nèi)容如下:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mytest
  namespace: default
spec:
  replicas: 5
  template:
    metadata:
      labels:
        run: mytest
    spec:
      containers:
      - image: wangzan18/mytest:v1
        imagePullPolicy: IfNotPresent
        name: mytest

使用下面命令創(chuàng)建應(yīng)用。

[root@master ~]# kubectl create -f mytest.yaml 
deployment.extensions/mytest created

Kubernetes 是通過 label 來實(shí)現(xiàn)這個(gè)功能的。label 是 key-value 對,各種資源都可以設(shè)置 label,靈活添加各種自定義屬性。比如執(zhí)行如下命令標(biāo)注 node01 是配置了 SSD 的節(jié)點(diǎn)。

kubectl label node node01.wzlinux.com disktype=ssd

然后使用命令kubectl get node --show-labels我們查看。

NAME                 STATUS   ROLES    AGE   VERSION   LABELS
master.wzlinux.com   Ready    master   26h   v1.12.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master.wzlinux.com,node-role.kubernetes.io/master=
node01.wzlinux.com   Ready    <none>   25h   v1.12.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node01.wzlinux.com
node02.wzlinux.com   Ready    <none>   91m   v1.12.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node02.wzlinux.com

disktype=ssd 已經(jīng)成功添加到 node01,除了 disktype,Node 還有幾個(gè) Kubernetes 自己維護(hù)的 label。

有了disktype這個(gè)自定義 label,接下來就可以指定將 Pod 部署到 node01。編輯 mytest.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mytest
  namespace: default
spec:
  replicas: 5
  template:
    metadata:
      labels:
        run: mytest
    spec:
      containers:
      - image: wangzan18/mytest:v1
        imagePullPolicy: IfNotPresent
        name: mytest
      nodeSelector:
        disktype: ssd

在 Pod 模板的spec里通過nodeSelector指定將此 Pod 部署到具有 label disktype=ssd 的 Node 上。

重新部署 Deployment 并查看 Pod 的運(yùn)行節(jié)點(diǎn):

[root@master ~]# kubectl apply -f mytest.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.extensions/mytest configured

[root@master ~]# kubectl get pod -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP            NODE                 NOMINATED NODE
mytest-6f7fbbfdc7-2tr6s   1/1     Running   0          63s   10.244.1.19   node01.wzlinux.com   <none>
mytest-6f7fbbfdc7-5g9tj   1/1     Running   0          61s   10.244.1.21   node01.wzlinux.com   <none>
mytest-6f7fbbfdc7-bnfxv   1/1     Running   0          61s   10.244.1.22   node01.wzlinux.com   <none>
mytest-6f7fbbfdc7-bqzqq   1/1     Running   0          60s   10.244.1.23   node01.wzlinux.com   <none>
mytest-6f7fbbfdc7-v6cqk   1/1     Running   0          63s   10.244.1.20   node01.wzlinux.com   <none>

全部 6 個(gè)副本都運(yùn)行在 node01 上,符合我們的預(yù)期。

要刪除 label disktype,執(zhí)行如下命令:

[root@master ~]# kubectl label node node01.wzlinux.com disktype-
node/node01.wzlinux.com labeled

不過此時(shí) Pod 并不會重新部署,依然在 node01 上運(yùn)行。

除非在mytest.yaml中刪除nodeSelector設(shè)置,然后通過kubectl apply重新部署。

關(guān)于對象資源的設(shè)定,大家可以使用命令kubectl explain,比如查看pod中nodeSelector的參數(shù)設(shè)定,我們可以使用命令 kubectl explain pod.spec.nodeSelector

小問題:手動重新添加到集群

如果因?yàn)槟承﹩栴},服務(wù)不能自動添加到集群中,我們就需要手動重新初始化添加一次。

master 節(jié)點(diǎn)上面先刪除 node02節(jié)點(diǎn)。

kubectl delete node node02.wzlinux.com

node02 上面 reset。

kubeadm reset

重新使用kubeadm init初始化,但是發(fā)現(xiàn)token過期了,我們需要在master節(jié)點(diǎn)重新生成token。

[root@master ~]# kubeadm token create
v269qh.2mylwtmc96kd28sq

生成ca-cert-hash sha256的值。

[root@master ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
>    openssl dgst -sha256 -hex | sed 's/^.* //'
84e50f7beaa4d3296532ae1350330aaf79f3f0d45ec8623fae6cd9fe9a804635

然后在node節(jié)點(diǎn)上面重新使用kubeadm init進(jìn)行添加集群中。

網(wǎng)站標(biāo)題:Kubernetes控制器之Deployment介紹(六)
網(wǎng)站地址:http://bm7419.com/article24/iihece.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、虛擬主機(jī)、企業(yè)建站、外貿(mào)建站、微信公眾號、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司