How to deploy nginx web-server on the kubernetes cluster and access through a custom domain name. Complete setup on easy Steps.

How to deploy nginx web-server on the kubernetes cluster and access through a custom domain name. Complete setup on easy Steps.

How to setup a nginx web server on your Linux cluster nodes and access through nginx reverse proxy and domain name

I have deployed kubernetes cluster on AWS cloud ec2 instances T2.Medium with 20 GB each storage and 4 GB RAM + 2 Core CPU with Ubuntu OS. I use this link to deploy cluster on ubuntu OS : https://www.linuxtechi.com/install-kubernetes-on-ubuntu-22-04/

So my kubernetes cluster is ready to work. Lets start

Step-1 : (Deploy static web application on Kubernetes Cluster and expose through NodePort.)

On Master Node (Cluster Node)

$ kubectl get nodes -o wide
$ mkdir assignment1
$ mkdir -p /home/ubuntu/assignment1/static-data
$ cd /home/ubuntu/assignment1/static-data
$ wget https://www.free-css.com/assets/files/free-css-templates/download/page296/oxer.zip
$ unzip oxer.zip
$ ls oxer-html/
about.html  blog.html  class.html  css  images  index.html  js
$ vi web-app.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: hostpath-volume
          mountPath: /usr/share/nginx/html
      volumes:
      - name: hostpath-volume
        hostPath:
          path: /home/ubuntu/assignment/static-data # static image kept here
$ kubectl create -f webapp.yml
deployment.apps/nginx-deployment created

$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6d64cb557d-n8rm4   1/1     Running   0          23s
nginx-deployment-6d64cb557d-xp8xd   1/1     Running   0          23s

$ kubectl expose deployment nginx-deployment --name=nginx-deployment-svc --port=80 --target-port=80 --type=NodePort
service/nginx-deployment-svc exposed

$ kubectl get svc
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes             ClusterIP   10.96.0.1        <none>        443/TCP        6h37m
nginx-deployment-svc   NodePort    10.108.123.135   <none>        80:31046/TCP   4s

Now we will go to AWS deployed kubernetes clusters node and copy public address and paste it to public browser like google chrome

AWS Cloud setup instances Public IP

So open google chrome broswer:

Master_Node_Public_Ip:nginx-deployment-svc-port-number

http://3.220.236.18:31046/

http://3.220.236.18:31046/

Till now we deployed our web application on cluster deployment servers. Now we have to require setup reverse-proxy, so we can access our web-app without specifying portnumber like 31046. so lets setup nginx reverse proxy for this.

Create a new AWS instance as an nginx-reverse-proxy server for specifying reverse proxy setup


Step-2 (On nginx server (A new aws instance for reverse proxy))

$ sudo apt-get update 
$ sudo apt-get install nginx
$ sudo systemctl restart nginx
$ sudo systemctl enable nginx
$ sudo cd /etc/nginx/conf.d

$ sudo vi nginx.conf
server {
    listen 80; # will listen app ipv4 and ipv6 address on port number 80
    listen [::]:80;

    server_name 3.90.48.34; # Public IP of nginx-server

    location / {
        proxy_pass http://3.220.236.18:31046/;  # public ip of clusters master node ip and nodeport


$ sudo nginx -t   # check .conf file vaildation
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo nginx -s reload    # reload file
$ sudo systemctl restart nginx    # reload nginx server

Now we setup nginx server completly. So take nginx-server public ip from aws console and paste it on chrome broser:

http://3.90.48.34

And reverse proxy setup works properly


Step - 3 (How to access our cluster web-server through domain name insted of servers public IP.)

For Example:- website.com

For this we have to require to purchase a domain name, like godaddy, hostinger, google domain, namecheap, bigrock etc. there are lots of option out there but here i am choosing namecheap domain registrar service.

As you see i have purchased a domain name "rakamodify.online" from namecheap domain registrar. Now you have to setup DNS nameserver and access public ip to domain name. I go to "advance DNS option" > Host Record > Add new record.

Note: we have to update our nginx-proxy configuration file with some changes

$ sudo vi nginx.conf
server {
    listen 80; # will listen app ipv4 and ipv6 address on port number 80
    listen [::]:80;

    server_name www.rakamodify.online ; # Insted of 3.90.48.34 Public IP of nginx-server, we use domain name    
    location / {
        proxy_pass http://3.220.236.18:31046/;  # public ip of clusters master node ip and nodeport

$ sudo systemctl restart nginx

Web application access by domain name insted of public IP


Reference: