kubectl editするときの--record
オプションについて挙動を調べたのでメモ。
TOC
–recordオプションとは
--record
の説明は以下のとおりです。
--record=false: Record current kubectl command in the resource annotation. If set to false, do not record the
command. If set to true, record the command. If not set, default to updating the existing annotation value only if one
already exists.
つまり、動作は以下のようになります。
- デフォルトだと無効化。
- 有効化されると使用したコマンドが記録される。
- もし、このオプションがセットされていない場合には、既存のアノテーションがあればアップデートすると書いてあります。
- ただし、kubectlを使う場合は、自動的にオプションが指定される
Deploymentでの–recordオプション
Deploymentを作成して、–recordオプションありなしでイメージを変更してみます。
–recordあり
Deploymentの作成
# kubectl create deploy nginx --image=nginx:1.19.0
deployment.apps/nginx created
作成されたDeploymentは下記のとおりです。
# kubectl describe deploy nginx
Name: nginx
Namespace: default
CreationTimestamp: Fri, 21 May 2021 10:28:15 +0900
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.19.0
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-85b45874d9 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6m27s deployment-controller Scaled up replica set nginx-85b45874d9 to 1
続いて、kubectl edit --record
を使ってイメージを変えます。
# kubectl edit deploy nginx --record
deployment.apps/nginx edited
変更後のDeploymentは下記のとおりになります。
# kubectl describe deploy nginx
Name: nginx
Namespace: default
CreationTimestamp: Fri, 21 May 2021 10:28:15 +0900
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 2
kubernetes.io/change-cause: kubectl edit deploy nginx --record=true
Selector: app=nginx
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.19.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-69576c95db (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 8m54s deployment-controller Scaled up replica set nginx-85b45874d9 to 1
Normal ScalingReplicaSet 7s deployment-controller Scaled up replica set nginx-69576c95db to 1
Normal ScalingReplicaSet 6s deployment-controller Scaled down replica set nginx-85b45874d9 to 0
--record
を利用するとannotationに下記の項目が追加されています。
kubernetes.io/change-cause: kubectl edit deploy nginx --record=true
また、kubectl rollout
コマンドで状態や履歴が確認できます。
# kubectl rollout status deploy nginx
deployment "nginx" successfully rolled out
# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 kubectl edit deploy nginx --record=true
–recordなし
同様の操作を行います。
# kubectl create deploy nginx --image=nginx:1.19.0
deployment.apps/nginx created
作成したDeploymentを確認。
# kubectl describe deploy nginx
Name: nginx
Namespace: default
CreationTimestamp: Fri, 21 May 2021 10:42:25 +0900
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.19.0
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-85b45874d9 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6s deployment-controller Scaled up replica set nginx-85b45874d9 to 1
続いて、--record
なしで編集します。
# kubectl edit deploy nginx
deployment.apps/nginx edited
編集されたDeploymentを確認します。
# kubectl describe deploy nginx
Name: nginx
Namespace: default
CreationTimestamp: Fri, 21 May 2021 10:42:25 +0900
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 2
Selector: app=nginx
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.19.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-69576c95db (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 20s deployment-controller Scaled up replica set nginx-85b45874d9 to 1
Normal ScalingReplicaSet 4s deployment-controller Scaled up replica set nginx-69576c95db to 1
Normal ScalingReplicaSet 3s deployment-controller Scaled down replica set nginx-85b45874d9 to 0
--record
を使用していないので、Annotationが追加されておりません。kubectl rollout
でも同様に履歴やステータスが見れますが、変更理由が記載されません。
# kubectl rollout status deploy nginx
deployment "nginx" successfully rolled out
# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
PersistentVolumeClaimでの–recordオプション
今回使うPVCとPodのマニフェストは以下のとおりです。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-test
spec:
storageClassName: standard
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-with-pvc
spec:
volumes:
- name: pv-volume
persistentVolumeClaim:
claimName: pvc-test
containers:
- name: nginx-with-pvc
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: pv-volume
PVCの容量拡大が現在サポートされているので、容量拡大時に--record
を指定するとどうなるか見ていきます。
PodとPVCの作成が完了すると、下記の通りPodが正しく起動します。
# kubectl get pv,pvc,pod
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvc-8b1ca614-926a-4069-85e7-154c186a8552 1Gi RWO Delete Bound default/pvc-test standard 68s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-test Bound pvc-8b1ca614-926a-4069-85e7-154c186a8552 1Gi RWO standard 69s
NAME READY STATUS RESTARTS AGE
pod/nginx-with-pvc 1/1 Running 0 69s
続いて、kubectl edit --record
を使って2Giに変更します。
# kubectl edit pvc pvc-test --record
persistentvolumeclaim/pvc-test edited
Deploymentと同様にkubernetes.io/change-cause
のアノテーションが追加されています。
# kubectl describe persistentvolumeclaim/pvc-test
Name: pvc-test
Namespace: default
StorageClass: standard
Status: Bound
Volume: pvc-8b1ca614-926a-4069-85e7-154c186a8552
Labels: <none>
Annotations: kubernetes.io/change-cause: kubectl edit pvc pvc-test --record=true
pv.kubernetes.io/bind-completed: yes
pv.kubernetes.io/bound-by-controller: yes
volume.beta.kubernetes.io/storage-provisioner: kubernetes.io/gce-pd
volume.kubernetes.io/storage-resizer: kubernetes.io/gce-pd
Finalizers: [kubernetes.io/pvc-protection]
Capacity: 1Gi
Access Modes: RWO
VolumeMode: Filesystem
Used By: nginx-with-pvc
Conditions:
Type Status LastProbeTime LastTransitionTime Reason Message
---- ------ ----------------- ------------------ ------ -------
FileSystemResizePending True Mon, 01 Jan 0001 00:00:00 +0000 Fri, 21 May 2021 02:12:04 +0000 Waiting for user to (re-)start a pod to finish file system resize of volume on node.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ProvisioningSucceeded 3m19s persistentvolume-controller Successfully provisioned volume pvc-8b1ca614-926a-4069-85e7-154c186a8552 using kubernetes.io/gce-pd
Conditionsにあるとおり、Podがリスタートされるまで新しいPVがアタッチされないので、下記の通りPVは2GiですがPVCは1Giのままです。
$ kubectl get pv,pvc,pod
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvc-8b1ca614-926a-4069-85e7-154c186a8552 2Gi RWO Delete Bound default/pvc-test standard 3m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-test Bound pvc-8b1ca614-926a-4069-85e7-154c186a8552 1Gi RWO standard 3m1s
NAME READY STATUS RESTARTS AGE
pod/nginx-with-pvc 1/1 Running 0 3m1s
Podを再作成します。
# kubectl delete pod nginx-with-pvc
pod "nginx-with-pvc" deleted
# cat << EOF | kubectl apply -f-
apiVersion: v1
kind: Pod
metadata:
name: nginx-with-pvc
spec:
volumes:
- name: pv-volume
persistentVolumeClaim:
claimName: pvc-test
containers:
- name: nginx-with-pvc
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: pv-volume
EOF
pod/nginx-with-pvc created
作成されたPodを確認します。
kubectl get pv,pvc,pod
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvc-8b1ca614-926a-4069-85e7-154c186a8552 2Gi RWO Delete Bound default/pvc-test standard 7m38s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-test Bound pvc-8b1ca614-926a-4069-85e7-154c186a8552 2Gi RWO standard 7m39s
NAME READY STATUS RESTARTS AGE
pod/nginx-with-pvc 1/1 Running 0 2m7s
PVCも正しく2Giに変更されました。
最後にkubectl rollout
についてですが、PVC向けにはサポートされないため下記のようなエラーが見られます。
# kubectl rollout status pvc/pvc-test
error: no status viewer has been implemented for PersistentVolumeClaim
# kubectl rollout history pvc/pvc-test
error: error retrieving history for "PersistentVolumeClaim", no visitor method exists for { PersistentVolumeClaim}