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
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-28-1024x74.png)
To validate the deployment.
kubectl get deployment
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-29-1024x86.png)
To validate the pods.
kubectl get pods
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-30-1024x127.png)
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
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-31-1024x430.png)
We can replace the port number and network type as per your need.
To deploy the nginx service.
kubectl create -f nginx-service.yaml
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-32-1024x81.png)
To validate the nginx service.
kubectl get svc
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-33-1024x112.png)
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.
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-34-1024x533.png)
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
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-35-1024x422.png)
To apply new change.
kubectl apply -f nginx-deployment.yaml
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-36-1024x102.png)
To validate the nginx running pods.
kubectl get pods
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-37-1024x200.png)
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
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-38-1024x65.png)
To destroy nginx deployment.
kubectl delete deployment nginx
![](https://www.geohosting.in/wp-content/uploads/2023/05/image-39-1024x79.png)
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.