Redirect Loop with nginx ingress & Kubernetes and url-subpath

  • What Grafana version and what operating system are you using?
    grafana/grafana Version 10.1.5

  • What are you trying to achieve?
    I would like to be able to access Grafana when I go to url/grafana.

  • How are you trying to achieve it?
    The first thing I did was follow these instructions: Deploy Grafana on Kubernetes
    Then I created an Ingress for Grafana. Finally, I adapted the environment variables for Grafana: Behind a proxy

  • What happened?
    I am always redirected from url/grafana to url/grafana.

  • What did you expect to happen?
    I actually expect to see the login page.

  • Can you copy/paste the configuration(s) that you are having problems with?

ingress.yaml

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: grafana-ingress
  namespace: grafana
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: nginx
  rules:
    - host: example.com
      http:
        paths:
          - path: /grafana(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: grafana
                port:
                  number: 3000
  tls:
    - hosts:
        - example.com
      secretName: secret-tls

grafana.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: grafana
spec:
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      securityContext:
        fsGroup: 472
        supplementalGroups:
          - 0
      containers:
        - name: grafana
          image: grafana/grafana:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: GF_SERVER_DOMAIN
              value: "example.com"
            - name: GF_SERVER_ROOT_URL
              value: "https://example.com/grafana"
            - name: GF_SERVER_SERVE_FROM_SUB_PATH
              value: "true"
          ports:
            - containerPort: 3000
              name: http-grafana
              protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /robots.txt
              port: 3000
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 30
            successThreshold: 1
            timeoutSeconds: 2
          livenessProbe:
            failureThreshold: 3
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 1
            tcpSocket:
              port: 3000
            timeoutSeconds: 1
          resources:
            requests:
              cpu: 250m
              memory: 750Mi
          volumeMounts:
            - mountPath: /var/lib/grafana
              name: grafana-pv
      volumes:
        - name: grafana-pv
          persistentVolumeClaim:
            claimName: grafana-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: grafana
spec:
  ports:
    - port: 3000
      protocol: TCP
      targetPort: http-grafana
  selector:
    app: grafana
  sessionAffinity: None
  type: ClusterIP
  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    The logger does not show any new entries when I go to the page.

  • Did you follow any online instructions? If so, what is the URL?
    See links attached above.

Many thanks for your help

The problem could be solved with some testing of the environment variables. Contrary to the instructions, GF_SERVER_SERVE_FROM_SUB_PATH must be set to false.
I did not have to adjust anything else.