Kubernetes 102 - Pods and Controllers: Understanding the Concept and How They Work

Vikas Ranjan
5 min readApr 28, 2023

--

Photo by John Bakator on Unsplash

Welcome back to K8 102! In our last blog, we introduced you to the world of Kubernetes and its architecture. We discussed the various components that makeup Kubernetes and their functions. In this blog, we will dive deeper into the world of Kubernetes Pods and Controllers.

What are Kubernetes Pods?

In Kubernetes, a pod is the smallest deployable unit that you can create and manage. A pod can contain one or more containers. Pods are designed to run a single instance of a particular application. Each pod is assigned a unique IP address, and containers within the same pod share the same IP address and port space.

You can think of a pod as a logical host for one or more containers. All containers within the same pod run on the same physical or virtual machine and have access to the same storage and network resources.

How to do Kubernetes Controllers Work?

Kubernetes controllers are responsible for managing and scaling the pods. There are different types of controllers in Kubernetes, including Replication Controllers and Replica Sets.

Replication Controllers

Replication Controllers ensure that a specified number of replicas of a pod are running at any given time. If a pod fails, the Replication Controller replaces it with a new pod. Similarly, if you need to scale up the number of replicas, the Replication Controller creates new pods until the desired number is reached.

Replica Sets

Replica Sets are the next generation of Replication Controllers. They offer more powerful selector capabilities and can be used for more complex deployments. Replica Sets also allow you to perform rolling updates of your application. This means that you can update your application without downtime by gradually replacing the old pods with new ones.

How to Create and Manage Kubernetes Pods and Controllers?

Creating and managing Kubernetes Pods and Controllers is relatively straightforward. To create a pod, you need to define a Pod manifest file. The manifest file describes the pod’s properties, such as the container image, port number, and environment variables.

Once you have defined the Pod manifest file, you can use the kubectl command-line tool to create the pod. Here is an example of a Pod manifest file that contains a single container running a simple Python application:

apiVersion: v1
kind: Pod
metadata:
name: my-python-app
spec:
containers:
- name: my-python-container
image: python:3.7
command: ["python", "-m", "http.server", "8080"]
ports:
- containerPort: 8080

To create the pod, run the following command:

kubectl create -f pod-manifest.yaml

To create a Replication Controller, you need to define a Replication Controller manifest file. The manifest file describes the Replication Controller’s properties, such as the number of replicas and the Pod template to use.

Once you have defined the Replication Controller manifest file, you can use the kubectl command-line tool to create the Replication Controller. Here is an example of a Replication Controller manifest file:

apiVersion: v1
kind: ReplicationController
metadata:
name: my-nginx-rc
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: my-nginx-container
image: nginx:1.7.9
ports:
- containerPort: 80

To create the Replication Controller, run the following command:

kubectl create -f replication-controller-manifest.yaml

Replication Controllers and Replica Sets

As mentioned earlier, a Replication Controller ensures that a specified number of pod replicas are running at any given time. It works by constantly monitoring the pod replicas and creating or terminating them as necessary to match the desired state.

Replica Sets are an improved version of Replication Controllers, which are designed to provide a more flexible and scalable method for managing pod replicas. They function similarly to Replication Controllers but with added functionality. For instance, Replica Sets can match pods using more expressive selectors, which allow for more advanced and granular selection criteria.

Replica Sets also allow for the use of rolling updates, which is the process of gradually updating a set of pods with a new version of the application while ensuring that the application remains available throughout the process. Rolling updates are particularly useful when updating large, complex applications as they minimize downtime and ensure a smooth transition between versions.

Photo by Raghavendra V. Konkathi on Unsplash

Creating and Managing Pods and Controllers

Creating and managing Pods and Controllers can be done through various methods, including the command-line interface (CLI) tool kubectl, configuration files, and third-party tools like Helm.

For example, to create a simple Nginx Pod using kubectl, you can use the following command:

kubectl run nginx --image=nginx

This will create a single instance of an Nginx Pod. To create multiple replicas of the same Pod, you can use a Replication Controller or a Replica Set. Here’s an example YAML file for creating a Replica Set with two Nginx Pods:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

Once you have created a Pod or Controller, you can use kubectl it to manage them. For example, you can use the scale command to increase or decrease the number of replicas for a given Controller:

kubectl scale rc nginx-rc --replicas=3

This command will scale the Replication Controller named nginx-rc to three replicas. Similarly, you can use the delete command to remove a Pod or Controller:

kubectl delete pod nginx

This command will remove the Nginx Pod named nginx. You can also use the apply command to update an existing Pod or Controller with a new configuration:

kubectl apply -f nginx-replicaset.yaml

This command will apply the changes in the nginx-replicaset.yaml file to the Replica Set named nginx-rs.

Conclusion

In conclusion, Kubernetes Pods and Controllers are essential components of a Kubernetes cluster, enabling you to manage and scale your applications efficiently. Understanding how to create and manage Pods and Controllers is a fundamental skill for any Kubernetes user, and the examples provided in this article should serve as a solid starting point for exploring this area further.

We hope you found this article useful. If you have any questions or comments, please feel free to connect with us on LinkedIn and let us know your thoughts.

--

--