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
apiVersionforPersistentVolumeandPersistentVolumeClaimtov1. - Fixed indentation issues (e.g.,
persistentVolumeClaimundervolumes). - Corrected
matchLabels(wasmatchlabels) inDeployment. - Fixed
accessModesandpersistentVolumeReclaimPolicyformatting. - Ensured
storageunits match (250Miinstead of250MB). - Corrected
apiVersionforDeploymentfromapp/v1toapps/v1.
1. PersistentVolume Section
| Field | Old | New | Reason |
|---|---|---|---|
apiVersion | apps/v1 | v1 | PersistentVolume is a core resource, not part of apps/v1. |
kind | Persistentvolume | PersistentVolume | Capitalization must match Kubernetes spec. |
accessModes | ReadWriteOnce | - ReadWriteOnce | Must be a list (array) format. |
persistentVolumeReclaimPolicy | - Retain | Retain | Should be a string, not a list. |
2. PersistentVolumeClaim Section
| Field | Old | New | Reason |
|---|---|---|---|
apiVersion | apps/v1 | v1 | PVC is a core resource, not part of apps/v1. |
kind | Persistentvolumeclaim | PersistentVolumeClaim | Correct capitalization. |
accessModes | ReadWriteOnce | - ReadWriteOnce | Must be a list. |
storage | 250MB | 250Mi | Kubernetes expects binary units like Mi, not MB. |
3. Service Section
| Field | Old | New | Reason |
|---|---|---|---|
selector.app | mysql_app | mysql-app | Must match the label used in Deployment and PVC. |
4. Deployment Section
| Field | Old | New | Reason |
|---|---|---|---|
apiVersion | app/v1 | apps/v1 | Correct API version for Deployments. |
matchlabels | matchlabels | matchLabels | Correct field name casing. |
env.secretKeyRef indentation | Incorrect (extra indent under name) | Corrected indentation | YAML is indentation-sensitive; incorrect nesting breaks parsing. |
volumes.persistentVolumeClaim | Incorrect indentation | Corrected | Must be properly nested under volumes. |
5. General Formatting Fixes
| Issue | Old | New | Reason |
|---|---|---|---|
| Indentation | Several places had extra/missing spaces | All indentation corrected | YAML requires strict indentation for parsing. |
| Consistency in labels | Mixed use of mysql_app and mysql-app | Unified to mysql-app | Labels 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