Micro services deployments using Nginx reverse proxy with NodePort and ClusterIp services on Kubernetes

Sanjeev Rohila
4 min readAug 29, 2020

In the above deployment

we are creating two ClusterIP micro services, sports and fitness with 5 replicas each deployment

Another NodePort micro service which has a nginx reverse proxy configured to serve the fitness and sports micro service with /fitness & /sports respectively

Github repository

https://github.com/sanjeevrohila/k8s/tree/master/nginx-ingress-application

Lets look at the sample micro service fitness and sports which does contains the required dockerfilles and sample flask app directory and could be found at below locations

Creating the fitness deployment and a cluster ip service

fitness micro service
ubuntu@ubuntu1604:~$ cat fitness-deployment-cluster-ip.yamlapiVersion: v1
kind: Service
metadata:
name: fitnesssvc
spec:
selector:
app: fitness-app
type: ClusterIP
ports:
- targetPort: 80
port: 8886
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: fitness-deployment
spec:
selector:
matchLabels:
app: fitness-app
replicas: 5
template:
metadata:
labels:
app: fitness-app
spec:
containers:
- name: wsgi-flask-fitness
image: docker.io/justsanjeev/wsgi-flask-fitness:latest
ports:
- name: http
containerPort: 80
ubuntu@ubuntu1604:~$ kubectl apply -f https://raw.githubusercontent.com/sanjeevrohila/k8s/master/nginx-ingress-application/fitness-deployment-cluster-ip.yamlubuntu@ubuntu1604:~$ kubectl get svc fitnesssvc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
fitnesssvc ClusterIP 10.104.13.165 <none> 8886/TCP 70d

Creating a sports deployment and a cluster ip service

sports micro service
ubuntu@ubuntu1604:~$ cat sports-deployment-cluster-ip.yamlapiVersion: v1
kind: Service
metadata:
name: sportssvc
spec:
selector:
app: sports-app
type: ClusterIP
ports:
- targetPort: 80
port: 8885
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sports-deployment
spec:
selector:
matchLabels:
app: sports-app
replicas: 5
template:
metadata:
labels:
app: sports-app
spec:
containers:
- name: wsgi-flask-sports
image: docker.io/justsanjeev/wsgi-flask-sports:latest
ports:
- name: http
containerPort: 80
ubuntu@ubuntu1604:~$ kubectl apply -f https://raw.githubusercontent.com/sanjeevrohila/k8s/master/nginx-ingress-application/sports-deployment-cluster-ip.yamlubuntu@ubuntu1604:~$ kubectl get svc sportssvc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
sportssvc ClusterIP 10.101.28.198 <none> 8885/TCP 70d

Now we have the below services


ubuntu@ubuntu1604
:~$ kubectl run test-fitnesssvc --image=busybox --command --restart=Never -it -- /bin/sh -c "wget -O- fitnesssvc:8886"
Connecting to fitnesssvc:8886 (10.104.13.165:8886)writing to stdout
<html>
<body>
<h2> Fitness Application from fitness-deployment-6994b88f8c-457l9/10.244.1.141</h2>
<body>
- 100% |********************************| 120 0:00:00 ETA
written to stdout
ubuntu@ubuntu1604:~$
ubuntu@ubuntu1604:~$ kubectl run test-sportsssvc --image=busybox --command --restart=Never -it -- /bin/sh -c "wget -O- sportssvc:8885"
Connecting to sportssvc:8885 (10.101.28.198:8885)writing to stdout
<html>
<body>
<h2> Sports Application from sports-deployment-74645f6bcb-qnrhq/10.244.1.134</h2>
<body>
- 100% |********************************| 118 0:00:00 ETA
written to stdout
ubuntu@ubuntu1604:~$

Both the cluster ip services are working as expected.

Now create a reverse proxy deployment from where we can reach both the microservices using the http://<nodeip>/<servicename>.

ubuntu@ubuntu1604:~$ cat reverse-proxy-node-port.yaml
kind: Service
apiVersion: v1
metadata:
name: rp-frontend-service
spec:
type: NodePort
selector:
app: rp-frontend
ports:
- nodePort: 32100
port: 8080
targetPort: 80

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: reverseproxy-dployment
spec:
selector:
matchLabels:
app: rp-frontend
replicas: 3
template:
metadata:
labels:
app: rp-frontend
spec:
containers:
- name: apps-frontend
image: docker.io/justsanjeev/rp-nginx:latest
ports:
- name: http
containerPort: 80
ubuntu@ubuntu1604:~$ kubectl apply -f https://raw.githubusercontent.com/sanjeevrohila/k8s/master/nginx-ingress-application/reverse-proxy-node-port.yamlubuntu@ubuntu1604:~$ kubectl get svc rp-frontend-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
rp-frontend-service NodePort 10.102.151.158 <none> 8080:32100/TCP 69d
ubuntu@ubuntu1604:~$

The reverse proxy service to access the application is created as NodePort service, and we can access the services using the ip and the port of the reverse proxy service with a /fitness or /sports for desired service by the IP of the any of the node in the K8s cluster with the port (on which the NodePort service is created)

Lets look how my k8s cluster services/deployments looks

--

--