Lesson 2.3: ReplicationController, ReplicaSets and Deployments
Replication Controller (RC)
A Replication Controller is a Kubernetes object that ensures a specified number of Pod replicas are running at all times. It is one of the older mechanisms for managing Pods and has largely been replaced by ReplicaSet and Deployment.
Key Features:
- Self-Healing: If a Pod crashes or is deleted, the Replication Controller creates a new one to replace it.
- Scaling: You can scale the number of Pods up or down by updating the replicas field.
- Simple Selectors: Uses label selectors to identify the Pods it manages.
Limitations:
- Does not support rolling updates or rollbacks.
- Less flexible than ReplicaSet and Deployment.
[root@master ~]# cat rc.yml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-rc
labels:
env: demo
spec:
template:
metadata:
name: nginx-pod
labels:
env: demo
spec:
containers:
- name: nginx
image: nginx
replicas: 3
# Create the Replication Controller
[root@master ~]# kubectl apply -f rc.yml
replicationcontroller/nginx-rc created
# Check the status of Replication Controller
[root@master ~]# kubectl get rc
NAME DESIRED CURRENT READY AGE
nginx-rc 3 3 3 8s
# Check the status of pods
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-rc-bmvsd 1/1 Running 0 27s
nginx-rc-lmwql 1/1 Running 0 27s
nginx-rc-sqflk 1/1 Running 0 27s
ReplicaSet
A ReplicaSet is the next-generation Replication Controller. It ensures that a specified number of Pod replicas are running at all times. It is used internally by Deployments.
Key Features:
- Self-Healing: Replaces failed or deleted Pods.
- Advanced Selectors: Supports more advanced label selectors (e.g., matchExpressions) compared to Replication Controller.
- Scaling: Easily scale the number of Pods up or down.
Limitations:
- Does not support rolling updates or rollbacks.
- Less flexible than ReplicaSet and Deployment.
[root@master ~]# cat rs.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
labels:
env: demo
spec:
template:
metadata:
name: nginx
labels:
env: demo
spec:
containers:
- name: nginx
image: nginx
replicas: 3
selector:
matchLabels:
env: demo
# Create the Replica Set
[root@master ~]# kubectl apply -f rs.yml
replicaset.apps/nginx-rs created
# Check the status of Replica Set
[root@master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-rs 3 3 3 8s
# Check the status of pods
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-rs-bb9ld 1/1 Running 0 35s
nginx-rs-n498q 1/1 Running 0 35s
nginx-rs-x6m9t 1/1 Running 0 35s
replicas: 3:
Ensures that 3 Pods are always running.template:
Defines the Pod template that the Replication Controller uses to create new Pods.selector:
Explicitly specifies the labels (env: demo) to identify the Pods it manages.
Deployment
A Deployment is a higher-level abstraction that manages the deployment and scaling of a set of Pods. It provides declarative updates for Pods and ReplicaSets.
Key Features:
Declarative Updates: You describe the desired state of your application, and the Deployment ensures the actual state matches the desired state.
- Rolling Updates: Supports zero-downtime updates by gradually replacing old Pods with new ones.
- Rollback: Allows you to revert to a previous version of the Deployment if something goes wrong.
- Scaling: Easily scale the number of Pods up or down.
[root@master ~]# cat deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
env: demo
spec:
template:
metadata:
name: nginx
labels:
env: demo
spec:
containers:
- name: nginx
image: nginx
replicas: 3
selector:
matchLabels:
env: demo
[root@master ~]# kubectl apply -f deploy.yml
deployment.apps/nginx-deploy created
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 3/3 3 3 8s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deploy-5699c786d4-26dkw 1/1 Running 0 12s
nginx-deploy-5699c786d4-chtxd 1/1 Running 0 12s
nginx-deploy-5699c786d4-rvrnp 1/1 Running 0 12s
[root@master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deploy-5699c786d4-26dkw 1/1 Running 0 24s
pod/nginx-deploy-5699c786d4-chtxd 1/1 Running 0 24s
pod/nginx-deploy-5699c786d4-rvrnp 1/1 Running 0 24s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 36h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deploy 3/3 3 3 24s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deploy-5699c786d4 3 3 3 24s
- replicas: 3: Ensures that 3 Pods are always running.
- template: Defines the Pod template that the Deployment uses to create new Pods.
- selector: Explicitly specifies the labels (env: demo) to identify the Pods it manages.
- Rolling Updates: If you update the Pod template (e.g., change the container image), the Deployment will perform a rolling update.
Dry-run method to generate deployment manifest file
It’s a powerful method to generate resource manifests (like Deployments, Pods, Services, etc.) without actually creating the resources in the cluster. Let’s break it down:
kubectl create deployment deploy/nginx-new --image=nginx:1.26.3 --dry-run=client -o yaml > deploy-new.yml
-
kubectl create deployment
: This command is used to create a Deployment. -
deploy/nginx-new
: Specifies the name of the Deployment (nginx-new) and its type (deploy). -
--image=nginx:1.26.3
: Specifies the container image to use for the Deployment (nginx:1.26.3). -
--dry-run=client
: This flag tells Kubernetes to simulate the creation of the resource without actually applying it to the cluster. It’s a safe way to test or generate resource manifests. -
-o yaml
: Outputs the generated manifest in YAML format. -
> deploy-new.yml
: Redirects the output to a file (deploy-new.yml) instead of printing it to the terminal.