是否可以使用kubernetes kubectl命令生成yaml?需要澄清的是-我不是在谈论从现有的部署(例如kubectl get XXXX -o yaml)生成yaml,而只是在第一次针对pod,服务,入口等生成yaml。
PS有一种从kubernetes.io网站(1,2)获取yaml文件的方法,但我正在寻找是否有一种仅用kubectl生成yamls模板的方法。
最佳答案
create
中有一个kubectl
命令可以解决上述问题,并取代了过去使用的run
:让我们为要创建一个运行nginx:latest Docker镜像的Deployment镜像。
# kubectl create deployment my_deployment --image=busybox --dry-run=client --output=yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: my_deployment
name: my_deployment
spec:
replicas: 1
selector:
matchLabels:
app: my_deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: my_deployment
spec:
containers:
- image: busybox
name: busybox
resources: {}
status: {}
让我们分析每个参数:my_deployment
是您选择的部署名称--image
是您要部署的Docker镜像--dry-run=client
将不会执行资源创建,主要用于验证。对于较旧版本的Kubernetes,将'client'替换为'true'。 --output=yaml
将部署资源的YAML定义输出到标准输出。 显然,您只需很少的Kubernetes默认资源即可执行此选项:
# kubectl create
clusterrole Create a ClusterRole.
clusterrolebinding Create a ClusterRoleBinding for a particular ClusterRole
configmap Create a configmap from a local file, directory or literal value
deployment Create a deployment with the specified name.
job Create a job with the specified name.
namespace Create a namespace with the specified name
poddisruptionbudget Create a pod disruption budget with the specified name.
priorityclass Create a priorityclass with the specified name.
quota Create a quota with the specified name.
role Create a role with single rule.
rolebinding Create a RoleBinding for a particular Role or ClusterRole
secret Create a secret using specified subcommand
service Create a service using specified subcommand.
serviceaccount Create a service account with the specified name
据此,您可以在无需部署资源的情况下呈现模板。
关于docker - 如何使用kubectl命令生成yaml模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57696087/