How to generate yaml template with kubectl command?
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=yamlapiVersion: apps/v1kind: Deploymentmetadata: creationTimestamp: null labels: app: my_deployment name: my_deploymentspec: 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.--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.
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: PodVERSION: v1DESCRIPTION: 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> . . .