如何使用 kubectl 命令生成 yaml 模板?
How to generate yaml template with kubectl command?
是否可以使用 kubernetes kubectl 命令生成 yaml?澄清 - 我不是在谈论从现有部署生成 yaml,如 kubectl get XXXX -o yaml,而只是第一次为 pod、服务、入口等生成 yaml。
PS 有一种方法可以从 kubernetes.io 站点 ( 1 , 2 ) 获取 yaml 文件,但我正在寻找是否有一种方法可以仅使用 kubectl 生成 yamls 模板。
kubectl
中的命令 create
可以解决问题并取代过去使用的 run
:假设您要创建一个 Deployment 运行宁一个 nginx:latest Docker image.
# 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'。 client
和 server
都不会实际创建资源,但如果没有干 运行(即:资源已经存在)。区别非常细微。
--output=yaml
打印到 标准输出 Deployment 资源的 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
据此,您无需事先部署资源即可呈现模板。
另外kubectl explain
可用于不同的资源。它不会为标准 pod 生成 yaml 文件,但会显示一个描述,例如:
kubectl explain pods
获取广告连播中 section/property 的详细信息:
kubectl explain pods.spec
也可以将生成的解释输出到 yaml 文件并进行编辑:
kubectl explain pods > mypod.yaml
并且!
与
kubectl explain pod --recursive
无需解释即可获得资源的整体结构;导出到 yaml 文件可以代表预期资源的空框架;在广告连播段下方:
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
kind <string>
metadata <Object>
annotations <map[string]string>
clusterName <string>
creationTimestamp <string>
deletionGracePeriodSeconds <integer>
deletionTimestamp <string>
finalizers <[]string>
generateName <string>
generation <integer>
labels <map[string]string>
managedFields <[]Object>
apiVersion <string>
fieldsType <string>
fieldsV1 <map[string]>
manager <string>
operation <string>
time <string>
name <string>
namespace <string>
ownerReferences <[]Object>
apiVersion <string>
blockOwnerDeletion <boolean>
controller <boolean>
kind <string>
name <string>
uid <string>
resourceVersion <string>
selfLink <string>
uid <string>
spec <Object>
activeDeadlineSeconds <integer>
affinity <Object>
nodeAffinity <Object>
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
preference <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchFields <[]Object>
key <string>
operator <string>
values <[]string>
weight <integer>
requiredDuringSchedulingIgnoredDuringExecution <Object>
nodeSelectorTerms <[]Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchFields <[]Object>
key <string>
operator <string>
values <[]string>
podAffinity <Object>
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
podAffinityTerm <Object>
labelSelector <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchLabels <map[string]string>
namespaces <[]string>
topologyKey <string>
weight <integer>
requiredDuringSchedulingIgnoredDuringExecution <[]Object>
labelSelector <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchLabels <map[string]string>
namespaces <[]string>
topologyKey <string>
podAntiAffinity <Object>
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
podAffinityTerm <Object>
labelSelector <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
.
.
.
您可以使用yq tool基于现有资源生成没有特定元数据(或其他字段)的yaml模板。例如:
kubectl get deploy my-nginx -o yaml | \
yq eval 'del(.metadata.resourceVersion, .metadata.uid, .metadata.annotations, .metadata.creationTimestamp, .metadata.selfLink, .metadata.managedFields, .status.conditions)' \
- > nginx_template.yaml
稍后您可以使用 kubectl apply -f nginx_template.yaml
应用该资源。它适用于其他资源类型,包括 CustomResourceDefinitions.
(我知道它并没有完全回答 OP 问题,但这个主题可能会引导人们在这里寻找这个特定的答案)。
是否可以使用 kubernetes kubectl 命令生成 yaml?澄清 - 我不是在谈论从现有部署生成 yaml,如 kubectl get XXXX -o yaml,而只是第一次为 pod、服务、入口等生成 yaml。
PS 有一种方法可以从 kubernetes.io 站点 ( 1 , 2 ) 获取 yaml 文件,但我正在寻找是否有一种方法可以仅使用 kubectl 生成 yamls 模板。
kubectl
中的命令 create
可以解决问题并取代过去使用的 run
:假设您要创建一个 Deployment 运行宁一个 nginx:latest Docker image.
# 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'。client
和server
都不会实际创建资源,但如果没有干 运行(即:资源已经存在)。区别非常细微。--output=yaml
打印到 标准输出 Deployment 资源的 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
据此,您无需事先部署资源即可呈现模板。
另外kubectl explain
可用于不同的资源。它不会为标准 pod 生成 yaml 文件,但会显示一个描述,例如:
kubectl explain pods
获取广告连播中 section/property 的详细信息:
kubectl explain pods.spec
也可以将生成的解释输出到 yaml 文件并进行编辑:
kubectl explain pods > mypod.yaml
并且! 与
kubectl explain pod --recursive
无需解释即可获得资源的整体结构;导出到 yaml 文件可以代表预期资源的空框架;在广告连播段下方:
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
kind <string>
metadata <Object>
annotations <map[string]string>
clusterName <string>
creationTimestamp <string>
deletionGracePeriodSeconds <integer>
deletionTimestamp <string>
finalizers <[]string>
generateName <string>
generation <integer>
labels <map[string]string>
managedFields <[]Object>
apiVersion <string>
fieldsType <string>
fieldsV1 <map[string]>
manager <string>
operation <string>
time <string>
name <string>
namespace <string>
ownerReferences <[]Object>
apiVersion <string>
blockOwnerDeletion <boolean>
controller <boolean>
kind <string>
name <string>
uid <string>
resourceVersion <string>
selfLink <string>
uid <string>
spec <Object>
activeDeadlineSeconds <integer>
affinity <Object>
nodeAffinity <Object>
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
preference <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchFields <[]Object>
key <string>
operator <string>
values <[]string>
weight <integer>
requiredDuringSchedulingIgnoredDuringExecution <Object>
nodeSelectorTerms <[]Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchFields <[]Object>
key <string>
operator <string>
values <[]string>
podAffinity <Object>
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
podAffinityTerm <Object>
labelSelector <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchLabels <map[string]string>
namespaces <[]string>
topologyKey <string>
weight <integer>
requiredDuringSchedulingIgnoredDuringExecution <[]Object>
labelSelector <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
matchLabels <map[string]string>
namespaces <[]string>
topologyKey <string>
podAntiAffinity <Object>
preferredDuringSchedulingIgnoredDuringExecution <[]Object>
podAffinityTerm <Object>
labelSelector <Object>
matchExpressions <[]Object>
key <string>
operator <string>
values <[]string>
.
.
.
您可以使用yq tool基于现有资源生成没有特定元数据(或其他字段)的yaml模板。例如:
kubectl get deploy my-nginx -o yaml | \
yq eval 'del(.metadata.resourceVersion, .metadata.uid, .metadata.annotations, .metadata.creationTimestamp, .metadata.selfLink, .metadata.managedFields, .status.conditions)' \
- > nginx_template.yaml
稍后您可以使用 kubectl apply -f nginx_template.yaml
应用该资源。它适用于其他资源类型,包括 CustomResourceDefinitions.
(我知道它并没有完全回答 OP 问题,但这个主题可能会引导人们在这里寻找这个特定的答案)。