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

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

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

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

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

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


当前回答

现在——export已弃用,要以'原始'格式从资源中获得输出(刚刚清理了,没有任何关于当前对象状态的信息(在这种情况下不必要的元数据)),您可以使用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)' -

其他回答

要获取kubernetes上当前运行部署的YAML,可以运行这个命令:

kubectl get deployment <deployment_name> -o yaml

要生成YAML用于部署,可以运行命令式命令。

kubectl create deployment <deployment_name>--image=<image_name> -o yaml

要生成并导出部署,可以运行imperative命令。

 kubectl create deployment <deployment_name>--image=<image_name> --dry-run=client -o yaml > example.yaml

如何获得由创建的部署、服务和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

我们可以使用下面的命令获取已部署资源的yaml。

kubectl get <resource name> -o yaml

                OR
kubectl get <resource name> <name of pod> -o yaml

例子:-

  kubectl get deploy Nginx -o yaml

以上命令将为您提供yaml输出。

如果您想将输出存储到任何文件中,可以使用以下命令。

kubectl get pod nginx -o yaml > Nginx-pod.yaml

上面的命令会将输出重定向到Nginx-pod。Yaml在您的当前目录。

关于秘密的第二个问题,这来自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.

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

如上所述,“——export”是获取与kubeernetes对象对应的清单的一个选项 但是"——export"被认为是有bug的,有人建议弃用它 目前更好的选择是执行"-o yaml"或"-o json"并删除不必要的字段 主要的区别是“——export”被期望删除集群特定的设置(例如k8s服务的集群服务IP)。但在这方面,它被发现是不一致的