我试图将我的应用程序部署到运行在谷歌容器中的Kubernetes 引擎。

该应用程序可以在https://github.com/Industrial/docker-znc上找到。

Dockerfile内置于谷歌容器注册表上的映像中。

我已经通过+按钮在Kubernetes中部署了应用程序。我没有YAML 对于这个。

我已经在Kubernetes中插入了一个Secret,用于应用程序所需的PEM文件。

如何获得由创建的部署、服务和Pod的YAML Kubernetes通过填写表格? 我如何把秘密进入我的豆荚使用?


当前回答

所有的服务

kubectl get service --all-namespaces -o yaml  > all-service.yaml

所有部署

kubectl get deploy --all-namespaces -o yaml  > all-deployment.yaml

其他回答

可以使用以下命令-存储已部署kubernetes服务的输出 Kubectl get SVC -n -o yaml > SVC -output.yaml

对于部署-

kubectl get deploy <deployment-name> -n <your-namespace> -o yaml > deploy-output.yaml

豆荚

kubectl get pod <pod-name> -n <your-namespace> -o yaml > pod-output.yaml

您可以使用以下命令-获取您的秘密详细信息 Kubectl得到秘密-n -o yaml 要使用更新部署文件,请使用以下命令- Kubectl编辑部署-n 在你的pod模板下添加以下-

这将在吊舱容器部分下,将秘密体积挂载到容器 volumeMounts: -名称:foo mountPath:“/ etc / foo” 只读的:真 这将在部署时进入pod模板部分 卷: -名称:foo 秘密: secretName: mysecret

如何获得由创建的部署、服务和Pod的YAML Kubernetes通过填写表格?

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

回答@Sinaesthetic的问题:

您知道如何为整个集群(所有部署)执行此操作吗?

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

此方法的问题是导出不包括名称空间。因此,如果你想同时导出许多资源,我建议逐个命名空间执行:

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

不幸的是,kubernetes仍然不支持真正的get all命令,因此您需要手动列出想要导出的资源类型。可以获得资源类型的列表

kubectl api-resources

关于秘密的第二个问题,这来自k8s的文档。更多信息请参见https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets。

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.

我用过这个,它工作得很好。

从kubernetes下载yaml的语法

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

从运行的pod创建yaml文件:

kubectl得到po -n nginx nginxdeploymentt55cfc7dcf -5s7j8 -o yaml > podDetail.yaml

从运行的pod中创建副本yaml文件:

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

从运行的pod创建部署yaml文件:

kubectl get deploy -n nginx -o yaml > latestdeployment .yaml

kubernetes GitHub问题页面讨论了同样的问题,用户“alahijani”制作了一个bash脚本,导出所有yaml并将它们写入单个文件和文件夹。

由于这个问题在谷歌上排名很好,而且我发现这个解决方案非常好,所以我在这里表示它。

Bash脚本导出yaml到子文件夹:

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

另一个用户“acondrat”编写了一个不使用目录的脚本,这使得以后让kubectl应用-f变得很容易。

Bash脚本导出yaml到当前文件夹:

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

最后一个脚本不包括服务帐户。