K3s traefik ingress returns 网关超时

K3s traefik ingress returns gateway timeout

我目前正在玩一个基于 rpi 的 k3s 集群,我观察到一个奇怪的现象。

我部署了两个应用程序。 第一个是 nginx,我可以根据以下入口规则在 url http://external-ip/foo 上访问它:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: foo
  namespace: foo
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/rule-type: "PathPrefixStrip"
    traefik.ingress.kubernetes.io/rewrite-target: "/"
spec:
  rules:
  - http:
      paths:
      - path: /foo
        backend:
          serviceName: foo-service
          servicePort: 8081

另一个是 grafana,根据以下入口规则,我无法在 url http://external-ip/grafana 上访问它:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: grafana
  namespace: grafana
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/rule-type: "PathPrefixStrip"
    traefik.ingress.kubernetes.io/rewrite-target: "/"
spec:
  rules:
  - http:
      paths:
      - path: /grafana
        backend: 
          serviceName: grafana-service
          servicePort: 3000

当我直接在 pod 上执行端口转发时,我可以访问 grafana 应用程序,当我在 grafana 服务上使用端口转发时,它也有效。

但是,一旦我尝试通过子路径访问它,我就会遇到网关超时。

有没有人猜到我错过了什么?

这里是grafana部署的部署和服务:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: grafana
  labels:
    app: grafana
    tier: frontend
    service: monitoring
spec:
  selector:
    matchLabels:
      app: grafana
      tier: frontend
  template:
    metadata:
      labels:
        app: grafana
        tier: frontend
        service: monitoring
    spec:
      containers:
      - image: grafana
        imagePullPolicy: IfNotPresent
        name: grafana
        envFrom:
        - configMapRef:
            name: grafana-config
        ports:
        - name: frontend
          containerPort: 3000
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: grafana-service
  namespace: grafana
  labels:
    app: grafana
    tier: frontend
    service: monitoring
spec:
  selector:
    app: grafana
    tier: frontend
  type: NodePort
  ports:
  - name: frontend
    port: 3000
    protocol: TCP
    targetPort: 3000

解决方案

我必须将以下两个参数添加到我的 configmap 才能使其工作:

GF_SERVER_ROOT_URL=http://localhost:3000/grafana/   
GF_SERVER_FROM_SUB_PATH=true

正如我在评论中提到的那样,grafana 没有像默认 nginx 那样监听 /

关于这个有相关的 github issue,如果你想让它工作你应该指定 root_url

grafana.ini:
  server:
    root_url: https://subdomain.example.com/grafana

具体看this and this评论


@tehemaroo 添加他自己的解决方案,包括在 configmap

中更改根 url 和 sub_path

I had to add the following two parameters to my configmap to make it work:

GF_SERVER_ROOT_URL=http://localhost:3000/grafana/   
GF_SERVER_FROM_SUB_PATH=true

documentation有关

To serve Grafana behind a sub path:

Include the sub path at the end of the root_url.

Set serve_from_sub_path to true.

[server]
domain = example.com
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
serve_from_sub_path = true