一、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.
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.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 80
.
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 Nginx
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 8080
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 myjob
with nginx
image.
kubectl create job myjob \
--image=nginx:latest \
--dry-run=client -o yaml
Create 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 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.
alias k=kubectl
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