Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get YAML for deployed Kubernetes services?

People also ask

How do I see POD YAML?

To view the entire configuration of the pod, just run kubectl describe pod nginx in your terminal. The terminal will now display the YAML for the pod, starting with the name nginx, its location, the Minikube node, start time and current status.

Where are Kubernetes YAML files stored?

These YAML files can be stored in a common directory and may all be applied using kubectl apply -f <directory>. This is fairly simple. However, as your application evolves and changes are made to the definition of your Kubernetes objects, you'll have to consider which version and which change is currently deployed.

What is Kubernetes Deployment YAML?

Kubernetes Deployment is the process of providing declarative updates to Pods and ReplicaSets. It allows users to declare the desired state in the manifest (YAML) file, and the controller will change the current state to the declared state.


To get the yaml for a deployment (service, pod, secret, etc):

kubectl get deploy deploymentname -o yaml

How do I get the YAML for the Deployment, Service and Pod created by Kubernetes by filling in the form?

kubectl get deployment,service,pod yourapp -o yaml --export

Answering @Sinaesthetic question:

any idea how to do it for the full cluster (all deployments)?

kubectl get deploy --all-namespaces -o yaml --export

The problem with this method is that export doesn't include the namespace. So if you want to export many resources at the same time, I recommend doing it per namespace:

kubectl get deploy,sts,svc,configmap,secret -n default -o yaml --export > default.yaml

Unfortunately kubernetes still doesn't support a true get all command, so you need to list manually the type of resources you want to export. You can get a list of resource types with

kubectl api-resources

The same issue is discussed at kubernetes GitHub issues page and the user "alahijani" made a bash script that exports all yaml and writes them to single files and folders.

Since this question ranks well on Google and since I found that solution very good, I represent it here.

Bash script exporting yaml to sub-folders:

for n in $(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob)
do
    mkdir -p $(dirname $n)
    kubectl get -o=yaml --export $n > $n.yaml
done

Another user "acondrat" made a script that do not use directories, which makes it easy to make a kubectl apply -f later.

Bash script exporting yaml to current folder:

for n in $(kubectl get -o=name pvc,configmap,ingress,service,secret,deployment,statefulset,hpa,job,cronjob | grep -v 'secret/default-token')
do
    kubectl get -o=yaml --export $n > $(dirname $n)_$(basename $n).yaml
done

The last script does not include service account.


Syntax for downloading yaml's from kubernetes

kubectl get [resource type] -n [namespace] [resource Name] -o yaml > [New file name]

Create yaml file from running pod:

  1. kubectl get po -n nginx nginx-deployment-755cfc7dcf-5s7j8 -o yaml > podDetail.yaml

Create replicaset yaml file from running pod:

  1. kubectl get rs -n nginx -o yaml > latestReplicaSet.yaml

Create deployement yaml file from running pod:

  1. kubectl get deploy -n nginx -o yaml > latestDeployement.yaml