Loki is a horizontally scalable, highly available, multi-tenant log aggregation system inspired by Prometheus. It is designed to be very cost effective and easy to operate. It does not index the contents of the logs, but rather a set of labels for each log stream.
In this guide, we will learn how to install Grafana Loki and Promtail with Docker and Docker Compose. For production systems, please consider installing Grafana Loki with Tanka or Helm.
The configuration acquired with these installation instructions run Loki as a single binary.
Related content:
- How to run Grafana OSS in docker and docker-compose
- How to run Prometheus with docker and docker-compose
- How to Setup Promtail, Grafana and Loki for free Log Management in Debian 11
- How to install and set up Grafana in Ubuntu 20.04 using Ansible
- How To Install and Configure Prometheus On a Linux Server
- How To Monitor Linux Servers Using Prometheus Node Exporter
Ensure that docker and docker compose is installed
Since we will be using docker to run our set up, it is important that it is installed and running. Please ensure that you are have docker installed. If you are using an Ubuntu system, checkout this guide on How to Install and Use Docker in Ubuntu 22.04.
Confirm that docker is working as expecting by checking the version:
|
|
Next, ensure that docker-compose is installed. Docker compose is available as a python pip package. Ensure that python and pip is installed then install docker-compose with this command:
|
|
This is the version of docker-compose installed in my system:
|
|
Running Grafana in docker
Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources.
In our case, we will need Grafana as a UI to the Loki system. We can use it to query the logs or create specific dashboards based on the log patterns.
To run Grafana with docker, use the following command. We are running the latest version of Grafana OSS - version 9.
|
|
You can use the -d
argument to start in detached mode. Once the server is started, access it by visiting http://server_ip:3000
in your browser.
Running Loki with docker
Loki is a logging backend optimized for users running Prometheus and Kubernetes with great logs search and visualization. Loki works with promtail
, an agent which ships the contents of local logs to an instance of Grafana Loki.
First, let us create a directory to store content and use as the current directory.
|
|
Next, create a configuration file required to start and run loki. Save these to loki-config.yaml
:
|
|
Now we can run our loki instance with this command:
|
|
Once loki is running, let us also run promtail in a separate tab to export logs from /var/log/*log
to our loki instance. Create a file named promtail-config.yaml
.
|
|
In a new tab, start promtail with this command:
|
|
When both loki and promtail are running, navigate to http://server_ip:3100/metrics
to view the metrics and http://server_ip:3100/ready
for readiness.
The image is configured to run by default as user loki
with UID 10001
and GID 10001
. You can use a different user, specially if you are using bind mounts, by specifying the UID with a docker run
command and using --user=UID
with numeric UID suited to your needs. Root user is UID 1000
.
Using docker-compose to run Grafana, Loki and promtail
We can add the instructions above to a docker compose file to make it easy for us to create, update and manage the deployments. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your applicationās services. Then, with a single command, you create and start all the services from your configuration.
Add the following to a docker-compose.yaml
file:
|
|
Save the file in the directory with the configurations then start the services with this command:
|
|
Or you can add the -d
argument for detached mode. Since we are binding the same ports, you can access in the same way as listed above.
Logging in to Grafana
Login with your admin user (default admin
/admin
). You will be prompted to change your password the first time you login.
Open side menu (click the Grafana icon in top menu) head to Data Sources and add your data source. Search for loki then add source url to http://loki:3100
(To add our loki instance) then save.
Next go to explore and choose Loki as the data source. To view our var log logs, choose labels, job then varlogs then query.
|
|
You should see the logs streaming in.
Conclusion
In this guide we managed to run Grafana Loki using docker and docker-compose.