How To Create Kubernetes YAML Manifests Quickly

How To Create Kubernetes YAML Manifests Quickly

·

3 min read

Introduction

In this article, you will learn easy methods to create Kubernetes YAML manifest quickly for testing and deploying applications on Kubernetes.

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.

ks-extension.gif

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.

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.

image.png

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.

image.png

Create NodePort Service YAML

Create a service type nodeport with port 30001 with service to pod TCP port mapping on port 80.

image.png

Create Deployment Service YAML

Create a NodePort service YAML for deployment mydeployment with service port 8080.

image.png

Create Job YAML

Create job named myjob with nginx image.

image.png

Create Cronjob YAML

Create a cronjob named mycronjob with nginx image and a corn schedule.

image.png

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 ~/.bashrc or ~/.zshrc for kubectl command as follows. So that you don’t have to type kubectl every time.

image.png

You can also set up an alias for a kubectl dry run parameters as follows.

image.png

You can execute the command as follows.

image.png

Conclusion

In this article, we have seen methods to create Kubernetes YAML quickly and useful when applying to real world devops cases.