8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kubectl diffを使ってみる

Posted at

kubectl diffを使ってみる

kubernetesのドキュメントを読んでて、kubectl apply -f hoge.ymlはよく使うけど、 kubectl diffを使ってなかったので、どのように表示させれるか気になって試した。

使用した環境

  • GKE
  • version 1.13.7-gke.19

試す

変更前のmanifest

deployment_old.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

変更後のmanifest

deployment_new.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3 # <- ここを変更する
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

deployment_oldのapply

$ kubectl apply -f deployment_old.yml
deployment.apps/nginx-deployment created

$ kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-76bf4969df-c7btx   1/1     Running   0          2m7s
nginx-deployment-76bf4969df-jvnjx   1/1     Running   0          2m7s

deployment_oldとdeployment_newとのdiff

$ kubectl diff -f deployment_new.ymlをしたところ・・・
普通にdiffだった。


diff -u -N /var/folders/3p/67s9vdms1vqd95dyynttcvyr0000gn/T/LIVE-194678655/apps.v1.Deployment.default.nginx-deployment /var/folders/3p/67s9vdms1vqd95dyynttcvyr0000gn/T/MERGED-809569490/apps.v1.Deployment.default.nginx-deployment
--- /var/folders/3p/67s9vdms1vqd95dyynttcvyr0000gn/T/LIVE-194678655/apps.v1.Deployment.default.nginx-deployment	2019-08-20 22:52:12.000000000 +0900
+++ /var/folders/3p/67s9vdms1vqd95dyynttcvyr0000gn/T/MERGED-809569490/apps.v1.Deployment.default.nginx-deployment	2019-08-20 22:52:12.000000000 +0900
@@ -6,7 +6,7 @@
     kubectl.kubernetes.io/last-applied-configuration: |
       {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"nginx-deployment","namespace":"default"},"spec":{"replicas":2,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx:1.7.9","name":"nginx","ports":[{"containerPort":80}]}]}}}}
   creationTimestamp: "2019-08-20T13:45:17Z"
-  generation: 1
+  generation: 2
   name: nginx-deployment
   namespace: default
   resourceVersion: "1135"
@@ -14,7 +14,7 @@
   uid: bef21344-c350-11e9-8bb0-42010a92004b
 spec:
   progressDeadlineSeconds: 600
-  replicas: 2
+  replicas: 3
   revisionHistoryLimit: 10
   selector:
     matchLabels:
exit status 1

感想

CDのパイプラインでも、diffで確認したあとに、applyってしたほうが安全だし、変更点に気づきやすいんじゃね?と思った。

8
1
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
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?