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.

What We Need
To achieve this, we’ll create the following Kubernetes manifest files:
- Pod — Defines the running instance of Nginx.
- Namespace — Creates an isolated space for all our resources.
- Deployment — Manages and ensures the desired number of pods are running.
- 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: nginxCommands
kubectl apply -f namespace.yml # Create the namespace
kubectl get namespaces # List all namespaces
kubectl delete namespace nginx # Delete the namespaceStep 2: Creating a Pod
A 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: 80Commands
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 PodStep 3: Creating a Deployment
A 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: 80Commands
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 PodStep 4: Creating a Service
A 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 externallyCommands
kubectl apply -f service.yml # Create the Service
kubectl get service -n nginx # Get the external IP of the ServiceOnce 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
Post a Comment