【问题标题】:Kubernetes Ingress not adding the application URL for grafana dashboardKubernetes Ingress 没有为 grafana 仪表板添加应用程序 URL
【发布时间】:2018-01-23 20:31:02
【问题描述】:

我已经在我的 Kubenernetes 1.9 集群中安装了 Grafan。当我使用我的入口 URL (http://sample.com/grafana/) 访问获取第一页时。在那个 javascript 之后,css 下载不添加 /grafana 到 URL。

这是我的入口规则:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: grafana-ingress-v1
  namespace: monitoring
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
spec:
  tls:
  - hosts:
    - sample.com
    secretName: ngerss-tls
  rules:
  - host: sample.com
    http:
      paths:
      - path: /grafana/
        backend:
          serviceName: grafana-grafana
          servicePort: 80

在这里我看到了关于同一主题的讨论。但它对我的问题没有帮助。

https://github.com/kubernetes/contrib/issues/860 下图显示第一个请求转到 /grafana/,但第二个请求未在 url 中添加 /grafana/

【问题讨论】:

    标签: kubernetes grafana


    【解决方案1】:

    您的入口规则是正确的,并且 nginx 创建了正确的虚拟主机以将流量转发到 grafana 的服务(我只留下了需要显示的字符串):

     server {
        server_name sample.com;
        listen 80;
        listen [::]:80;
        set $proxy_upstream_name "-";
    
        location ~* ^/grafana/(?<baseuri>.*) {
    
            set $proxy_upstream_name "default-grafana-grafana-80";
    
            set $namespace      "default";
            set $ingress_name   "grafana-ingress-v1";
    
    
        rewrite /grafana/(.*) /$1 break;
        rewrite /grafana/ / break;
        proxy_pass http://default-grafana-grafana-80;
    
        }
    

    是的,当您转到 sample.com/grafana/ 时,您会收到来自 grafana pod 的响应,但它会重定向到 sample.com/login 页面(从您提供的屏幕截图中可以看到):

    $ curl -v -L http://sample.com/grafana/
    *   Trying 192.168.99.100...
    * Connected to sample.com (192.168.99.100) port 80 (#0)
    > GET /grafana/ HTTP/1.1
    > Host: sample.com
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 302 Found
    < Server: nginx/1.13.5
    < Date: Tue, 30 Jan 2018 21:55:21 GMT
    < Content-Type: text/html; charset=utf-8
    < Content-Length: 29
    < Connection: keep-alive
    < Location: /login
    < Set-Cookie: grafana_sess=c07ab2399d82fef4; Path=/; HttpOnly
    < Set-Cookie: redirect_to=%252F; Path=/
    < 
    * Ignoring the response-body
    * Connection #0 to host sample.com left intact
    * Issue another request to this URL: 'http://sample.com/login'
    * Found bundle for host sample.com: 0x563ff9bf7f20 [can pipeline]
    * Re-using existing connection! (#0) with host sample.com
    * Connected to sample.com (192.168.99.100) port 80 (#0)
    > GET /login HTTP/1.1
    > Host: sample.com
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 404 Not Found
    < Server: nginx/1.13.5
    < Date: Tue, 30 Jan 2018 21:55:21 GMT
    < Content-Type: text/plain; charset=utf-8
    < Content-Length: 21
    < Connection: keep-alive
    < 
    * Connection #0 to host sample.com left intact
    default backend 404
    

    因为默认情况下 grafana 的 root_url 只是 /

    root_url = %(protocol)s://%(domain)s:%(http_port)s/
    

    当请求仅重定向到 sample.com 时,nginx 会将其转发到默认后端 404。

    解决方案:

    你需要将root_urlgrafana的服务器设置改为/grafana/

    root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana/
    

    您可以在 grafana 的 configmap 对象中更改此设置。

    【讨论】:

    • @nickoly 感谢您的回答。我会测试一下。
    • 支持所有域使用 --> root_url = %(protocol)s://0.0.0.0:%(http_port)s/grafana/
    【解决方案2】:

    为了使用前缀 /grafana(例如,http://k8s.example.com/grafana)为 Grafana 提供服务,请将以下内容添加到您的 helm values.yaml 中。

    ingress:
      enabled: true
      annotations:
        kubernetes.io/ingress.class: "nginx"
        nginx.ingress.kubernetes.io/rewrite-target: /$1
        nginx.ingress.kubernetes.io/use-regex: "true"
    
      path: /grafana/?(.*)
      hosts:
        - k8s.example.com
    
    grafana.ini:
      server:
        root_url: http://localhost:3000/grafana # this host can be localhost
    

    并升级helm grafana版本如下:

    helm -n namespace_name upgrade -f values.yaml relase_name stable/grafana
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 2021-12-25
      • 1970-01-01
      • 2019-04-28
      • 2022-07-08
      • 2019-09-26
      • 2019-12-10
      相关资源
      最近更新 更多