Installing Loki on Ubuntu 22.04 LTS

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.

Prerequisites

  1. Ubuntu up and running linux machine
  2. Root permission
  3. Internet connectivity
  4. Basic knowledge of linux commands
  5. Up and running Grafana Machine

You will learn how to set up an Loki server on your Ubuntu 22.04 server in this guide.

Step 1: Run System Updates

You need to update the ubuntu current repository by following the given command.

sudo apt-get update 

Step 2: Download Loki

We need to download the Grafana Loki binaries for ubuntu, To get latest version you need to click here and get copy the latest link and change with given command in order to download the package.

wget https://github.com/grafana/loki/releases/download/v2.9.1/loki-linux-amd64.zip

To unzip the zip file.

unzip loki-linux-amd64.zip

to move in system binary location.

sudo mv loki-linux-amd64 /usr/local/bin/loki

to create directory for data and configuration file.

sudo mkdir /etc/loki && sudo mkdir -p /data/loki

Step 3: Create Loki Config file

We are ready to create a config file to configure listen port, check setting, schema config prefix and other required setting, Execute the given command one by one.

To create a main config file with named loki-local-config.yaml

sudo vim /etc/loki-local-config.yaml

Paste the following standard configuration.

auth_enabled: false

server:
  http_listen_port: 3100

ingester:
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 5m
  chunk_retain_period: 30s
  max_transfer_retries: 0

schema_config:
  configs:
    - from: 2018-04-15
      store: boltdb
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 168h

storage_config:
  boltdb:
    directory: /data/loki/index

  filesystem:
    directory: /data/loki/chunks

limits_config:
  enforce_metric_name: false
  reject_old_samples: true
  reject_old_samples_max_age: 168h

chunk_store_config:
  max_look_back_period: 0s

table_manager:
  retention_deletes_enabled: false
  retention_period: 0s

Save and exit from the vim text editor.

Step 4: Creating SystemD Service

The SystemD service is critical to create because it will handle the Grafana-Loki process, which we can easily start, stop, restart, enable, and disable during server boot, Use the given command for the same.

To create a systemD config file with named loki.service

sudo vim /etc/systemd/system/loki.service

Paste the following configuration.

[Unit]
Description=Loki service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/loki -config.file /etc/loki-local-config.yaml

[Install]
WantedBy=multi-user.target

Save and exit from the text editor and now we are good to reload the daemon.

sudo systemctl daemon-reload

To start the Grafana;s Loki service.

sudo systemctl start loki.service

To check loki service status.

sudo systemctl status loki.service

It should show in running state with 3100 port number.

You loki server endpoint would be like http://localhost:3100 or http://IP_ADDRESS:3100, You should open the port 3100 in your OS level firewall and network level firewall.

Note: We have only installed Grafana Loki, which collects the logs from the target machine, but we need a Promtail agent in every machine to get logs from the client machine to the Grafana Loki server.

Step 5: Installing Promtail Agent

Pomtail is an agent that is typically installed on every machine that hosts applications that require logs monitoring. It transmits the contents of local logs to Grafana Loki server or the Grafana cloud.

To download Promtail package, You can download the latest version of Promtail by clicking here and update in given command.

curl -LO https://github.com/grafana/loki/releases/download/v2.9.4/promtail-linux-amd64.zip

To unzip Promtail’s zip file.

unzip promtail-linux-amd64.zip

To move the promtail’s file in system’s binary location.

sudo mv promtail-linux-amd64 /usr/local/bin/promtail

To create promtail main configuration file.

sudo vim /etc/promtail-local-config.yaml

Paste the following configuration.

server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /data/loki/positions.yaml

clients:
  - url: http://localhost:3100/loki/api/v1/push

scrape_configs:
- job_name: system
  static_configs:
  - targets:
      - localhost
    labels:
      job: varlogs
      __path__: /var/log/*log

Note: In the above promtail configuration we are only adding /var/log directory to ship to loki server.

The above configuration contains the log path, a Grafana endpoint with a port number, and more. You can add more jobs here to monitor your application. We need to update the log path.

To create a SystemD service for promtail.

sudo vim /etc/systemd/system/promtail.service

Paste the following configuration.

[Unit]
Description=Promtail service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/promtail -config.file /etc/promtail-local-config.yaml

[Install]
WantedBy=multi-user.target

Save and exit from the text editor and we are good to reload the daemon.

To reload the daemon.

sudo systemctl daemon-reload

To start the promtail serivce.

sudo systemctl start promtail.service

To check promtail service status.

sudo systemctl status promtail.service

You should get Promtail service up and running in your screen.

We are good to configure Loki endpoint in our grafana server to receive the logs from promtail agent.

Step 6: Configure Loki with Grafana

We need to add the Loki data source in our Grafana server by using our Loki endpoint, and after that, we will be able to see the logs in our Grafana Loki Query section, and later on, we can create log metrics with alerting.

Login in your Grafana account.

Add the Loki data source.

Enter the Loki server access point and port number.

Test the Loki data source.

Test logs in your Grafana’s Loki query box.

Conclusion

We have successfully installed Grafana-loki with promtail agent on ubuntu 22.04 LTS server, If you still have questions, please post them in the comments section below.

Author

Installing Loki on Ubuntu 22.04 LTS

2 thoughts on “Installing Loki on Ubuntu 22.04 LTS

Leave a Reply

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

Scroll to top