kubectl export yaml OR How to generate YAML for deployed kubernetes resources


In this post, we are going to see how to get YAML of deployed Kubernetes resources(pvc, configmap, ingress, service, secret, deployment, statefulset, hpa, job, cronjob).

Most of the time we create the deployment and expose services inside the kubernetes cluster using YAMLs but here in this the post we will focus on a reverse to re-generate YAML of the deployed resources.

  1. My Cluster Setup
  2. Generate YAML for service
  3. Generate YAML for deployment
  4. Bash script to generate all the YAML for existing k8s cluster



1. This is my Kubernetes cluster setup -

  1. I have already setup my Kubernetes cluster with 1 master and 2 Worker nodes alongwith one helloworld deployment.
  2. Deployment : deployment.apps/hellworldexample-helloworld
  3. Service : service/hellworldexample-helloworld

Here are complete details of my current cluster

1kubectl get all
 1NAME                                           READY   STATUS    RESTARTS   AGE
 2pod/myreleasename-helloworld-7c4dd5588-wc6js   1/1     Running   1          8d
 3
 4NAME                               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
 5service/kubernetes                 ClusterIP   10.233.0.1     <none>        443/TCP   13d
 6service/myreleasename-helloworld   ClusterIP   10.233.58.70   <none>        80/TCP    8d
 7
 8NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
 9deployment.apps/myreleasename-helloworld   1/1     1            1           8d
10
11NAME                                                 DESIRED   CURRENT   READY   AGE
12replicaset.apps/myreleasename-helloworld-7c4dd5588   1         1         1       8d


2. Let's generate the YAML for the "service"

Use the following kubectl command to get the YAML of service running with the name of myreleasename-helloworld inside your kubernetes cluster.

1kubectl get service hellworldexample-helloworld -n default -o yaml > service.yaml 

Few points to notice -
  1. The above kubectl command will generate the YAML and will save into service.yaml
  2. Output of the service.yaml is long, so I thought of not mentioning it in the post
  3. Please do substitute the service-name in the above command as per your need.

2.1 How to generate the YAML for all the service resources inside the kubernetes cluster

If you are looking to generate single YAML for all the service resources inside the Kubernetes cluster then you need to use the following kubectl command

1kubectl get service --all-namespaces -o yaml  > all-service.yaml

Few points to notice -
  1. The above kubectl command will generate the YAML and will save into all-service.yaml
  2. Output of the all-service.yaml is really long, so I thought of not mentioning in the post


3. Let's generate the YAML for "deployment"

To get the YAML for the deployment is also pretty much the same as we have seen in the previous point for service.

Here is the command for generating the YAML for the deployment -

1kubectl get deployment myreleasename-helloworld -n default -o yaml > deployment.yaml 

Few points to notice -
  1. The above kubectl command will generate the YAML and will save into deployment.yaml
  2. Output of the deployment.yaml is long, so I thought of not mentioning it in the post
  3. Please do substitute the deployment-name in the above command as per your need.

3.1 How to generate the YAML for all the deployed resources inside the kubernetes cluster

If you are looking to generate a single YAML for all the deployed resources inside the Kubernetes cluster then you need to use the following kubectl command

1kubectl get deploy --all-namespaces -o yaml  > all-deployment.yaml

Few points to notice -
  1. The above kubectl command will generate the YAML and will save into all-deployment.yaml
  2. Output of the all-deployment.yaml is long, so I thought of not mentioning in the post


4. Bash/shell script way to generate the YAML files for a complete existing cluster

Now in the previous point 2 and point 3 we have seen how to generate the YAML of service and deployment.

But is there a way to generate all the YAML files for the complete existing Kubernetes cluster?

The answer is YES : - You can use the following shell script to achieve that -

  1. Create a bash file for example generate-yaml.sh and save the following script into it -
1for n in $(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob)
2do
3    mkdir -p $(dirname $n)
4    kubectl get -o=yaml $n > $n.yaml
5done
  1. After saving the file use the following command to execute the bash script
1. generate-yaml.sh
  1. Here is the list of a directory which we will be generated by scripts e.g. - configmap, deployment.apps, secret, service, serviceaccount

list of directory generated by the shell script for generating the yamls of deployed kuberentes resources

Here are all the yamls with the names -

generated all the YAMLs for the deployed kuberentes resources.

(Reference - I prepared this blog post after reading this GitHub issue and StackOver Flow discussion)




Learn more On Kubernetes -

  1. Setup kubernetes on Ubuntu
  2. Setup Kubernetes on CentOs
  3. Setup HA Kubernetes Cluster with Kubespray
  4. Setup HA Kubernetes with Minikube
  5. Setup Kubernetes Dashboard for local kubernetes cluster
  6. Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
  13. How to setup kubernetes jenkins pipeline on AWS?
  14. Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
  15. How to fix kubernetes pods getting recreated?
  16. How to delete all kubernetes PODS?
  17. How to use Kubernetes secrets?
  18. Share kubernetes secrets between namespaces?
  19. How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
  20. Delete Kubernetes POD stuck in terminating state?

Posts in this Series