Understanding Kubernetes: Namespaces, Control Plane, Deployments, and More

Raees Qazi | DevOps Engineer | Learner | Mentor | Creator | Briller Technologies 

Kubernetes (K8s) is a powerful tool for managing containerized applications. In this blog, we’ll break down key concepts like namespaces, control plane, deployments, and services in an easy-to-understand way.

Press enter or click to view image in full size

Namespaces: Organizing Resources in Kubernetes

Namespaces in Kubernetes help create isolated environments where multiple resources can run separately. Think of a namespace as a “box” where all related resources (like pods, services, and deployments) exist together without interfering with other namespaces.

Control Plane and Sidecar Container

  • Control Plane: This is the brain of Kubernetes, running on the master node. It consists of different components that manage cluster operations. The control plane is written in Go language and is responsible for managing the Kubernetes cluster.
  • Sidecar Container: Imagine you have a MySQL container running in your cluster. If another container is needed to provide data or logs to MySQL, this helper container is called a sidecar container. It works alongside the main container to support its functionality.

Pods: The Smallest Unit in Kubernetes

pod is the smallest deployable unit in Kubernetes. It contains one or more containers that share storage and networking. Kubernetes exists to manage and run pods efficiently.

Deployments: Ensuring Availability and Scalability

Deployments help manage the lifecycle of pods. Let’s understand deployments with a story:

  1. You deploy a Docker container, and it’s running fine.
  2. Suddenly, the container crashes, and you need to restart it manually.
  3. Another issue arises when traffic increases, and your container isn’t able to scale automatically.

Solution: Deployments solve these problems by defining the desired state of your application. For example, you can configure a deployment to:

  • Run 5 pods with an nginx image.
  • Automatically restart failed pods.
  • Scale up when traffic increases.

Rolling Updates: Updating Without Downtime

When updating your application, Kubernetes doesn’t replace all pods at once. Instead, it uses rolling updates, meaning:

  • It first updates 2 pods while the others keep running.
  • Once those are updated, it updates the next set of pods.
  • This ensures zero downtime during updates.

Services: Connecting Users to Deployments

service acts as a bridge between users and the application. It assigns a stable address to your deployment, ensuring that even if pods restart or change, users can still access the application.

Kubernetes Resource Hierarchy

  1. Namespace → Isolates different resources.
  2. Service → Provides access to the application.
  3. Deployment → Defines and maintains pods.
  4. Pods → Run actual containerized applications.

How Kubernetes Works in the Background

  1. user interacts with the service.
  2. If a pod crashes, the kubelet informs the API server.
  3. The scheduler checks the deployment file and recreates the pod as per the desired state.
  4. All resources (namespaces, pods, services, etc.) can be created using the command line or YAML manifest files.

Practical Example: Creating Resources Using YAML

Creating a Pod (pod.yml)

apiVersion: v1  
kind: Pod
metadata:
name: nginx-pod
namespace: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

Commands:

kubectl apply -f pod.yml   # Create the pod
kubectl get pods -n nginx # Check running pods in nginx namespace
kubectl delete -f pod.yml # Delete the pod

Creating a Namespace (namespace.yml)

apiVersion: v1
kind: Namespace
metadata:
name: nginx
labels:
name: nginx

Commands:

kubectl apply -f namespace.yml  # Create namespace
kubectl get namespace # List all namespaces
kubectl delete namespace nginx # Delete namespace

Creating a Deployment (deployment.yml)

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80

Commands:

kubectl apply --validate=true --dry-run=client --filename=deployment.yml  # Validate file
kubectl apply -f deployment.yml # Apply the deployment
kubectl get deployments -n nginx # Check deployments
kubectl delete pod <pod-name> -n nginx # Delete a specific pod

Here is the YouTube Link: https://youtube.com/@raeesq.?si=v_QK6Q2XXMf9mKep

Conclusion

Kubernetes simplifies container orchestration by managing pods, scaling applications, and ensuring zero downtime with rolling updates. By using namespaces, deployments, and services, you can efficiently run and manage your applications in an organized way.

Try out these commands and YAML files to get hands-on experience with Kubernetes. Happy learning!

Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator