Preview changes before applying a yaml file to a kubernetes cluster

Applying changes to a kubernetes cluster using yaml files it's very similar to applying changes to source code: It's always best to have the diff for being able to actually see the changed that we are going to make.

That's exactly what kubectl diff does: It shows that changes that an kubectl apply would make if we apply them but without actually making any changes. If there's no actual change it will just return a 0 as an exit code without any output:

$ kubectl diff -f 03-app.yaml 

In case there are changed it will output a regular diff output. Bear in mind that when you are changing a setting, some other metadata is also updated so you will see more differences than the actual change you made. For example, in the example below we just modified the replicas setting. All the other changes are related to the metadata that it's going to be changed due to this change:

$ kubectl diff -f 03-app.yaml 
diff -u -N /tmp/LIVE-673797332/apps.v1.Deployment.kube-system.ampa-3.2 /tmp/MERGED-952513059/apps.v1.Deployment.kube-system.ampa-3.2
--- /tmp/LIVE-673797332/apps.v1.Deployment.kube-system.ampa-3.2 2020-12-30 10:51:47.683825513 +0100
+++ /tmp/MERGED-952513059/apps.v1.Deployment.kube-system.ampa-3.2 2020-12-30 10:51:47.699825466 +0100
@@ -6,7 +6,7 @@
     kubectl.kubernetes.io/last-applied-configuration: |
       {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"ampa-3.2"},"name":"ampa-3.2","namespace":"kube-system"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"ampa-3.2"}},"template":{"metadata":{"labels":{"app":"ampa-3.2"}},"spec":{"containers":[{"image":"172.18.1.46:5000/ampa:3.2","name":"ampa","ports":[{"containerPort":8000,"name":"http"}],"resources":{"limits":{"cpu":"500m","memory":"200Mi"},"requests":{"cpu":"250m","memory":"50Mi"}},"volumeMounts":[{"mountPath":"/code/xls","name":"xls-data-ampa"},{"mountPath":"/code/static","name":"static-ampa"}]}],"volumes":[{"name":"xls-data-ampa","persistentVolumeClaim":{"claimName":"nfs-ampa-pvc"}},{"name":"static-ampa","persistentVolumeClaim":{"claimName":"nfs-static-pvc"}}]}}}}
   creationTimestamp: "2020-12-08T20:15:30Z"
-  generation: 3
+  generation: 4
   labels:
     app: ampa-3.2
   managedFields:
@@ -15,6 +15,39 @@
     fieldsV1:
       f:metadata:
         f:annotations:
+          f:deployment.kubernetes.io/revision: {}
+      f:status:
+        f:availableReplicas: {}
+        f:conditions:
+          .: {}
+          k:{"type":"Available"}:
+            .: {}
+            f:lastTransitionTime: {}
+            f:lastUpdateTime: {}
+            f:message: {}
+            f:reason: {}
+            f:status: {}
+            f:type: {}
+          k:{"type":"Progressing"}:
+            .: {}
+            f:lastTransitionTime: {}
+            f:lastUpdateTime: {}
+            f:message: {}
+            f:reason: {}
+            f:status: {}
+            f:type: {}
+        f:observedGeneration: {}
+        f:readyReplicas: {}
+        f:replicas: {}
+        f:updatedReplicas: {}
+    manager: k3s
+    operation: Update
+    time: "2020-12-16T20:42:51Z"
+  - apiVersion: apps/v1
+    fieldsType: FieldsV1
+    fieldsV1:
+      f:metadata:
+        f:annotations:
           .: {}
           f:kubectl.kubernetes.io/last-applied-configuration: {}
         f:labels:
@@ -96,40 +129,7 @@
                   f:claimName: {}
     manager: kubectl-client-side-apply
     operation: Update
-    time: "2020-12-08T20:23:56Z"
-  - apiVersion: apps/v1
-    fieldsType: FieldsV1
-    fieldsV1:
-      f:metadata:
-        f:annotations:
-          f:deployment.kubernetes.io/revision: {}
-      f:status:
-        f:availableReplicas: {}
-        f:conditions:
-          .: {}
-          k:{"type":"Available"}:
-            .: {}
-            f:lastTransitionTime: {}
-            f:lastUpdateTime: {}
-            f:message: {}
-            f:reason: {}
-            f:status: {}
-            f:type: {}
-          k:{"type":"Progressing"}:
-            .: {}
-            f:lastTransitionTime: {}
-            f:lastUpdateTime: {}
-            f:message: {}
-            f:reason: {}
-            f:status: {}
-            f:type: {}
-        f:observedGeneration: {}
-        f:readyReplicas: {}
-        f:replicas: {}
-        f:updatedReplicas: {}
-    manager: k3s
-    operation: Update
-    time: "2020-12-16T20:42:51Z"
+    time: "2020-12-30T09:51:47Z"
   name: ampa-3.2
   namespace: kube-system
   resourceVersion: "3866194"
@@ -137,7 +137,7 @@
   uid: 53cd9b9b-ce8e-4bdf-b94b-84d2b9b14cc3
 spec:
   progressDeadlineSeconds: 600
-  replicas: 1
+  replicas: 2
   revisionHistoryLimit: 10
   selector:
     matchLabels:

Posted on 07/01/2021

Categories