config-grafana-promtheus
Configuration serveur sous vagrantfile et install Grafana et Prometheus.
Pour configurer un serveur Debian ou Ubuntu avec Vagrant, puis install Prometheus et Grafana, voici quelques étapes à suivre.
- Nous allons créer un Vagrantfile pour provisionner la machoine virtuelle, puis utiliser un script bash pour installer et configurer Prometheus et Grafana. Notre Hypervisuer est VMware
- Créer le Vagrantfile
créez un fichier Vagrantfile pour configurer la machine virtuelle debian 12.
Vagrant.configure("2") do |config|
config.vm.box = "debian/bookworm64"
# Debian 12 (Bookworm)
config.vm.hostname = "monitoring-server"
config.vm.network "private_network", type: "dhcp"
config.vm.provider :vmware_desktop do |vmware|
vmware.memory = "2048" # 2 GO de RAM
vmware.cpus = 2 # 2 GO de CPU
vmware.gui = true
vmware.vmx["ethernet0.virtualdev"] = "vmxnet3"
vmware.ssh_info_public = true
vmware.linked_clone = false
end
# Provisionning avec un script bash
config.vm.provision "shell", path:"install_monitoring.sh"
end
- Créer le script d'installation (install_monitoring.sh)
Créez un script bash nommé install_monitoring.sh pour installer Prometheus, Grafana et leurs dépendances.
- Mettre à jour le système
#!/bin/bash
# Mettre à jour le système
apt-get update
apt-get upgrade -y
- Installer les dépendances
#!/bin/bash
# Installer les dépendances
apt-get install -y wget curl gnupg2
- Install Prometheus
#!/bin/bash
# Installer Prometheus
PROMETHEUS_VERSION="2.47.0"
wget https://github.com/prometheus/prometheus/releases/download/v$PROMETHEUS_VERSION/prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz
tar xvfz prometheus-$PROMETHEUS_VERSION.linux-amd64.tar.gz
mv prometheus-$PROMETHEUS_VERSION.linux-amd64 /opt/prometheus
- Créer un utilisateur pour Prometheus
# Créer un utilisateur pour Prometheus
useradd --no-create-home --shell /bin/false prometheus
chown -R prometheus:prometheus /opt/prometheus
- Configuration de Prometheus
# Configurer Prometheus
cat <<EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/prometheus \
--config.file=/opt/prometheus/prometheus.yml \
--storage.tsdb.path=/opt/prometheus/data
Restart=always
[Install]
WantedBy=multi-user.target
EOF
- Démarrer et activer Prometheus
#Démarrer et activer Prometheus
systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
- Installer Grafana
#!/bin/bash
# Installer Grafana
apt-get install -y apt-transport-https
wget -q -O - https://packages.grafana.com/gpg.key | apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | tee -a /etc/apt/sources.list.d/grafana.list
apt-get update
apt-get install -y grafana
# Démarrer et activer Grafana
systemctl start grafana-server
systemctl enable grafana-server
# Ouvrir les ports nécessaires
ufw allow 9090/tcp # Prometheus
ufw allow 3000/tcp # Grafana
# Afficher les informations d'accès
echo "Prometheus est installé et accessible sur : http://$(hostname -I | awk '{print $1}'):9090"
echo "Grafana est installé et accessible sur : http://$(hostname -I | awk '{print $1}'):3000"
Démarrer la machine virtuelle
- Placez le Vagrantfile et le script ***install_monitoring.sh *** dans le même répertoire.
- Exécutez la commande suivante pour démarrer la machine:
vagrant up
Acceder à Grafana et prometheus
une fois la machine virtuelle lancée, vous pouvez accéder aux services suivvants:
- prometheus:
http://IP_VM:9090
- Grafana:
http://IP_VM:3000
Pour obtenir l'adresse ip d ela machine virtuelle:
vagrant ssh -c "hostname -I"
Configuration de Grafana
- Connectez-vous à Grafana:
http://IP_VM:3000
Identifiant: admin Mot de passe: admin - Ajoutez prometheus comme source de données:
- Allez dans configuration > Data Sources
- Selectionnez Prometheus.
- Entrez l'URL de Prometheus:
http://IP_VM:9090
- Cliquez sur save & Test
Importer des tableaux de bord dans Grafana
Vous pouvez importer des tableaux de bord prédéfinis pour visualiser les métriques de prometheus. Par exemple:
- Allez dans Create > Import.
- Entrez l'ID d'un tableau de bord (par exemple, 1860 pour le tableau de bord Node Exporter).
- Sélectionnez la source de données Prometheus.
- Cliquez sur Import
Conclusion
Vous avez maintenant un serveur Debian 12 avec Prometheus et Grafana installé et configurés. Vous pouvez surveiller vos métriques et créer des tabaleaux de bord personnalisés dans Grafana.