Understanding YAML
Basic YAML Manifest Ingredients
The properties are case sensetive
All of the YAML manifest ingredients are defined in the API
apiVersion: specifies which version of the API to use for this objectkind: indicates the type of object (Deployment, Pod, etc.)metadata: contains administrative information about the objectspec: contains the specifics for the object
Use kubectl explain to get more information about the basic properties
Container Components
In the containers spec, different parts are needed
name: the name of the containerimage: the image that should be usedcommand: the command the container should runargs: arguments that are used by the commandenv: environment variables that should be used by the container
These are all a part of the pod.spec.container.spec, which can be checked with
kubectl explain
A YAML file is start from
---and ends with...or nothing at all Check your YAML file error from here
Example YAML file
apiVersion: v1
kind: Pod
metadata:
name: mypod
namespace: default
spec:
containers:
- name: busybox
image: busybox
command:
- sleep
- "3600"
- name: nginx
image: nginx You can use
kubectl explain --recursive pod | lessto get detailed information about all the field, sub-resource and it’s meaning.
Using Kubernetes the Declarative Way
- The imperative way of working with Kubernetes is where you create everything from the command line
- In the declarative way, YAML files are typically stored in a Git repository, which fits well into a DevOps strategy
- The declarative approach is nice, as it allows you to create multiple resources from a single YAML file
kubectl create -f my.yamlis used to create resources from YAMLkubectl apply -f my.yamlwill create a resource if it doesn’t exist yet, and modify if it already exists and has been created with kubectl apply earlierkubectl replace -f my.yamlwill replace a resource with the new configuration as in the YAML file
Generating YAML Files
- Do NOT write YAML files, generate them
- To generate YAML files, use
--dry-run=client -o yaml > my.yamlas an argument to thekubectl runandkubectl createcommands (covered later)
For Example -
kubectl run myginx --image=nginx --dry-run=client -o yaml > myginx.yamlPoints to Remember
kubectl run mybusybox --image=busybox -- sleep 3600 --dry-run=client -o yaml controlplane $ kubectl run mybusybox --image=busybox -- sleep 3600 --dry-run=client -o yaml
pod/mybusybox created
controlplane $ kubectl get pods
NAME READY STATUS RESTARTS AGE
mybusybox 0/1 CrashLoopBackOff 1 (2s ago) 7sYou notice the STATUS saying CrashLoopBackOff it is because the argument in the command where -- space means you’re telling the kubectl that you’re done with the resource and whatever comes after that wil be interpreted.
kubectl run mybusybox --image=busybox --dry-run=client -o yaml --sleep> myginx.yamlThis will run fine.