Kubernetesコマンド忘備録 / Deployments

# Deployments

[Deployments - Kubernetes](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/)

###deployment-definition.yml

<details><div>

```yaml:deloyment-definition.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  labels:
    app: myapp
    type: front-end

spec:
  template:
    metadata:
      name: myapp-pod
      labels:
          app: myapp
          type: front-app
    spec:
      containers:
        - name: nginx-container
          image: nginx

  replicas: 6

  selector:
    matchLabels:
      app: myapp
```
</div></details>

###基本コマンド

```bash:
# 作成する
$ kubectl apply -f deployment-definition.yml --record

# Deploymentが作成されたか確認する
$ kubectl rollout status deployment.apps/myapp-deploy

# 変更履歴を確認する
$ kubectl rollout history deployment.apps/myapp-deploy
deployments "myapp-deploy"
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=deployment-definition.yml --record=true

# 情報を表示する
$ kubectl get deploy
NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy   6         6         6            6           4m

# 削除する
kubectl delete deploy myapp-deploy
```

###テスト / nginxコンテナイメージのタグを変更する

####新しいdeployment-definition.yml

<details><div>

```yaml:deployment-definition.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  labels:
    app: myapp
    type: front-end

spec:
  template:
    metadata:
      name: myapp-pod
      labels:
          app: myapp
          type: front-app
    spec:
      containers:
        - name: nginx-container
          image: nginx:perl

  replicas: 6

  selector:
    matchLabels:
      app: myapp
```
</div></details>

```bash:
# 作成する（nginxイメージのバージョンはlatest)
$ kubectl apply -f deployment-definition.yml --record

# Deploymentの情報を取得する（nginxのバージョンを確認する）
$ kubectl describe deploy
Name:                   myapp-deploy
Namespace:              default
CreationTimestamp:      Thu, 28 Mar 2019 14:42:02 +0900
Labels:                 app=myapp
                        type=front-end
Annotations:            deployment.kubernetes.io/revision=1
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"myapp","type":"front-end"},"name":"myapp-deploy","namespace":...
Selector:               app=myapp
Replicas:               6 desired | 6 updated | 6 total | 6 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=myapp
           type=front-app
  Containers:
   nginx-container:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   myapp-deploy-f7cdcb579 (6/6 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  32s   deployment-controller  Scaled up replica set myapp-deploy-f7cdcb579 to 6


# nginxのバージョンを変更する
vi deployment-definition.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  labels:
    app: myapp
    type: front-end

spec:
  template:
    metadata:
      name: myapp-pod
      labels:
          app: myapp
          type: front-app
    spec:
      containers:
        - name: nginx-container
          image: nginx:perl

  replicas: 6

  selector:
    matchLabels:
      app: myapp

# Deploymentをアップデートする
kubectl apply -f deployment-definition.yml --record


# Deploymentの情報を取得する（nginxのバージョンが健康されている事を確認する）
$ kubectl describe deploy
Name:                   myapp-deploy
Namespace:              default
CreationTimestamp:      Thu, 28 Mar 2019 14:42:02 +0900
Labels:                 app=myapp
                        type=front-end
Annotations:            deployment.kubernetes.io/revision=2
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"myapp","type":"front-end"},"name":"myapp-deploy","namespace":...
Selector:               app=myapp
Replicas:               6 desired | 6 updated | 6 total | 6 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=myapp
           type=front-app
  Containers:
   nginx-container:
    Image:        nginx:perl
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   myapp-deploy-69cc7ccff8 (6/6 replicas created)
Events:
  Type    Reason             Age                From                   Message
  ----    ------             ----               ----                   -------
  Normal  ScalingReplicaSet  3m                 deployment-controller  Scaled up replica set myapp-deploy-f7cdcb579 to 6
  Normal  ScalingReplicaSet  24s                deployment-controller  Scaled up replica set myapp-deploy-69cc7ccff8 to 2
  Normal  ScalingReplicaSet  24s                deployment-controller  Scaled down replica set myapp-deploy-f7cdcb579 to 5
  Normal  ScalingReplicaSet  24s                deployment-controller  Scaled up replica set myapp-deploy-69cc7ccff8 to 3
  Normal  ScalingReplicaSet  18s                deployment-controller  Scaled down replica set myapp-deploy-f7cdcb579 to 4
  Normal  ScalingReplicaSet  18s                deployment-controller  Scaled up replica set myapp-deploy-69cc7ccff8 to 4
  Normal  ScalingReplicaSet  18s                deployment-controller  Scaled down replica set myapp-deploy-f7cdcb579 to 3
  Normal  ScalingReplicaSet  18s                deployment-controller  Scaled up replica set myapp-deploy-69cc7ccff8 to 5
  Normal  ScalingReplicaSet  18s                deployment-controller  Scaled down replica set myapp-deploy-f7cdcb579 to 2
  Normal  ScalingReplicaSet  12s (x3 over 17s)  deployment-controller  (combined from similar events): Scaled down replica set myapp-deploy-f7cdcb579 to 0

# Rollout履歴を確認する
$ kubectl rollout history deployment.apps/myapp-deploy
deployments "myapp-deploy"
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=deployment-definition.yml --record=true
2         kubectl apply --filename=deployment-definition.yml --record=true


```


###テスト / nginxコンテナイメージのタグを戻す

```bash:
# リビジョンを一つ戻す
$ kubectl rollout undo deployment.apps/myapp-deploy

# 履歴を確認する
$ kubectl rollout history deployment.apps/myapp-deploy
deployments "myapp-deploy"
REVISION  CHANGE-CAUSE
2         kubectl apply --filename=deployment-definition.yml --record=true
3         kubectl apply --filename=deployment-definition.yml --record=true

# nginxイメージのタグが変更されたのを確認する
$ kubectl describe deploy
Name:                   myapp-deploy
Namespace:              default
CreationTimestamp:      Thu, 28 Mar 2019 14:59:02 +0900
Labels:                 app=myapp
                        type=front-end
Annotations:            deployment.kubernetes.io/revision=3
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=deployment-definition.yml...
                        kubernetes.io/change-cause=kubectl apply --filename=deployment-definition.yml --record=true
Selector:               app=myapp
Replicas:               6 desired | 6 updated | 6 total | 6 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=myapp
           type=front-app
  Containers:
   nginx-container:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   myapp-deploy-f7cdcb579 (6/6 replicas created)
Events:
  Type    Reason              Age                From                   Message
  ----    ------              ----               ----                   -------
  Normal  ScalingReplicaSet   6m                 deployment-controller  Scaled up replica set myapp-deploy-f7cdcb579 to 6
  Normal  ScalingReplicaSet   4m                 deployment-controller  Scaled up replica set myapp-deploy-69cc7ccff8 to 2
  Normal  ScalingReplicaSet   4m                 deployment-controller  Scaled down replica set myapp-deploy-f7cdcb579 to 5
  Normal  ScalingReplicaSet   4m                 deployment-controller  Scaled up replica set myapp-deploy-69cc7ccff8 to 3
  Normal  ScalingReplicaSet   4m                 deployment-controller  Scaled down replica set myapp-deploy-f7cdcb579 to 4
  Normal  ScalingReplicaSet   4m                 deployment-controller  Scaled up replica set myapp-deploy-69cc7ccff8 to 4
  Normal  ScalingReplicaSet   4m                 deployment-controller  Scaled down replica set myapp-deploy-f7cdcb579 to 3
  Normal  ScalingReplicaSet   4m                 deployment-controller  Scaled up replica set myapp-deploy-69cc7ccff8 to 5
  Normal  ScalingReplicaSet   4m                 deployment-controller  Scaled down replica set myapp-deploy-f7cdcb579 to 2
  Normal  DeploymentRollback  14s                deployment-controller  Rolled back deployment "myapp-deploy" to revision 1
  Normal  ScalingReplicaSet   10s (x14 over 4m)  deployment-controller  (combined from similar events): Scaled down replica set myapp-deploy-69cc7ccff8 to 0

```