Hướng dẫn cài đặt Prometheus trên Linux

Hướng dẫn cài đặt Prometheus trên LinuxCuongquach.com | Prometheus vào thời điểm này đang là một trong những công cụ giải pháp dùng để giám sát hệ thống dịch vụ của từng hạ tầng vật lý và cả cloud khá là nổi. Trở thành 1 trong những dự án đứng thứ 2 trên Cloud Computing Foundation Native, hôm nay chúng ta cùng tìm hiểu các bước cài đặt dịch vụ đầu tiên là Prometheus nhé.

cai-dat-prometheus-tren-linux

Mô hình lab cài đặt Prometheus

Nếu bạn đã đọc qua bài viết giới thiệu “Prometheus là gì?” thì bạn cũng hiểu sơ là các thành phần hệ thống giám sát dịch vụ Prometheus có thể cài đặt riêng lẻ, rời rạc và cấu hình để hoạt động tích hợp với nhau. Nên bạn hoàn toàn có thể cài đặt các thành phần dịch vụ ở những máy chủ khác nhau nhưng vẫn làm việc với nhau được.

Nhưng ở bài lab hướng dẫn cài đặt bộ chương trình giám sát Prometheus căn bản này, chúng ta sẽ đi từ từ từng bước cài đặt các thành phần khác nhau. Chúng ta sẽ cài các chương trình dịch vụ quan trọng trên cùng 1 máy chủ để dễ theo dõi lab.

Đầu tiên chúng ta cùng bước đến dịch vụ “Cài đặt Prometheus trên Linux” nhé.

Cài đặt Prometheus trên Linux

Hệ điều hành sử dụng trong bài lab này có thể là : CentOS 7 hoặc Ubuntu 16.04 trở lên. Tức chỉ lab trên các OS nền tảng systemd mới.

Đầu tiên bạn cần tắt SELinux nếu bạn không rành việc cấu hình SELinux.

# sed -i 's/\(^SELINUX=\).*/\SELINUX=disabled/' /etc/sysconfig/selinux
# sed -i 's/\(^SELINUX=\).*/\SELINUX=disabled/' /etc/selinux/config
# setenforce 0

Sau đó bạn download chương trình Prometheus về để chạy. Việc download khá đơn giản, bạn chỉ cần truy cập link sau : https://prometheus.io/download/

# cd /opt/
# wget https://github.com/prometheus/prometheus/releases/download/v2.12.0/prometheus-2.12.0.linux-amd64.tar.gz

Giải nén file package Prometheus vừa download.

# cd /opt/
# tar -xzvf prometheus-2.12.0.linux-amd64.tar.gz
prometheus-2.12.0.linux-amd64/
prometheus-2.12.0.linux-amd64/prometheus.yml
prometheus-2.12.0.linux-amd64/promtool
prometheus-2.12.0.linux-amd64/console_libraries/
prometheus-2.12.0.linux-amd64/console_libraries/menu.lib
prometheus-2.12.0.linux-amd64/console_libraries/prom.lib
prometheus-2.12.0.linux-amd64/consoles/
prometheus-2.12.0.linux-amd64/consoles/node.html
prometheus-2.12.0.linux-amd64/consoles/node-overview.html
prometheus-2.12.0.linux-amd64/consoles/index.html.example
prometheus-2.12.0.linux-amd64/consoles/node-disk.html
prometheus-2.12.0.linux-amd64/consoles/node-cpu.html
prometheus-2.12.0.linux-amd64/consoles/prometheus.html
prometheus-2.12.0.linux-amd64/consoles/prometheus-overview.html
prometheus-2.12.0.linux-amd64/NOTICE
prometheus-2.12.0.linux-amd64/prometheus
prometheus-2.12.0.linux-amd64/LICENSE

Tạo user để chạy dịch vụ Prometheus.

# useradd --no-create-home --shell /bin/false prometheus

Tạo thư mục cấu hình và data cho Prometheus. Sau đó phân quyền owner tương ứng.

# mkdir /etc/prometheus
# mkdir /var/lib/prometheus
# chown prometheus:prometheus /etc/prometheus
# chown prometheus:prometheus /var/lib/prometheus

Copy 2 file binary ‘prometheus‘ và ‘promtool‘ vào thư mục ‘/usr/local/bin/‘.

# cp prometheus-2.12.0.linux-amd64/prometheus /usr/local/bin/
# cp prometheus-2.12.0.linux-amd64/promtool /usr/local/bin/
# chown prometheus:prometheus /usr/local/bin/prometheus
# chown prometheus:prometheus /usr/local/bin/promtool
# chmod +x /usr/local/bin/prometheus /usr/local/bin/promtool

Copy 2 thư mục ‘consoles‘ và ‘consoles_libraries‘ vào thư mục ‘/etc/prometheus/‘.

# cp -r prometheus-2.12.0.linux-amd64/consoles /etc/prometheus
# cp -r prometheus-2.12.0.linux-amd64/console_libraries /etc/prometheus
# chown -R prometheus:prometheus /etc/prometheus/consoles
# chown -R prometheus:prometheus /etc/prometheus/console_libraries

Tạo file cấu hình Prometheus cơ bản. Với cấu hình cơ bản này thì Prometheus đầu tiên nó sẽ tự monitor chính nó.

# vi /etc/prometheus/prometheus.yml
global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'prometheus_master'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']

Khởi tạo một file khởi động dịch vụ Prometheus trên systemd.

# vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target

Khởi động dịch vụ Prometheus.

# systemctl daemon-reload
# systemctl start prometheus
# systemctl status prometheus
● prometheus.service - Prometheus
Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2019-08-21 15:02:13 +07; 3s ago
Main PID: 57316 (prometheus)
CGroup: /system.slice/prometheus.service
└─57316 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles --web....

Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.875Z caller=main.go:333 vm_limits="(soft=unlimited, hard=unlimited)"
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.879Z caller=main.go:654 msg="Starting TSDB ..."
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.885Z caller=web.go:448 component=web msg="Start listening for connections" address=0.0.0.0:9090
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.886Z caller=head.go:509 component=tsdb msg="replaying WAL, this may take awhile"
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.886Z caller=head.go:557 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.887Z caller=main.go:669 fs_type=XFS_SUPER_MAGIC
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.887Z caller=main.go:670 msg="TSDB started"
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.887Z caller=main.go:740 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.969Z caller=main.go:768 msg="Completed loading of configuration file" filename=/etc/prometh...metheus.yml
Aug 21 15:02:13 cuongquach-prometheus prometheus[57316]: level=info ts=2019-08-21T08:02:13.969Z caller=main.go:623 msg="Server is ready to receive web requests."
Hint: Some lines were ellipsized, use -l to show in full.

Nếu mà bạn có sử dụng tường lửa thì cần mở firewall rule cho port mặc định của Prometheus TCP 9090. Ví dụ với ‘iptables’.

# iptables -I INPUT -p tcp --dport 9090 -j ACCEPT

Lúc này dịch vụ Prometheus đã chạy và bạn có thể truy xuất dịch vụ Prometheus qua giao diện quản lý đơn giản của Prometheus tại port mặc định TCP 9090. Ví dụ: 10.0.253.179:9090

cài đặt prometheus
cài đặt prometheus

Đơn giản phải không nào, bước đầu tiên là cài đặt Prometheus để khởi chạy dịch vụ monitor Prometheus trước đã. Còn những thành phần kế tiếp gồm : Node_exporter, Grafana, Alertmanager,… ta sẽ lại gặp nhau ở các bài viết khác.

Nguồn: https://cuongquach.com/

Previous articleHướng dẫn cài đặt dịch vụ Vault trên Linux
Next articleGiám sát máy chủ Linux với Prometheus Node Exporter
Bạn đang theo dõi website "https://cuongquach.com/" nơi lưu trữ những kiến thức tổng hợp và chia sẻ cá nhân về Quản Trị Hệ Thống Dịch Vụ & Mạng, được xây dựng lại dưới nền tảng kinh nghiệm của bản thân mình, Quách Chí Cường. Hy vọng bạn sẽ thích nơi này !