one ingress name having multiple rules

12/7/2020

I would like to use multiple rules under the one ingress name.

{
    "kind": "Ingress",
    "spec": {
        "rules": [
            {
                "host": "gke-service-xyz.com",
                "http": {
                    "paths": [
                        {
                            "path": "/",
                            "backend": {
                                "serviceName": "gke-service",
                                "servicePort": 8080
                            }
                        }
                    ]
                }
            },
            {
                "host": "gke-service1-xyz.com",
                "http": {
                    "paths": [
                        {
                            "path": "/",
                            "backend": {
                                "serviceName": "gke-service1",
                                "servicePort": 8081
                            }
                        }
                    ]
                }
            }
        ]
    },
    "apiVersion": "extensions/v1beta1",
    "metadata": {
        "annotations": {
            "kubernetes.io/ingress.class": "nginx",
            "nginx.ingress.kubernetes.io/ssl-redirect": "false"
        },
        "name": "javaservice1-ingress"
    },
    "namespace": "abc-namespace"
}

YAML CODE

kind: Ingress
spec:
  rules:
  - host: gke-service-xyz.com
    http:
      paths:
      - path: "/"
        backend:
          serviceName: gke-service
          servicePort: 8080
  - host: gke-service1-xyz.com
    http:
      paths:
      - path: "/"
        backend:
          serviceName: gke-service1
          servicePort: 8081
apiVersion: extensions/v1beta1
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: 'false'
  name: javaservice1-ingress
namespace: abc-namespace

Here the problem is the first ingress host is working fine but while fetching the second ingress host is not working and it is showing like, 503 Service Temporarily Unavailable. I want both the hosts in a working state. Is there any way to achieve the same?

  • ingress name: service-ingress.
  1. gke-service-xyz.com
  2. gke-service1-xyz.com

Above points 1 and 2 are the ingress endpoints both should work but here only 1 is in working. Both the above YAML codes are not working.

-- Hushen
kubernetes
kubernetes-ingress
python

1 Answer

12/7/2020

Try this -

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    kubernetes.io/ingress.class: nginx
  name: staging-ingress
spec:
  rules:
  - host: gke-service-xyz.com
    http:
      paths:
      - path: /(.*)
        backend:
          serviceName: gke-service
          servicePort: 8080
      - path: /api/(.*)
        backend: 
          serviceName: gke-service1
          servicePort: 8081

The question you are asking is something like that you want to have only one domain and use it to route to two services, hence you can use something like URL rewrite for this

-- Tushar Mahajan
Source: StackOverflow