How to get the YAML file from Kubernetes objects (Pod, Deployment, Services, and combined)?

get the YAML file from Kubernetes objects

Using the “-o yaml ” option with the “kubectl get” command will get you the latest YAML file of currently deployed objects (like Pod, Deployment, Services, and combined)

In this article, We are going to know more about, Generating YAML files for

  • Currently deployed Kubernetes objects
  • All the available Kubernetes objects combined
  • Before deploying the objects (Specific)

Generate YAML file for deployed Kubernetes objects

Pod:

To get a YAML file for the currently running pod, We can simply use “-o yaml” with the “kubectl get pod” command

Why we need this:

  • Let’s say you need to edit a pod with a new value (like a new image version). We can get the latest YAML file from the current running pod
  • Edit the Image version and redeploy the pod with the latest YAML file
  • This can help us in tracking the latest changes and also Save time in creating new YAML files from scratch
kubectl get pod nginx -n default -o yaml > pod_default.yaml
cat pod_default.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2022-11-28T11:01:21Z"
  name: nginx
  namespace: default
  resourceVersion: "673"
  uid: fc295d83-f72c-418e-bc2f-57fb9c8a68c0
spec:
  containers:
  - image: nginx:1.14.2
    imagePullPolicy: IfNotPresent
    name: nginx
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-kxkzj
      readOnly: true

NOTE: To get a pod from a different namespace, Just change the “-n default” to “-n “

Deployment:

Similarly, We can get the YAML file for the currently deployed deployment

kubectl get deployment nginx-deployment -n default -o yaml > deployment.yaml 
cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  generation: 1
  labels:
    app: nginx
  name: nginx-deployment
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File

Services:

To get a YAML file from the deployed services

kubectl get service my-service -n default -o yaml > service.yaml 
cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2022-11-28T11:05:25Z"
  name: my-service
  namespace: default
spec:
  clusterIP: 10.43.188.136
  ports:
  - port: 80
    protocol: TCP
    targetPort: 9376
  selector:
    app.kubernetes.io/name: MyApp
  sessionAffinity: None
  type: ClusterIP
status:

Generate YAML file for all available Kubernetes objects combined

The below command helps us to take a backup of all the running Kubernetes objects from all the namespace.

This will generate a single YAML file with a designation of all the Kubernetes objects(pod, deployment, services, etc)

kubectl get all —all-namespaces -o yaml > all-pod-deploy-svc.yaml

Check here to know more about Kubernetes backup and restore

Generate the YAML file before deploying the objects

We can create a YAML file using “–dry-run=client” option before deploying the actual object, This is useful when you want to create a YAML file with custom requirements.

like creating a deployment with replica 5 (For example)

– We can create a sample YAML file

– Edit the YAML file to change the replica to 5

– deploy the object (kubectl create -f )

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deployment.yaml

The above command will create a YAML file with the image “nginx” and name “nginx”

From there, We can edit the YAML for further changes like the number of replicas, labels, new containers, etc

Check,here to learn more about creating a temporary pod and running commands inside a pod

Good Luck with your Learning !!

Similar Posts