このセクションでは、Kubernetes クラスタ上の各リソースにアノテーション( annotations )というメタデータを付与する方法について紹介します。

アノテーションは、アノテーションをもとに何か処理をするシステムコンポーネントがない場合は、ただのメモ書きとなります。リソースに対してメモをつけておきたい場合に利用できます。

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

[root@kube-master sample-annotation]# vi sample-pod.yaml 

metadata に annotations を記述しています。

apiVersion: v1
kind: Pod
metadata:
  name: sample-pod
  annotations:
    annotaions1: this is sample pod
    annotaions2: sample manifest
spec:
  containers:
    - name: nginx-container
      image: nginx:1.13

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

[root@kube-master sample-annotation]# kubectl apply -f sample-pod.yaml 
pod/sample-pod created
[root@kube-master sample-annotation]# 

Master サーバーから Kubernetes クラスタ上のリソースを確認します。 sample-pod の Pod リソースが稼働していることが確認できます。

[root@kube-master sample-annotation]# kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
sample-pod   1/1     Running   0          5s
[root@kube-master sample-annotation]# 

Master サーバーから sample-pod の リソース詳細情報を確認します。Annotations の部分にマニフェストで記述した アノテーションの情報が表示されていることが確認できます。

[root@kube-master sample-annotation]# kubectl describe pods sample-pod
Name:               sample-pod
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               kube-work1/192.168.25.101
Start Time:         Sun, 06 Jan 2019 20:04:14 +0900
Labels:             <none>
Annotations:        annotaions1: this is sample pod
                    annotaions2: sample manifest
                    kubectl.kubernetes.io/last-applied-configuration:
                      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{"annotaions1":"this is sample pod","annotaions2":"sample manifest"},"name":"sam...
Status:             Running
IP:                 10.244.1.104
Containers:
  nginx-container:
    Container ID:   docker://53f131bfdafbd96740f338807f018c642d00833bd1f9c04a002b60726a75a2ca
    Image:          nginx:1.13
    Image ID:       docker-pullable://nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sun, 06 Jan 2019 20:04:15 +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/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From                 Message
  ----    ------     ----  ----                 -------
  Normal  Scheduled  21s   default-scheduler    Successfully assigned default/sample-pod to kube-work1
  Normal  Pulled     20s   kubelet, kube-work1  Container image "nginx:1.13" already present on machine
  Normal  Created    20s   kubelet, kube-work1  Created container
  Normal  Started    20s   kubelet, kube-work1  Started container
[root@kube-master sample-annotation]#

Master サーバーから Kubernetes クラスタ上の Pod リソースを削除します。

[root@kube-master sample-annotation]# kubectl delete -f sample-pod.yaml 
pod "sample-pod" deleted
[root@kube-master sample-annotation]# 

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

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