Ingress In Kubernetes
What is ingress?
Ingress is set of routing rules that you define inside .yml file. which route should point to service.
There are two types of routing
1. Host Based :
Host based routing such as -> api.example.com, example.com, cms.example.com
2. Path Based
Path based routing such as -> example.com, example.com/products, example.com/payments
So in this blog we will implement path based routing.
If you want to check how to deploy app on kubernetes then first visit below.
Step 1.
Start minikube
minikube start
Install or add ingress controller
minikube addons enable ingress
Step 2.
minikube addons list | grep ingress
If you have successfully enable ingress then you will see ingress in list
Step 3.
Create two deployments for two different microservices one is nodejs another one is flask
Deployment for Node API
kubectl create deploy node-api-deploy --image=shanikr/nodeapi
Deployment for Flask API
kubectl create deploy flask-api-deploy --image=shanikr/flaskapi:v1
Check list of deployments
kubectl get deploy
Step 4.
Create Services for these deployments
Service for Node API
kubectl expose deployment node-api-deploy --name=node-api-svc --type=NodePort --port=80 --target-port=3000
Service for Flask API
kubectl expose deployment flask-api-deploy --name=flask-api-svc --type=NodePort --port=80 --target-port=5000
Check list of services
kubectl get svc
Go to dashboard check list of pods, deployments, services till now.
minikube dashboard --url
Step 5.
Create ingress file that defines routes and associated services.
ingress-file.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: flask-node-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: myapp.com
http:
paths:
- path: /nodeapi
pathType: Prefix
backend:
service:
name: node-api-svc
port:
number: 80
- path: /flaskapi
pathType: Prefix
backend:
service:
name: flask-api-svc
port:
number: 80
Create ingress from this file
kubectl apply -f ingress-file.yaml
Check list of ingress
kubectl get ing
After few second you will see the address has been added
Now open hosts file
sudo vi /etc/hosts
#add this one
192.168.49.2 myapp.com
Save the file and close it
That’s it, it has been done.
Now open terminal make a curl request
curl myapp.com/nodeapi # this will fetch data from nodeapi service app.
curl myapp.com/flaskapi # this will fetch data from flask api service app.
That’s great you have done!