Get YAML for deployed Kubernetes services?

2020-05-13 20:16发布

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?

9条回答
放荡不羁爱自由
2楼-- · 2020-05-13 20:50
  • 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
查看更多
狗以群分
3楼-- · 2020-05-13 20:51

Use this command to get yaml format of your service

kubectl get service servicename -o yaml

You can put it in some file also

kubectl get service servicename -o yaml >service.yaml

查看更多
beautiful°
4楼-- · 2020-05-13 21:00
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.

查看更多
太酷不给撩
5楼-- · 2020-05-13 21:02

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
查看更多
看我几分像从前
6楼-- · 2020-05-13 21:06

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

kubectl get deploy deploymentname -o yaml --export
查看更多
祖国的老花朵
7楼-- · 2020-05-13 21:10

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.

查看更多
登录 后发表回答