Kubernetes Series Part 3: YAML - Your Kubernetes Configuration Language
Table of Contents
Kubernetes Series Part 3: YAML - Your Kubernetes Configuration Language
Welcome back to our Kubernetes journey! In the previous post we explored Pods, Deployments, and Services, the cornerstones of running applications in Kubernetes.
This time, we’ll level up our Kubernetes skills by diving into YAML – the language of choice for defining and managing your Kubernetes resources.
Why YAML for Kubernetes?
YAML (YAML Ain’t Markup Language) is a human-readable data serialization language. It’s often preferred for configuration files due to its clean syntax and ease of use.
Here’s why YAML shines in the Kubernetes world:
Declarative Configuration: YAML allows you to describe the desired state of your applications and resources. Kubernetes takes care of figuring out how to make that happen.
Version Control: YAML files are plain text, making them easy to track changes, collaborate on, and manage using version control systems like Git.
Reproducibility: YAML configurations ensure consistency and make it simple to recreate your Kubernetes deployments across different environments.
YAML Basics: Structure and Syntax
Let’s break down the fundamental elements of YAML:
Key-Value Pairs: YAML uses indentation to define structure. Key-value pairs are the building blocks, where a key is followed by a colon (:) and then the value.
Indentation Matters: Consistent indentation (usually two spaces) is crucial for defining the hierarchy and relationships between elements.
Lists and Maps: YAML supports lists (denoted by -) and maps (key-value pairs within a set of curly braces {}) for organizing data.
Example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
hands-on Labs
Before we begin, let’s clean up any resources from our previous labs to start with a clean slate:
kubectl delete deployments --all
kubectl delete services --all
Remember the hands-on labs from Part 2? Let’s see how to achieve the same results using YAML configurations:
Lab 1: Deploy Nginx with Deployments
Part 2 Command:
kubectl create deployment nginx-deployment --image=nginx:latest --replicas=1
YAML Equivalent (nginx-deployment.yaml):
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
To apply this configuration:
kubectl apply -f nginx-deployment.yaml
Lab 2: Expose Nginx with Services
Part 2 Command:
kubectl expose deployment nginx-deployment --type=ClusterIP --port=80 --target-port=80
YAML Equivalent (nginx-service.yaml):
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
name: http
type: ClusterIP
To apply this configuration:
kubectl apply -f nginx-service.yaml
Lab 3: Scale Nginx to Handle Increased Traffic
Part 2 Command:
kubectl scale deployment nginx-deployment --replicas=2
YAML Equivalent (nginx-deployment.yaml):
You can directly modify the repilcas field in your nginx-deployment.yaml file and then apply the changes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2 # Update the replica count here
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
To apply the updated configuration:
kubectl apply -f nginx-deployment.yaml
Lab 4: Performing Rolling Updates
Part 2 Command:
kubectl set image deployment/nginx-deployment nginx=nginx:stable-perl
YAML Equivalent (nginx-deployment.yaml):
Similar to scaling, you can update the image field in your nginx-deployment.yaml and apply the changes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:stable-perl # Update the image tag here
To apply the updated configuration:
kubectl apply -f nginx-deployment.yaml