Introduction
Nginx is an open-source and free web server and reverse proxy server. It is designed to handle high traffic websites efficiently and reliably. Nginx is known for its low memory usage and ability to handle large numbers of concurrent connections with minimal resources.
Kubernetes is a free and open source container management tool, we can deploy and manage containerized applications across a cluster of nodes. It provides features such as automated rollouts and rollbacks, self-healing, load balancing, and scaling, all of which make it easier to manage and scale containerized applications.
In this post, We will show you how to deploy nginx web server on Kubernetes cluster.
Step 1: Creating Nginx Deployment YAML
We need to create the nginx web server deployment yaml config file and where we can update the replica count and image version and other things.
nano nginx-deployment.yaml
Paste the following configuration.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 80
Step 2: Deploy Nginx Web Server
We are good to deploy the nginx web server on k8s resources by following the given commands.
To deploy the nginx.
kubectl create -f nginx-deployment.yaml
To validate the deployment.
kubectl get deployment
To validate the pods.
kubectl get pods
Step 3: Deploy Nginx Service
We need to create the nginx service yaml config to make accessible nginx web server from network, Use the following command for the same.
nano nginx-service.yaml
Paste the following configuration.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: nginx
We can replace the port number and network type as per your need.
To deploy the nginx service.
kubectl create -f nginx-service.yaml
To validate the nginx service.
kubectl get svc
Step 4: Accessing Nginx Web Server
We need to use the external IP address / dns name with port 80 to get nginx web server on the browser.
Step 5: Scaling Nginx Web Server
We need to edit and put the number of count of nginx replicas in the nginx-depoyment.yaml
file.
To edit the nginx-deployment.yaml
file.
nano nginx-deployment.yaml
Update the replica count like 5 online number 6.
replicas: 5
To apply new change.
kubectl apply -f nginx-deployment.yaml
To validate the nginx running pods.
kubectl get pods
We can control the apache replicas count by using replicas
count in nginx-deployment.yaml
file.
Step 6: Clean Up
If we want to destroy the nginx resources, We can do that by following given commands.
To destroy nginx service.
kubectl delete svc nginx-service
To destroy nginx deployment.
kubectl delete deployment nginx
Conclusion
We have successfully deployed the nginx web server on the K8S cluster. If you are still facing problems, feel free to leave a commit.