Skip to content

Install Alloy Binary and Start as a Service

Description

Now we will create the Alloy agent service that will act as the collector for Loki.

We can also get the Alloy binary from github.

To check the latest version of Alloy, visit its releases page.

https://github.com/grafana/alloy/releases

#
cd /usr/local/bin
#
curl -O -L "https://github.com/grafana/alloy/releases/download/v1.10.2/alloy-linux-amd64.zip"
#
unzip "alloy-linux-amd64.zip"

And allow the execute permission on the Alloy binary.

#
chmod a+x "alloy-linux-amd64"

Create the Alloy config

Create a new file named config.alloy.

#
nano config.alloy

Copy/paste this content below into it.

local.file_match "system" {
        path_targets = [{
                __address__ = "localhost",
                __path__    = "/var/log/*log",
                job         = "varlogs",
                stream      = "stdout",
        }]
}

loki.source.file "system" {
        targets               = local.file_match.system.targets
        forward_to            = [loki.write.default.receiver]
        legacy_positions_file = "/tmp/positions.yaml"
}

loki.write "default" {
        endpoint {
                url = "http://localhost:3100/loki/api/v1/push"
        }
        external_labels = {}
}

Configure Alloy as a Service

Now we will configure Alloy as a service so that we can keep it running in the background.

Create user specifically for the Alloy service

#
useradd --system alloy

Create a file named alloy.service.

#
nano /etc/systemd/system/alloy.service

And add this script,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[Unit]
Description=Alloy service
After=network.target

[Service]
Type=simple
User=alloy
ExecStart=/usr/local/bin/alloy-linux-amd64 run --storage.path=/tmp/alloy/ /usr/local/bin/config.alloy

[Install]
WantedBy=multi-user.target

Now start and check the service is running.

#
#
#
service alloy start
service alloy status
systemctl enable alloy.service

We can now leave the new Alloy service running.

Read Alloy logs

#
journalctl -u alloy

Explore in Grafana

Open the Explore tab, select the Loki datasource, and use {job="varlogs"} as your first query.

To Convert a Promtail Config to Alloy

This command will read an existing promtail-local-config.yaml, re-create it in the Alloy format and save it as config.alloy.

#
#
cd /usr/local/bin
alloy-linux-amd64 convert --source-format=promtail --output=config.alloy promtail-local-config.yaml