Minikube Nginx Ingress 找不到服务端点

Minikube Nginx Ingress not finding service endpoint

我在让 Nginx 入口控制器在我的 Minikube 集群中工作时遇到了一些问题。很可能是Ingress配置有问题,我挑不出。

首先,我部署了一个服务,它在没有入口的情况下运行良好。

kind: Service
apiVersion: v1
metadata:
  name: online
  labels:
    app: online
spec:
  selector:
    app: online
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 5001
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: online
  labels:
    app: online
spec:
  replicas: 1
  selector:
    matchLabels:
      app: online
  template:
    metadata:
      labels:
        app: online
      annotations:
        dapr.io/enabled: "true"
        dapr.io/app-id: "online"
        dapr.io/app-port: "5001"
        dapr.io/log-level: "debug"
        dapr.io/sidecar-liveness-probe-threshold: "300"
        dapr.io/sidecar-readiness-probe-threshold: "300"
    spec:
      containers:
      - name: online
        image: online:latest
        ports:
        - containerPort: 5001
        env:
        - name: ADDRESS
          value: ":5001"
        - name: DAPR_HTTP_PORT
          value: "8080"
        imagePullPolicy: Never

然后检查其url

minikube service online --url
http://192.168.49.2:32323

请求看起来没问题。

curl http://192.168.49.2:32323/userOnline
OK

之后尝试使用minikube提供的nginx ingress。 我参考this装了ingress和运行一个例子就ok了

最后,我配置了 Ingress。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: online-ingress
  annotations:
spec:
  rules:
    - host: online
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: online
                port:
                  number: 8080

并通过添加行

更改 /etc/hosts
192.168.49.2    online

并测试:

curl online/userOnline
502 Bad Gateway

日志是这样的:

192.168.49.1 - - [26/Aug/2021:09:45:56 +0000] "GET /userOnline HTTP/1.1" 502 150 "-" "curl/7.68.0" 80 0.002 [default-online-8080] [] 172.17.0.5:5001, 172.17.0.5:5001, 172.17.0.5:5001 0, 0, 0 0.004, 0.000, 0.000 502, 502, 502 578ea1b1471ac973a2ac45ec4c35d927
2021/08/26 09:45:56 [error] 2514#2514: *426717 upstream prematurely closed connection while reading response header from upstream, client: 192.168.49.1, server: online, request: "GET /userOnline HTTP/1.1", upstream: "http://172.17.0.5:5001/userOnline", host: "online"
2021/08/26 09:45:56 [error] 2514#2514: *426717 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.49.1, server: online, request: "GET /userOnline HTTP/1.1", upstream: "http://172.17.0.5:5001/userOnline", host: "online"
2021/08/26 09:45:56 [error] 2514#2514: *426717 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.49.1, server: online, request: "GET /userOnline HTTP/1.1", upstream: "http://172.17.0.5:5001/userOnline", host: "online"
W0826 09:45:56.918446       7 controller.go:977] Service "default/online" does not have any active Endpoint.
I0826 09:46:21.345177       7 status.go:281] "updating Ingress status" namespace="default" ingress="online-ingress" currentValue=[] newValue=[{IP:192.168.49.2 Hostname: Ports:[]}]
I0826 09:46:21.349078       7 event.go:282] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"online-ingress", UID:"b69e2976-09e9-4cfc-a8e8-7acb51799d6d", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"23100", FieldPath:""}): type: 'Normal' reason: 'Sync' Scheduled for sync

我发现错误主要是关于 Ingress 的注释。如果我把它改成:

  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /

错误将是:

404 page not found

和日志:

I0826 09:59:21.342251       7 status.go:281] "updating Ingress status" namespace="default" ingress="online-ingress" currentValue=[] newValue=[{IP:192.168.49.2 Hostname: Ports:[]}]
I0826 09:59:21.347860       7 event.go:282] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"online-ingress", UID:"8ba6fe97-315d-4f00-82a6-17132095fab4", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"23760", FieldPath:""}): type: 'Normal' reason: 'Sync' Scheduled for sync
192.168.49.1 - - [26/Aug/2021:09:59:32 +0000] "GET /userOnline HTTP/1.1" 404 19 "-" "curl/7.68.0" 80 0.002 [default-online-8080] [] 172.17.0.5:5001 19 0.000 404 856ddd3224bbe2bde9d7144b857168e0

其他信息。

NAME     TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
online   LoadBalancer   10.111.34.87   <pending>     8080:32323/TCP   6h54m

上面我举的例子是NodePort服务,而我的例子是LoadBalancer,这是最大的区别。但是我不知道为什么它对我不起作用。

将其从评论中移出以便可见。


入口

主要问题是入口规则中的 path,因为应用程序在 online/userOnline 上提供流量。如果请求转到 online 然后进入 returns 404.

在这种情况下也不需要重写注释。

ingress.yaml 应如下所示:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: online-ingress
#  annotations:
spec:
  rules:
    - host: online
      http:
        paths:
          - path: /userOnline
            pathType: Prefix
            backend:
              service:
                name: online
                port:
                  number: 8080

有关 ingress

的更多详细信息

Minikube 上的负载均衡器

由于 minikube 被视为 bare metal 安装,要为 service/ingress 获得 external IP,必须使用专门设计的 metallb 解决方案。

MetalLB 是裸机 Kubernetes 集群的负载均衡器实现,使用标准路由协议。

它作为 minikube 的附加组件提供,可以通过以下方式启用:

minikube addons enable metallb

它需要使用设置创建一个 configMap。请参考metallb configuration