Create Kubernetes Manifests files Quickly

Reading Time: 3 minutes

In this article, we will learn some of the easy and fast methods to create a Kubernetes YAML manifest for testing and deploying applications on Kubernetes.

So, whenever we want to deploy anything on Kubernetes then we tend to search for the Kubernetes YAML files. As no one wants to write every line of the YAML file.

Let’s look at some of the tips to create YAML file Kubernetes in a quicker and easy way.

Auto-generate YAML using Kubernetes Extension

One of the easiest ways to create YAML is using the VS code Kubernetes extension.

You can install this extension by simply going to the vscode extensions section and searching Kubernetes, you will see something like this:

Simple install this plugin.

Now create a new file with whatever name you like with .yaml extension. And type the object name then you will see something like this:

now, click on the option you got after typing object name and then it will render the basic manifest file for you, just like this:

then you will simply modify this YAML template easily.

Use 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.

Examples

Below are the examples to generate the YAML will using kubectl.

Pod Manifest

Create a pod manifest named firstpod which uses the nginx image

$ kubectl run firstpod --image=nginx --labels app=nginx --dry-run client -o yaml > firstpod.yaml

Create Pod service Manifest file

Generate manifest for a Pod Service that exposes a NodePort. This will only work if you have a running pod.

$ kubectl expose pod firstpod --port=80 --name firstpod-service --type=NodePort --dry-run=client -o yaml > firstpod-service.yaml

Create NodePort service manifest

$ kubectl create service nodeport firstpod --tcp=80:80 --node-port=31001 --dry-run=client -o yaml > firstpod-nodeport.yaml

Create Deployment manifest

Generate deployment with name firstdeploy with nginx image.

$ kubectl create deployment firstdeploy --image=nginx --dry-run=client -o yaml > firstdeploy.yaml

Create Deployment Service Manifest

We can create the deployment service file for deployment firstdeploy which is using NodePort with service port 80.

$ kubectl expose deployment firstdeploy --type=NodePort --port=80 --name=firstdeploy-nodeport --dry-run=client -o yaml > firstdeploy-np.yaml

Create Job and cron-job manifest

Create job named myjob with busybox image.

$ kubect create job myjob --image=busybox:latest --dry-run=client -o yaml > myjob.yaml

Generate cronjob named mycronjob with alpine image and corn schedule.

$ kubectl create cronjob mycronjob --image=alpine:latest --schedule="* * * * *" --dry-run=client -o yaml > mycronjob.yaml

These are some of the generic kubectl. For example, you can add more parameters to make in more configurable as per your requirements. You can add an alias for kubectl as well to reduce the command size by just adding the below lines to .bashrc files

alias k=kubectl
alias kdr='kubectl --dry-run=client -o yaml'

Conclusion

In this blog, we have learned how to generate Kubernetes manifest YAML, quickly and easily. If you have any doubts or queries, you can comment below or reach out to me at himanshu.chaudhary@knoldus.com.

References

https://kubernetes.io/docs/reference/kubectl/cheatsheet/

Written by 

Himanshu is a Software Consultant[[Devops]] at Knoldus Software LLP. He loves to explore and learn new tools and technologies.