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.
- My Cluster Setup
- Generate YAML for service
- Generate YAML for deployment
- Bash script to generate all the YAML for existing k8s cluster
1. This is my Kubernetes cluster setup -
- I have already setup my Kubernetes cluster with 1 master and 2 Worker nodes alongwith one
helloworld
deployment. - Deployment :
deployment.apps/hellworldexample-helloworld
- 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 -
- The above
kubectl
command will generate theYAML
and will save intoservice.yaml
- Output of the
service.yaml
is long, so I thought of not mentioning it in the post - 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 -
- The above
kubectl
command will generate theYAML
and will save intoall-service.yaml
- 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 -
- The above
kubectl
command will generate theYAML
and will save intodeployment.yaml
- Output of the
deployment.yaml
is long, so I thought of not mentioning it in the post - 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 -
- The above
kubectl
command will generate theYAML
and will save intoall-deployment.yaml
- 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 -
- 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
- After saving the file use the following command to execute the
bash script
1. generate-yaml.sh
- Here is the list of a directory which we will be generated by scripts e.g. - configmap, deployment.apps, secret, service, serviceaccount
Here are all the yamls with the names -
(Reference - I prepared this blog post after reading this GitHub issue and StackOver Flow discussion)
Learn more On Kubernetes -
- Setup kubernetes on Ubuntu
- Setup Kubernetes on CentOs
- Setup HA Kubernetes Cluster with Kubespray
- Setup HA Kubernetes with Minikube
- Setup Kubernetes Dashboard for local kubernetes cluster
- Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
- How to use Persistent Volume and Persistent Volume Claims in Kubernetes
- Deploy Spring Boot Microservice on local Kubernetes cluster
- Deploy Spring Boot Microservice on Cloud Platform(GCP)
- Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
- How to setup kubernetes jenkins pipeline on AWS?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- How to fix kubernetes pods getting recreated?
- How to delete all kubernetes PODS?
- How to use Kubernetes secrets?
- Share kubernetes secrets between namespaces?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Delete Kubernetes POD stuck in terminating state?
Posts in this Series
- Kubernetes Cheat Sheet for day to day DevOps operations?
- Delete Kubernetes POD stuck in terminating state?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Share kubernetes secrets between namespaces?
- How to use Kubernetes secrets?
- How to delete all kubernetes PODS?
- kubernetes pods getting recreated?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- kubectl export yaml OR How to generate YAML for deployed kubernetes resources
- Kubernetes Updates
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- Kubernetes cluster setup with Jenkins
- How to use Persistent Volume and Persistent Claims | Kubernetes
- How to fix ProvisioningFailed persistentvolume controller no volume plugin matched
- Fixing – Cannot bind to requested volume: storageClasseName does not match
- Fixing – pod has unbound immediate persistentvolumeclaims or cannot bind to requested volume incompatible accessmode
- How to fix kubernetes dashboard forbidden 403 error – message services https kubernetes-dashboard is forbidden User
- How to fix Kubernetes – error execution phase preflight [preflight]
- Deploy Spring Boot microservices on kubernetes?
- How to fix – ansible_memtotal_mb minimal_master_memory_mb
- How to use kubespray – 12 Steps for Installing a Production Ready Kubernetes Cluster
- How to setup kubernetes on CentOS 8 and CentOS 7
- How to fix – How to fix - ERROR Swap running with swap on is not supported. Please disable swap
- 14 Steps to Install kubernetes on Ubuntu 20.04(bento/ubuntu-20.04), 18.04(hashicorp/bionic64)
- Kubernetes Dashboard | Kubernetes Admin GUI | Kubernetes Desktop Client
- Install Kubernetes with Minikube