Pod�у�跺��-Deployment

��绔���寮�彭�锛�广大�瀵规�у�跺�ㄩ��涓�杩�琛�璁茶В������锛���浠���璁茶В���雾�涓���甯哥�ㄧ���у�跺��锛�Deployment锛�

  • �у�跺�ㄥ�硅薄����绫�
  • What is Deployment?
  • Deployment���存�版�哄��
  • ReplicaSet
  • �戒护琛ュ��
  • Deployment-demo
  • 澶�娉�
  • 1.�у�跺�ㄥ�硅薄����绫�

    1.疏浚��よ�绋���

    1.���舵��搴���:��绯荤�绾у���锛�Nginx绛�锛�

    �ㄨ��浣跨��锛�Deployment锛�ReplicaSet

    2.���舵��搴���:绯荤�绾у���

    搴��ㄥ�烘��锛��ュ������ф�堕��疏浚㈡�风��锛��烘��广大辨��姣�涓�node���归��瑕�涓�����瑕�杩�琛�1涓�pod

    �ㄨ��浣跨��锛�DaemonSet

    3.���舵��搴���

    搴��ㄥ�烘��锛�mysql��redis��缇ょ��

    �ㄨ��浣跨��锛�statefulSet

    2.��疏浚��よ�绋���

    Job锛�涓�娆℃�т换��

    Cronjob锛�疏浚��朵换��

    2. What is Deployment?

    Deploymen��涓�涓���岛��虫��Pod�存�板��Reolica Sets�舵�����у�跺�ㄣ���㈠�ヨ��璇达�

    浣���deployment瀵硅薄涓���杩颁�涓�涓������舵��锛��ョ��deployment�у�跺�ㄤ�璁╁����舵�����ㄦ�锋�����舵��淇���涓��淬��姣�濡�������杩�琛�2涓�nginx Pod锛�褰�涓�涓�Pod��涓轰�������绱�涓�绾跨���跺��deployment�у�跺�ㄥ氨浼��规���ㄦ�锋�������舵�������ㄤ�涓�nginx pod��

    deployment����-1

    绗�河渠�绔�����kubernetes��缇ゆ�舵����锛���璇磋�tomcat��redis����杩��稿��service杩�琛�"杩���"��锛�杩��跺������涓轰�澶у�惰�芥�寸��������瑙c���跺��serice浼��绘�惧�板�瑰���deployment锛��跺��deployment�规���虫����Replica Sets����缃�锛��у�跺�瑰�Pod疏浚瑰�ㄧ���伴�����舵����

    3.Deployment���存�版�哄��

    deployment�存�版�哄��
    浣���浠ュ����Deployment���存�版�哄�舵���轰�婊��ㄦ�存�扮演��锛��蜂�椤哄�濡�涓�锛�

  • 兄弟���锛���寤轰�涓��扮演��RS�у�跺��锛�����涓�v2锛�
  • �ョ��广大��ф�у�跺�ㄧ��pod��缁�涓�绾匡����舵�扮演��RS�у�跺�ㄥ��姝ヤ�绾垮�瑰�Pod锛�
  • Pod�存�板������锛�寮��ㄦ�х��RS�у�跺��锛�婊��ㄥ��甯�广大辨�ゅ������
  • 浣���浠ヤ娇��kubectl get pod -o wide -w瑙�瀵�pod婊��ㄦ�存�版���碉���浠ヤ娇��kubectl get rs -o wide瑙�瀵�RS�у�跺�ㄧ����海洋����舵��绛�淇℃����

    浣�涔���浠ヤ娇��pause�戒护瀹��板�轰�deployment����涓�����甯�绛��ャ��

    杩�����琛ュ��河渠�涓�涓�RS�у�跺�ㄧ�舵��锛�浣���浠ヨ�瀵����起����у�跺�ㄧ���藉���������舵����褰����舵����广大辩华�舵����

    
    #浣跨�ㄥ�戒护�ョ��rs�у�跺�ㄧ�����茬����    
    [root@centos-1 mainfasts]# kubectl get rs -o wide
    NAME               DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES       SELECTOR
    myapp-67f698f887   0         0         0       53m     myapp        nginx:1.16   app=myapp,pod-template-hash=67f698f887,rel=stable
    myapp-7c488c6f44   5         5         5       48m     myapp        nginx:1.17   app=myapp,pod-template-hash=7c488c6f44,rel=stable
    myapp-98f644994    0         0         0       46m     myapp        nginx:1.15   app=myapp,pod-template-hash=98f644994,rel=stable
    ngx-new-cb79d555   2         2         2       2d22h   nginx        nginx        app=ngx-new,pod-template-hash=cb79d555
    

    1.婊��ㄥ��甯�����婊�疏浚���

    1) ��浠�兄弟���缂�杈�deployment-nginx.yaml锛�骞�apply -f锛���甯�nginx1.10������
    �朵腑��浠�缁�疏浚�河渠�婊��ㄧ���ワ���澶��板�1涓�(maxSurge)��广大�涓�绾�1涓�(maxUnavailable)

    绗�涓�娆″��甯����跺�����板�1涓�锛�涓�绾�2涓�

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deploy-nginx
    spec:
      replicas: 3
      minReadySeconds: 10
      strategy:
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 1
        type: RollingUpdate
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.10-alpine
            ports:
            - containerPort: 80
              name: http
            readinessProbe:
              periodSeconds: 1
              httpGet:
                path: /
                port: http

    2) �ョ��锛���浠���杩�淇���deployment-nginx.yaml��image: nginx:1.10-alpine����涓�1.13锛���甯�骞惰�瀵�����浠ュ����deployment瀵瑰���rs�у�跺�ㄩ��姝ュ��ㄨ��deploy-nginx-567c45c74锛�nginx:1.13-alpine锛�

    [root@centos-1 chapter5]# kubectl get rs -o wide
    NAME                      DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES              SELECTOR
    deploy-nginx-567c45c748   2         2         0       51s     nginx        nginx:1.13-alpine   app=nginx,pod-template-hash=567c45c748
    deploy-nginx-5745bb45d7   2         2         2       7m2s    nginx        nginx:1.10-alpine   app=nginx,pod-template-hash=5745bb45d7
    deploy-nginx-67f876bcb6   0         0         0       5m51s   nginx        nginx:1.11-alpine   app=nginx,pod-template-hash=67f876bcb6
    
    [root@centos-1 chapter5]# kubectl get rs -o wide
    NAME                      DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES              SELECTOR
    deploy-nginx-567c45c748   3         3         2       2m40s   nginx        nginx:1.13-alpine   app=nginx,pod-template-hash=567c45c748
    deploy-nginx-5745bb45d7   0         0         0       8m51s   nginx        nginx:1.10-alpine   app=nginx,pod-template-hash=5745bb45d7
    deploy-nginx-67f876bcb6   0         0         0       7m40s   nginx        nginx:1.11-alpine   app=nginx,pod-template-hash=67f876bcb6
    

    3) ���讹���浠���浠ユ�ョ�����茬����锛�绗�4�℃����浠����扮演���������变�����娆″��甯�娌℃���板�--record=true海洋�娈碉���浠ユ�剧ず涓�none

    [root@centos-1 chapter5]# kubectl rollout history deployment/deploy-nginx
    deployment.apps/deploy-nginx 
    REVISION  CHANGE-CAUSE
    2         <none>
    3         <none>
    4         kubectl apply --filename=deploy-nginx.yaml --record=true
    

    4) �ヤ��ワ���广大�郭�绀哄�浣���婊��充�涓�������
    ��浠�浣跨��rollout undo�戒护杩�琛���婊�锛�榛�璁�--to-revision=0锛�涓�涓�涓�����锛���瑙�瀵�rs����,���板凡缁��ㄩ�ㄥ���㈣��1.10��nginx,�虫�ゆ��ㄥ��甯���绛��ュ����婊�宸茬�郭�绀哄��姣�

    [root@centos-1 chapter5]# kubectl rollout undo deployment/deploy-nginx --to-revision=0
    deployment.apps/deploy-nginx rolled back
    
    [root@centos-1 chapter5]# kubectl get rs -o wide
    NAME                      DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES              SELECTOR
    deploy-nginx-567c45c748   2         2         2       4m58s   nginx        nginx:1.13-alpine   app=nginx,pod-template-hash=567c45c748
    deploy-nginx-5745bb45d7   2         2         2       11m     nginx        nginx:1.10-alpine   app=nginx,pod-template-hash=5745bb45d7
    deploy-nginx-67f876bcb6   0         0         0       9m58s   nginx        nginx:1.11-alpine   app=nginx,pod-template-hash=67f876bcb6
    
    [root@centos-1 chapter5]# kubectl get rs -o wide
    NAME                      DESIRED   CURRENT   READY   AGE    CONTAINERS   IMAGES              SELECTOR
    deploy-nginx-567c45c748   0         0         0       5m6s   nginx        nginx:1.13-alpine   app=nginx,pod-template-hash=567c45c748
    deploy-nginx-5745bb45d7   3         3         3       11m    nginx        nginx:1.10-alpine   app=nginx,pod-template-hash=5745bb45d7
    deploy-nginx-67f876bcb6   0         0         0       10m    nginx        nginx:1.11-alpine   app=nginx,pod-template-hash=67f876bcb6
    

    2.��涓�����甯�疏浚���

    1) 杩���锛���浠��轰�涓�����1.10��nginx锛���甯���涓�������锛�1.14

    [root@centos-1 chapter5]# kubectl set image deployment deploy-nginx nginx=nginx:1.14-alpine && kubectl rollout pause deployment deploy-nginx
    deployment.apps/deploy-nginx image updated
    deployment.apps/deploy-nginx paused

    2) 姝ゆ�跺����pod�拌�������卞��锛�2涓��扮演����2涓���������浣���浠ラ��杩��у�跺�ㄥ��绉板���㈢��HASH�板��锛�娓��拌�瀵��颁����������у�跺�ㄣ��

    ^C[root@centos-1 dingqishi]# kubectl get pod  -w
    NAME                            READY   STATUS    RESTARTS   AGE
    deploy-nginx-5745bb45d7-5wfml   1/1     Running   0          18m
    deploy-nginx-5745bb45d7-84s4c   1/1     Running   0          18m
    deploy-nginx-5745bb45d7-dqt8q   1/1     Running   0          18m
    
    deploy-nginx-754874567-l6q7h    0/1     Pending   0          0s
    deploy-nginx-754874567-l6q7h    0/1     Pending   0          0s
    deploy-nginx-5745bb45d7-5wfml   1/1     Terminating   0          18m
    deploy-nginx-754874567-l6q7h    0/1     ContainerCreating   0          0s
    deploy-nginx-754874567-q4bsh    0/1     Pending             0          0s
    deploy-nginx-754874567-q4bsh    0/1     Pending             0          0s
    deploy-nginx-754874567-q4bsh    0/1     ContainerCreating   0          1s
    deploy-nginx-5745bb45d7-5wfml   0/1     Terminating         0          18m
    deploy-nginx-5745bb45d7-5wfml   0/1     Terminating         0          18m
    deploy-nginx-5745bb45d7-5wfml   0/1     Terminating         0          18m
    deploy-nginx-754874567-l6q7h    0/1     Running             0          24s
    deploy-nginx-754874567-l6q7h    1/1     Running             0          25s
    deploy-nginx-754874567-q4bsh    0/1     Running             0          27s
    deploy-nginx-754874567-q4bsh    1/1     Running             0          27s
    
    [root@centos-1 dingqishi]# kubectl get pod  
    NAME                            READY   STATUS    RESTARTS   AGE
    deploy-nginx-5745bb45d7-84s4c   1/1     Running   0          19m
    deploy-nginx-5745bb45d7-dqt8q   1/1     Running   0          19m
    deploy-nginx-754874567-l6q7h    1/1     Running   0          61s
    deploy-nginx-754874567-q4bsh    1/1     Running   0          61s

    3) 濡����扮演�������ㄦ�锋弧��搴�涓�楂�锛���瑕���婊���璇�锛�姝ゆ�舵��浠�涔���浠ョ�ㄤ������扮演��rollout�戒护��

    ��娆℃��绀猴�--to-revision=0涓洪�璁ゅ���起�������涓�涓�涓�����锛�濡���瑕����版��疏浚�����锛�������疏浚�广大辫�河渠���

    kubectl rollout undo deployment/deploy-nginx --to-revision=0

    4) 濡����扮演�����ㄦ�锋弧��搴�涓���锛���瑕�疏浚����╀�Pod�存�扮演��璇�锛���瑕�浣跨��resume�戒护

    
    [root@centos-1 chapter5]# kubectl rollout resume deployment deploy-nginx
    deployment.apps/deploy-nginx resumed

    [root@centos-1 dingqishi]# kubectl get pod -w
    NAME READY STATUS RESTARTS AGE
    deploy-nginx-5745bb45d7-84s4c 1/1 Running 0 27m
    deploy-nginx-5745bb45d7-dqt8q 1/1 Running 0 27m
    deploy-nginx-754874567-l6q7h 1/1 Running 0 8m35s
    deploy-nginx-754874567-q4bsh 1/1 Running 0 8m35s

    deploy-nginx-5745bb45d7-84s4c 1/1 Terminating 0 30m
    deploy-nginx-5745bb45d7-dqt8q 1/1 Terminating 0 30m
    deploy-nginx-754874567-l6zz8 0/1 Pending 0 0s
    deploy-nginx-754874567-l6zz8 0/1 Pending 0 0s
    deploy-nginx-754874567-l6zz8 0/1 ContainerCreating 0 0s
    deploy-nginx-5745bb45d7-84s4c 0/1 Terminating 0 30m
    deploy-nginx-5745bb45d7-dqt8q 0/1 Terminating 0 30m
    deploy-nginx-754874567-l6zz8 0/1 Running 0 3s
    deploy-nginx-754874567-l6zz8 1/1 Running 0 3s
    deploy-nginx-5745bb45d7-84s4c 0/1 Terminating 0 30m
    deploy-nginx-5745bb45d7-84s4c 0/1 Terminating 0 30m
    deploy-nginx-5745bb45d7-dqt8q 0/1 Terminating 0 30m
    deploy-nginx-5745bb45d7-dqt8q 0/1 Terminating 0 30m

    [root@centos-1 dingqishi]# kubectl get pod
    NAME READY STATUS RESTARTS AGE
    deploy-nginx-754874567-l6q7h 1/1 Running 0 14m
    deploy-nginx-754874567-l6zz8 1/1 Running 0 3m33s
    deploy-nginx-754874567-q4bsh 1/1 Running 0 14m

    
    ## 4.ReplicaSet
    ReplicaSet缁�浠剁��浣���锛��冲��板�ㄤ�宸茬���河渠�璁哥��河渠�瑙d�锛�
    - �ㄧ�疏浚���浠讳��堕�达�淇�璇�涓�涓���纭���pod杩�琛��伴��
    - 绠$��搴�灞�Pod
    - 涓�搴�璇ヤ汉涓轰��ヨ�琛�璋��淬��绠$��
    
    ## 5.�戒护琛ュ��
    ```bash
    #疏浚��惰�瀵�Pod锛�
    kubectl get pod -w

    6.Deployment-demo

    deployment-demo

    7.澶�娉�

    ��������浣�河渠�����Github锛���浼���缁�广大�����涓�棰��存�拌��ワ��朵腑����docker��k8s��ceph��istio��prometheus锛��ㄥ�ㄥ��河渠�河渠�����涓�澶ц���ㄧ�������ヨ���瑰��疏浚���杩�绋�锛�濡���瀵逛�����锛�璇�follow��star��潘�������github锛�杩�涔������存�般����河渠�涓��荤���ㄥ��锛�璋㈣阿锝�