同一后端服务中多个路径的 K8s Ingress 规则
K8s Ingress rule for multiple paths in same backend service
我正在尝试设置入口负载平衡器。
基本上,我有一个具有多个路径的后端服务。
假设我的后端 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 - - [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 -
我查看了 this link 中提到的示例,但它假设不同的路径指的是不同的后端服务。在我的例子中,多个路径属于同一个后端服务。
看起来完整路径没有从入口转发到下游后端服务,导致请求无效。
有人可以建议为上述要求配置入口的正确方法是什么吗?
要将多路径与 glbc 入口一起使用,您需要具有不同的服务名称,例如下面的示例,并且每个服务(后端)都有不同的路径,并且可以配置一个入口(而不是两个)。
所以,你不需要两个入口,除非你想要两个负载均衡器
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
有多端口服务,Kubernetes 支持在一个服务对象上定义多个端口。使用多个端口时,您必须提供所有端口名称。见下面的例子
这是使用 kubernetes ingress 和 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
了解更多有关 ingress 的信息后回答我的问题。
不是向下游转发路径错误的问题。
基本上是 gke 入口控制器,期望在后端出现就绪探测器。
我在我的部署中遗漏了这个,因为它入口将后端标记为“未知”
最终阅读下面关于它的其他 Whosebug 问题帮助我解决了问题
gcp-load-balancer-backend-status-unknown
如下引入就绪探测器后,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
我正在尝试设置入口负载平衡器。 基本上,我有一个具有多个路径的后端服务。
假设我的后端 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 - - [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 -
我查看了 this link 中提到的示例,但它假设不同的路径指的是不同的后端服务。在我的例子中,多个路径属于同一个后端服务。
看起来完整路径没有从入口转发到下游后端服务,导致请求无效。 有人可以建议为上述要求配置入口的正确方法是什么吗?
要将多路径与 glbc 入口一起使用,您需要具有不同的服务名称,例如下面的示例,并且每个服务(后端)都有不同的路径,并且可以配置一个入口(而不是两个)。
所以,你不需要两个入口,除非你想要两个负载均衡器
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
有多端口服务,Kubernetes 支持在一个服务对象上定义多个端口。使用多个端口时,您必须提供所有端口名称。见下面的例子
这是使用 kubernetes ingress 和 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
了解更多有关 ingress 的信息后回答我的问题。
不是向下游转发路径错误的问题。 基本上是 gke 入口控制器,期望在后端出现就绪探测器。 我在我的部署中遗漏了这个,因为它入口将后端标记为“未知”
最终阅读下面关于它的其他 Whosebug 问题帮助我解决了问题
gcp-load-balancer-backend-status-unknown
如下引入就绪探测器后,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