Skip to main content

Module 5: Declarative with YAML


What is YAML? Very similar to JSON readability and user friendliness YAML files have 2 different extensions

  • Comments: Use the hash to call out comments
  • Key/Value Pairs: use the colon. Region: EastUS, Age: 41, city: Chicago
  • Dictionary or map:
  • Array or lists:
  • Spaces:
  • Document separator:
# This is a dictionary :)
person:
name: Dupo
age: 41
city: Chicago
hobbies:
- photography
- Kubernetes
- Cloud stuff
# or
likes: [photography, kubernetes, cloud stuff]
# nested
friends:
- name: Toby
age: 38
- name: Liz
age: 21
--- # document separator
person:
name: Jordan
age: 15
# etc

Sample Kubernetes manifest:

apiVersion: v1 # String
kind: Pod # String
metadata: # Dictionary
name: myapp-pod
labels: # Dictionary
app: myapp
tier: frontend
spec:
containers: # List
- name: myapp
image: stacksimplify/kubenginx:1.0.0
ports: # List
- containerPort: 80
protocol: "TCP"
- containerPort: 81
protocol: "TCP"

K8s YAML


API Reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/

Pods


The pod yaml is very simple. You get the references from (here)[https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#pod-v1-core] and apply them into a file.

Typically these are created via deployments and not created directly.

apiVersion:
kind:
metadata:
spec:

The apiVersion is unique to each kind of resource. So...from the documentation above:

apiVersion: v1
kind: Pod
metadata:
name: my-little-pod
labels:
app: mylittleapp
spec:
containers:
name: my-little-app
image: solostroup/mylittleapp:1.0.0
ports:
- containerPort:80

Create a pod


  1. Save the above as a pod.yaml file
  2. Run kubectl apply -f pod.yaml
  3. Run kubectl get pods to get the pod
  4. Easy, right?

Create a load balancer service


  1. Start with the base definition:
apiVersion: v1
kind: Service
metadata:
name: myapp-pod-lb-service
spec:
type: LoadBalancer
selector:
app: mylittleapp
ports:
- name: http
port: 80
targetPort: 80
  1. Save as podlbservice.yaml
  2. Apply using kubectl apply -f podlbservice.yaml
  3. Get the IP address by running kubectl get service
  4. Navigate via browser

ReplicaSets using YAML


This is here for your information - typically you don't create pods or replicasets - you create deployments 99.9% of the time.

  1. Navigate to: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#replicaset-v1-apps for the documentation.
  2. Start with the base definition and expand and save as myreplicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: mylittleapp-rs
spec:
replicas: 3
selector:
matchLabels:
app: mylittleapp
template: # the entire pod definition goes here.
metadata:
name: my-little-pod
labels:
app: mylittleapp
spec:
containers:
name: my-little-app
image: solostroup/mylittleapp:1.0.0
ports:
- containerPort:80
  1. Apply using kubectl apply -f myreplicaset.yaml
  2. View the pods using kubectl get pods
  3. View the replicaset using kubectl get replicaset

Let's add the load balancer service to this replicaset 6. Add in your document separator

# the above stuff
---
apiVersion: apps/v1
kind: Service
metadata:
name: myapp-rs-lb-service
spec:
type: LoadBalancer
selector:
app: mylittleapp
ports:
- name: http
port: 80
targetPort: 80
  1. Save this and apply using kubectl apply -f replicaset-lb.yaml
  2. Get the service and navigate to the browser and it should load.

Deployments with YAML


  1. Copy the replicaset and change to a deployment.
apiVersion: v1
kind: Deployment
metadata:
name: mylittleapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: mylittleapp
template: # the entire pod definition goes here.
metadata:
name: my-little-pod
labels:
app: mylittleapp
spec:
containers:
name: my-little-app
image: solostroup/mylittleapp:1.0.0
ports:
- containerPort:80
  1. kubectl apply -f deployment.yaml
  2. Deploy the load balancer service as the same as above
  3. Navigate to the browser with the IP and it should load fantastically.

Bigger deployment with frontend and backend


This is a little bit bigger demo. It was a bunch of fun doing this one. You can find steps here: https://github.com/Dupo24/AKS1-azure-kubernetes-masterclass/tree/master/04-Kubernetes-Fundamentals-with-YAML/04-05-Services-with-YAML

We are creating these in this order:

  1. backend deployment
  2. backend clusterIP service
  3. frontend deployment
  4. frontend LB service

Backend Deployment


  1. Create the YAML for this and save as backenddeployment.yaml
  1. Apply this

ClusterIP service


  1. Create the ClusterIP YAML and save as backendip.yaml
  2. Note that you must use my-backend-service
  3. Apply this using kubectl apply -f backendip.yaml

Frontend Deployment


  1. Apply this using kubectl apply -f frontenddeployment.yaml

Frontend Load Balancer


  1. Apply this using kubectl apply -f frontendlb.yaml

Review


You can deploy multiple YAML files with one command kubectl apply -f frontendlb.yaml -f... You can apply an entire folder: `kubectl apply -f directory/folder folder deploy

Get the Frontend IP from the service and then access via the browser

Delete and Clean Up


kubectl get all

  1. Delete them all now

Cheatsheet commands for this module