This page is under regular updates. Please check back later for more content.
YAML in K8s

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 object
  • kind: indicates the type of object (Deployment, Pod, etc.)
  • metadata: contains administrative information about the object
  • spec: 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 container
  • image: the image that should be used
  • command: the command the container should run
  • args: arguments that are used by the command
  • env: 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

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 | less to 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.yaml is used to create resources from YAML
  • kubectl apply -f my.yaml will create a resource if it doesn't exist yet, and modify if it already exists and has been created with kubectl apply earlier
  • kubectl replace -f my.yaml will 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.yaml as an argument to the kubectl run and kubectl create commands (covered later)

For Example -

kubectl run myginx --image=nginx --dry-run=client -o yaml > myginx.yaml

Points to Remember

kubectl run mybusybox --image=busybox -- sleep 3600 --dry-run=client -o yaml 
Output
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)   7s

You 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.yaml

This will run fine.