k 8 s基本概念-Deploymentsの使い方


k 8 s基本概念-Deploymentsの使い方
2018/1/5
Deploymentsの使用例
  • app
  • を作成
  • appステータス
  • を表示
  • app
  • を更新
  • ロールバックapp
  • 拡張容量app
  • app
  • を削除
    appを作成
  • yamlプロファイルによるDeploymentの定義
  • apiVersionの書き方については、
  • を参照してください.
  • https://github.com/kubernetes/kubernetes/blob/630dbedef9de9ef678f16132796b103b8a03fcda/pkg/api/testing/defaulting_test.go
  • metadataのname
  • について
  • 定義deploymentの名前
  • 同じnamespaceの下の名前は
  • を繰り返すことはできません.
  • metadataのlabels
  • について
  • k/vキー値対
  • keyの命名規則
  • でなければなりません
  • は、64文字より小さい任意のprefixおよびnameを含む
  • .
  • nameはアルファベットと数字[a-z 0-9 A-Z]で始まりと終わり、真ん中はdashes(-)、underscores()、dots(.)、アルファベットと数字
  • であることができる.
  • prefixはオプションです.同様に、「kubernetes.io/」は
  • を議論しません.
  • Valueの命名規則
  • は、空白または64文字未満の
  • であってもよい
  • はアルファベットと数字[a-z 0-9 A-Z]で始まりと終わり、真ん中はdashes(-)、underscores()、dots(.)、アルファベットと数字
  • であることができる.
  • metadataのnamespace
  • について
  • デフォルトdefault
    [root@tvm-00 ~]# cat ~/k8s_install/test/whoami/app.yaml
    apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
    kind: Deployment
    metadata:
    name: test-deployment-app-whoami
    labels:
    app: whoami
    spec:
    replicas: 3
    selector:
    matchLabels:
    app: whoami
    template:
    metadata:
    labels:
    app: whoami
    spec:
    containers:
    - name: whoami
      image: opera443399/whoami:0.9
      ports:
        - containerPort: 80
  • app
    ###   :   --record         history      rolling update   
    [root@tvm-00 ~]# kubectl apply -f ~/k8s_install/test/whoami/app.yaml --record
    deployment "test-deployment-app-whoami" created
  • を実行
    appステータスの表示
  • 情報表示
    [root@tvm-00 ~]# kubectl get deployments
    NAME                         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    test-deployment-app-whoami   3         3         3            3           5m
    [root@tvm-00 ~]# kubectl get rs
    NAME                                    DESIRED   CURRENT   READY     AGE
    test-deployment-app-whoami-6cf9cd6bf4   3         3         3         5m
    [root@tvm-00 ~]# kubectl get pods
    NAME                                          READY     STATUS    RESTARTS   AGE
    test-deployment-app-whoami-6cf9cd6bf4-59h9x   1/1       Running   0          5m
    test-deployment-app-whoami-6cf9cd6bf4-978ht   1/1       Running   0          5m
    test-deployment-app-whoami-6cf9cd6bf4-w5dhk   1/1       Running   0          5m
    [root@tvm-00 ~]# kubectl get pods --show-labels
    NAME                                          READY     STATUS    RESTARTS   AGE       LABELS
    test-deployment-app-whoami-6cf9cd6bf4-59h9x   1/1       Running   0          7m        app=whoami,pod-template-hash=2795782690
    test-deployment-app-whoami-6cf9cd6bf4-978ht   1/1       Running   0          7m        app=whoami,pod-template-hash=2795782690
    test-deployment-app-whoami-6cf9cd6bf4-w5dhk   1/1       Running   0          7m        app=whoami,pod-template-hash=2795782690
  • appの更新
  • ミラーを1回更新してみた後、情報を確認し、バージョン履歴
    
    ###    image      :
    [root@tvm-00 ~]# kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.7
    deployment "test-deployment-app-whoami" image updated
  • に重点を置いてください.
    情報の表示
    [root@tvm-00 ~]# kubectl rollout status deployments/test-deployment-app-whoamideployment "test-deployment-app-whoami"successfully rolled out[root@tvm-00 ~]# kubectl get deploymentsNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEtest-deployment-app-whoami 3 3 3 3 11m[root@tvm-00 ~]# kubectl get rsNAME DESIRED CURRENT READY AGEtest-deployment-app-whoami-68c6cd964 3 3 3 42stest-deployment-app-whoami-6cf9cd6bf4 0 0 0 11m[root@tvm-00 ~]# kubectl get podsNAME READY STATUS RESTARTS AGEtest-deployment-app-whoami-68c6cd964-bfznh 1/1 Running 0 54stest-deployment-app-whoami-68c6cd964-r7vp5 1/1 Running 0 56stest-deployment-app-whoami-68c6cd964-v***j 1/1 Running 0 53s
    [root@tvm-00~]#kubectl describe deployments/test-deployment-app-whoamiName:test-deployment-app-whoamiNamespace:defaultCreationTimestamp:Wed,27 Dec 2017 17:44:30+0800 Labels:app=whoamiAnnotations:deployment.kubernetes.io/revision=2 kubectl.kubernetes.io/last-applied-configuration=(略)kubernetes.io/change-cause=kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.7Selector: app=whoamiReplicas: 3 desired
    3 updated
    3 total
    3 available
    0 unavailableStrategyType: RollingUpdateMinReadySeconds: 0RollingUpdateStrategy: 25% max unavailable, 25% max surgePod Template:Labels: app=whoamiContainers:whoami:Image: opera443399/whoami:0.7Port: 80/TCPEnvironment: Mounts: Volumes: Conditions:Type Status Reason
    Available True MinimumReplicasAvailableProgressing True NewReplicaSetAvailableOldReplicaSets: NewReplicaSet: test-deployment-app-whoami-68c6cd964 (3/3 replicas created)Events:Type Reason Age From Message
    Normal ScalingReplicaSet 12m deployment-controller Scaled up replica set test-deployment-app-whoami-6cf9cd6bf4 to 3Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set test-deployment-app-whoami-68c6cd964 to 1Normal ScalingReplicaSet 1m deployment-controller Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 2Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set test-deployment-app-whoami-68c6cd964 to 2Normal ScalingReplicaSet 1m deployment-controller Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 1Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set test-deployment-app-whoami-68c6cd964 to 3Normal ScalingReplicaSet 1m deployment-controller Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 0
    [root@tvm-00 ~]# kubectl rollout history deployments/test-deployment-app-whoamideployments "test-deployment-app-whoami"REVISION CHANGE-CAUSE1 kubectl apply --filename=/root/k8s_install/test/whoami/app.yaml --record=true2 kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.7
    
    -     1     ,      
    ```bash
    [root@tvm-00 ~]# kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.8
    deployment "test-deployment-app-whoami" image updated
    [root@tvm-00 ~]# kubectl rollout status deployments/test-deployment-app-whoami
    deployment "test-deployment-app-whoami" successfully rolled out
    [root@tvm-00 ~]# kubectl get deployments
    NAME                         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    test-deployment-app-whoami   3         3         3            3           17m
    [root@tvm-00 ~]# kubectl get rs
    NAME                                    DESIRED   CURRENT   READY     AGE
    test-deployment-app-whoami-68b94dd7bf   3         3         3         46s
    test-deployment-app-whoami-68c6cd964    0         0         0         6m
    test-deployment-app-whoami-6cf9cd6bf4   0         0         0         17m
    [root@tvm-00 ~]# kubectl get pods
    NAME                                          READY     STATUS    RESTARTS   AGE
    test-deployment-app-whoami-68b94dd7bf-5g89x   1/1       Running   0          45s
    test-deployment-app-whoami-68b94dd7bf-75xjd   1/1       Running   0          52s
    test-deployment-app-whoami-68b94dd7bf-pchpw   1/1       Running   0          49s
    [root@tvm-00 ~]# kubectl describe deployments/test-deployment-app-whoami
    Name:                   test-deployment-app-whoami
    Namespace:              default
    CreationTimestamp:      Wed, 27 Dec 2017 17:44:30 +0800
    Labels:                 app=whoami
    Annotations:            deployment.kubernetes.io/revision=3
                            kubectl.kubernetes.io/last-applied-configuration=( )
                            kubernetes.io/change-cause=kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.8
    Selector:               app=whoami
    Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=whoami
      Containers:
       whoami:
        Image:        opera443399/whoami:0.8
        Port:         80/TCP
        Environment:  
        Mounts:       
      Volumes:        
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  
    NewReplicaSet:   test-deployment-app-whoami-68b94dd7bf (3/3 replicas created)
    Events:
      Type    Reason             Age              From                   Message
      ----    ------             ----             ----                   -------
      Normal  ScalingReplicaSet  18m              deployment-controller  Scaled up replica set test-deployment-app-whoami-6cf9cd6bf4 to 3
      Normal  ScalingReplicaSet  7m               deployment-controller  Scaled up replica set test-deployment-app-whoami-68c6cd964 to 1
      Normal  ScalingReplicaSet  7m               deployment-controller  Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 2
      Normal  ScalingReplicaSet  7m               deployment-controller  Scaled up replica set test-deployment-app-whoami-68c6cd964 to 2
      Normal  ScalingReplicaSet  7m               deployment-controller  Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 1
      Normal  ScalingReplicaSet  7m               deployment-controller  Scaled up replica set test-deployment-app-whoami-68c6cd964 to 3
      Normal  ScalingReplicaSet  7m               deployment-controller  Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 0
      Normal  ScalingReplicaSet  2m               deployment-controller  Scaled up replica set test-deployment-app-whoami-68b94dd7bf to 1
      Normal  ScalingReplicaSet  2m               deployment-controller  Scaled down replica set test-deployment-app-whoami-68c6cd964 to 2
      Normal  ScalingReplicaSet  2m               deployment-controller  Scaled up replica set test-deployment-app-whoami-68b94dd7bf to 2
      Normal  ScalingReplicaSet  2m (x3 over 2m)  deployment-controller  (combined from similar events): Scaled down replica set test-deployment-app-whoami-68c6cd964 to 0
    [root@tvm-00 ~]# kubectl rollout history deployments/test-deployment-app-whoami
    deployments "test-deployment-app-whoami"
    REVISION  CHANGE-CAUSE
    1         kubectl apply --filename=/root/k8s_install/test/whoami/app.yaml --record=true
    2         kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.7
    3         kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.8
    
    ###     ,       3    

    ロールバックapp
  • 前バージョン
    
    [root@tvm-00 ~]# kubectl rollout undo deployments/test-deployment-app-whoami
    deployment "test-deployment-app-whoami"
    [root@tvm-00 ~]# kubectl rollout history deployments/test-deployment-app-whoami
    deployments "test-deployment-app-whoami"
    REVISION  CHANGE-CAUSE
    1         kubectl apply --filename=/root/k8s_install/test/whoami/app.yaml --record=true
    3         kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.8
    4         kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.7
  • にロールバック
    [root@tvm-00 ~]# kubectl get rsNAME DESIRED CURRENT READY AGEtest-deployment-app-whoami-68b94dd7bf 0 0 0 16mtest-deployment-app-whoami-68c6cd964 3 3 3 22mtest-deployment-app-whoami-6cf9cd6bf4 0 0 0 33m
    [root@tvm-00~]#kubectl describe deployments/test-deployment-app-whoamiName:test-deployment-app-whoamiNamespace:defaultCreationTimestamp:Wed,27 Dec 2017 17:44:30+0800 Labels:app=whoamiAnnotations:deployment.kubernetes.io/revision=4 kubectl.kubernetes.io/last-applied-configuration=(略)kubernetes.io/change-cause=kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.7Selector: app=whoamiReplicas: 3 desired
    3 updated
    3 total
    3 available
    0 unavailableStrategyType: RollingUpdateMinReadySeconds: 0RollingUpdateStrategy: 25% max unavailable, 25% max surgePod Template:Labels: app=whoamiContainers:whoami:Image: opera443399/whoami:0.7Port: 80/TCPEnvironment: Mounts: Volumes: Conditions:Type Status Reason
    Available True MinimumReplicasAvailableProgressing True NewReplicaSetAvailableOldReplicaSets: NewReplicaSet: test-deployment-app-whoami-68c6cd964 (3/3 replicas created)Events:Type Reason Age From Message
    Normal ScalingReplicaSet 27m deployment-controller Scaled up replica set test-deployment-app-whoami-6cf9cd6bf4 to 3Normal ScalingReplicaSet 17m deployment-controller Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 2Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set test-deployment-app-whoami-68c6cd964 to 2Normal ScalingReplicaSet 17m deployment-controller Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 1Normal ScalingReplicaSet 17m deployment-controller Scaled up replica set test-deployment-app-whoami-68c6cd964 to 3Normal ScalingReplicaSet 16m deployment-controller Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 0Normal ScalingReplicaSet 11m deployment-controller Scaled up replica set test-deployment-app-whoami-68b94dd7bf to 1Normal ScalingReplicaSet 11m deployment-controller Scaled down replica set test-deployment-app-whoami-68c6cd964 to 2Normal ScalingReplicaSet 11m deployment-controller Scaled up replica set test-deployment-app-whoami-68b94dd7bf to 2Normal ScalingReplicaSet 1m (x2 over 17m) deployment-controller Scaled up replica set test-deployment-app-whoami-68c6cd964 to 1Normal DeploymentRollback 1m deployment-controller Rolled back deployment "test-deployment-app-whoami"to revision 2Normal ScalingReplicaSet 1m (x8 over 11m) deployment-controller (combined from similar events): Scaled down replica set test-deployment-app-whoami-68b94dd7bf to 0
    
    -         
    ```bash
    [root@tvm-00 ~]# kubectl rollout undo deployments/test-deployment-app-whoami --to-revision=1
    deployment "test-deployment-app-whoami"
    [root@tvm-00 ~]# kubectl rollout history deployments/test-deployment-app-whoami
    deployments "test-deployment-app-whoami"
    REVISION  CHANGE-CAUSE
    3         kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.8
    4         kubectl set image deployments/test-deployment-app-whoami whoami=opera443399/whoami:0.7
    5         kubectl apply --filename=/root/k8s_install/test/whoami/app.yaml --record=true
    
    [root@tvm-00 ~]# kubectl get rs
    NAME                                    DESIRED   CURRENT   READY     AGE
    test-deployment-app-whoami-68b94dd7bf   0         0         0         16m
    test-deployment-app-whoami-68c6cd964    0         0         0         22m
    test-deployment-app-whoami-6cf9cd6bf4   3         3         3         33m
    
    [root@tvm-00 ~]# kubectl describe deployments/test-deployment-app-whoami
    Name:                   test-deployment-app-whoami
    Namespace:              default
    CreationTimestamp:      Wed, 27 Dec 2017 17:44:30 +0800
    Labels:                 app=whoami
    Annotations:            deployment.kubernetes.io/revision=5
                            kubectl.kubernetes.io/last-applied-configuration=( )
                            kubernetes.io/change-cause=kubectl apply --filename=/root/k8s_install/test/whoami/app.yaml --record=true
    Selector:               app=whoami
    Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=whoami
      Containers:
       whoami:
        Image:        opera443399/whoami:0.9
        Port:         80/TCP
        Environment:  
        Mounts:       
      Volumes:        
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      True    MinimumReplicasAvailable
      Progressing    True    NewReplicaSetAvailable
    OldReplicaSets:  
    NewReplicaSet:   test-deployment-app-whoami-6cf9cd6bf4 (3/3 replicas created)
    Events:
      Type    Reason              Age                 From                   Message
      ----    ------              ----                ----                   -------
      Normal  ScalingReplicaSet   29m                 deployment-controller  Scaled up replica set test-deployment-app-whoami-6cf9cd6bf4 to 3
      Normal  ScalingReplicaSet   19m                 deployment-controller  Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 2
      Normal  ScalingReplicaSet   19m                 deployment-controller  Scaled up replica set test-deployment-app-whoami-68c6cd964 to 2
      Normal  ScalingReplicaSet   19m                 deployment-controller  Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 1
      Normal  ScalingReplicaSet   19m                 deployment-controller  Scaled up replica set test-deployment-app-whoami-68c6cd964 to 3
      Normal  ScalingReplicaSet   19m                 deployment-controller  Scaled down replica set test-deployment-app-whoami-6cf9cd6bf4 to 0
      Normal  ScalingReplicaSet   13m                 deployment-controller  Scaled up replica set test-deployment-app-whoami-68b94dd7bf to 1
      Normal  ScalingReplicaSet   13m                 deployment-controller  Scaled down replica set test-deployment-app-whoami-68c6cd964 to 2
      Normal  ScalingReplicaSet   13m                 deployment-controller  Scaled up replica set test-deployment-app-whoami-68b94dd7bf to 2
      Normal  ScalingReplicaSet   3m (x2 over 19m)    deployment-controller  Scaled up replica set test-deployment-app-whoami-68c6cd964 to 1
      Normal  DeploymentRollback  3m                  deployment-controller  Rolled back deployment "test-deployment-app-whoami" to revision 2
      Normal  DeploymentRollback  29s                 deployment-controller  Rolled back deployment "test-deployment-app-whoami" to revision 1
      Normal  ScalingReplicaSet   27s (x12 over 13m)  deployment-controller  (combined from similar events): Scaled up replica set test-deployment-app-whoami-6cf9cd6bf4 to 3
      Normal  ScalingReplicaSet   27s                 deployment-controller  Scaled down replica set test-deployment-app-whoami-68c6cd964 to 1
      Normal  ScalingReplicaSet   26s                 deployment-controller  Scaled down replica set test-deployment-app-whoami-68c6cd964 to 0

    拡張容量app
  • 拡張
    
    ###         10  
    [root@tvm-00 ~]# kubectl scale deployments/test-deployment-app-whoami --replicas=10
    deployment "test-deployment-app-whoami" scaled
  • 情報の表示
    [root@tvm-00 ~]# kubectl get deploymentsNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEtest-deployment-app-whoami 10 10 10 10 39m[root@tvm-00 ~]# kubectl get rsNAME DESIRED CURRENT READY AGEtest-deployment-app-whoami-68b94dd7bf 0 0 0 23mtest-deployment-app-whoami-68c6cd964 0 0 0 29mtest-deployment-app-whoami-6cf9cd6bf4 10 10 10 39m[root@tvm-00 ~]# kubectl get podsNAME READY STATUS RESTARTS AGEtest-deployment-app-whoami-6cf9cd6bf4-2dd5m 1/1 Running 0 36stest-deployment-app-whoami-6cf9cd6bf4-4nx7x 1/1 Running 0 36stest-deployment-app-whoami-6cf9cd6bf4-bb7v7 1/1 Running 0 10mtest-deployment-app-whoami-6cf9cd6bf4-c7cht 1/1 Running 0 36stest-deployment-app-whoami-6cf9cd6bf4-dph22 1/1 Running 0 36stest-deployment-app-whoami-6cf9cd6bf4-jhtqz 1/1 Running 0 10mtest-deployment-app-whoami-6cf9cd6bf4-jjfp5 1/1 Running 0 36stest-deployment-app-whoami-6cf9cd6bf4-nlqq5 1/1 Running 0 36stest-deployment-app-whoami-6cf9cd6bf4-px24h 1/1 Running 0 10mtest-deployment-app-whoami-6cf9cd6bf4-rldhd 1/1 Running 0 36s
    
    -   
    ```bash
    ###         5  
    [root@tvm-00 ~]# kubectl scale deployments/test-deployment-app-whoami --replicas=5
    deployment "test-deployment-app-whoami" scaled
    
    ###     
    [root@tvm-00 ~]# kubectl get deployments
    NAME                         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    test-deployment-app-whoami   5         5         5            5           41m
    [root@tvm-00 ~]# kubectl get rs
    NAME                                    DESIRED   CURRENT   READY     AGE
    test-deployment-app-whoami-68b94dd7bf   0         0         0         25m
    test-deployment-app-whoami-68c6cd964    0         0         0         30m
    test-deployment-app-whoami-6cf9cd6bf4   5         5         5         41m
    [root@tvm-00 ~]# kubectl get pods
    NAME                                          READY     STATUS    RESTARTS   AGE
    test-deployment-app-whoami-6cf9cd6bf4-2dd5m   1/1       Running   0          2m
    test-deployment-app-whoami-6cf9cd6bf4-bb7v7   1/1       Running   0          12m
    test-deployment-app-whoami-6cf9cd6bf4-c7cht   1/1       Running   0          2m
    test-deployment-app-whoami-6cf9cd6bf4-jhtqz   1/1       Running   0          12m
    test-deployment-app-whoami-6cf9cd6bf4-px24h   1/1       Running   0          12m

    削除
    [root@tvm-00 test]# kubectl delete deployments/test-deployment-app-whoami
    deployment "test-deployment-app-whoami" deleted
    [root@tvm-00 test]# kubectl get deployments
    No resources found.