In this post I will demonstrate how to deploy Grafana Labs’s Loki on Multipass using cloud-init so that you can run your own dev environment and run a couple of queries to get you started.
About
If you haven’t heard of Multipass, it allows you to run Ubuntu VMs on your Mac or Windows workstation.
If you haven’t heard of Loki, as described by Grafana Labs: “Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus.”
Install Multipass
Head over to multipass.run to get the installer for your operating system, and if you are curious about Multipass, I wrote a beginners guide on Multipass which can be found here
Cloud Init for Loki
We will be making use of cloud-init to bootstrap Loki v2.0.0 to our multipass instance.
V2.0.0 is the current release of the time of writing, so depending on the time when you read this, have a look at the Loki Releases page for the latest version and adjust the cloud-init.yml according to the version if it differs from the one I’m mentioning.
(Optional) If you want to use SSH to your Multipass VM, you can use your existing SSH key or generate a new one, if you want to create a new key, you can follow this post
Copy your public key, in my case ~/.ssh/id_rsa.pub
and paste it under the ssh authorized_keys
section.
Our cloud-init.yml
has a couple of sections, but to break it down it will do the following:
- We provide it our public ssh key so that we can ssh with our private key
- Updates the index repository
- Installs the packages, unzip and wget
- Creates the loki systemd unit file and places it under /etc/systemd/system/
- When the vm boots it will create the user loki and creates the loki etc directory
- Once that completes, we are downloading the loki, logcli and promtail binaries from github
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
You will notice that the VM will have loki
, logcli
and promtail
available on it, so you will have an environment to use all of them together.
As you can see once we start loki, we are piping this is a test
to Loki using Promtail, so that we can verify that the data is visible in Loki. That step is not required, but just added it to this demo.
Deploy Loki on Multipass
We will provision a Multipass VM using the Ubuntu Focal distribution and spec our VM with 1 CPU, 512MB of Memory and 1GB of disk and then bootstrap our installation of Loki using cloud-init:
1 2 3 4 5 6 7 8 9 10 |
|
We can validate if our Multipass VM is running:
1 2 3 |
|
Test Loki inside the VM
First we will exec into the VM (or SSH), then we will test out Loki inside the VM since we already have logcli available:
1 2 3 4 5 |
|
Remembered in our cloud-init, we instructed this command to run:
1
|
|
So if we use logcli, we can inspect our visible labels:
1 2 3 4 5 |
|
And as we expect, we will see the app label from the --client.external-labels=app=cli
argument that we passed. We can also look at the values for a given label:
1 2 |
|
Now let’s query our logs using the label selector: {app="cli"}
:
1 2 |
|
If we remove the extra arguments, we will see more verbose output like the following:
1 2 3 4 5 6 7 |
|
We can pipe some more output to Loki:
1
|
|
And querying our logs:
1 2 3 |
|
Testing Loki Outside our VM
Let’s exit the VM and test Loki from our local workstation, first you will need to get the logcli for your OS, head over to the releases page and get the binary of your choice.
I will be demonstrating using a mac:
1 2 3 4 |
|
Now we need to tell logcli where our Loki server resides, so let’s get the IP address of Loki:
1 2 |
|
We can either set the Loki host as an environment variable:
1
|
|
or you can specify it using the --addr
argument:
1
|
|
For the sake of simplicity and not having to type the --addr
the whole time, I will be setting the Loki address as an environment variable:
1
|
|
And when we inspect our labels using logcli, we can see that we are getting our labels from Loki on our Multipass VM:
1 2 3 4 5 6 |
|
Write Logs to Loki using the Loki Docker Driver
We have used promtail before to pipe logs to Loki and in this example we will be making use of the Loki Docker Logging Plugin to write data to Loki.
If you have docker installed, install the Loki plugin:
1 2 3 4 |
|
Now we will use a docker container to echo stdout to the loki docker driver, which will send the output to Loki.
Let’s alias a command loki_echo that we will use to send our output to the docker container:
1
|
|
So every time we run loki_echo {string}
we will run a docker container from the busybox image and pass the {string}
as an argument to the echo command inside the container, which will be sent to the loki log driver and land up in Loki.
Let’s push 100 log events to Loki:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
We can verify how many log events we have with:
1 2 |
|
To see how many logs we have with the line “blue” in it:
1 2 |
|
Let’s look for logs with blue or green and limit the results to 5:
1 2 3 4 5 6 |
|
Teardown
If you followed along, you can terminate your Multipass VM with:
1
|
|
You can get the example code in my multipassfiles github repository
Thanks
Thanks for reading, if you like my content, check out my website or follow me at @ruanbekker on Twitter.