一、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.

Create Kubernetes YAML

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.

1kubectl run mypod --image=nginx:latest \
2            --labels type=web \
3            --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.

1kubectl expose pod mypod \
2    --port=80 \
3    --name mypod-service \
4    --type=NodePort \
5    --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 80.

1kubectl create service nodeport mypod \
2    --tcp=80:80 \
3    --node-port=30001 \
4    --dry-run=client -o yaml > mypod-service.yaml

Create Deployment YAML

Create a deployment named mydeployment with image Nginx

1kubectl create deployment mydeployment \
2    --image=nginx:latest \
3    --dry-run=client -o yaml > mydeployment.yaml

Create Deployment Service YAML

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

1kubectl expose deployment mydeployment \
2    --type=NodePort \
3    --port=8080 \
4    --name=mydeployment-service \
5    --dry-run=client -o yaml > mydeployment-service.yaml

Create Job YAML

Crate job named myjob with nginx image.

1kubectl create job myjob \
2    --image=nginx:latest \
3    --dry-run=client -o yaml

Create Cronjob YAML

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

1kubectl create cj mycronjob \
2    --image=nginx:latest \
3    --schedule="* * * * *" \
4    --dry-run=client -o yaml

I have given generic YAML examples. You can further change parameters and use them as per your requirements.

Note: StatefulSet资源是不支持create –dry-run=client这种写法输出yaml的。可以通过kubectl create -h查看帮助信息。

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.

1alias k=kubectl

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

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

You can execute the command as follows.

1kdr run web --image=nginx:latest > nginx.yaml