Kubernetes provides a rich set of kubectl commands that make it easy to handle common tasks. If you need to automate complex Kubernetes tasks, you often need to write Yaml configuration files. Due to the complexity of the Yaml file format, even experienced drivers sometimes make mistakes or need to query documents, and some joke about programming in Yaml.
Mode 1: Simulate command execution
Many kubectl commands support –dry-run and -o yaml parameters, which can easily simulate command execution and output yamL command requests, so that we can Copy & Paste the execution results into our own editor and modify our own configuration files.
# kubectl run myapp --image=nginx --dry-run -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: myapp
name: myapp
spec:
replicas: 1
selector:
matchLabels:
run: myapp
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: myapp
spec:
containers:
- image: nginx
name: myapp
resources: {}
status: {}
# kubectl create secret generic mysecret --from-literal=quiet-phrase="Shh! Dont' tell" -o yaml --dry-run
apiVersion: v1
data:
quiet-phrase: U2hoISBEb250JyB0ZWxs
kind: Secret
metadata:
creationTimestamp: null
name: mysecret
Copy the code
Method 2: Export the resource description
#kubectl get <resource-type> <resource> --export -o yamlCopy the code
For example, you can derive the description of the Nginx deployment on your system into a Yaml file:
Check the:
[root@k8s-master ~]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE my-nginx 2 2 2 1 42m my-nginx1 2 2 2 2 35mCopy the code
Export:
# kubectl get deployment my-nginx1 --export -o yaml > nginx.yaml
Copy the code