141

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.

  1. How do I get the YAML for the Deployment, Service and Pod created by Kubernetes by filling in the form?
  2. How do I get the Secret into my Pod for usage?
Industrial
  • 1,651
  • 2
  • 12
  • 9
  • You can follow this lab session on how to export yaml of deployed k8s services- https://youtu.be/Wc9T4tdcsr0 – Rahul Wagh Apr 03 '21 at 20:29

16 Answers16

177

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

kubectl get deploy deploymentname -o yaml
Janos Lenart
  • 19,021
  • 4
  • 56
  • 64
  • 3
    any 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. – Sinaesthetic Feb 14 '18 at 23:30
  • 1
    @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. https://github.com/kubernetes/kubernetes/issues/24855#issuecomment-329889844 – mababin Jan 11 '19 at 14:40
  • 27
    As of Kubernetes 1.14, `--export` is deprecated; see [here](https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.14.md#deprecations). You can use `get -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. – Josh Kelley Jul 05 '19 at 13:45
  • 1
    Still need to remove some current state from the yaml generated by "-o yaml", for example, ```spec.clusterIP``` and ```metadata.resourceVersion``` in service. – Tony Lee Apr 17 '20 at 02:45
  • --export option does not work now. Need alternative approach – learner Feb 09 '21 at 14:07
  • `kubectl get deploy deploymentname -o yaml > mydepl.yaml` – humble_wolf Feb 11 '21 at 14:00
  • This answer is deprecated and should be edited, as `--export` is now an error. – Tommy May 05 '21 at 11:06
20

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
victortv
  • 4,366
  • 2
  • 16
  • 23
  • 2
    `kubectl get $(kubectl api-resources | awk '{print $1}' | tail -n +2 | tr '\n' ',' | sed s/,\$//) -o yaml > manifest.yaml` – mkingston Jan 09 '20 at 15:13
13

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.

thephper
  • 1,235
  • 12
  • 17
13

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
Mohammad Rajabloo
  • 1,369
  • 12
  • 16
Soumya
  • 246
  • 4
  • 5
4

for the 2nd question regarding the secret, this is from the k8s documentation. see https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets for more info.

  1. Create a secret or use an existing one. Multiple pods can reference the same secret.
  2. Modify your Pod definition to add a volume under spec.volumes[]. Name the volume anything, and have a spec.volumes[].secret.secretName field equal to the name of the secret object.
  3. Add a spec.containers[].volumeMounts[] to each container that needs the secret. Specify spec.containers[].volumeMounts[].readOnly = true and spec.containers[].volumeMounts[].mountPath to an unused directory name where you would like the secrets to appear.
  4. Modify your image and/or command line so that the program looks for files in that directory. Each key in the secret data map becomes the filename under mountPath.

I have used this and it works fine.

JamStar
  • 351
  • 1
  • 8
4
  • Like mentioned above "--export" is one option to get the manifest corresponding to the kubeernetes objects
  • But "--export" is considered to be buggy and there is a proposal to deprecate it
  • Currently the better option is to do "-o yaml" or "-o json" and remove the unnecessary fields
  • The main difference is "--export" is expected to remove the cluster specific settings (e.g. cluster service IP of a k8s service). But it is found to be inconsistent in this regard
pr-pal
  • 1,794
  • 15
  • 11
4

Use this command to get yaml format of your service

kubectl get service servicename -n <namespace> -o yaml

You can put it in some file also

kubectl get service servicename -n <namespace> -o yaml > service.yaml

famen
  • 15
  • 5
Sachin Arote
  • 597
  • 8
  • 17
3

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)' -
Grant
  • 361
  • 1
  • 2
  • 10
0
kubectl -n <namespace> get <resource type> <resource Name> -o yaml 

With the command above, any resource defined in Kubernetes can be exported in YAML format.

iminiki
  • 2,087
  • 7
  • 21
  • 39
nimit garg
  • 21
  • 2
0

If you need to view and edit the file use:

kubectl edit service servicename

Ken M
  • 65
  • 1
  • 1
  • 5
0
  1. You can get the yaml files of the resources using this command

    kubectl -n <namespace> get <resource type> <resource Name> -o yaml

  2. To get the secret into your pod,

use something like this

env
- valueFrom
    secretKeyRef:
      name: secret_name
      key: key_name

or

envFrom
- secretRef:
    name: secret_name

 
Ankur Kothari
  • 537
  • 6
  • 9
0

Is only minor difference from @Janos Lenart's answer!

kubectl get deploy deploymentname -o yaml > outputFile.yaml will do

Arun
  • 1,131
  • 2
  • 13
  • 27
0

The following code will extract all your K8s definitions at once and place them on individual folders below the current folder.

for OBJ in $(kubectl api-resources --verbs=list --namespaced -o name)
do
   for DEF in $(kubectl get --show-kind --ignore-not-found $OBJ -o name)
   do
      mkdir -p $(dirname $DEF)
      kubectl get $DEF -o yaml \
      | yq eval 'del(.metadata.resourceVersion, .metadata.uid, .metadata.annotations, .metadata.creationTimestamp, .metadata.selfLink, .metadata.managedFields)' - > $DEF.yaml 
   done
done
  • 1
    While your answer may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) 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](https://stackoverflow.com/review/late-answers/28401429) – Adam Marshall Feb 24 '21 at 17:45
0

Also its possible to use the view-last-applied command e.g.

kubectl apply view-last-applied services --all > services.yaml

which will return all the manifests applied to create services. Also you can specify a certain k8 resource by services/resource-name label.

0

You can try use kube-dump bash script

With this utility, you can save Kubernetes cluster resources as a pure yaml manifest without unnecessary metadata.

enter image description here

WoozyMasta
  • 13
  • 3
-1

I know it is too old to answer, but hopefully, someone will find it helpful.

We can try below command to fetch a kind export from all namespace -

kubectl get <kind> --all-namespaces --export -o yaml