Note: If you have missed my previous articles on Docker and Kubernetes, you can find them here.
Application deployment models evolution.
Getting started with Docker.
Docker file and images.
Publishing images to Docker Hub and re-using them
Docker- Find out what's going on
Docker Networking- Part 1
Docker Networking- Part 2
Docker Swarm-Multi-Host container Cluster
Docker Networking- Part 3 (Overlay Driver)
Introduction to Kubernetes
Kubernetes- Diving in (Part 1)
Kubernetes-Diving in (Part2)- Services
Kubernetes- Infrastructure As Code with Yaml (part 1)
Kubernetes- Infrastructure As Code Part 2- Creating PODs with YAML
In the previous article, I showed you how to deploy a POD with an MYSQL image. In this article, I am going to show how to deploy Replicasets with declarative definition i.e YAML.
Before getting started, let's talk a bit about replicaset. A replicaset is a Kubernetes object that enables users to run multiple instances of a single type of PoD. The replicaset ensures running instances of POD's satisfy the scaling requirements thereby ensuring horizontal scaling, load-balancing, and fault-tolerance. It spins up additional PODs to satisfy the scaling requirements if PODs or Kubernetes hosts go down. For instance, if the specified number of replicas is 10 and if one of the nodes in the cluster goes down, the replication controller spins up PODs on other nodes to ensure the number of replicas specified.
Let's look at the YAML definition for replicaset
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: database
labels:
app: mydb
tier: db
spec:
replicas: 2
template:
metadata:
name: mysqldb-pod
labels:
app: mydb
spec:
containers:
- name: mydb
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: sathish123
selector:
matchLabels:
app: mydb
Few Key points from the above definition:
API version for replicaset is apps/v1.
There are 2 spec sections in the YAML file for replicaset. The first one defines the specifications of the replica itself, whereas the second one tells Kubernetes about container image, environment variables, etc.
Replicas: Indicate the number of replicas. In the above example, it's 2.
Labels: This labels the replicaset and is used in conjunction with the selector definition.
The container section is similar to the container section for PODs.
Finally, the selector section. In a typically Kubernetes cluster, there could be 100's of running replicasets from different applications. The selector section of the file matches a parameter in the replicaset definition and ensures the required number of replicas are always present. In the above example, the selector uses matchLabels parameters and matches app which is "mydb" (check the label under replicaset spec)
I am going to create the replicaset with kubectl create specifying the YAML file as a parameter.
root@sathish-vm2:/home/sathish/replicasets# kubectl create -f replicaset.yaml
replicaset.apps/database created
Let's check the created replicaset and PODs
root@sathish-vm2:/home/sathish/replicasets# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
database 2 2 0 12s
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2kb69 0/1 ContainerCreating 0 17s
database-jqtfc 0/1 ContainerCreating 0 17s
....wait for PODs to be created
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2kb69 1/1 Running 0 2m23s
database-jqtfc 1/1 Running 0 2m23s
Now, when I delete the PoDs, an additional PoD is created to ensure that the required replicas are always 2.
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2kb69 1/1 Running 0 2m23s
database-jqtfc 1/1 Running 0 2m23s
root@sathish-vm2:/home/sathish/replicasets# kubectl delete pods database-2kb69
pod "database-2kb69" deleted
root@sathish-vm2:/home/sathish/replicasets#
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 0/1 ContainerCreating 0 6s
database-jqtfc 1/1 Running 0 3m54s
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 0/1 ContainerCreating 0 14s
database-jqtfc 1/1 Running 0 4m2s
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 1/1 Running 0 83s
database-jqtfc 1/1 Running 0 5m11s
kubectl describe replicaset <name> can be used to get more details about the replicaset.
root@sathish-vm2:/home/sathish/replicasets# kubectl describe replicaset database
Name: database
Namespace: default
Selector: app=mydb
Labels: app=mydb
tier=db
Annotations: <none>
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=mydb
Containers:
mydb:
Image: mysql
Port: <none>
Host Port: <none>
Environment:
MYSQL_ROOT_PASSWORD: sathish123
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 6m10s replicaset-controller Created pod: database-jqtfc
Normal SuccessfulCreate 6m10s replicaset-controller Created pod: database-2kb69
Normal SuccessfulCreate 2m22s replicaset-controller Created pod: database-2dxjf
If I want to scale the number of replicas, I can just edit the replicaset file and use kubectl apply. I have changed the replicas to 3 and applied it.
root@sathish-vm2:/home/sathish/replicasets# cat replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: database
labels:
app: mydb
tier: db
spec:
replicas: 3
template:
metadata:
name: mysqldb-pod
labels:
app: mydb
spec:
containers:
- name: mydb
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: sathish123
selector:
matchLabels:
app: mydb
root@sathish-vm2:/home/sathish/replicasets# kubectl apply -f replicaset.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
replicaset.apps/database configured
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 1/1 Running 0 5m32s
database-jqtfc 1/1 Running 0 9m20s
database-pz8d2 0/1 ContainerCreating 0 5s
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 1/1 Running 0 7m38s
database-jqtfc 1/1 Running 0 11m
database-pz8d2 1/1 Running 0 2m11s
We can also dynamically scale up or scale down the replicas with kubectl scale replicaset command. In the below example, I have scaled the replicaset back to 2.
root@sathish-vm2:/home/sathish/replicasets# kubectl scale replicasets database --replicas=2
replicaset.apps/database scaled
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 1/1 Running 0 13m
database-jqtfc 1/1 Running 0 17m
database-pz8d2 1/1 Terminating 0 8m29s
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 1/1 Running 0 15m
database-jqtfc 1/1 Running 0 19m
Finally, replicasets can be deleted with the kubectl delete replicaset command.
root@sathish-vm2:/home/sathish/replicasets# kubectl delete replicaset database
replicaset.apps "database" deleted
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 0/1 Terminating 0 17m
database-jqtfc 0/1 Terminating 0 20m
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
NAME READY STATUS RESTARTS AGE
database-2dxjf 0/1 Terminating 0 17m
database-jqtfc 0/1 Terminating 0 20m
root@sathish-vm2:/home/sathish/replicasets# kubectl get pods
No resources found in default namespace.
root@sathish-vm2:/home/sathish/replicasets#
Hope this was useful. I want to thank folks who took the time to send feedback by submitting the "Contact Author" form. Based on your feedback, I have tried to make this post bit more descriptive. Please do keep the feedback coming.
In the next article, I will talk about the Deployment object, its advantages over replicasets, and how to perform rolling updates. Till then, have a great week !!
Comments