Tuesday, 14 October 2025

Fixing Broken Kubernetes YAML Templates: Real-World Debugging & Best Practices

Our course you can check :-   Udemy course  


Ques:-  

DevOps team members was working on to update an existing Kubernetes template. Somehow, he made some mistakes in the template and it is failing while applying. We need to fix this as soon as possible, so take a look into it and make sure you are able to apply it without any issues. Also, do not remove any component from the template like pods/deployments/volumes etc.


Ans:-

                                                            Wrong yaml file:-                                                         

apiVersion: apps/v1 

kind: Persistentvolume 

metadata:

  name: mysql-pv

  labels:

    type: local

spec:

  storageClassName: standard       

  capacity:

    storage: 250Mi

  accessModes: ReadWriteOnce 

  hostPath:                       

    path: "/mnt/data"

  persistentVolumeReclaimPolicy: 

    - Retain   

---    

apiVersion: apps/v1 

kind: Persistentvolumeclaim       

metadata:                          

  name: mysql-pv-claim

  labels:

    app: mysql-app 

spec:                              

  storageClassName: standard       

  accessModes: ReadWriteOnce             

  resources:

    requests:

      storage: 250MB 

---

apiVersion: v1                    

kind: Service                      

metadata:

  name: mysql         

  labels:             

  app: mysql-app  

spec:

  type: NodePort

  ports:

    - targetPort: 3306

      port: 3306

      nodePort: 30011

  selector:                       

    app: mysql_app

    tier: mysql

---

apiVersion: app/v1 

kind: Deployment                    

metadata:

  name: mysql-deployment           

  labels:                         

  app: mysql-app   

spec:

  selector:

    matchlabels:                  

    app: mysql-app 

    tier: mysql 

  strategy:

    type: Recreate 

  template:         

    metadata:

      labels:        

        app: mysql-app

        tier: mysql

    spec:            

      containers:

      - image: mysql:5.6 

        name: mysql

        env:              

        - name: MYSQL_ROOT_PASSWORD 

          valueFrom:     

            secretKeyRef:

            name: mysql-root-pass 

              key: password

        - name: MYSQL_DATABASE

          valueFrom:

            secretKeyRef:

            name: mysql-db-url 

              key: database

        - name: MYSQL_USER

          valueFrom:

            secretKeyRef:

            name: mysql-user-pass 

              key: username

        - name: MYSQL_PASSWORD

          valueFrom:

            secretKeyRef:

            name: mysql-user-pass 

              key: password

        ports:

        - containerPort: 3306              

          name: mysql

        volumeMounts:

        - name: mysql-persistent-storage  

          mountPath: /var/lib/mysql

      volumes:                        

      - name: mysql-persistent-storage

          persistentVolumeClaim: 

          claimName: mysql-pv-claim


                                                        Correct yaml files:-                                                                 

apiVersion: v1

kind: PersistentVolume

metadata:

  name: mysql-pv

  labels:

    type: local

spec:

  storageClassName: standard

  capacity:

    storage: 250Mi

  accessModes:

    - ReadWriteOnce

  hostPath:

    path: "/mnt/data"

  persistentVolumeReclaimPolicy: Retain

---

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: mysql-pv-claim

  labels:

    app: mysql-app

spec:

  storageClassName: standard

  accessModes:

    - ReadWriteOnce

  resources:

    requests:

      storage: 250Mi

---

apiVersion: v1

kind: Service

metadata:

  name: mysql

  labels:

    app: mysql-app

spec:

  type: NodePort

  ports:

    - targetPort: 3306

      port: 3306

      nodePort: 30011

  selector:

    app: mysql-app

    tier: mysql

---

apiVersion: apps/v1

kind: Deployment

metadata:

  name: mysql-deployment

  labels:

    app: mysql-app

spec:

  selector:

    matchLabels:

      app: mysql-app

      tier: mysql

  strategy:

    type: Recreate

  template:

    metadata:

      labels:

        app: mysql-app

        tier: mysql

    spec:

      containers:

        - image: mysql:5.6

          name: mysql

          env:

            - name: MYSQL_ROOT_PASSWORD

              valueFrom:

                secretKeyRef:

                  name: mysql-root-pass

                  key: password

            - name: MYSQL_DATABASE

              valueFrom:

                secretKeyRef:

                  name: mysql-db-url

                  key: database

            - name: MYSQL_USER

              valueFrom:

                secretKeyRef:

                  name: mysql-user-pass

                  key: username

            - name: MYSQL_PASSWORD

              valueFrom:

                secretKeyRef:

                 -pass

                  key: password

          ports:

            - containerPort: 3306

              name: mysql

          volumeMounts:

            - name: mysql-persistent-storage

              mountPath: /var/lib/mysql

      volumes:

        - name: mysql-persistent-storage

          persistentVolumeClaim:

             claimName: mysql-pv-claim


Key Fixes Made:

  • Corrected apiVersion for PersistentVolume and PersistentVolumeClaim to v1.
  • Fixed indentation issues (e.g., persistentVolumeClaim under volumes).
  • Corrected matchLabels (was matchlabels) in Deployment.
  • Fixed accessModes and persistentVolumeReclaimPolicy formatting.
  • Ensured storage units match (250Mi instead of 250MB).
  • Corrected apiVersion for Deployment from app/v1 to apps/v1.

1. PersistentVolume Section

FieldOldNewReason
apiVersionapps/v1v1PersistentVolume is a core resource, not part of apps/v1.
kindPersistentvolumePersistentVolumeCapitalization must match Kubernetes spec.
accessModesReadWriteOnce- ReadWriteOnceMust be a list (array) format.
persistentVolumeReclaimPolicy- RetainRetainShould be a string, not a list.

2. PersistentVolumeClaim Section

FieldOldNewReason
apiVersionapps/v1v1PVC is a core resource, not part of apps/v1.
kindPersistentvolumeclaimPersistentVolumeClaimCorrect capitalization.
accessModesReadWriteOnce- ReadWriteOnceMust be a list.
storage250MB250MiKubernetes expects binary units like Mi, not MB.

3. Service Section

FieldOldNewReason
selector.appmysql_appmysql-appMust match the label used in Deployment and PVC.

4. Deployment Section

FieldOldNewReason
apiVersionapp/v1apps/v1Correct API version for Deployments.
matchlabelsmatchlabelsmatchLabelsCorrect field name casing.
env.secretKeyRef indentationIncorrect (extra indent under name)Corrected indentationYAML is indentation-sensitive; incorrect nesting breaks parsing.
volumes.persistentVolumeClaimIncorrect indentationCorrectedMust be properly nested under volumes.

5. General Formatting Fixes

IssueOldNewReason
IndentationSeveral places had extra/missing spacesAll indentation correctedYAML requires strict indentation for parsing.
Consistency in labelsMixed use of mysql_app and mysql-appUnified to mysql-appLabels must match across resources for selectors to work.



Conclusion:-

In this hands-on video, we walk through a real-world scenario where a DevOps team encounters a broken Kubernetes YAML template. You'll learn how to identify and fix common issues in Kubernetes manifests—without removing any components like PersistentVolumes, Deployments, Services, or PVCs.

We’ll cover:

  • Correcting API versions and resource kinds
  • Fixing indentation and formatting errors
  • Ensuring label consistency across resources
  • Understanding access modes and storage units
  • Validating the final YAML for successful deployment

This tutorial is perfect for DevOps engineers, SREs, and Kubernetes learners who want to sharpen their troubleshooting skills with practical examples.


No comments:

Post a Comment