In this blog, you will learn easy methods to create Kubernetes YAML manifest quickly for testing and deploying applications on Kubernetes. You can also these use methods in the Kubernetes certification exam.
One common thing while working with kubernetes is that we tend to search for Kubernetes YAML files whenever we want to deploy a test pod, deployment, or any other object. Who wants to write every line of a YAML file, right?
Let’s look at some of the Kubernetes tips to make the YAML creation process easy.
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
kubectl run mypod --image=nginx:latest \ --labels type=web \ --dry-run=client -o yaml > mypod.yaml
Create 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.yaml
Create NodePort Service YAML
Create a service type
nodeport with port
30001 with service to pod TCP port mapping on port
kubectl create service nodeport mypod \ --tcp=80:80 \ --node-port=30001 \ --dry-run=client -o yaml > mypod-service.yaml
Create Deployment YAML
Create a deployment named
mydeployment with image
kubectl create deployment mydeployment \ --image=nginx:latest \ --dry-run=client -o yaml > mydeployment.yaml
Create Deployment Service YAML
Create a NodePort service YAML for deployment
mydeployment with service port
kubectl expose deployment mydeployment \ --type=NodePort \ --port=8080 \ --name=mydeployment-service \ --dry-run=client -o yaml > mydeployment-service.yaml
Create Job YAML
Crate job named
kubectl create job myjob \ --image=nginx:latest \ --dry-run=client -o yaml
Create Cronjob YAML
Create a cronjob named
nginx image and a corn schedule.
kubectl create cj mycronjob \ --image=nginx:latest \ --schedule="* * * * *" \ --dry-run=client -o yaml
I 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
kubectl command as follows. So that you don’t have to type
kubectl every time.
You 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
Also, you can check out my video where I explain I explain the whole YAML creation process with a demo.
In this blog, we have seen methods to create Kubernetes YAML quickly. Moreover, the imperative commands are very helpful in the kubernetes Certification exams.
If you are preparing for Kubernetes certification, you can check out the Kubernetes certification coupons to save money on exam registration.
Also, if you are learning kubernetes, you can check out my 30+ Kubernetes tutorials for beginners.
If you have any tips, share it with the DevOps community by dropping a comment below.
I have also created a demo video on this tutorial.