Loki installation in Ubuntu

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

In this post, we will see how to install Loki and Promtail in Ubuntu to visualize the logs in Grafana. But before that, we will see what is Loki and why we need it.
Source: Nidhinkumar

A quick look at what we are going to do
What is Loki
Why do we need Loki
Loki Stack
After that, let’s get our hand’s dirty by installing

Let’s start the installation
4. Loki in Ubuntu
5. Promtail in Ubuntu
And finally, we start to
6. Visualize the logs in Grafana

Visualize logs in Grafana

#1-Didyouknow
Loki is a horizontally scalable, highly available, multi-tenant log aggregation system. It is designed to be very cost-effective and easy to operate.

Horizontally-scalable
It doesn’t index the contents of the logs but rather a set of labels for each log stream. Log data itself can be compressed in chunks in object stores such as S3 or GCS, or even in the local file system. A small index and highly compressed chunks can simplify the operation as well as lower the cost.

#2-Didyouknow
Comparing to other log aggregation systems Loki
Doesn’t do full-text indexing on logs they would index only on metadata which makes it easier to operate and cheaper to run
It is a good fit for storing Kubernetes Pod logs. Metadata such as pod labels is automatically scrapped and indexed
It has native support with Grafana

#3-Didyouknow
Loki based logging stack consists of 3 components

Loki Stack
Promtail — Agent responsible for gathering logs, and sending them to Loki
Loki — Main server, responsible for storing logs and processing queries
Grafana — Querying and displaying logs
Now we have seen the basics of Loki, let’s start with the installation now

Installation on Loki
Navigate to the release page and then scroll down to the assets section and select the latest version and download the .zip file

Warning
Once the zip file is downloaded create a new directory named of your own choice :) and then unzip the Loki binary to the newly-created directory.
Now we will download the config file for Loki from the below link. Open the terminal in the directory where you have unzipped Loki and then copy the below command to download the config file
wget https://raw.githubusercontent.com/grafana/loki/v2.2.1/cmd/loki/loki-local-config.yaml
Don’t worry just copy the below steps to install Loki (#Ninja Mode :)
$ curl -O -L "https://github.com/grafana/loki/releases/download/v2.2.1/loki-linux-amd64.zip"
# extract the binary
$ unzip "loki-linux-amd64.zip"
# make sure it is executable
$ chmod a+x "loki-linux-amd64"
# download config file
$ wget https://raw.githubusercontent.com/grafana/loki/v2.2.1/cmd/loki/loki-local-config.yaml

#4-Didyouknow
once done we can proceed to the Promtail installation.
Navigate to the release page and then scroll down to the assets section and select the latest version of Promtail and download the .zip file.
Once the zip file is downloaded extract the Promtail binary to the directory which you have created during Loki installation.
Now we will download the config file for Promtail from the below link. Open the terminal in the directory where you have unzipped Loki and then copy the below command to download the config file
wget https://raw.githubusercontent.com/grafana/loki/v2.2.1/cmd/promtail/promtail-local-config.yaml
Don’t worry just copy the below steps to install Promtail (#Ninja Mode :)
$ curl -O -L "https://github.com/grafana/loki/releases/download/v2.2.1/promtail-linux-amd64.zip"
# extract the binary
$ unzip "promtail-linux-amd64.zip"
# make sure it is executable
$ chmod a+x "promtail-linux-amd64"
# download config file
$ wget https://raw.githubusercontent.com/grafana/loki/v2.2.1/cmd/promtail/promtail-local-config.yaml

#5-Didyouknow
We have completed the installation of Loki as well as Promtail and started them in the local machine. Let’s make some changes in the Grafana to view the logs.
Open Grafana (if it doesn’t open check whether the Grafana-server is running or not)
If the Grafana server is not running run using the below command(Linux based system)
sudo service grafana-server start
Once Grafana is running click Settings->Datasource and then click Add Datasource and then select Loki

Grafana-Datasource
Once the data source is added click Loki again

Grafana-Loki Configuration
Now add the URL as your localhost with port number 3100

Loki Configuration
Now the Loki setup in Grafana is completed.

#6-Didyouknow
Let us explore some logs using Loki in Grafana. Now click Explore which will open the explore tab and then select Loki in the drop-down which will open the Loki Panel where we can write the LogQL queries.
Add the below query in Loki query input box to view the logs
{filename="/var/log/syslog"} |= "msg" |= "Alert Rule returned no data"
Once the query is added click run which will show an output like below

LogQL-Query#1
You can view the Query history by clicking the Query history button

LogQL-QueryHistory
You can click the inspector to view Stats, Query, JSON, and Data like below

LogQL
Now we will write some queries to filter the logs
{filename="/var/log/syslog"} |= "msg" |= "Alert Rule returned no data"
The above query will filter the logs which has the msg “Alert Rule returned no data”
And similarly to get the number of count for a particular msg in a given time interval we would use the below query
count_over_time(({filename="/var/log/syslog"} |= "msg" |= "Alert Rule returned no data"[$__interval]))
Similarly, we can try other queries to extract data from the logs.
Grab the LogQL cheatsheet 📖 to know more about LogQL.

Loki Cheatsheet
You have learned how to install Loki in Ubuntu and learned how to query the logs in Loki using LogQL.You can play around with LogQL.
Catch you up in a new post till then Happy Learning!