Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 merely about generating yamls for the very first time for pod, service, ingress, etc.

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 yamls templates with kubectl only.

like image 339
Andy Avatar asked Aug 28 '19 15:08

Andy


People also ask

How do I create a YAML file with kubectl?

It is easy to get started with generating YAML files for most of the resources with kubectl. You can use “ — dry-run=client -oyaml > yaml_file. yaml” flag on the “kubectl create” or “kubectl run” commands to generate most of the resources. The file needs to be cleaned a bit, but it is a good starting point.

Which tool generates Kubernetes YAML files from templates?

Kustomize is a command-line tool that can create and transform YAML files — just like yq .


2 Answers

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.

like image 105
prometherion Avatar answered Oct 20 '22 00:10

prometherion


Also kubectl explain can be used for different resources. It will not generate an yaml file for a standard pod, but it will display a description for one, e.g.:

kubectl explain pods

for getting details for a section/property in the pod:

kubectl explain pods.spec

One can also output the resulting explanation to an yaml file and edit that:

kubectl explain pods > mypod.yaml

AND! with

kubectl explain pod --recursive

one gets the whole structure of a resource without the explaining; exporting to a yaml file can represent an empty skeleton for the intended resource; below a segment for the 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>
                        .
                        .
                        .
like image 30
Petronella Avatar answered Oct 20 '22 02:10

Petronella