Search code examples
dockerkubernetesyamlkubectl

How to generate YAML template with kubectl command?


Is it possible to generate YAML with Kubernetes' kubectl command? To clarify - I'm not talking about generating YAML from existing deployments like kubectl get XXXX -o yaml, but about generating YAML files to create a Pod, Service, Ingress, etc. for the first time.

PS: There is a way to get YAML files from kubernetes.io site (1, 2) but I am looking if there is a way to generate YAML templates with kubectl only.


Solution

  • 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=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: {}
    

    Let's analyze each parameter:

    • my_deployment is the Deployment name you chose
    • --image is the Docker image you want to deploy
    • --dry-run=client won't execute the resource creation, used mainly for validation. Replace 'client' with 'true' for older versions of Kubernetes. Neither client nor server will actually create the resource, though server will return an error if the resource cannot be created without a dry run (ie: resource already exists). The difference is very subtle.
    • --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
    

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