Nginx as Reverse Proxy for Kubernetes Services
Feb 10, 2024
0
Nginx is a popular open-source web server and reverse proxy server. Nginx is used by many high-traffic websites and is known for its high performance and low resource usage. We can use Nginx as a reverse proxy for Kubernetes services.
Although Kubernetes provides us Ingress to expose services to the outside world, Where we can use Ingress Controller to route traffic to the appropriate services. There are many Ingress Controllers available like Nginx, Traefik, and many more but these two are the most popular ones.
For our this simple application and for learning purposes, we will use Nginx as standalone reverse proxy server.
Prerequisites
- Just clone this repository and switch to the
kubernetes-setup
branch.
you can read this blog for more details about the repository and how to run the application in the local environment.
Creating Nginx Docker Image#
We will create a Docker image for Nginx with a custom configuration file. We will use this image to deploy Nginx as a reverse proxy for our application.
First create a new directory called nginx
in the root of the project. Inside the nginx
directory, create a file called default.conf
with the following content:
In the above configuration file, we have defined two upstream servers: client
and backend
. The client
server is used to proxy requests to the frontend application, and the backend
server is used to proxy requests to the backend application. We have also defined a server block that listens on port 80 and proxies requests to the client
and backend
servers based on the URL path.
Next, create a file called Dockerfile
in the nginx
directory with the following content:
In the Dockerfile
, we have used the nginx
base image and copied the default.conf
file to the /etc/nginx/conf.d
directory in the image.
/etc/nginx/conf.d
is the directory where Nginx looks for configuration files.
Docker - The Complete Guide to Build and Deploy your Application
Docker is an open platform for developing, shipping, and running applications, enabling quick software delivery by separating applications from infrastructure.
Read Full PostConfiguring Deployment and Service#
We have already created a deployment and service for our application in the previous blog. We will use the same deployment and service for need to add a few more things.
Deployment#
As you can see, we have added a new deployment for Nginx. We have used the full-nginx
image for the Nginx deployment. I have also added the resources
section to limit the memory and CPU usage for the all deployments.
We are also using PVC
for the mongo deployment to store the data in the persistent volume. You can read more about PVC here.
Service#
For service We just have to add a new service for the Nginx deployment and since we are using the NGINX as a reverse proxy for the frontend and backend services, We don't need to expose them to the outside world. So we will use the ClusterIP
type instead of LoadBalancer
so that they are only accessible within the cluster.
ConfigMap#
We have already created a ConfigMap for the backend and frontend application but since we gonna build a new image for frontend which will be production-ready, we need to update the ConfigMap.
We just removed the config for frontend app since we are not using it anymore as environment variables for the frontend app will be passed at build time.
PVC#
We need to create a PVC for the mongo deployment to store the data in the persistent volume. PVC is used to request storage resources from the cluster. We can use PVC to request a specific amount of storage and access mode for the persistent volume.
With the above changes, we have updated the deployment, service, and configMap for our application. Now we can deploy the application to the Kubernetes cluster.
But before that we need to update the Dockerfile
for the frontend application to build the production-ready image.
Building the Docker Images#
We need to build the Docker images for the frontend, backend, and Nginx applications. We can use the following commands to build the images.
Since if your following the previous blog, you know that we are using minikube
to run the kubernetes cluster locally. So we need to use the minikube
docker environment to build the images.
You can read the previous blog to know more about the minikube
and how to use and configure it.
You need to add output: "standalone"
in the next.config.js
file to build
the image since in our image we are using the server.js
file to run the
application.
Then we can build the images using the following commands:
Deploying the Application#
We have built all the required Docker images and kubernetes configuration files. Now we can deploy the application to the Kubernetes cluster.
We can use the following commands to deploy the application, services, and PVC to the Kubernetes cluster.
Make sure you are in the kubernetes-setup
branch and in the root of the project.
The above command will apply all the configuration files in the current directory to the Kubernetes cluster. It will create the deployments, services, and PVC for the application.
Accessing the Application#
Once the application is deployed, we can access it using the minikube
IP address and the port number of the Nginx service.
The above command will open the application in the default web browser. Where you can see the application running.
Testing the Application#
As you will see that application is empty so for that you can take pull from the nginx-proxy
branch of the repository where I have added the all the required files and configurations to run the application in the kubernetes cluster, Which we have discussed in this blog.
You can rebuild the images and deploy the application to the kubernetes cluster using the same commands.
Next.Js RSC data revalidate issue#
Once you took pull and add the new user you will see that the new user is not showing up in the list. This is because of the Next.js need know which proxy is being used to fetch the data.
and since we are using the Nginx as a reverse proxy, and the app is running in the kubernetes cluster and we are using the minikube
to run the cluster locally we have to configure the next.config.js
file to tell the Next.js to use the correct proxy.
For that you need to add your current URL which is being used to access the application in the next.config.js
file.
You can add your URL in place of the current URL in the allowedOrigins
array.
Once you have added the URL, you can rebuild the frontend image and deploy the application to the kubernetes cluster using the same commands.
Conclusion#
Nginx is a good tool to use as a reverse proxy for Kubernetes services. It provides a simple and efficient way to route traffic to the appropriate services. We also learn how we build the production-ready image for the frontend application and how to configure the Next.js to use the correct proxy.
You can also take pull from the nginx-proxy
branch of the repository to see the complete code and configurations
I hope you find this blog helpful and if you have any questions or suggestions, feel free to leave a comment below. .
Comments (2)