GKE ingress multi path base routing

2.6k Views Asked by At

Need help to create multi path base routing in GKE ingress (Not nginx). I have a host pointed to the GKE ingress and it's working fine with single backend, Now I tried to updated another backend with different path (/app1) and service name But it's giving error "Cannot GET /app1".

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.global-static-ip-name: "ingress"
spec:
  rules:
  - host: "mydomain.com"
    http:
      paths:
      - path: /
        pathType: ImplementationSpecific
        backend:
          service:
            name: my-app
            port:
              number: 8080
      - path: /app1
        pathType: ImplementationSpecific
        backend:
          service:
            name: app1
            port:
              number: 8080

I got 404 error while accessing /app1

1

There are 1 best solutions below

1
On

There are three possibilities for this issue:

1. Check whether the container ports opened. You can check it using netstat:

 $ kubectl exec -ti <hostname> -c container -- bin/sh / 
  / # netstat -plnt

2. The issue might also be caused by the Firewall configuration. Make sure you have proper firewall settings.

3. Another reason might be misconfiguration between port, containerPort and targetPort. Verify whether you have set proper port, targetPort and servicePort.

GCP Ingress supports multiple paths. I have tested ingress multipath based routing using Setting up HTTPS(S) Load balancing with Ingress by using both Hello-world v1 and v2.

Here are the configuration files which I have deployed.

First Deployment Configuration file:

$ nano Deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: firstdeployment
  labels:
    app: api
spec:
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        -  name: container
           image: gcr.io/google-samples/hello-app:1.0
           ports:
           - containerPort: 8080
---
 
apiVersion: v1
kind: Service
metadata:
  name: first-service
   labels:
     app: api
spec:
  type: NodePort
  selector:
    app: api
  ports:
  - port: 5000
    targetPort: 8080

Second Deployment Configuration file:

$ nano second.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: second-deployment
  labels:
    app: api-2
spec:
  selector:
    matchLabels:
      app: api-2
  template:
    metadata:
      labels:
        app: api-2
    spec:
      containers:
        - name: container
          image: gcr.io/google-samples/hello-app:2.0
          ports:
          - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: second-service
  labels:
    app: api-2
spec:
  type: NodePort
  selector:
    app: api-2
    ports:
    - port: 6000
      targetPort: 8080

Ingress resource Configuration file:

Ingress is a Kubernetes resource that encapsulates a collection of rules and configuration for routing external HTTP(S) traffic to internal services.

$ nano Ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-ingress
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: first-service
            port:
              number: 5000
      - path: /app1
        pathType: Prefix
        backend:
          Service:
            name: second-service
            port:
              number: 6000

Now, find out the external IP address of the load balancer serving your application by running:

$ kubectl get ingress <ingress-name>

enter image description here

NOTE : You can specify a default backend by providing a defaultBackend field in your Ingress manifest. Any requests that don't match the paths in the rules field are sent to the Service and port specified in the defaultBackend field. For example, in the above Ingress, any requests that don't match / or /app1 are sent to a Service named first-service on port 5001.