Aller au contenu principal

Grafana Alloy

1. Description​

Le package alloy remplace l'ancien package promtail de Grafana.

2. Informations​

2.1. alloy1.home.lab​

Caractéristiques
RAM2 Go
CPU2 coeurs
Disque10Go
Hostnamealloy1.home.lab
IP admin192.168.50.23

3. Installation manuelle​

3.1. Importer la clé GPG du dépôt Grafana​

$ wget -q -O gpg.key https://rpm.grafana.com/gpg.key
$ sudo rpm --import gpg.key

3.2. Créer le fichier /etc/yum.repos.d/grafana.repo​

[grafana]
name=grafana
baseurl=https://rpm.grafana.com
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://rpm.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt

3.3. Installer le package alloy​

$ sudo dnf update
$ sudo dnf install alloy

3.4. Modifier le fichier de configuration config.alloy​

/etc/alloy/config.alloy
// For a full configuration reference, see https://grafana.com/docs/alloy
logging {
level = "warn"
}

prometheus.exporter.unix "default" {
include_exporter_metrics = true
disable_collectors = ["mdadm"]
}

prometheus.scrape "default" {
targets = array.concat(
prometheus.exporter.unix.default.targets,
[{
// Self-collect metrics
job = "alloy",
__address__ = "127.0.0.1:12345",
}],
)

forward_to = [
// TODO: components to forward metrics to (like prometheus.remote_write or
// prometheus.relabel).
]
}

loki.source.syslog "local" {
listener {
address = "0.0.0.0:51893"
labels = { component = "loki.source.syslog", protocol = "tcp" }
}

listener {
address = "0.0.0.0:51898"
protocol = "udp"
labels = { component = "loki.source.syslog", protocol = "udp" }
}

forward_to = [loki.write.local.receiver]
}

loki.write "local" {
endpoint {
url = "http://loki1.home.lab:3100/loki/api/v1/push"
}
}

3.5. Ouvrir le port 51893/tcp et 51898/udp sur firewalld​

$ sudo firewall-cmd --permanent --add-port=51893/tcp
$ sudo firewall-cmd --permanent --add-port=51898/udp
$ sudo firewall-cmd --reload

3.6. Démarrer le service alloy​

$ sudo systemctl enable --now alloy

4. Installation par ansible​

Pour automatiser l'installation du package alloy par ansible, j'ai créé un rôle ansible alloy.

4.1. files/grafana.repo​

files/grafana.repo
[grafana]
name=grafana
baseurl=https://rpm.grafana.com
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://rpm.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt

4.2. templates/config.alloy.j2​

templates/config.alloy.j2
// For a full configuration reference, see https://grafana.com/docs/alloy
logging {
level = "warn"
}

prometheus.exporter.unix "default" {
include_exporter_metrics = true
disable_collectors = ["mdadm"]
}

prometheus.scrape "default" {
targets = array.concat(
prometheus.exporter.unix.default.targets,
[{
// Self-collect metrics
job = "alloy",
__address__ = "127.0.0.1:12345",
}],
)

forward_to = [
// TODO: components to forward metrics to (like prometheus.remote_write or
// prometheus.relabel).
]
}

loki.source.syslog "local" {
listener {
address = "0.0.0.0:51893"
labels = { component = "loki.source.syslog", protocol = "tcp" }
}

listener {
address = "0.0.0.0:51898"
protocol = "udp"
labels = { component = "loki.source.syslog", protocol = "udp" }
}

forward_to = [loki.write.local.receiver]
}

loki.write "local" {
endpoint {
url = "http://{{ loki_host }}:{{ loki_port }}/loki/api/v1/push"
}
}

4.3. defaults/main.yml​

defaults/main.yml
loki_host: "loki1.home.lab"
loki_port: 3100

4.4. tasks/redhat.yml​

tasks/redhat.yml
- name: "Import Grafana GPG key"
ansible.builtin.rpm_key:
key: "https://rpm.grafana.com/gpg.key"
state: present

- name: "Add Grafana repository"
ansible.builtin.copy:
src: "grafana.repo"
dest: "/etc/yum.repos.d/grafana.repo"

- name: "Install alloy package"
ansible.builtin.dnf:
name: "alloy"
state: present

4.5. tasks/main.yml​

tasks/main.yml
- name: "Add grafana repository [RedHat]"
ansible.builtin.include_tasks: redhat.yml
when: ansible_os_family == "RedHat"

- name: "Copy alloy config file"
template:
src: "config.alloy.j2"
dest: "/etc/alloy/config.alloy"
mode: "0644"

- name: "Enable and start alloy service"
ansible.builtin.systemd_service:
name: "alloy"
enabled: true
state: started

5. Envoi des logs vers Loki​

6. Ressources​