问题
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?
回答1:
To get the yaml for a deployment (service, pod, secret, etc):
kubectl get deploy deploymentname -o yaml --export
回答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
回答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.
回答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.
- Create a secret or use an existing one. Multiple pods can reference the same secret.
- 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.
- 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.
- 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.
回答5:
- 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
回答6:
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
回答7:
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
回答8:
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.
回答9:
If you need to view and edit the file use:
kubectl edit service servicename
来源:https://stackoverflow.com/questions/43941772/get-yaml-for-deployed-kubernetes-services