Setup Cluster monitoring using Prometheus, Grafana and Loki
Cheulong Sear

Cheulong Sear @cheulong

About: Hi, I’m Cheulong Sear. Love learning new staff. I will show my journey in the IT field and what I'm building during my free time.

Location:
Bangkok, Thailand
Joined:
Jun 30, 2024

Setup Cluster monitoring using Prometheus, Grafana and Loki

Publish Date: Jul 6
1 0

  • Prometheus is An open-source monitoring system with a dimensional data model, flexible query language, efficient time series database and modern alerting approach.
  • Grafana is a multi-platform open source analytics and interactive visualization web application. It can produce charts, graphs, and alerts.
  • Loki is a log aggregation system designed to store and query logs.

Prerequisite

  • Helm
  • Kubernetes

Setup

Install kube-premetheus-stack

  1. Use helm to install kube-premetheus-stack
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

helm repo update
Enter fullscreen mode Exit fullscreen mode

then you can run helm search repo prometheus-community to see the charts

helmsearch

If you can't see kube-prometheus-stack you can search by type
helm search repo prometheus-community/kube-prometheus-stack

If you want to install different version, you can search the available versions
helm search repo prometheus-community/kube-prometheus-stack --versions

and install it using

helm install prometheus prometheus-community/kube-prometheus-stack --version 45.7.1 --namespace monitoring --create-namespace
Enter fullscreen mode Exit fullscreen mode

kube-prometheus-stack contains:

  • prometheus operator
  • node exporter
  • kube state metrics
  • grafana

1

Install Loki

Before installing Loki, you need to create a Persistent Volumes if you are planning to use local storage instead of cloud storage

Create PVC

# loki-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: storage-loki-0
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: ''
  claimRef:
    namespace: monitoring
    name: storage-loki-0
  hostPath:
    path: "/mnt/data/loki"
    type: DirectoryOrCreate
Enter fullscreen mode Exit fullscreen mode
# loki-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: storage-loki-0
  namespace: monitoring
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  volumeName: storage-loki-0
  storageClassName: ""  # Important: disables dynamic provisioning
Enter fullscreen mode Exit fullscreen mode

type kubectl apply -f loki-pv.yaml and
type kubectl apply -f loki-pvc.yaml

Now we can install loki

helm repo add grafana http://grafana.github.io/helm-charts

helm repo update

helm search repo grafana

# you will see helm chart name 'grafana/loki'

helm install loki grafana/loki --namespace monitoring --version 6.29.0 -f loki-values.yaml 

Enter fullscreen mode Exit fullscreen mode
# loki-values.yaml

# Deployment mode: SingleBinary (simple), SimpleScalable (medium), Distributed (large)
deploymentMode: SingleBinary

# Number of replicas
singleBinary:
  replicas: 1

# Component replicas
write:
  replicas: 0
read:
  replicas: 0
backend:
  replicas: 0

# Caching
chunksCache:
  enabled: false
resultsCache:
  enabled: false

# Helm test
test:
  enabled: false

# All loki settings combined under one key
loki:
  auth_enabled: false
  commonConfig:
    replication_factor: 1
  storage:
    type: filesystem

  useTestSchema: true

  persistence:
    enabled: true
    existingClaim: storage-loki-0
Enter fullscreen mode Exit fullscreen mode

Prometheus UI

To access Prometheus UI

kubectl port-forward -n monitoring svc/prometheus-operated 9090:9090
Enter fullscreen mode Exit fullscreen mode

go to http://localhost:9090

2

Grafana UI

To access Grafana UI

kubectl port-forward -n monitoring svc/prometheus-grafana 3000:80
Enter fullscreen mode Exit fullscreen mode

go to http://localhost:3000

username: admin
password: prom-operator or you can check
to make sure by typing kubectl get secret -n monitoring prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 --decode;

3

Add Loki data source

In Grafana UI go to Configuration > Data sources, click on add data source and search for loki

4

add http://loki:3100 to URL and click on Save & Test

5

To test sending datas to loki

# port-forwarding the gateway to your local machine

kubectl port-forward --namespace monitoring svc/loki-gateway 3100:80
Enter fullscreen mode Exit fullscreen mode

open a new tab

# send test datas

curl -H "Content-Type: application/json" -XPOST -s "http://127.0.0.1:3100/loki/api/v1/push"  \
--data-raw "{\"streams\": [{\"stream\": {\"job\": \"test\"}, \"values\": [[\"$(date +%s)000000000\", \"fizzbuzz\"]]}]}"

------
# verify that Loki did receive the data

curl "http://127.0.0.1:3100/loki/api/v1/query_range" --data-urlencode 'query={job="test"}'
Enter fullscreen mode Exit fullscreen mode

Go to Grafana UI

choose Loki

6

7

=== Done ===

Code for this article

(back to top)

Leave a comment if you have any questions.

===========
Please keep in touch
Portfolio
Linkedin
Github
Youtube

Comments 0 total

    Add comment