Published on

Setup Grafana On Kubernetes

Kube-state-metrics

Grafana is an open-source lightweight dashboard tool. It can be integrated with many data sources like Prometheus, AWS cloud watch, Stackdriver, etc. running Grafana on Kubernetes.

In our previous posts, we have looked at the following.

  1. Setup Prometheus on Kubernetes
  2. Setup Kube State Metrics
  3. Setup alert manager on kubernetes

Grafana Kubernetes Manifests

All kubernetes manifest (yaml files) used in this tutorial are hosted on Github

    git clone git@github.com:magarGanga/kubernetes-monitor.git

Deploy Grafana On Kubernetes

Step 1: Create a file named grafana-datasource-config.yaml and add this
    apiVersion: v1
    kind: ConfigMap
    metadata:
        name: grafana-datasources
        namespace: monitoring
    data:
      prometheus.yaml: |-
        {
            "apiVersion": 1,
            "datasources": [
                {
                "access":"proxy",
                    "editable": true,
                    "name": "prometheus",
                    "orgId": 1,
                    "type": "prometheus",
                    "url": "http://prometheus-service.monitoring.svc:8080",
                    "version": 1
                }
            ]
        }
Step 2: Create the configmap using the following command.
    kubectl create -f grafana-datasource-config.yaml
Step 3: Create a file named deployment.yaml wit following contents
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      name: grafana
      labels:
        app: grafana
    spec:
      securityContext:
        fsGroup: 472
        supplementalGroups:
          - 0
      containers:
      - name: grafana
        image: grafana/grafana:latest
        ports:
        - name: grafana
          containerPort: 3000
        resources:
          limits:
            memory: "1Gi"
            cpu: "400m"
          requests:
            memory: 500M
            cpu: "250m"
        volumeMounts:
          - mountPath: /var/lib/grafana
            name: grafana-storage
          - mountPath: /etc/grafana/provisioning/datasources
            name: grafana-datasources
            readOnly: false
      volumes:
        - name: grafana-storage
          persistentVolumeClaim:
            claimName: grafana-pvc
        - name: grafana-datasources
          configMap:
              defaultMode: 420
              name: grafana-datasources

Note: If Grafana deployment does not use a persistent volume and restarted the pod, all changes will be gone. Use a persistent volume if you are deploying Grafana for your project requirements. It will persist all the configs and data that Grafana uses.

Here, I have use persistentVolumeClaim also. You can check how PVC are created in Github

Step 4: Create the deployment
    kubectl create -f deployment.yaml
Step 5: Create a service file named service.yaml with following contents
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: monitoring
  annotations:
      prometheus.io/scrape: 'true'
      prometheus.io/port:   '3000'
spec:
  selector:
    app: grafana
  type: NodePort
  ports:
    - port: 3000
      targetPort: 3000
      nodePort: 32000

Step 6: Create the service.

    kubectl create -f service.yaml

Now you should be able to access the Grafana dashboard using any node IP on port 32000. Make sure the port is allowed in the firewall to be accessed from your workstation.

You can also access the dashboard using Loadbalancer after replacing NodePort with Loadbalancer in Service.yml file

Use the following default username and password to log in. Once you log in with default credentials, it will prompt you to change the default password.

    User: admin
    Pass: admin