Rewrite context path for Google Kubernetes Engine Ingress Controller

3/9/2020

I have an application running in the tomcat path /app1, how should I access this from the ingress path?

When accessing "/", it gives the default tomcat 404 - not found page, and when accessing via /app1 it shows "default backend -404"

What I wanna know is: Is there anyway to configure the context path without using ngnix's ingress controller? (Just using GKE's default ingress controller)

Here is a sample of my ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: gke-my-ingress-1
  annotations:
    kubernetes.io/ingress.global-static-ip-name: gke-my-static-ip
    networking.gke.io/managed-certificates: gke-my-certificate
spec:
  rules:
  - host: mydomain.web.com
    http:
      paths:
      - path: /
        backend:
          serviceName: my-service
          servicePort: my-port

Edit: service output

kubectl get svc
my-service   NodePort    <IP_REDACTED>   <none>        8080:30310/TCP   5d16h
kubectl describe svc my-service
Name:                     my-service
Namespace:                default
Labels:                   <none>
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"my-service","namespace":"default"},"spec":{"ports":[{"name"...
Selector:                 app=my-deployment-1
Type:                     NodePort
IP:                       <IP_REDACTED>
Port:                     my-port  8080/TCP
TargetPort:               8080/TCP
NodePort:                 my-port  30310/TCP
Endpoints:                <IP_REDACTED>:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

and this is my Node Port service yaml:

---
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  ports:
  - name: my-port
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: my-deployment-1
  type: NodePort
-- user10518
google-kubernetes-engine
kubernetes
kubernetes-ingress
nginx-ingress

1 Answer

3/11/2020

Unfortunately current implementation of default GKE Ingress Controller doesn't support rewrite targets. There is still an open github issue that you can find here.

What you're trying to achieve is rewriting your ingress path to some specific path exposed by your application, in your case Apache Tomcat web server.

Isn't there any possibility to reconfigure your app to be served from the main path by Apache Tomcat ? If so, you can make it available on <IngressLoadBalancerIP>/app1 by configuring the following path in your ingress resource like in the example below:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: default-backend
          servicePort: 8080
      - path: /app1
        backend:
          serviceName: my-service
          servicePort: 8080

But unfortunately you cannot configure rewrite in the way that when you go to <IngressLoadBalancerIP>/app1 it rewrites to your <my-service>/app1.

It seems that for now the only solution is to install different ingress controller such as mentioned nginx insgress controller.

-- mario
Source: StackOverflow