LoginSignup
1
0

More than 3 years have passed since last update.

Deploymentの動作を確認する 2

Last updated at Posted at 2020-03-14

はじめに

前回は、Deploymentの基本的な動作を確認しましたので、今回はDeploymentがアップデートする条件を確認してみたいと思います。

アップデートする条件

Kubernetesのドキュメントに以下のような記載があります。マニフェストのspec.templateに変更があると、ReplicaSetを新規に作成し、ローリングアップデートされます。

Deploymentのロールアウトは、DeploymentのPodテンプレート(この場合.spec.template)が変更された場合にのみトリガーされます。例えばテンプレートのラベルもしくはコンテナーイメージが更新された場合です。Deploymentのスケールのような更新では、ロールアウトはトリガーされません。
Kubernetes.io

また、spec.templateに変更があると、ReplicaSetに自動で付与される「pod-template-hashラベル」の値が変わります。

sampleDep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-pod4
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx-dep
  template: -------------------------これ以降に変更があるとアップデートする-----------------------
    metadata:                        
      labels:                                                   
        app: nginx-dep
    spec:
      containers:
        - name: nginx
          image: nginx:1.17

動作の確認

レプリカ数の変更

レプリカ数の変更では、Deploymentはアップデートされないはずです。実際に確認してみます。

初期状態の確認

$ kubectl get deployment
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
sample-pod4   3/3     3            3           14s
$ kubectl get rs
NAME                     DESIRED   CURRENT   READY   AGE
sample-pod4-597898b879   3         3         3       28s
$ kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
sample-pod4-597898b879-8ljll   1/1     Running   0          33s
sample-pod4-597898b879-rxcp4   1/1     Running   0          33s
sample-pod4-597898b879-xmpg5   1/1     Running   0          33s

レプリカ数3のDeploymentがデプロイされています。

ReplicaSetの「pod-template-hash」も確認しておきます。

$ kubectl describe rs sample-pod4-597898b879
Name:           sample-pod4-597898b879
・・・
Pod Template:
  Labels:  app=nginx-dep
           pod-template-hash=597898b879
  Containers:
   nginx:
・・・

レプリカ数の変更

レプリカ数を3から4に変更します。
変更したyamlファイルをapplyします。

$ kubectl apply -f sampleDep.yaml
deployment.apps/sample-pod4 configured

確認します。

$ kubectl get deployment
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
sample-pod4   4/4     4            4           5m40s
$ kubectl get rs
NAME                     DESIRED   CURRENT   READY   AGE
sample-pod4-597898b879   4         4         4       5m50s
$ kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
sample-pod4-597898b879-8ljll   1/1     Running   0          5m53s
sample-pod4-597898b879-rxcp4   1/1     Running   0          5m53s
sample-pod4-597898b879-wcrsz   1/1     Running   0          72s
sample-pod4-597898b879-xmpg5   1/1     Running   0          5m53s

レプリカ数が4に増えていますが、新しいReplicaSetは作成されていません。

「pod-template-hash」が変わっていないことも確認しておきます。

$ kubectl describe rs sample-pod4-597898b879 | grep 597898b879
Name:           sample-pod4-597898b879
Selector:       app=nginx-dep,pod-template-hash=597898b879
                pod-template-hash=597898b879
           pod-template-hash=597898b879
  Normal  SuccessfulCreate  7m18s  replicaset-controller  Created pod: sample-pod4-597898b879-rxcp4
  Normal  SuccessfulCreate  7m17s  replicaset-controller  Created pod: sample-pod4-597898b879-xmpg5
  Normal  SuccessfulCreate  7m17s  replicaset-controller  Created pod: sample-pod4-597898b879-8ljll
  Normal  SuccessfulCreate  2m37s  replicaset-controller  Created pod: sample-pod4-597898b879-wcrsz

今気づきましたが、ReplicaSetの名前は「[Deployment名]+[pod-template-hash値]」になってるんですね。

Deploymetのアップデート

ReplicaSetの変更ではアップデートされないことを確認しましたので、imageのバージョンを変更して、Deploymentがアップデートすることを確認します。
yamlファイルのimageのバージョンを1.16から1.17に変更してapplyします。

$ kubectl apply -f sampleDep.yaml
deployment.apps/sample-pod4 configured

確認します。

$ kubectl get rs -o wide
NAME                     DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES       SELECTOR
sample-pod4-597898b879   0         0         0       3h53m   nginx        nginx:1.16   app=nginx-dep,pod-template-hash=597898b879
sample-pod4-65fb458568   4         4         4       83s     nginx        nginx:1.17   app=nginx-dep,pod-template-hash=65fb458568
$ kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
sample-pod4-65fb458568-5nxnn   1/1     Running   0          92s
sample-pod4-65fb458568-6lgks   1/1     Running   0          94s
sample-pod4-65fb458568-gjj2q   1/1     Running   0          94s
sample-pod4-65fb458568-mml4v   1/1     Running   0          92s

1.17のReplicaSetが作られてますね。Podも入れ替わってます。
pod-template-hash値を確認します。

$ kubectl describe rs sample-pod4-65fb458568
Name:           sample-pod4-65fb458568
・・・
Pod Template:
  Labels:  app=nginx-dep
           pod-template-hash=65fb458568
  Containers:
   nginx:
    Image:        nginx:1.17
・・・

1.16とは異なるハッシュ値が割り当てられています。

再度、yamalファイルのimageのバージョンを1.16にしてapplyしてみます。

$ kubectl apply -f sampleDep.yaml
deployment.apps/sample-pod4 configured
$ kubectl get rs -o wide
NAME                     DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES       SELECTOR
sample-pod4-597898b879   4         4         4       3h59m   nginx        nginx:1.16   app=nginx-dep,pod-template-hash=597898b879
sample-pod4-65fb458568   0         0         0       7m37s   nginx        nginx:1.17   app=nginx-dep,pod-template-hash=65fb458568
$ kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
sample-pod4-597898b879-2phdh   1/1     Running   0          64s
sample-pod4-597898b879-5m6z8   1/1     Running   0          66s
sample-pod4-597898b879-bsbrk   1/1     Running   0          66s
sample-pod4-597898b879-xmpm7   1/1     Running   0          64s

yamlファイルの記述を元に戻しただけですので、ハッシュ値も同じになるはずですね。
想定通り、同じハッシュ値(1.16のReplicaSet)のレプリカ数が4になって、1.17のReplicaSetのレプリカ数が0に変わっています。

まとめ

このように、Deploymentのyamlファイルをapplyすると、spec.templateのハッシュ値が計算され、
・そのハッシュ値のReplicaSetがなければ作成し、
・あればそのReplicaSetを使用する
という動作になるようです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0