YAML Autogeneration using Kubernetes Extention
One of the easiest ways to create Kubernetes YAML is using the visual studio kubernetes extension.
Install the Kubernetes VS code extension, and it will help develop k8s manifests for most kubernetes objects. It also supports deploying apps to local and remote k8s clusters.
All you have to do is, start typing the Object name and it will automatically populate the options for you. Then, based on your selection, it will autogenerate the basic YAML structure for you as shown n the following image.
This extension supports YAML generation of Pods, Deployment, Statefulset, Replicationset, Persistent Volumes (PV), Persistent Volume Claims (PVC), etc.
Create YAML Manifest Using Kubectl Dry Run
You can create the manifests using the kubectl imperative commands. There is a flag called --dry-run that helps you create the entire manifest template.
Also, you cannot create all the Kubernetes resource YAML using dry-run. For example, you cannot create a Statefulset or a persistent volume using dry-run.
Note: If you are preparing for Kubernetes certifications like CKA, CKAD, or CKS, imperative commands come in handy during the exam.
Kubectl YAML Dry Run Examples
Let’s look at the examples to generate YAML using a dry run and write it to an output file.
Create Pod YAML
Create a pod YAML named myapp which uses image nginx:latest.
kubectl run mypod --image=nginx:latest \
--labels type=web \
--dry-run=client -o yaml > mypod.yamlCreate a Pod service YAML
Generate YAML for a Pod Service that exposes a NodePort. This will only work if you have a running pod.
kubectl expose pod mypod \
--port=80 \
--name mypod-service \
--type=NodePort \
--dry-run=client -o yaml > mypod-service.yamlCreate NodePort Service YAML
Create a service type nodeport with port 30001 with service to pod TCP port mapping on port 80.
kubectl create service nodeport mypod \
--tcp=80:80 \
--node-port=30001 \
--dry-run=client -o yaml > mypod-service.yamlCreate Deployment YAML
Create a deployment named mydeployment with image Nginx
kubectl create deployment mydeployment \
--image=nginx:latest \
--dry-run=client -o yaml > mydeployment.yamlCreate Deployment Service YAML
Create a NodePort service YAML for deployment mydeployment with service port 8080
kubectl expose deployment mydeployment \
--type=NodePort \
--port=8080 \
--name=mydeployment-service \
--dry-run=client -o yaml > mydeployment-service.yamlCreate Job YAML
Crate job named myjob with nginx image.
kubectl create job myjob \
--image=nginx:latest \
--dry-run=client -o yamlCreate Cronjob YAML
Create a cronjob named mycronjob with nginx image and a corn schedule.
kubectl create cj mycronjob \
--image=nginx:latest \
--schedule="* * * * *" \
--dry-run=client -o yamlI have given generic YAML examples. You can further change parameters and use them as per your requirements.
Kubectl & Dry Run Alias
To make things fast, you can set up an alias in ~/.bashrc or ~/.zshrc for kubectl command as follows. So that you don’t have to type kubectl every time.
alias k=kubectlYou can also set up an alias for a kubectl dry run parameters as follows.
alias kdr='kubectl --dry-run=client -o yaml'You can execute the command as follows.
kdr run web --image=nginx:latest > nginx.yaml
