Problem

As you have more frontend, backend, websockets .. you will have to create separate load balancers to expose all of them over the internet. This can be expensive

image.png

How can you make sure we don’t have to create multiple load balancers ?

We can create a test pod inside the same cluster where our backend, frontend, web socket pods and services lies. We can send all incoming request to this pod which will eventually redirect it to concerned services. This can be done by starting an nginx with reverse proxy that can redirect the request

ops/reverse-proxy : Here an nginx pod will be started that will reverse proxy the request

manifest.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: | 
    events {
      worker_connections 1024;
    }

    http {
      server {
        listen: 80;
        server_name: ingress-backend.ritikaxg.co.in;

        location / {
          proxy_pass <http://backend-service.backend-team.svc.cluster.local:3000>;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
      }
    }

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    containers:
      - name: nginx
        image: nginx:latest
        ports:
          - containerPort: 80
        volumeMounts:
          - name: nginx-config
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
    volumes:
      - name: nginx-config
        configMap:
          name: nginx-config

---

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

What are ConfigMap ?

ConfigMap are bunch of non secret key value pairs. It allows you to store key value pairs inside cluster that can be used by various pods.

# Here : nginx.conf is the key are rest is the value since its multiline therefore 
		#		 we used pipeline `|`
		
nginx.conf: | 
    events {
      worker_connections 1024;
    }

    http {
      server {
        listen 80;
        server_name ingress-backend.ritikaxg.co.in;

        location / {
          proxy_pass <http://backend-service.backend-team.svc.cluster.local:3000>;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
      }
    }

How is reverse proxy file mounted to nginx pod ?

Since nginx-config configMap exists in the cluster. It is accessible by nginx-deployment via

volumes and volumeMounts. After mounting is complete nginx pod will now have reverse proxy file at /etc/nginx/nginx.conf

    # Here : we specify that `nginx-config` configMap has the Key-Value pair that
				  #  needs to be mounted inside `nginx` pod at `/etc/nginx/nginx.conf`
				  # `nginx.conf` is the Key
        volumeMounts:
          - name: nginx-config
            mountPath: /etc/nginx/nginx.conf
            subPath: nginx.conf
    # This specify the volume that `nginx` container uses 
    volumes:
      - name: nginx-config # Name of the volume
        configMap:
          name: nginx-config # Name of the configMap that contains Key-Value pair 
										         # that needs to be mounted

image.png

kubectl apply -f manifest.yml

image.png