Compartir tecnología

Prometheus Grafana monitorea hosts Linux

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Instale Prometeo

1.1. Descargar Prometeo

Descargar URL

https://github.com/prometheus/prometheus/releases
  • 1

Seleccione la versión requerida

wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
  • 1

1.2. Instalar el software Prometeo

1.2.1. Crear grupo de usuarios

groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
  • 1
  • 2

1.2.2. Configuración de descompresión

tar -zxvf prometheus-2.53.0.linux-amd64.tar.gz
mv prometheus-2.53.0.linux-amd64 /usr/local/prometheus
sed -i 's/localhost/ 172.25.0.166/g' /usr/local/prometheus/prometheus.yml
  • 1
  • 2
  • 3

1.2.3. Agregar Prometheus a las variables de entorno del sistema.

vim /etc/profile
  • 1
export PROMETHEUS_HOME=/usr/local/prometheus
PATH=$PATH:$PROMETHEUS_HOME
export PATH
  • 1
  • 2
  • 3

Recargar el archivo de variables de entorno del sistema

source /etc/profile

  • 1
  • 2

1.2.4 Verifique la información de la versión de Prometheus.

prometheus --version	
  • 1

1.2.5. Crear directorio de datos

mkdir /usr/local/prometheus/data/
  • 1

1.2.6. Modificación del permiso

chown -R prometheus.prometheus /usr/local/prometheus
  • 1

1.2.7. Verificar y cargar el archivo de configuración.

cd /usr/local/prometheus/
./promtool check config prometheus.yml
  • 1
  • 2

1.2.8. Crear servicio systemd

cat <<EOF > /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/ --web.enable-lifecycle --storage.tsdb.retention.time=30d
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Notas de parámetros:

  • config.file: especificar el archivo de configuración
  • web.enable-lifecycle: admite la configuración de recarga a través de una solicitud http
  • Storage.tsdb.path: especifique el directorio de almacenamiento de datos (el valor predeterminado es el directorio de datos del directorio actual; si no existe, cree uno nuevo)
  • Storage.tsdb.retention.time: especifique el tiempo de retención de datos (predeterminado 15d)

1.2.9. Iniciar el servicio

systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus && systemctl enable prometheus
  • 1
  • 2
  • 3

1.2.10 Confirmar que el puerto está siendo monitoreado

ss -lnput | grep 9090
  • 1
tcp     LISTEN   0        512                    *:9090                *:*       users:(("prometheus",pid=20863,fd=7))
  • 1

1.2.11. Verificar y cargar el archivo de configuración.

http://172.25.0.166:9090/metrics
  • 1

Insertar descripción de la imagen aquí


2. Supervisar los hosts de Linux

2.1. Instale el complemento exportador en el host Linux.

2.1.1. Descargar nodo_exportador

Descargar URL

https://github.com/prometheus/node_exporter/releases
  • 1

Descargue la versión requerida

wget https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
  • 1

2.1.2. Crear grupo de usuarios

groupadd prometheus
useradd -g prometheus -m -s /sbin/nologin prometheus
  • 1
  • 2

2.1.3. Instalar y descomprimir

tar -zxvf node_exporter-1.8.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv node_exporter-1.8.1.linux-amd64  node_exporter
  • 1
  • 2
  • 3

2.1.4. Modificación del permiso

chown -R prometheus.prometheus /usr/local/node_exporter
  • 1

2.1.5 Agregue el directorio del archivo de ejecución node_exporter a la variable de entorno.

vim /etc/profile
  • 1
export NODE_EXPORTER=/usr/local/node_exporter
export PATH=$PATH:$NODE_EXPORTER
export PATH
  • 1
  • 2
  • 3

Recargar el archivo de variables de entorno del sistema

source /etc/profile
  • 1

2.1.6. Crear servicio systemd

cat > /usr/lib/systemd/system/node_exporter.service <<EOF

[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2.1.7. Iniciar el servicio

systemctl daemon-reload
systemctl enable node_exporter && systemctl start node_exporter
systemctl status node_exporter
  • 1
  • 2
  • 3

2.1.8 Confirmar que el puerto está siendo monitoreado.

ss -lnput | grep 9100
  • 1
tcp     LISTEN   0        512                    *:9100                *:*       users:(("node_exporter",pid=21904,fd=3))
  • 1

2.2 Agregue el complemento exportador del host Linux al servicio de software Prometheus.

Modificar el archivo de configuración de Prometheus

vim /usr/local/prometheus/prometheus.yml
  • 1
scrape_configs:
... 
  - job_name: 'Linux Node'					# 添加监控项的名字
    static_configs:							
      - targets: ['172.25.0.166:9100']		# 监控主机的ip地址和端口号
  • 1
  • 2
  • 3
  • 4
  • 5

Reinicie el servicio prometheus

systemctl restart prometheus
  • 1

2.3. Pruebe Prometheus para monitorear el estado del host Linux.

http://172.25.0.166:9090/targets?search=&scrapePool=Linux+Node
  • 1

Insertar descripción de la imagen aquí

http://172.25.0.166:9100/metrics
  • 1

Insertar descripción de la imagen aquí


3. Utilice Grafana para mostrar datos.

3.1. Descargar grafaña

Descargar URL

https://grafana.com/grafana/download
  • 1

Descargue la versión requerida

wget  https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.0-1.x86_64.rpm
  • 1

3.2. Instalar grafana

rpm -ivh grafana-enterprise-11.1.0-1.x86_64.rpm
  • 1

3.3. Iniciar grafana

systemctl daemon-reload 
systemctl enable grafana-server && systemctl start grafana-server
  • 1
  • 2

3.4, cultura china grafana

vim /etc/grafana/grafana.ini
  • 1
[users]
# 设置默认语言为中文 
default_language = zh-Hans
  • 1
  • 2
  • 3

Reiniciar servicio

systemctl restart grafana-server
  • 1

3.5. Descargar e importar plantilla de grafana

https://grafana.com/grafana/dashboards/15172-node-exporter-for-prometheus-dashboard-based-on-11074/
  • 1

Insertar descripción de la imagen aquí

3.6. Cargar el complemento grafana-piechart-panel

grafana-cli plugins install grafana-piechart-panel
  • 1

o

tar -xvf grafana-piechart-panel.tar.gz
mv grafana-piechart-panel /var/lib/grafana/plugins/grafana-piechart-panel
  • 1
  • 2

3.7. Acceso al navegador

Insertar descripción de la imagen aquí


4. Referencias

https://prometheus.io/
https://blog.csdn.net/heian_99/article/details/126989356
https://www.cnblogs.com/saneri/p/14667301.html
https://blog.csdn.net/qq_31725371/article/details/114697770