GKE Ingress Won't Correctly Rewrite Paths

4/15/2019

I've recently set up an nginx ingress in GKE, and somehow the ingress can't correctly rewrite the path for the mini-apps (laravel lumen) inside the target pods to resolve. The apps have a function called 'testing' which will print 'testing one' and 'testing two' for 'alpha' and 'beta'. the function should be accessible through http://<base_url>/public/testing.

There are two paths defined inside the ingress yaml, /alpha/* and /beta/* both of which will point to two services lumen-alpha-svc and lumen-beta-svc. I don't have any domain for this ingress so I left the 'host' in ingress undefined and the ingress should still be accessible by the external ip address provided by GKE for the ingress.

I tested the ingress by accessing http://<external-ip-address>/alpha/public/testing/ for both ingress paths and I got HTTP Error 404, and this thing still makes me confused. So, I set up a new service called path-check-svc and add new path /check/* in ingress which will point to the service. The service then points to a nodejs program that will print url path. I tested the path by accessing http://<external-ip-address>/check/something/ and the nodejs returned url path /check/something/. This makes me even more confused. Since the ingress path is defined with /check/* to redirect it to the path-check-svc, wouldn't that leave the path for the program to caught to only /something/? This made me realize that this is what caused the http error 404 in the lumen apps since it wasn't able to resolve http://<external-ip-address>/alpha/public/testing/ because lumen apps doesn't know what /alpha/ is.

So how should I defined the ingress so that the url path can be rewritten to the valid url path for the apps to catch. Thanks.

This is the yaml file for the kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lumen-alpha
  labels:
    app: lumen-alpha
spec:
  selector:
    matchLabels:
      app: lumen-alpha
  template:
    metadata:
      labels:
        app: lumen-alpha
    spec:
      containers:
        - image: husnurrsyafni/lumen-test-alpha:php
          name: lumen-test-alpha                 
          ports:
            - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lumen-beta
  labels:
    app: lumen-beta
spec:
  selector:
    matchLabels:
      app: lumen-beta
  template:
    metadata:
      labels:
        app: lumen-beta
    spec:
      containers:
        - image: husnurrsyafni/lumen-test-beta:php
          name: lumen-test-beta                 
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: lumen-alpha-svc
spec:
  type: NodePort
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  selector:
    app: lumen-alpha
---
apiVersion: v1
kind: Service
metadata:
  name: lumen-beta-svc
spec:
  type: NodePort
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  selector:
    app: lumen-beta
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: lumen-ingress
  annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - http:
      paths:
      - path: /alpha/*
        backend:
          serviceName: lumen-alpha-svc
          servicePort: 80
      - path: /beta/*
        backend:
          serviceName: lumen-beta-svc
          servicePort: 80
-- Husnur Ridha Syafni
google-kubernetes-engine
kubernetes
kubernetes-ingress
nginx-ingress
node.js

1 Answer

4/16/2019

If you want to do nginx.ingress.kubernetes.io/rewrite-target annotation you need to create your own nginx ingress controller and it's service.

Or use helm (preferably):

helm install stable/nginx-ingress

Ingress controller provided by GKE doesn't support rewrite-target

-- A_Suh
Source: StackOverflow