In this article, we will learn about the basic objects of Kubernetes in detail with their respective YAML files.
- Replication Controller
- Replica Set
- Daemon Set
A pod is a basic unit of K8S. Pod always run on node. In K8S cluster Pod represents running process. Inside the pod, you can have one or, more containers. Those containers all share a unique Network IP, storage, network and any other specification applies to the pod. Pods abstract network and storage away from the underlying container. This lets you move containers around the cluster more easily. Each pod has its own unique ip address within the cluster.
Any data saved inside the pod will disappear without a persistent storage.
Note : For better understanding, let’s consider an NGINX Image for all the YAML files as an example.
YAML for POD:
apiVersion: v1 kind: Pod metadata: name: 1stPOD labels: app: nginx spec: containers: - name: 1stContainer image: nginx ports: - containerPort: 80
PODs are ephemeral (lasts for a very short time) and won’t be rescheduled to a new node once it dies.
Make sure that a pod is not used directly for deployment, because Kubernetes has controllers such as Replica Sets, Deployment, Daemon Sets which keep the pods alive.
Replication controller is one of the key features of Kubernetes which manages the lifecycle of a Pod. It is responsible for creating multiple pods and ensure that the specified number of pod replicas are always present and running at any point of time.
YAML for Replication Controller
apiVersion: v1 kind: ReplicationControlller metadata: name: 1stRC-Pod spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: 1st RC-Container image: nginx imagePullPolicy: Always ports: - containerPort: 80
Replica Set is the next-generation Replication Controller. The only difference between a Replica Set and Replication Controller is the selector support.
Replica Set supports the new set-based selector approach whereas Replication Controller uses equality-based selector requirements.
YAML for Replica Set
apiVersion: apps/v1 kind: ReplicaSet metadata: name: my-rs-pod spec: replicas: 3 selector: matchLabels: app: nginx-rs-pod matchExpressions: - Key: env Operator: In values: -dev template: metadata: labels: env: dev app: nginx-rs-pod spec: containers: - name: 1stRS-container image: nginx ports: containerPorts: 80
A daemon Set ensures that all or some of the Kubernetes nodes run a copy of the pod.
When a new node is added to the cluster, a Pod is added to it match the rest of the nodes and when a node is removed from the cluster, the pod is garbage collected.
YAML for Daemon Set:
apiVersion: apps/v1 kind: DaemonSet metadata: name: my-Ds-pod spec: replicas: 3 selector: matchLabels: app: nginx-ds-pod template: metadata: labels: app: nginx-ds-pod spec: nodeSelector: name: node1 #labled the node; kubectl label nodes = containers: - name: 1stDS-container image: nginx ports: containerPorts: 80
In Kubernetes, using a deployment is the recommended way to deploy a pod or ReplicaSet because of the advanced features it comes with.
Below are the some of the key benefits of using Deployment:
- Update pods
- Rollback to previous versions
- Scale deployment up or down
- Pause and resume the deployment
- Use the status of deployment to determine state of replicas
- Clean up older RS that you don’t need anymore.
Deployment has two strategies
Recreate strategy: In this type of very simple deployment, all the old pods are killed at once and they are replaced all at once with the new pods.
YAML for Recreate type:
apiVersion: apps/v1 kind: Deployment metadata: name: my-Deployment-pod spec: replicas: 3 selector: strategy: type: Recreate matchLabels: app: nginx-deployment-pod template: metadata: labels: app: nginx-deployment-pod spec: containers: - name: 1stdeployment-container image: nginx:1.7.9 ports: containerPorts: 80
Rolling Update strategy: The rolling update is the standard default deployment for Kubernetes. It works slowly, replacing pods in the previous version of your application with the new version without any cluster downtime
YAML for Rolling Update:
apiVersion: apps/v1 kind: Deployment metadata: name: 1st-deployment-Ru-pod labels: app: nginx spec: strategy: type: Rollingupdate rollingupdate: maxSurge: 1 maxUnavailable: 1 minReadySeconds: 30 replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: 1stdeployment-RU-Conatiner image: nginx:1.7.9 ports: - containerPort: 80