このセクションでは、DaemonSet を OnDelete でアップデートする方法について紹介します。 OnDelete は、DaemonSet のマニフェストを変更してイメージなどを差し替えたとしても既存の Pod のアップデートは行われません。DaemonSet は死活監視やログ転送といった用途に利用されることが多いため、アップデートを次回再作成時や手動による任意のタイミングでアップデートできるようになっています。

サンプルのマニフェストファイルを新規作成し、以下コードを記述します。

[root@kube-master sample-daemonset]# vi sample-daemonset-ondelete.yaml 

spec の updateStrategy に type: OnDelete を指定します。

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: sample-daemonset-ondelete
spec:
  updateStrategy:
    type: OnDelete
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.13

Master サーバーから 作成したマニフェストを実行し、Kubernetes クラスタ上にリソースを作成します。

[root@kube-master sample-daemonset]# kubectl apply -f sample-daemonset-ondelete.yaml 
daemonset.apps/sample-daemonset-ondelete created
[root@kube-master sample-daemonset]# 

Master サーバーから Kubernetes クラスタ上の Pod リソースを確認します。各ノードに Pod が 一つずつ起動していることが確認できます。

[root@kube-master sample-daemonset]# kubectl get pods -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
sample-daemonset-ondelete-fntwh   1/1     Running   0          3s    10.244.2.79    kube-work2   <none>           <none>
sample-daemonset-ondelete-hnh42   1/1     Running   0          3s    10.244.1.150   kube-work1   <none>           <none>
[root@kube-master sample-daemonset]# 

Master サーバーから Kubernetes クラスタ上の Pod の詳細情報を確認します。ここでは、各 Pod 上のコンテナで nginx 1.3 のイメージが起動していることが確認できます。

[root@kube-master sample-daemonset]# kubectl describe pods sample-daemonset-ondelete-fntwh
Name:               sample-daemonset-ondelete-fntwh
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               kube-work2/192.168.25.102
Start Time:         Sat, 02 Feb 2019 00:13:37 +0900
Labels:             app=sample-app
                    controller-revision-hash=679c64dbd8
                    pod-template-generation=1
Annotations:        <none>
Status:             Running
IP:                 10.244.2.79
Controlled By:      DaemonSet/sample-daemonset-ondelete
Containers:
  nginx-container:
    Container ID:   docker://935960b073eb6d66001e22eced05e5adb6866993857132d1bf821c529833898a
    Image:          nginx:1.13
    Image ID:       docker-pullable://nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 02 Feb 2019 00:13:39 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75dfq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75dfq:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75dfq
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/disk-pressure:NoSchedule
                 node.kubernetes.io/memory-pressure:NoSchedule
                 node.kubernetes.io/not-ready:NoExecute
                 node.kubernetes.io/unreachable:NoExecute
                 node.kubernetes.io/unschedulable:NoSchedule
Events:
  Type    Reason     Age   From                 Message
  ----    ------     ----  ----                 -------
  Normal  Scheduled  119s  default-scheduler    Successfully assigned default/sample-daemonset-ondelete-fntwh to kube-work2
  Normal  Pulled     117s  kubelet, kube-work2  Container image "nginx:1.13" already present on machine
  Normal  Created    117s  kubelet, kube-work2  Created container
  Normal  Started    117s  kubelet, kube-work2  Started container
[root@kube-master sample-daemonset]# 
[root@kube-master sample-daemonset]# kubectl describe pods sample-daemonset-ondelete-hnh42
Name:               sample-daemonset-ondelete-hnh42
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               kube-work1/192.168.25.101
Start Time:         Sat, 02 Feb 2019 00:13:37 +0900
Labels:             app=sample-app
                    controller-revision-hash=679c64dbd8
                    pod-template-generation=1
Annotations:        <none>
Status:             Running
IP:                 10.244.1.150
Controlled By:      DaemonSet/sample-daemonset-ondelete
Containers:
  nginx-container:
    Container ID:   docker://75e2f8785c840b79105e209ce9125a55e1d245c02f83005f8ecd91cafae9db2c
    Image:          nginx:1.13
    Image ID:       docker-pullable://nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 02 Feb 2019 00:13:39 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75dfq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75dfq:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75dfq
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/disk-pressure:NoSchedule
                 node.kubernetes.io/memory-pressure:NoSchedule
                 node.kubernetes.io/not-ready:NoExecute
                 node.kubernetes.io/unreachable:NoExecute
                 node.kubernetes.io/unschedulable:NoSchedule
Events:
  Type    Reason     Age    From                 Message
  ----    ------     ----   ----                 -------
  Normal  Scheduled  2m26s  default-scheduler    Successfully assigned default/sample-daemonset-ondelete-hnh42 to kube-work1
  Normal  Pulled     2m24s  kubelet, kube-work1  Container image "nginx:1.13" already present on machine
  Normal  Created    2m24s  kubelet, kube-work1  Created container
  Normal  Started    2m24s  kubelet, kube-work1  Started container
[root@kube-master sample-daemonset]# 

サンプルのマニフェストファイルを編集し、コンテナイメージを変更します。

[root@kube-master sample-daemonset]# vi sample-daemonset-ondelete.yaml 

spec の containers イメージ を nginx:1.3 から nginx:1.12 に変更します。

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: sample-daemonset-ondelete
spec:
  updateStrategy:
    type: OnDelete
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.12

Master サーバーから 変更したマニフェストを実行し、Kubernetes クラスタ上にリソースを更新してみます。

[root@kube-master sample-daemonset]# kubectl apply -f sample-daemonset-ondelete.yaml --record
daemonset.apps/sample-daemonset-ondelete configured
[root@kube-master sample-daemonset]# 

Master サーバーから Kubernetes クラスタ上の Pod の詳細情報を確認します。ここでは、各 Pod 上でのコンテナーイメージが nginx 1.3 のままである(変更されていない)ことが確認できます。

[root@kube-master sample-daemonset]# kubectl describe pods sample-daemonset-ondelete-fntwh
Name:               sample-daemonset-ondelete-fntwh
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               kube-work2/192.168.25.102
Start Time:         Sat, 02 Feb 2019 00:13:37 +0900
Labels:             app=sample-app
                    controller-revision-hash=679c64dbd8
                    pod-template-generation=1
Annotations:        <none>
Status:             Running
IP:                 10.244.2.79
Controlled By:      DaemonSet/sample-daemonset-ondelete
Containers:
  nginx-container:
    Container ID:   docker://935960b073eb6d66001e22eced05e5adb6866993857132d1bf821c529833898a
    Image:          nginx:1.13
    Image ID:       docker-pullable://nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 02 Feb 2019 00:13:39 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75dfq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75dfq:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75dfq
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/disk-pressure:NoSchedule
                 node.kubernetes.io/memory-pressure:NoSchedule
                 node.kubernetes.io/not-ready:NoExecute
                 node.kubernetes.io/unreachable:NoExecute
                 node.kubernetes.io/unschedulable:NoSchedule
Events:
  Type    Reason     Age    From                 Message
  ----    ------     ----   ----                 -------
  Normal  Scheduled  4m48s  default-scheduler    Successfully assigned default/sample-daemonset-ondelete-fntwh to kube-work2
  Normal  Pulled     4m46s  kubelet, kube-work2  Container image "nginx:1.13" already present on machine
  Normal  Created    4m46s  kubelet, kube-work2  Created container
  Normal  Started    4m46s  kubelet, kube-work2  Started container
[root@kube-master sample-daemonset]#
[root@kube-master sample-daemonset]# kubectl describe pods sample-daemonset-ondelete-hnh42
Name:               sample-daemonset-ondelete-hnh42
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               kube-work1/192.168.25.101
Start Time:         Sat, 02 Feb 2019 00:13:37 +0900
Labels:             app=sample-app
                    controller-revision-hash=679c64dbd8
                    pod-template-generation=1
Annotations:        <none>
Status:             Running
IP:                 10.244.1.150
Controlled By:      DaemonSet/sample-daemonset-ondelete
Containers:
  nginx-container:
    Container ID:   docker://75e2f8785c840b79105e209ce9125a55e1d245c02f83005f8ecd91cafae9db2c
    Image:          nginx:1.13
    Image ID:       docker-pullable://nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 02 Feb 2019 00:13:39 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75dfq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75dfq:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75dfq
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/disk-pressure:NoSchedule
                 node.kubernetes.io/memory-pressure:NoSchedule
                 node.kubernetes.io/not-ready:NoExecute
                 node.kubernetes.io/unreachable:NoExecute
                 node.kubernetes.io/unschedulable:NoSchedule
Events:
  Type    Reason     Age    From                 Message
  ----    ------     ----   ----                 -------
  Normal  Scheduled  4m56s  default-scheduler    Successfully assigned default/sample-daemonset-ondelete-hnh42 to kube-work1
  Normal  Pulled     4m54s  kubelet, kube-work1  Container image "nginx:1.13" already present on machine
  Normal  Created    4m54s  kubelet, kube-work1  Created container
  Normal  Started    4m54s  kubelet, kube-work1  Started container
[root@kube-master sample-daemonset]#

Master サーバーから 1つの Pod( sample-daemonset-ondelete-fntwh )だけを再作成してアップデートします。

[root@kube-master sample-daemonset]# kubectl delete pod sample-daemonset-ondelete-fntwh
pod "sample-daemonset-ondelete-fntwh" deleted
[root@kube-master sample-daemonset]# 

Master サーバーから Kubernetes クラスタ上の Pod リソースを確認します。sample-daemonset-ondelete-fntwh の Pod が削除され、sample-daemonset-ondelete-n85zk の Pod が新規で作成されていることが確認できます。

[root@kube-master sample-daemonset]# kubectl get pods -o wide
NAME                              READY   STATUS    RESTARTS   AGE     IP             NODE         NOMINATED NODE   READINESS GATES
sample-daemonset-ondelete-hnh42   1/1     Running   0          6m49s   10.244.1.150   kube-work1   <none>           <none>
sample-daemonset-ondelete-n85zk   1/1     Running   0          29s     10.244.2.80    kube-work2   <none>           <none>
[root@kube-master sample-daemonset]# 

Master サーバーから Kubernetes クラスタ上の Pod(  sample-daemonset-ondelete-n85zk )の詳細情報を確認します。ここでは、Pod 上のコンテナーのイメージが nginx 1.2 であることが確認できます。

[root@kube-master sample-daemonset]# kubectl describe pods sample-daemonset-ondelete-n85zk
Name:               sample-daemonset-ondelete-n85zk
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               kube-work2/192.168.25.102
Start Time:         Sat, 02 Feb 2019 00:19:57 +0900
Labels:             app=sample-app
                    controller-revision-hash=d88f4f445
                    pod-template-generation=2
Annotations:        <none>
Status:             Running
IP:                 10.244.2.80
Controlled By:      DaemonSet/sample-daemonset-ondelete
Containers:
  nginx-container:
    Container ID:   docker://0605da7b7f2ad45ceba2052f5a3523cafabef43e4baa326cc2a47c7118e8209f
    Image:          nginx:1.12
    Image ID:       docker-pullable://nginx@sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 02 Feb 2019 00:19:58 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75dfq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75dfq:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75dfq
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/disk-pressure:NoSchedule
                 node.kubernetes.io/memory-pressure:NoSchedule
                 node.kubernetes.io/not-ready:NoExecute
                 node.kubernetes.io/unreachable:NoExecute
                 node.kubernetes.io/unschedulable:NoSchedule
Events:
  Type    Reason     Age   From                 Message
  ----    ------     ----  ----                 -------
  Normal  Scheduled  47s   default-scheduler    Successfully assigned default/sample-daemonset-ondelete-n85zk to kube-work2
  Normal  Pulled     46s   kubelet, kube-work2  Container image "nginx:1.12" already present on machine
  Normal  Created    46s   kubelet, kube-work2  Created container
  Normal  Started    46s   kubelet, kube-work2  Started container
[root@kube-master sample-daemonset]# 

Master サーバーから 作成した DaemonSet リソースを削除します。

[root@kube-master sample-daemonset]# kubectl delete daemonset sample-daemonset-ondelete
daemonset.extensions "sample-daemonset-ondelete" deleted
[root@kube-master sample-daemonset]# 

Master サーバーから Kubernetes クラスタ上の Pod リソースを確認します。ここでは、DaemonSet リソースで作成した Pod が削除されていることが確認できます。

[root@kube-master sample-daemonset]# kubectl get pods -o wide
No resources found.
[root@kube-master sample-daemonset]#