Deploying an Nginx Web Application on Kubernetes Using NodePort Service
In this blog post, we’ll walk you through the process of deploying a Nginx web application on a Kubernetes cluster and exposing it using a NodePort service. We will also configure Nginx to serve your web application on a custom domain, “abc.syedusmanahmad.com”.
Prerequisites
Before we begin, ensure you have the following:
- A running Kubernetes cluster. I am using “minikube” cluster for this article.
kubectl
configured to interact with your cluster.- Access to your domain’s DNS settings.
Step 1: Create the Nginx Deployment
First, we need to create a deployment for the Nginx web server. A deployment ensures that the specified number of pod replicas are running at all times.
Here’s the deployment configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 6
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Save this YAML configuration to a file named nginx-deployment.yaml
.
To apply the deployment, run:
kubectl apply -f nginx-deployment.yaml
Step 2: Create the NodePort Service
Next, we create a NodePort service to expose the Nginx application outside the Kubernetes cluster. This service will listen on a specific port on each node and forward traffic to the Nginx pods.
Here’s the service configuration:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30007
type: NodePort
kubectl apply -f nginx-service.yaml
Step 3: Configure Nginx for Your Domain
To access your application via the custom domain “abc.syedusmanahmad.com”, you need to configure Nginx as a reverse proxy. This configuration will forward traffic from the domain to the NodePort service.
Here’s the Nginx configuration:
server {
listen 80;
server_name abc.syedusmanahmad.com;
location / {
proxy_pass http://192.168.49.2:30007;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Replace 192.168.49.2
with the IP address of one of your Kubernetes nodes. You can find the IP addresses of your nodes by running:
kubectl get nodes -o wide
or using below command
minikube ip
Step 4: Update DNS Settings
Update your domain’s DNS settings to point “abc.syedusmanahmad.com” to the IP address of your Kubernetes node. This step is usually done through your domain registrar’s DNS management interface. In our case its AWS Route53, so I have added the record set “abc.syedusmanahmad.com” and redirect it to the server public IP address as A record.
Step 5: Access your web page
Conclusion
In this tutorial, we deployed a Nginx web application on a Kubernetes cluster, exposed it using a NodePort service, and configured Nginx to serve the application on a custom domain. This setup ensures that your application is accessible externally via a user-friendly domain name.
I hope you enjoyed reading this article, please feel free to contact me Syedusmanahmad if you have any questions.
Please feel free to write @ engr.syedusmanahmad@gmail.com | Linkedin https://www.linkedin.com/in/engrusman-ahmad for any queries on AWS/DevOps & stay tuned for the next write-up.
If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇