Installing Grafana

16 Oct 2022 10:37 k3s kubernetes grafana

Using ArgoCD CLI:

% argocd app create grafana --repo https://git.k3s.differentpla.net/roger/grafana.git --path . --dest-server https://kubernetes.default.svc --dest-namespace grafana
application 'grafana' created

The rest of this is taken from https://grafana.com/docs/grafana/latest/setup-grafana/installation/kubernetes/, except that I’ll use an Ingress, rather than a LoadBalancer.

Deployment

Unchanged from the Grafana docs.

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:9.1.0
          imagePullPolicy: IfNotPresent
          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

PersistentVolumeClaim

Add storageClassName: longhorn.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-pvc
  namespace: grafana
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: longhorn

Service

Add a name to the port, for use in the Ingress.

apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: grafana
spec:
  ports:
    - port: 3000
      protocol: TCP
      name: http-grafana
      targetPort: http-grafana
  selector:
    app: grafana

Ingress

Basically the same as all of my other Ingress objects: It uses Traefik, HTTPS and Cert-Manager.

apiVersion: networking.k8s.io/v1
kind: Ingress

metadata:
  name: grafana
  namespace: grafana
  annotations:
    traefik.ingress.kubernetes.io/router.entrypoints: websecure
    traefik.ingress.kubernetes.io/router.tls: "true"
    cert-manager.io/cluster-issuer: k3s-ca-cluster-issuer

spec:
  tls:
  - hosts:
      - grafana.k3s.differentpla.net
    secretName: grafana-tls
  rules:
  - host: grafana.k3s.differentpla.net
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: grafana
            port:
              name: http-grafana

I also had to edit the ConfigMap for my custom CoreDNS. At some point, I’ll add a controller that scans for Ingress and Service annotations and does that automatically.

That’s pretty much it.