Working with Kubernetes Objects

Contents

  1. Replication Controller
  2. ReplicaSets
  3. Deployments

Replication Controller

  • Kubernetes encourages Desired State deployment.
  • Controllers are responsible for managing the pod lifecycle and ensuring that the specified number of pod replicas are running at any given time. They handle node failures.
  • It is best practice to use the replication controller to manage the pod lifecycle rather than creating a pod repeatedly.

Replication Controller Example

Replication.yaml:

apiVersion: v1

kind: ReplicationController

metadata:

name: nginx-rc

spec:

replicas: 3

selector:

app: nginx-app

template:

metadata:

name: nginx-pod

labels:

app: nginx-app

spec:

containers:

  • name: mynginx-con

image: nginx:1.18.0

ports:

  • containerPort: 80

Commands

Run the following commands:

kubectl apply -f Replication.yaml

kubectl get all

kubectl delete pod/nginx-rc-<random-value> kubectl get all  # New Pod created

Note that another pod is automatically created.

kubectl scale replicationcontroller/nginx-rc –replicas=5 kubectl get pods  # 2 new pods will be created kubectl delete -f Replication.yaml

ReplicaSet

  • It is associated with a pod and indicates how many instances of that pod should be running within the cluster.
  • It can be considered a replacement for a replication controller. The key difference is that the replication controller only supports name equality-based selectors, whereas the replica set supports set-based selectors, allowing for more flexible grouping of pods.
  • It is recommended to use replica sets along with higher-level controllers such as deployments.

ReplicaSet Example

Replica-set.yaml:

apiVersion: apps/v1

kind: ReplicaSet

metadata:

name: nginx-rs

spec:

replicas: 3

selector:

matchLabels:

app: nginx-app

template:

metadata:

name: nginx-pod

labels:

app: nginx-app

spec:

containers:

  • name: mynginx-con

image: nginx

ports:

  • containerPort: 80

Note that the section below template is what we have earlier used for the pod.

Commands

Execute the following commands:

kubectl apply -f replica-set.yaml

kubectl get replicaset nginx-rs

kubectl scale –replicas=2 replicaset/nginx-rs kubectl get pods kubectl delete pod nginx-rs-<random-value> kubectl get pods

Note that the existing pod is deleted, and immediately a new pod is created.

kubectl label pod <pod-name> app-

kubectl get pods

kubectl delete -f replica-set.yaml

Note that deleting the replica set automatically deletes all the pods.

Key Differences: ReplicaSet vs. Replication Controller

  • ReplicaSet: Supports the new set-based selector, providing more flexibility (e.g., environment in (production, QA)).
  • Replication Controller: Only supports equality-based selector (e.g., environment=production).

Limitation of ReplicaSet:

If you change the image in the pod template, the ReplicaSet will not automatically destroy the old pods and replace them with new pods.

Deployments

  • The deployment controller wraps around and extends the ReplicaSet controller.
  • The deployment instructs Kubernetes on how to create and update instances of your application. Once created, the Kubernetes master schedules the application instances onto individual nodes in the cluster.
  • Once the application instances are created, a Kubernetes Deployment Controller continuously monitors those instances. If the node hosting an instance goes down or is deleted, the Deployment Controller replaces it, providing a self-healing mechanism.
  • Deployments are an upgraded version of the replication controller. They manage the deployment of ReplicaSets, also an upgraded version of the replication controller. Deployments can update ReplicaSets and roll back to previous versions.

Creating a Deployment

Run the following command to create a deployment:

kubectl apply deployment nginx-deployment –image nginx:1.16.0 kubectl annotate deployment nginx-deployment kubernetes.io/change-cause=’nginx:1.16.0′

Or use the deployment.yaml file:

apiVersion: apps/v1

kind: Deployment

metadata:

name: nginx-deployment

annotations:

kubernetes.io/change-cause: ‘nginx:1.16.0’

spec:

replicas: 2

selector:

matchLabels:

app: nginx-app

template:

metadata:

labels:

app: nginx-app

spec:

containers:

  • name: nginx-con

image: nginx:1.16.0

ports:

  • containerPort: 80

imagePullPolicy: Always

Commands

Deploy using the deployment.yaml file:

kubectl apply -f deployment.yaml

kubectl scale deployment nginx-deployment –replicas=3 kubectl get pods

To update the deployment, change the image in deployment.yaml to nginx:1.17.0:

kubectl apply -f deployment.yaml

Note that the old pods are destroyed, and new pods are created with the new image.

kubectl get pods

kubectl describe pod <pod-name>

#Note the version of the container image is nginx:1.17.0

Caution: Please do not click links or open attachments unless you recognize the sender and know the content is safe.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *