K8s Ingress rule for multiple paths in same backend service

馋奶兔 提交于 2019-12-06 06:29:53

问题


I am trying to setup ingress load balancer. Basically, I have a single backend service with multiple paths.

Let's say my backend NodePort service name is hello-app. The pod associated with this service exposes multiple paths like /foo and /bar. Below is the example

NodePort service and associated deployment

apiVersion: v1
kind: Service
metadata:
  name: hello-app
spec:
  selector:
    app: hello-app
  type: NodePort
  ports:
    - protocol: "TCP"
      port: 7799
      targetPort: 7799
---
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: hello-app
  labels:
    app: hello-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-app
  template:
    metadata:
      labels:
        app: hello-app
    spec:
      containers:
      - name: hello-app
        image: us.gcr.io/hello-app:latest

Now onn making request like below I am facing 404 error.

http://{ingress-address:port}/foo
http://{ingress-address:port}/bar

I have tried below ingress configurations alternatively, but in both cases it didn't helped.

Ingress configuration 1

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: basic-ingress
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: hello-app
          servicePort: 7799

Ingress configuration 2

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: basic-ingress
spec:
  backend:
    serviceName: hello-app
    servicePort: 7799

Error Message

10.88.16.10 - - [20/Jan/2019 08:50:55] "GET / HTTP/1.1" 404 - [2019-01-20 08:50:55] [INFO] [_internal] [_log] 10.88.16.10 - - [20/Jan/2019 08:50:55] "GET / HTTP/1.1" 404 -

I have looked into example mention in this link, but it assumes that different path refers to different backend service. In my case, multiple paths belong to the same backend service.

It looks like the full path is not being forwarded to downstream backend service from ingress which is resulting into the invalid request. Can somebody please suggest what is the correct way to configure ingress for the above requirement?


回答1:


Answering my question after learning more about ingress.

It wasn't an issue of wrong path forwarding to downstream. Basically gke ingress controller, expects a readyness probe to be present in backend. I was missing this in my deployment and becuase of it ingress was marking backend as "unknown"

Eventually reading other stackoverflow questions below on it helped me to solve the problem

gcp-load-balancer-backend-status-unknown

kubernetes-ingress-gce-keeps-returning-502-error

After introducing readyness probe as below, ingress was able to detect backend properly and passes on the request to backend.

apiVersion: apps/v1 
kind: Deployment
metadata:
  name: hello-app
  labels:
    app: hello-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-app
  template:
    metadata:
      labels:
        app: hello-app
    spec:
      containers:
      - name: hello-app
        image: us.gcr.io/hello-app:latest
        readinessProbe:
          httpGet:
            path: /healthz
            port: 7799
          periodSeconds: 1
          timeoutSeconds: 1
          successThreshold: 1
          failureThreshold: 10     



回答2:


To use multipath with the glbc ingress you need to have different services name such as the below example and each service (backend) has different path and one ingress can be configured (not two).

So , you don't need two ingress unless if you want to have two loadbalancer

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: fanout-ingress
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: web
          servicePort: 8080
      - path: /v2/*
        backend:
          serviceName: web2
          servicePort: 8080

There is Multi-Port Services, Kubernetes supports multiple port definitions on a Service object. When using multiple ports you must give all of your ports names. see below example

Here is answer using kubernetes ingress with nginx .

kind: Service
apiVersion: v1

    metadata:
      name: my-service
    spec:
      selector:
        app: MyApp
      ports:
      - name: http
        protocol: TCP
        port: 80
        targetPort: 9376
      - name: https
        protocol: TCP
        port: 443
        targetPort: 9377


来源:https://stackoverflow.com/questions/54274992/k8s-ingress-rule-for-multiple-paths-in-same-backend-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!