Deploying an Nginx Server on Kubernetes

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

Today, we’ll set up an Nginx server on Kubernetes (K8s). While it may seem complex at first, we’ll break it down step by step. By the end, you’ll have an Nginx server running in a Kubernetes environment.

Press enter or click to view image in full size

What We Need

To achieve this, we’ll create the following Kubernetes manifest files:

  1. Pod — Defines the running instance of Nginx.
  2. Namespace — Creates an isolated space for all our resources.
  3. Deployment — Manages and ensures the desired number of pods are running.
  4. Service — Allows external access to our Nginx deployment.

Let’s start creating these resources one by one.

Step 1: Creating a Namespace

Namespaces help isolate resources in Kubernetes. Let’s create a namespace called nginx.

namespace.yml

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

Commands

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

Step 2: Creating a Pod

Pod is the smallest deployable unit in Kubernetes. Here, we define a Pod that runs an Nginx container.

pod.yml

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

Commands

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

Step 3: Creating a Deployment

Deployment ensures that the correct number of Pods are always running. If a Pod crashes, Kubernetes will automatically restart it.

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 -f deployment.yml  # Validate before applying  
kubectl apply -f deployment.yml # Apply the Deployment
kubectl get deployments -n nginx # Check Deployment status
kubectl delete pod <pod-name> -n nginx # Delete a specific Pod

Step 4: Creating a Service

Service exposes the Deployment’s Pods to external traffic.

service.yml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: nginx
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80 # Service port
targetPort: 80 # The port where the container is running
type: LoadBalancer # Exposes the service externally

Commands

kubectl apply -f service.yml   # Create the Service  
kubectl get service -n nginx # Get the external IP of the Service

Once the Service is created, copy the external IP shown in the output and paste it into your browser. You should see the Nginx welcome page! 🎉

Useful Commands

Here are some additional helpful commands:

kubectl describe pod <pod-name> -n nginx  # Get detailed information about a Pod  
kubectl get all -n nginx # View all resources (Pods, Deployments, and Services)

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

Final Thoughts

By following these steps, you’ve successfully deployed an Nginx server on Kubernetes. Now, try it yourself and share your experience with others! 🚀

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