Deploy Loki on Kubernestes Cluster with Persistent Volume

Introduction

Grafana Loki is a logging platform developed by Grafana Labs. It is designed to be a highly scalable, multi-tenant log aggregation system that is easy to use and operate. It is built on top of the popular open-source log aggregation tool, Prometheus, and is compatible with its data model and query language.

In this post, We will show you how to deploy Loki on Kubernetes cluster.

nano loki-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: YWZ0YWI=  # Base64-encoded "admin"
  password: QmFmN1N=  # Base64-encoded "1f2d1e2e67df"

You can encode the username and password online by using this URL – encode online https://www.base64encode.org/

apiVersion: apps/v1
kind: Deployment
metadata:
  name: loki
spec:
  replicas: 3
  selector:
    matchLabels:
      app: loki
  template:
    metadata:
      labels:
        app: loki
    spec:
      containers:
        - name: loki
          image: aftab70/loki:latest
          imagePullPolicy: "IfNotPresent"
          env:
          - name: My_USERNAME
            valueFrom:
              secretKeyRef:
                name: my-secret
                key: username
          - name: MY_PASSWORD
            valueFrom:
              secretKeyRef:
                name: my-secret
                key: password
          ports:
            - containerPort: 3100
          volumeMounts:
            - mountPath: /data
              name: loki
      volumes:
        - name: loki
          persistentVolumeClaim:
            claimName: loki-pv-claim

cat loki-storage.yaml 
kind: PersistentVolume
apiVersion: v1
metadata:
  name: loki-pv-volume
  labels:
    type: local
    app: loki
spec:
  storageClassName: manual
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/loki_data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: loki-pv-claim
  labels:
    app: loki
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
apiVersion: v1
kind: Service
metadata:
  name: loki-service
  labels:
    app: loki
spec:
  type: LoadBalancer #NodePort
  ports:
   - port: 3100
  selector:
   app: loki

Step 1: Deploy Storage

To create a storage configuration yaml file.

nano prometheus-storage.yaml

Paste the following storage configuration.

kind: PersistentVolume
apiVersion: v1
metadata:
  name: prometheus-pv-volume
  labels:
    type: local
    app: prometheus
spec:
  storageClassName: manual
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/prometheus_data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: prometheus-pv-claim
  labels:
    app: prometheus
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
aftab [ ~ ]$ kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
loki-6cd658f4b6-ddw6b   1/1     Running   0          96m
loki-6cd658f4b6-r9v6r   1/1     Running   0          53m
loki-6cd658f4b6-smdbj   1/1     Running   0          53m
aftab [ ~ ]$ kubectl logs loki-6cd658f4b6-ddw6b

We need to replace the k8s node mounting path and storage capicity and save the edit from nano editor.

To deploy the prometheus storage.

kubectl create -f prometheus-storage.yaml

To validate the volume storage.

kubectl get pvc

Step 2: Deploy Prometheus

To create prometheus-deployment yaml file.

nano prometheus-deployment.yaml

Paste the following deployment config like replica and docker container repo and ports.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus:latest
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 9090
          volumeMounts:
            - mountPath: /prometheus/data
              name: prometheus
      volumes:
        - name: prometheus
          persistentVolumeClaim:
            claimName: prometheus-pv-claim

To create Prometheus deployment.

kubectl create -f prometheus-deployment.yaml

To validate the deployment.

kubectl get deployment

To validate the pods.

kubectl get pods

Step 3: Deploy Prometheus Service

To make prometheus accessible from the network we need to create prometheus service yaml file.

nano prometheus-service.yaml

Paste the following configuration.

apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
  labels:
    app: prometheus
spec:
  type: NodePort
  ports:
   - port: 9090
  selector:
   app: prometheus

We need to replace the port number and network type as per your needs and save edit from nano editor.

To deploy service of Prometheus.

kubectl create -f prometheus-service.yaml

To validate the service.

kubectl get svc

Step 4: Accessing Loki

We need to use the external IP address / dns name with port 9090 to get Prometheus server on the browser.

Step 5: Clean Up

If we want to destroy the loki resources, We can do that by following given commands.

To destroy loki service.

kubectl delete svc loki-service

To destroy loki deployment.

kubectl delete deployment loki-deployment

Conclusion

We have successfully deployed the loki server on the K8S cluster. If you are still facing problems, feel free to leave a commit. 

Author

Deploy Loki on Kubernestes Cluster with Persistent Volume

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top