如何使用kubectl命令生成YAML模板?

问题描述 投票:0回答:3

是否可以使用 Kubernetes 的

kubectl
命令生成 YAML?澄清一下 - 我不是在谈论从现有部署(如
kubectl get XXXX -o yaml
)生成 YAML,而是在谈论生成 YAML 文件以首次创建 Pod、Service、Ingress 等。

PS:有一种方法可以从 kubernetes.io 站点(12)获取 YAML 文件,但我正在寻找是否有一种方法可以仅使用 kubectl 生成 YAML 模板。

docker kubernetes yaml kubectl
3个回答
61
投票

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
    是您选择的 Deployment 名称
  • --image
    是你要部署的Docker镜像
  • --dry-run=client
    不会执行资源创建,主要用于验证。 对于旧版本的 Kubernetes,将“client”替换为“true”。
    client
    server
    都不会实际创建资源,但如果在没有试运行的情况下无法创建资源(即:资源已存在),
    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

据此,您可以渲染模板,而无需事先部署资源。


31
投票

此外

kubectl explain
可用于不同的资源。它不会为标准 Pod 生成 yaml 文件,但会显示一个描述,例如:

kubectl explain pods

获取 pod 中某个部分/属性的详细信息:

kubectl explain pods.spec

还可以将结果解释输出到 yaml 文件并进行编辑:

kubectl explain pods > mypod.yaml

并且! 与

kubectl explain pod --recursive

无需解释即可了解资源的整体结构;导出到 yaml 文件可以代表目标资源的空骨架;下面是 pod 的一段:

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>
                        .
                        .
                        .

1
投票

您可以使用yq工具根据现有资源生成没有特定元数据(或其他字段)的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问题,但这个主题可能会引导人们寻找这个特定的答案)。

© www.soinside.com 2019 - 2024. All rights reserved.