DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Grafana and Prometheus Setup With Strimzi, a.k.a. Kafka on Kubernetes.
  • Mastering Daily Kubernetes Operations: A Guide To Useful kubectl Commands for Software Engineers
  • Secure the Cluster: A Blazing Kubernetes Developer’s Guide to Security
  • Kubernetes Today: The Growing Role of Serverless in Modern Kubernetes Clusters

Trending

  • Top Secrets Management Tools for 2024
  • The Power of Generative AI: How It Is Revolutionizing Business Process Automation
  • The Future of Kubernetes: Potential Improvements Through Generative AI
  • Deploying Heroku Apps To Staging and Production Environments With GitLab CI/CD
  1. DZone
  2. Coding
  3. Tools
  4. Centralized Logging for Kafka on Kubernetes With Grafana, Loki, and Promtail.

Centralized Logging for Kafka on Kubernetes With Grafana, Loki, and Promtail.

If there are multiple zookeeper and Kafka pods, a single window would be a boon for administrators. Grafana provides Loki and Promtail this functionality.

By 
Chandra Shekhar Pandey user avatar
Chandra Shekhar Pandey
·
Feb. 09, 21 · Tutorial
Like (2)
Save
Tweet
Share
10.2K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

In one of my another articles, I discussed how to set up strimzi (also known as Kafka on Kubernetes) on minikube. Also, we discussed how to set up Grafana and Prometheus to fetch metrics from Kafka and zookeeper instances. But wouldn't it have been more helpful and more administrator-friendly if Grafana could also be used to monitor logs of all the pods? If there are multiple zookeeper and Kafka pods, a single window would certainly be a boon for administrators and management.

Grafana provides Loki and Promtail a functionality to aggregate logs and view them from the same Grafana UI.

I have tested this in Fedora 33, minikube version: v1.15.1.

Prerequisites Needed

  • helm: Download Linux amd64 distribution. Also, set it in the system path. I have downloaded the latest one. At the time of writing this article, the latest version is Helm 3.5.2.
  • Grafana Helm charts
    • Loki: Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus.
    • Promtail: Promtail is an agent that ships the contents of local logs to a private Loki instance.

Step 1

Install Loki and Promtail via helm-charts. To do this, we have to add the helm repository with these charts. Finally, we will find the Loki and Promtail pods that are created. Also, a daemonset promtail is created.  We also see service/loki listening on port 3100.

Shell
 




x
24


 
1
$ kubectl create ns loki-promtail
2
$ kubectl config set-context --current --namespace=loki-promtail
3
$ helm repo add grafana https://grafana.github.io/helm-charts
4
$ helm repo update
5
$ helm install loki grafana/loki
6
$ helm install promtail grafana/promtail
7
$ kubectl get all
8
NAME                 READY   STATUS    RESTARTS   AGE
9
pod/loki-0           1/1     Running   2          108m
10
pod/promtail-ffw58   1/1     Running   1          79m
11
12
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
13
service/loki            ClusterIP   10.103.195.122   <none>        3100/TCP   108m
14
service/loki-headless   ClusterIP   None             <none>        3100/TCP   108m
15
16
NAME                      DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
17
daemonset.apps/promtail   1         1         1       1            1           <none>          107m
18
19
NAME                    READY   AGE
20
statefulset.apps/loki   1/1     108m
21
[chandrashekhar@localhost ~]$ 
22
23
24



Step 2

Check the logs of the Promtail pod and check that it is connected to the Loki pod with service/loki on port 3100.

Shell
 




x


 
1
$ kubectl logs -f promtail-ffw58
2

          
3
level=warn ts=2021-02-08T16:56:07.119631932Z caller=client.go:288 component=client host=loki-gateway msg="error sending batch, will retry" status=-1 error="Post \"http://loki-gateway/loki/api/v1/push\": dial tcp: lookup loki-gateway on 10.96.0.10:53: no such host"
4
level=warn ts=2021-02-08T16:56:11.368207816Z caller=client.go:288 component=client host=loki-gateway msg="error sending batch, will retry" status=-1 error="Post \"http://loki-gateway/loki/api/v1/push\": dial tcp: lookup loki-gateway on 10.96.0.10:53: server misbehaving"



Here we find that Promtail is trying to connect to loki-gateway. But it should connect to service Loki on port 3100. 

Step 3

To configure the correct url so that the Promtail agent connects to Loki correctly, we will edit the daemonset. We will have to add the argument -client.url=http://loki:3100/loki/api/v1/push. 

Shell
 




xxxxxxxxxx
1


 
1
$ kubectl edit daemonset promtail
2
# Check screen-shot "DaemonsetPromtail". Once we edit we will find that new promtail pod is created while previous one get terminated. 
3
$ kubectl get pods
4
NAME             READY   STATUS    RESTARTS   AGE
5
loki-0           1/1     Running   2          131m
6
promtail-mt2fn   1/1     Running   0          3m59s
7

          



Image: DaemonsetPromtail

DaemonsetPromtail

Step 4

Let us connect Grafana with Loki. I have downloaded Grafana from the Grafana Download Page. But before that, we have to port-forward the Loki port.

Shell
 




xxxxxxxxxx
1


 
1
[chandrashekhar@localhost ~]$ kubectl port-forward pod/loki-0 3100:3100
2
Forwarding from 127.0.0.1:3100 -> 3100
3
Forwarding from [::1]:3100 -> 3100
4
Handling connection for 3100
5
Handling connection for 3100
6

          



Step 5

Before we go to Grafana configurations, I want to highlight a few important concepts here:

  • If we check the details of the daemonset promtail, we will find that a path of nodes are mounted. An interesting one is /var/log/pods which has all pods logged of that node. Promtail keeps seeking these logs and sends these to the Loki instance.
Shell
 




x


 
1
  $ kubectl describe daemonset promtail
2
---
3
---
4
  Volumes:
5
   config:
6
    Type:        Secret (a volume populated by a Secret)
7
    SecretName:  promtail
8
    Optional:    false
9
   run:
10
    Type:          HostPath (bare host directory volume)
11
    Path:          /run/promtail
12
    HostPathType:  
13
   containers:
14
    Type:          HostPath (bare host directory volume)
15
    Path:          /var/lib/docker/containers
16
    HostPathType:  
17
   pods:
18
    Type:          HostPath (bare host directory volume)
19
    Path:          /var/log/pods
20
    HostPathType:  
21

          
22
$ minikube ssh -p grafana-loki-promtail
23
                         _             _            
24
            _         _ ( )           ( )           
25
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __  
26
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
27
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
28
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)
29

          
30
$ cd /var/log/pods
31
$ ls -ltr
32
total 68
33
drwxr-xr-x 3 root root 4096 Feb  8 14:25 kube-system_kube-controller-manager-grafana-loki-promtail_6cb144f7d82285562d6fc7ed0aeee754
34
drwxr-xr-x 3 root root 4096 Feb  8 14:25 kube-system_etcd-grafana-loki-promtail_88825ba870a02c365fe16e52439a90a4
35
drwxr-xr-x 3 root root 4096 Feb  8 14:25 kube-system_kube-scheduler-grafana-loki-promtail_38744c90661b22e9ae232b0452c54538
36
drwxr-xr-x 3 root root 4096 Feb  8 14:25 kube-system_kube-apiserver-grafana-loki-promtail_48f4e85267da14088611f2bc4ef06346
37
drwxr-xr-x 3 root root 4096 Feb  8 14:27 kube-system_storage-provisioner_06b572e5-fce0-488e-b59a-caabf7225ff6
38
drwxr-xr-x 3 root root 4096 Feb  8 14:27 kube-system_kube-proxy-b9j9l_c9f10bef-dc08-4761-847d-e147464f9a47
39
drwxr-xr-x 3 root root 4096 Feb  8 14:27 kube-system_coredns-f9fd979d6-h8zdd_049462d0-4b7e-44fe-9586-478ae2545f03
40
drwxr-xr-x 3 root root 4096 Feb  8 14:40 kafka_strimzi-cluster-operator-68c6747bc6-55wzp_d49274d8-0426-4051-a41a-1898b833f131
41
drwxr-xr-x 3 root root 4096 Feb  8 14:43 kafka_my-cluster-zookeeper-0_4832c0f4-497f-4cd9-bf98-8a854b76719b
42
drwxr-xr-x 3 root root 4096 Feb  8 14:43 kafka_my-cluster-zookeeper-1_9d9dd946-67a1-44e7-aefa-d396e898f4b4
43
drwxr-xr-x 3 root root 4096 Feb  8 14:43 kafka_my-cluster-zookeeper-2_283303f6-4e9d-4f9f-8256-db5a7894e315
44
drwxr-xr-x 3 root root 4096 Feb  8 14:45 kafka_my-cluster-kafka-2_392d9e35-33af-492b-a14e-63551af23d28
45
drwxr-xr-x 3 root root 4096 Feb  8 14:45 kafka_my-cluster-kafka-0_662e1456-28e9-4096-9a11-69fab28ccf4b
46
drwxr-xr-x 3 root root 4096 Feb  8 14:45 kafka_my-cluster-kafka-1_136a12f5-a5a1-452c-9888-8f779a767d99
47
drwxr-xr-x 5 root root 4096 Feb  8 14:46 kafka_my-cluster-entity-operator-5fd974964d-xhrct_fc9bbff5-a943-4a34-88a6-630f063252aa
48
drwxr-xr-x 3 root root 4096 Feb  8 17:38 loki-promtail_loki-0_6db579ab-9f17-4008-bf5b-b0bb1cc53d6f
49
drwxr-xr-x 3 root root 4096 Feb  8 17:40 loki-promtail_promtail-bzp78_1d608779-c719-4525-bb79-0e479b718f54
50
$ 
51

          



  • In a production environment, you might need Promtail pods only in a worker node, but not in infra nodes or a master node. This can be achieved with nodeAffinity:
Shell
 




xxxxxxxxxx
1


 
1
 spec:
2
      affinity:
3
        nodeAffinity:
4
          requiredDuringSchedulingIgnoredDuringExecution:
5
            nodeSelectorTerms:
6
              - matchExpressions:
7
                  - key: node-role.kubernetes.io/infra
8
                    operator: DoesNotExist



  • If we want a more customized Promtail agent setup, then we might need to modify the promtail.yaml configuration which is mounted as secret.  The secret's content is base64 encoded which can easily be decoded. Once decoded, we can get the configurations and customize them further. We can even use the following commands:
Shell
 




x
1
18


 
1
$ kubectl get daemonset promtail -o yaml
2

          
3
      containers:
4
      - args:
5
        - -config.file=/etc/promtail/promtail.yaml
6
        - -client.url=http://loki:3100/loki/api/v1/push
7

          
8
---
9
        volumeMounts:
10
        - mountPath: /etc/promtail
11
          name: config
12
---
13
      volumes:
14
      - name: config
15
        secret:
16
          defaultMode: 420
17
          secretName: promtail
18
          
19
$ kubectl get secret promtail -o yaml
20
apiVersion: v1
21
data:
22
  promtail.yaml: c2VydmVyOgogIGxvZ19sZXZlbDogaW5mbwogIGh0dHBfbGlzdGVuX3BvcnQ6IDMxMDEKCmNsaWVudDoKICB1cmw6IGh0dHA6Ly9sb2tpLWdhdGV3YXkvbG9raS9hcGkvdjEvcHVzaAoKcG9zaXRpb25zOgogIGZpbGVuYW1lOiAvcnVuL3Byb210YWlsL3Bvc2l0aW9ucy55YW1sCgpzY3JhcGVfY29uZmlnczoKICAjIFNlZSBhbHNvIGh0dHBzOi8vZ2l0aHViLmNvbS9ncmFmYW5hL2xva2kvYmxvYi9tYXN0ZXIvcHJvZHVjdGlvbi9rc29ubmV0L3Byb210YWlsL3
23
  ---



Step 6

Follow the screenshots to configure Grafana to connect to Loki instances and monitor logs. 

AddDatasource.png

Add Data Source


LokiAsLogDataSource

Loki As Log Data Source


Save and Test Connection

Save and Test Connection


Tail Pod Logs

Tail Pod Logs

That's it, guys. I hope you find this article interesting and informative.

Loki (C++) Grafana kafka Kubernetes pods

Opinions expressed by DZone contributors are their own.

Related

  • Grafana and Prometheus Setup With Strimzi, a.k.a. Kafka on Kubernetes.
  • Mastering Daily Kubernetes Operations: A Guide To Useful kubectl Commands for Software Engineers
  • Secure the Cluster: A Blazing Kubernetes Developer’s Guide to Security
  • Kubernetes Today: The Growing Role of Serverless in Modern Kubernetes Clusters

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: