同一后端服务中多个路径的K8s入口规则 [英] K8s Ingress rule for multiple paths in same backend service

查看:601
本文介绍了同一后端服务中多个路径的K8s入口规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置入口负载均衡器. 基本上,我只有一个具有多个路径的后端服务.

比方说,我的后端NodePort服务名称是hello-app.与该服务关联的pod公开了多个路径,例如/foo和/bar.下面是示例

NodePort服务和相关的部署

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

现在像下面这样发出请求,我面临404错误.

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

我尝试了以下入口配置,但是在两种情况下都没有帮助.

入口配置1

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

入口配置2

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

错误消息

10.88.16.10--[2019年1月20日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-

我已经研究了链接中的示例提及,但假设不同的路径引用了不同的后端服务.就我而言,多个路径属于同一后端服务.

似乎完整路径没有从入口转发到下游后端服务,这导致了无效请求. 有人可以建议为上述要求配置入口的正确方法是什么吗?

解决方案

在学习了有关入口的更多信息后回答了我的问题.

这不是错误的路径转发到下游的问题. 基本上是gke入口控制器,期望后端有一个就绪探针. 我在部署中缺少此功能,因为它的进入将后端标记为未知"

最终阅读下面其他有关stackoverflow的问题有助于我解决问题

gcp-load-balancer-backend-status-unknown

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

在按如下所述准备就绪探针之后,Ingress能够正确检测后端并将请求转发给后端.

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     

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?

解决方案

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     

这篇关于同一后端服务中多个路径的K8s入口规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆