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

And*_*ndy 3 yaml docker kubernetes kubectl

是否可以使用kubernetes kubectl命令生成yaml?需要澄清的是-我不是在谈论从现有的部署(例如kubectl get XXXX -o yaml)生成yaml,而只是在第一次针对pod,服务,入口等生成yaml。

PS有一种方式来获得kubernetes.io网站(YAML文件12),但我期待,如果有是生成只kubectl yamls模板的方式。

Pet*_*lla 10

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

kubectl explain pods
Run Code Online (Sandbox Code Playgroud)

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

kubectl explain pods.spec
Run Code Online (Sandbox Code Playgroud)

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

kubectl explain pods > mypod.yaml
Run Code Online (Sandbox Code Playgroud)

和!和

kubectl explain pod --recursive
Run Code Online (Sandbox Code Playgroud)

无需解释即可获得资源的整个结构;导出到 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>
                        .
                        .
                        .
Run Code Online (Sandbox Code Playgroud)


pro*_*ion 5

There's the command create in kubectl that does the trick and replaced the run used in the past: let's image you want to create a Deployment running a nginx:latest Docker image.

# kubectl create deployment my_deployment --image=busybox --dry-run=true --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: {}
Run Code Online (Sandbox Code Playgroud)

Let's analyze each parameter:

  • my_deployment is the Deployment name you chose
  • --image is the Docker image you want to deploy
  • --dry-run=true won't execute the resource creation, used mainly for validation
  • --output=yaml prints to standard output the YAML definition of the Deployment resource.

Obviously, you can perform this options just with few Kubernetes default resources:

# 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
Run Code Online (Sandbox Code Playgroud)

According to this, you can render the template without the prior need of deploying your resource.