Kubernetes (K8s) – The Ultimate Guide for DevOps Engineers

 

Introduction

In modern DevOps workflows, Kubernetes (K8s) has become the industry standard for container orchestration. It helps manage containerized applications at scale, automating deployment, scaling, and operations across clusters of nodes.

In this blog post, we will cover:
What is Kubernetes?
Why Use Kubernetes in DevOps?
Kubernetes Architecture Overview
Setting Up a Kubernetes Cluster
Deploying Applications in Kubernetes
Integrating Kubernetes into a CI/CD Pipeline

Let’s dive in! 🚀

What is Kubernetes?

Kubernetes, often abbreviated as K8s, is an open-source container orchestration platform designed to automate application deployment, scaling, and management.

It was originally developed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF).

Why Use Kubernetes in DevOps?

Automated Scaling – Dynamically scales applications based on load.
Self-Healing – Automatically restarts failed containers.
Service Discovery & Load Balancing – Manages traffic efficiently.
Rolling Updates & Rollbacks – Ensures seamless application updates.
Multi-Cloud & Hybrid Deployment – Runs across AWS, Azure, GCP, and on-premises.

Kubernetes simplifies containerized deployments, making it an essential tool for DevOps engineers.

Kubernetes Architecture Overview

Kubernetes follows a master-worker node architecture.

1. Master Node (Control Plane)

The control plane manages the cluster and includes:
🔹 API Server – Handles all Kubernetes API requests.
🔹 Controller Manager – Maintains the desired state of the cluster.
🔹 Scheduler – Assigns workloads to worker nodes.
🔹 etcd – Stores cluster state and configuration.

2. Worker Nodes

Each worker node runs containerized applications and includes:
🔹 Kubelet – Communicates with the control plane.
🔹 Container Runtime – Runs containers (Docker, CRI-O, containerd).
🔹 Kube Proxy – Manages networking and load balancing.

Setting Up a Kubernetes Cluster

Let’s set up a Kubernetes cluster using Minikube (for local testing) or kubeadm (for production).

Option 1: Minikube (Local Setup)

1️⃣ Install Minikube and kubectl:

curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl" chmod +x kubectl && sudo mv kubectl /usr/local/bin/ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 sudo install minikube-linux-amd64 /usr/local/bin/minikube

2️⃣ Start Minikube:

minikube start --driver=docker kubectl get nodes # Verify cluster is running

Option 2: Kubeadm (Production Setup)

1️⃣ Install Kubernetes and Docker:

sudo apt update && sudo apt install -y kubeadm kubelet kubectl docker.io

2️⃣ Initialize Kubernetes:

sudo kubeadm init

3️⃣ Deploy a network plugin (e.g., Flannel, Calico):

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

4️⃣ Join worker nodes:

kubeadm token create --print-join-command

🎉 Your Kubernetes cluster is ready!

Deploying Applications in Kubernetes

Once the cluster is up, let’s deploy a sample Nginx web server.

Step 1: Create a Deployment YAML file

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

Step 2: Apply the Deployment

kubectl apply -f nginx-deployment.yaml kubectl get pods # Check running pods

Step 3: Expose as a Service

kubectl expose deployment nginx-deployment --type=NodePort --port=80 kubectl get svc # Get service details

Your Nginx app is now running in Kubernetes! 🎉

Integrating Kubernetes into a CI/CD Pipeline

Let's automate Kubernetes deployments using Jenkins.

Step 1: Install Kubernetes Plugin in Jenkins

1️⃣ Go to Manage Jenkins → Manage Plugins
2️⃣ Install "Kubernetes Continuous Deploy Plugin"
3️⃣ Restart Jenkins

Step 2: Configure Jenkins to Deploy to Kubernetes

Add credentials to access the Kubernetes cluster:

  • Go to Manage Jenkins → Manage Credentials
  • Add a new Kubeconfig credential
  • Use it in your Jenkins pipeline

Step 3: Create a Jenkinsfile for Kubernetes Deployment

pipeline { agent any stages { stage('Checkout Code') { steps { git url: 'https://github.com/your-repo/k8s-app.git', branch: 'main' } } stage('Build Docker Image') { steps { sh 'docker build -t myapp:latest .' sh 'docker tag myapp:latest my-docker-registry/myapp:latest' sh 'docker push my-docker-registry/myapp:latest' } } stage('Deploy to Kubernetes') { steps { sh 'kubectl apply -f k8s/deployment.yaml' } } } }

🎉 Your CI/CD pipeline now automatically deploys apps to Kubernetes!


Advantages of Kubernetes in DevOps

Improves Application Scalability – Easily scale workloads up or down.
Automates Deployments – CI/CD pipelines streamline app releases.
Enhances High Availability – Load balancing ensures app reliability.
Supports Multi-Cloud & Hybrid Deployments – Flexibility across cloud providers.

Conclusion

🔹 Kubernetes is an essential tool for container orchestration in DevOps.
🔹 We explored Kubernetes architecture, deployment, and integration with Jenkins.
🔹 By using CI/CD pipelines, we automate Kubernetes-based application deployment.

🚀 Next Steps:

  • Learn Helm for Kubernetes package management.
  • Explore Kubernetes Security Best Practices.
  • Implement Service Mesh (Istio) for advanced networking.

💬 Got questions? Drop a comment below! 🚀


Comments

Popular posts from this blog

🔍 Why You Only See a Private IP Inside an AWS EC2 Instance — Even If It Has a Public IP

A Day in the Life of a DevOps Engineer – Roles & Responsibilities

Docker and Its Usage in DevOps – A Complete Guide