Get YAML for deployed Kubernetes services?
Solution 1
To get the yaml for a deployment (service, pod, secret, etc):
kubectl get deploy deploymentname -o yaml
Solution 2
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
Solution 3
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.
Solution 4
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:
kubectl get po -n nginx nginx-deployment-755cfc7dcf-5s7j8 -o yaml > podDetail.yaml
Create replicaset yaml file from running pod:
kubectl get rs -n nginx -o yaml > latestReplicaSet.yaml
Create deployement yaml file from running pod:
kubectl get deploy -n nginx -o yaml > latestDeployement.yaml
Solution 5
Now that --export
is deprecated, to get the output from your resources in the 'original' format (just cleaned up, without any information about the current object state (unnecessary metadata in this circumstance)) you can do the following using yq
v4.x
:
kubectl get <resource> -n <namespace> <resource-name> -o yaml \
| yq eval 'del(.metadata.resourceVersion, .metadata.uid, .metadata.annotations, .metadata.creationTimestamp, .metadata.selfLink, .metadata.managedFields)' -
Related videos on Youtube
Industrial
Updated on February 03, 2022Comments
-
Industrial over 2 years
I am trying to deploy my app to Kubernetes running in Google Container Engine.
The app can be found at: https://github.com/Industrial/docker-znc.
The Dockerfile is built into an image on Google Container Registry.
I have deployed the app in Kubernetes via the + button. I don't have the YAML for this.
I have inserted a Secret in Kubernetes for the PEM file required by the app.
- How do I get the YAML for the Deployment, Service and Pod created by Kubernetes by filling in the form?
- How do I get the Secret into my Pod for usage?
-
Rahul Wagh about 3 yearsYou can follow this lab session on how to export yaml of deployed k8s services- youtu.be/Wc9T4tdcsr0
-
Sinaesthetic over 6 yearsany idea how to do it for the full cluster (all deployments)? The idea being, of course, to create mirror environments with the exact same services.
-
mababin over 5 years@Sinaesthetic, List exports are not supported so far and don't seem to be coming soon. You will probably need a script to list all ressources then cycle through those ressources to build your list. github.com/kubernetes/kubernetes/issues/…
-
Josh Kelley almost 5 yearsAs of Kubernetes 1.14,
--export
is deprecated; see here. You can useget -o yaml
without--export
, although that includes information about the current object state, as well as the declarative configuration needed to (re)configure the object. -
mkingston over 4 years
kubectl get $(kubectl api-resources | awk '{print $1}' | tail -n +2 | tr '\n' ',' | sed s/,\$//) -o yaml > manifest.yaml
-
Tony Lee about 4 yearsStill need to remove some current state from the yaml generated by "-o yaml", for example,
spec.clusterIP
andmetadata.resourceVersion
in service. -
learner over 3 years--export option does not work now. Need alternative approach
-
humble_wolf over 3 years
kubectl get deploy deploymentname -o yaml > mydepl.yaml
-
Adam Marshall about 3 yearsWhile your answer may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. You can edit your answer to add explanations and give an indication of what limitations and assumptions apply. - From Review
-
Tommy about 3 yearsThis answer is deprecated and should be edited, as
--export
is now an error. -
VladoPortos about 3 yearsI like this one, just one note yq is not usually on OS
-
Felipe Valdes almost 3 yearsthis answer was useful while watching this very useful tutorial on k8s youtube.com/…
-
Govind Kailas almost 3 yearsThis is a much better solution.
-
Kharthigeyan about 2 years--export is deprecated, right?
-
Clark Updike almost 2 yearsThis seems like the cleanest answer.