Kubernetes Ingress path based routing not working as expected

3/15/2020

I installed NGINX Ingress in kubernetes cluster. When i am trying to access the micro service end via Ingress Controller its not working as expected

I have deployed two spring boot application

Ingress Rules

  • Path 1 -> /customer

  • Path 2 -> /prac

When i am trying to access one of the service ex. http://test.practice.com/prac/practice/getprac , it does not work

enter image description here

but when i try to access without Ingress path http://test.practice.com/practice/getprac, it works

enter image description here

I am not able to understand why with Ingress path its not working and same happens for other service

Micro service 1 (Port 9090)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: customer
  namespace: practice
  labels: 
    app: customer
spec:
  replicas: 5
  selector:
    matchLabels: 
      app: customer
  template: 
    metadata:
      labels: 
        app: customer
    spec: 
      imagePullSecrets:
      - name: testkuldeepsecret
      containers:
      - name: customer
        image: kuldeep99/customer:v1
        ports:
        - containerPort: 9090
          hostPort: 9090

---
apiVersion: v1
kind: Service
metadata:                     
  name: customer-service
  namespace: practice
  labels: 
spec:                         
  ports: 
  - port: 9090 
    targetPort: 9090
    protocol: TCP
    name: http
  selector:
    app: customer

Micro service 2 (port 8000)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prac
  namespace: practice
  labels: 
    app: prac
spec:
  replicas: 4
  selector:
    matchLabels: 
      app: prac
  template: 
    metadata:
      labels: 
        app: prac
    spec: 
      imagePullSecrets:
      - name: testkuldeepsecret
      containers:
      - name: prac
        image: kuldeep99/practice:v1
        ports:
        - containerPort: 8000
          hostPort: 8000

---
apiVersion: v1
kind: Service
metadata:                     
  name: prac-service
  namespace: practice
  labels: 
spec:                         
  ports: 
  - port: 8000 
    targetPort: 8000
    protocol: TCP
    name: http
  selector:
    app: prac

Service (customer-service and prac-service)

NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
customer-service   ClusterIP   10.97.203.19    <none>        9090/TCP   39m
ngtest             ClusterIP   10.98.74.149    <none>        80/TCP     21h
prac-service       ClusterIP   10.96.164.210   <none>        8000/TCP   15m
some-mysql         ClusterIP   None            <none>        3306/TCP   2d16h

Ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: practice-ingress
  namespace: practice
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec: 
  rules:
  - host: practice.example.com
    http: 
      paths:
      - backend:
          serviceName: customer-service
          servicePort: 9090
        path: /customer
      - backend:
          serviceName: prac-service
          servicePort: 8000
        path: /prac
-- Kuldeep
kubernetes
kubernetes-ingress
nginx-ingress
spring-boot

1 Answer

3/15/2020

You have installed this nginx ingress

nginx.ingress.kubernetes.io/rewrite-target: / annotation to work properly you need to install this nginx ingress.

Alternative way to solve this issue is to configure contextPath to /prac in the spring application

-- Arghya Sadhu
Source: StackOverflow