Open In App

How To Deploy Nginx In Kubernetes ?

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Kubernetes is an open-source container orchestration tool used to deploy, scale, and manage containerized applications. On the other hand, NGINX is a web service used for proxying and load balancing. Here in this article, I have first discussed what is Kubernetes and its features. Then I have discussed what is NGINX. After this, I have walked you through the different steps to deploy NGINX on Kubernetes and connect it through the web browser

What is Kubernetes?

Kubernetes is a container orchestration tool that is used to deploy, scale, and manage containerized applications. Kubernetes follows a master-slave architecture. The master node manages all the cluster operations while the worker node deploys and runs the containers. The master node consists of 4 main components, API Server which helps in interacting and giving commands to the Kubernetes cluster, Scheduler schedules the pod on a particular node for running, etc. to stores the Kubernetes cluster data and control managers for managing the different controllers like deployment, replica sets and many more. On the other hand, in the worker node, there is Kubelet which helps in managing the pod lifecycle, container runtime which helps in running the containers and Kube proxy which manages all the networking rules in the pods.

Kubernetes provides different features such as :

  • Autoscaling : Kubernetes has both horizontal and vertical autoscaler . Horizontal pod autoscaler automatically scales up or scales down the number of pods on the amount of traffic the application receives. On the other hand vertical pod autoscaler automatically adjusts the CPU and memory usage of a pod to handle all the requests.
  • Autohealing: In Kubernetes, there are many pods running . If any of the pod nodes nodesnodes is deleted by mistake or it is unhealthy then Kubernetes automatically schedules a new pod for running on any of the nodes.
  • High Availability: Kubernetes has a controller called replica set controller pods pods nodes. This controller helps in maintaining the desired number of pods always running. Even if someone tries to delete one of pods, then also Kubernetes automatically runs another pod to maintain the desired number of pods running in the cluster.
  • Deployment Process: Kubernetes helps in rolling out new updates of a software application with zero downtime.

What is NGINX?

NGINX is an open-source web service tool used mainly for reverse proxying, caching, and load balancing. Using NGINX reduces the loading time of a website. NGINX improves the overall user experience. It can also handle concurrent requests. NGINX provides better performance and scalability. NGINX can be used in microservice, where it can be used as a reverse proxy to protect the backend servers from direct exposure to the outside world. This results in reducing any potential attack. NGINX consumes fewer resources compared to other server software and is compatible with a variety of web applications which makes it a better cost-effective solution. In summary, we can say NGINX is a powerful tool that is used by various organizations for faster loading of web applications and also to handle a large number of requests.

Pre-requisites

Before moving to the next section make sure that you have installed a Kubernetes tool to create a cluster. To install Kubernetes refer to How to install and configure Kubernetes on Ubuntu.

Steps To Deploy NGINX In Kubernetes

Step 1: Start the minikube. (If you are using any other Kubernetes tool to create a cluster, you can still follow these steps )

minikube start

start-minikube

Step 2 : Create a deployment YAML configuration file . Here mention the NGINX image and container port.

Deployment.yaml

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

deployment

Step 3 : Create a service YAML configuration file . This will expose the NGINX at port 80 .

Service.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80

service

Step 4 : Apply all the YAML configuration file using kubectl .

kubectl apply -f Deployment.yaml
kubectl apply -f Service.yaml

kubectl

Here if you have used docker to run minikube then you have to write this additional command to access the service from your browser .

minikube service nginx-service

minikube-service

Step 5 : Now to connect host IP with container port to see whether the NGINX service is deployed or not .

successful-deployment

Conclusion

Here in this article you have first learned what is Kubernetes and some of its key features . Then you have learned about what is NGINX . After this you have followed the steps to deploy a NGINX using Kubernetes. Here you have first created a deployment YAML configurational file and then created a service YAML configuration file which helps in exposing the NGINX service at particular port . Finally you have accessed the NGINX web page from the web browser to check whether your entire Kubernetes cluster setup for NGINX is working or not .

How To Deploy NGINX In Kubernetes ?-FAQ’s

Why Kubernetes is used ?

Kubernetes is used to orchestrate containerized applications . It helps in deploying , scaling and managing containerized applications .

What is the function of selector Kubernetes ?

Selector helps in connecting different Kubernetes resources . For example we use a selector to make a connection between service and deployment , so that we can access the application from the web browser .

Which command is used to apply the YAML files in Kubernetes ?

Command used to apply YAML files in Kubernetes is : kubectl apply -f <YAML-File Name>

What is purpose of using NGINX ?

NGINX is a web service used mainly for faster webpage loading , load balancing , reverse proxying and caching .

How to see all the Kubernetes resources ?

You can use kubectl get all command to see all the Kubernetes resources .



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads