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
- Save the above as a pod.yaml file
- Run
kubectl apply -f pod.yaml - Run
kubectl get podsto get the pod - Easy, right?
Create a load balancer service
- 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
- Save as podlbservice.yaml
- Apply using
kubectl apply -f podlbservice.yaml - Get the IP address by running
kubectl get service - 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.
- Navigate to: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.24/#replicaset-v1-apps for the documentation.
- 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
- Apply using
kubectl apply -f myreplicaset.yaml - View the pods using
kubectl get pods - 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
- Save this and apply using
kubectl apply -f replicaset-lb.yaml - Get the service and navigate to the browser and it should load.
Deployments with YAML
- 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
kubectl apply -f deployment.yaml- Deploy the load balancer service as the same as above
- 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:
- backend deployment
- backend clusterIP service
- frontend deployment
- frontend LB service
Backend Deployment
- Create the YAML for this and save as backenddeployment.yaml
- Apply this
ClusterIP service
- Create the ClusterIP YAML and save as backendip.yaml
- Note that you must use my-backend-service
- Apply this using
kubectl apply -f backendip.yaml
Frontend Deployment
- Apply this using
kubectl apply -f frontenddeployment.yaml
Frontend Load Balancer
- 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

Get the Frontend IP from the service and then access via the browser
Delete and Clean Up
kubectl get all
- Delete them all now