Prometheus là ứng dụng giám sát miễn phí, mã nguồn mở và dựa trên web, thu thập số liệu từ các dịch vụ của bạn và lưu trữ chúng trong cơ sở dữ liệu chuỗi thời gian. Cấu hình mặc định của Prometheus chỉ xuất số liệu về chính nó. Tuy nhiên, bạn có thể mở rộng nó bằng cách cài đặt trình xuất và các chương trình khác. Nó hỗ trợ mô hình dữ liệu đa chiều, nhiều chế độ biểu đồ và bảng thông tin.
Trong bài đăng này, chúng tôi sẽ chỉ cho bạn cách cài đặt giám sát Prometheus trên Debian 11.
Tiếp theo, tải xuống phiên bản Prometheus mới nhất bằng cách chạy lệnh sau:
Sau khi Prometheus được đã tải xuống, bạn có thể xem tệp đã tải xuống bằng lệnh sau:
Bạn sẽ thấy đầu ra sau:
Tiếp theo, giải nén tệp đã tải xuống bằng lệnh sau:
Tiếp theo, thay đổi thư mục thành thư mục đã giải nén bằng lệnh sau:
Tiếp theo, tạo một số thư mục cần thiết bằng lệnh sau:
Tiếp theo, sao chép các thư mục cần thiết tệp cấu hình và công cụ bằng các lệnh sau:
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.
Thêm các dòng sau:
Lưu và đóng tệp sau đó đặt quyền sở hữu và quyền thích hợp cho thư mục cấu hình Prometheus:
Tiếp theo, tải lại daemon systemd để áp dụng các thay đổi:
Tiếp theo, khởi động dịch vụ Prometheus và cho phép dịch vụ này khởi động khi khởi động lại hệ thống bằng lệnh sau:
Bạn có thể kiểm tra trạng thái của Prometheus bằng lệnh sau:
Bạn sẽ nhận được kết quả đầu ra sau:
Theo mặc định, Prometheus lắng nghe trên cổng 9090. Bạn có thể kiểm tra bằng lệnh sau:
Bạn sẽ thấy kết quả đầu ra sau:
Sau khi hoàn tất, bạn có thể tiến hành bước tiếp theo.
Sau khi Nginx được cài đặt, hãy tạo tệp cấu hình máy chủ ảo Nginx bằng lệnh sau:
Thêm các dòng sau:
Lưu và đóng tệp, sau đó xác minh Nginx xem có lỗi cú pháp nào không bằng lệnh sau:
Nếu mọi thứ ổn, bạn sẽ nhận được kết quả sau:
Cuối cùng, khởi động lại dịch vụ Nginx để áp dụng các thay đổi:
Bạn cũng có thể xác minh trạng thái của dịch vụ Nginx bằng lệnh sau:
Bạn sẽ nhận được kết quả sau:
Tại thời điểm này, Nginx đã được cài đặt và cấu hình để phục vụ Prometheus. Bây giờ bạn có thể tiến hành bước tiếp theo.
Sau khi tải xuống hoàn tất, hãy giải nén tệp đã tải xuống bằng lệnh sau:
Tiếp theo, di chuyển thư mục đã giải nén đến thư mục /etc/prometheus/:
Tiếp theo, đặt quyền sở hữu phù hợp bằng lệnh sau:
Tiếp theo, tạo một tệp dịch vụ systemd để quản lý dịch vụ node_exporter:
Thêm các dòng sau:
Lưu và đóng tệp, sau đó tải lại daemon systemd để áp dụng các thay đổi:
Tiếp theo, khởi động dịch vụ node_exporter và cho phép dịch vụ này khởi động khi khởi động lại hệ thống:
Bạn có thể kiểm tra trạng thái của node_exporter bằng lệnh sau:
Bạn sẽ nhận được kết quả đầu ra sau:
Theo mặc định, node_exporter lắng nghe trên cổng 9100. Bạn có thể kiểm tra bằng lệnh sau:
Bạn sẽ nhận được kết quả đầu ra sau:
Dưới dòng 'scrape_config', thêm job_name node_exporter mới bằng cách thêm các dòng sau.
Lưu và đóng tệp, sau đó khởi động lại dịch vụ Prometheus để áp dụng các thay đổi:
Bây giờ, hãy quay lại trang chủ Prometheus và nhập node_memory_MemAvailable_bytes vào trường truy vấn và nhấp vào nút 'Thực thi'. Bạn sẽ nhận được kết quả sau:
Bạn cũng có thể lấy dữ liệu số liệu node_exporter bằng cách sử dụng URL như hiển thị bên dưới:
Trong bài đăng này, chúng tôi sẽ chỉ cho bạn cách cài đặt giám sát Prometheus trên Debian 11.
Điều kiện tiên quyết
- Một máy chủ chạy Debian 11.
- Mật khẩu gốc được cấu hình trên máy chủ.
Cài đặt Prometheus
Trước khi bắt đầu, bạn nên tạo một người dùng và nhóm chuyên dụng cho Prometheus. Bạn có thể tạo nó bằng lệnh sau:
Mã:
groupadd --system prometheus
useradd -s /sbin/nologin --system -g prometheus prometheus
Mã:
curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest|grep browser_download_url|grep linux-amd64|cut -d '"' -f 4|wget -qi -
Mã:
ls -l
Mã:
-rw-r--r-- 1 root root 72638078 Oct 5 16:46 prometheus-2.30.3.linux-amd64.tar.gz
Mã:
tar -xvf prometheus*.tar.gz
Mã:
cd prometheus-2.30.3.linux-amd64
Mã:
mkdir /etc/prometheus
mkdir /var/lib/prometheus
Mã:
mv prometheus.yml /etc/prometheus/prometheus.yml
mv consoles/ console_libraries/ /etc/prometheus/
mv prometheus promtool /usr/local/bin/
Tạo tệp dịch vụ Systemd cho Prometheus
Tiếp theo, bạn sẽ cần tạo tệp dịch vụ systemd để quản lý dịch vụ Prometheus. Bạn có thể tạo nó bằng cách chạy lệnh sau:
Mã:
nano /etc/systemd/system/prometheus.service
Mã:
[Unit]Description=PrometheusDocumentation=https://prometheus.io/docs/introduction/overview/Wants=network-online.targetAfter=network-online.target[Service]Type=simpleUser=prometheusGroup=prometheusExecReload=/bin/kill -HUP $MAINPIDExecStart=/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 \ --web.listen-address=0.0.0.0:9090 \ --web.external-url=SyslogIdentifier=prometheusRestart=always[Install]WantedBy=multi-user.target
Mã:
chown -R prometheus:prometheus /etc/prometheus/
chmod -R 775 /etc/prometheus/
chown -R prometheus:prometheus /var/lib/prometheus/
Mã:
systemctl daemon-reload
Mã:
systemctl start prometheus
systemctl enable prometheus
Mã:
systemctl status prometheus
Mã:
? prometheus.service - Prometheus Đã tải: đã tải (/etc/systemd/system/prometheus.service; đã tắt; cài đặt trước của nhà cung cấp: đã bật) Đang hoạt động: đang hoạt động (đang chạy) kể từ Thứ bảy 2021-10-16 14:06:39 UTC; 4 giây trước Tài liệu: https://prometheus.io/docs/introduction/overview/ PID chính: 18415 (prometheus) Nhiệm vụ: 5 (giới hạn: 2341) Bộ nhớ: 19,6M CPU: 79ms CGroup: /system.slice/prometheus.service ??18415 /usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.cons>16 tháng 10 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.172Z caller=head.go:513 component=tsdb msg="On-disk memory mappa>16 tháng 10 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.172Z caller=head.go:519 component=tsdb msg="Đang phát lại WAL, điều này >16 tháng 10 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.174Z caller=head.go:590 component=tsdb msg="WAL segment loaded" >16/10 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.174Z caller=head.go:596 component=tsdb msg="WAL replay completed>16/10 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.177Z caller=main.go:849 fs_type=EXT4_SUPER_MAGIC16/10 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.177Z caller=main.go:852 msg="TSDB đã bắt đầu"16 tháng 10 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.178Z caller=main.go:979 msg="Đang tải tệp cấu hình" filenam>16 tháng 10 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.178Z caller=main.go:1016 msg="Đã tải xong cấu hình>Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.179Z caller=main.go:794 msg="Máy chủ đã sẵn sàng để nhận yêu cầu web>Oct 16 14:06:39 debian11 prometheus[18415]: level=info ts=2021-10-16T14:06:39.179Z caller=tls_config.go:191 component=web msg="TLS is disabled>
Mã:
ss -antpl | grep 9090
Mã:
LISTEN 0 4096 *:9090 *:* users:(("prometheus",pid=18415,fd=7))
Cấu hình Nginx làm Proxy ngược cho Prometheus
Tiếp theo, bạn nên cài đặt và cấu hình Nginx làm proxy ngược cho Prometheus. Trước tiên, hãy cài đặt gói máy chủ web Nginx bằng lệnh sau:
Mã:
apt-get install nginx -y
Mã:
nano /etc/nginx/conf.d/prometheus.conf
Mã:
server { listen 80; server_name prometheus.example.com; location / { proxy_pass http://localhost:9090; proxy_http_version 1.1; proxy_set_header Nâng cấp $http_upgrade; proxy_set_header Kết nối 'nâng cấp'; proxy_set_header Máy chủ $host; proxy_cache_bypass $http_upgrade; }}
Mã:
nginx -t
Mã:
nginx: configuration file /etc/nginx/nginx.conf test is successful
Mã:
systemctl restart nginx
Mã:
systemctl status nginx
Mã:
? nginx.service - Máy chủ web hiệu suất cao và máy chủ proxy ngược Đã tải: đã tải (/lib/systemd/system/nginx.service; đã bật; cài đặt trước của nhà cung cấp: đã bật) Đang hoạt động: đang hoạt động (đang chạy) kể từ Sat 2021-10-16 14:08:15 UTC; 2 giây trước Tài liệu: man:nginx(8) Tiến trình: 18792 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Tiến trình: 18793 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) PID chính: 18794 (nginx) Nhiệm vụ: 2 (giới hạn: 2341) Bộ nhớ: 2,5M CPU: 35ms CGroup: /system.slice/nginx.service ??18794 nginx: tiến trình chính /usr/sbin/nginx -g daemon on; master_process on; ??18795 nginx: tiến trình công nhân16 tháng 10 14:08:15 debian11 systemd[1]: Đang khởi động Một máy chủ web hiệu suất cao và một máy chủ proxy ngược...16 tháng 10 14:08:15 debian11 systemd[1]: nginx.service: Không phân tích được PID từ tệp /run/nginx.pid: Đối số không hợp lệ16 tháng 10 14:08:15 debian11 systemd[1]: Đang khởi động Một máy chủ web hiệu suất cao và một máy chủ proxy ngược.
Truy cập Bảng điều khiển Prometheus
Bây giờ, hãy mở trình duyệt web của bạn và truy cập bảng điều khiển Prometheus bằng URL . Bạn sẽ thấy trang sau:Cài đặt và cấu hình node_exporter
node_exporter là một trình xuất dữ liệu sẽ theo dõi và lấy số liệu của máy chủ Prometheus. Trước tiên, hãy tải xuống phiên bản mới nhất của node_exporter bằng lệnh sau:
Mã:
wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
Mã:
tar -xvzf node_exporter-1.2.2.linux-amd64.tar.gz
Mã:
mv node_exporter-1.2.2.linux-amd64 /etc/prometheus/node_exporter
Mã:
chown -R prometheus:prometheus /etc/prometheus/node_exporter
Mã:
nano /etc/systemd/system/node_exporter.service
Mã:
[Unit]Description=Node ExporterWants=network-online.targetAfter=network-online.target[Service]User=prometheusExecStart=/etc/prometheus/node_exporter/node_exporter[Install]WantedBy=default.target
Mã:
systemctl daemon-reload
Mã:
systemctl start node_exporter
systemctl enable node_exporter
Mã:
systemctl status node_exporter
Mã:
? node_exporter.service - Node Exporter Đã tải: đã tải (/etc/systemd/system/node_exporter.service; đã tắt; cài đặt trước của nhà cung cấp: đã bật) Đang hoạt động: đang hoạt động (đang chạy) kể từ CN 2021-10-17 05:38:20 UTC; 4 giây trước PID chính: 513 (node_exporter) Nhiệm vụ: 4 (giới hạn: 2341) Bộ nhớ: 4,7M CPU: 11ms CGroup: /system.slice/node_exporter.service ??513 /etc/prometheus/node_exporter/node_exporter17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=thermal_zone17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=time17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=timex17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=udp_queues17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=uname17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=vmstat17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=xfs17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.382Z caller=node_exporter.go:115 collector=zfs17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.383Z caller=node_exporter.go:199 msg="Listening on" address=:91>17 tháng 10 05:38:20 debian11 node_exporter[513]: level=info ts=2021-10-17T05:38:20.383Z caller=tls_config.go:191 msg="TLS is disabled." http2=false
Mã:
ss -antpl | grep 9100
Mã:
LISTEN 0 4096 *:9100 *:* users:(("node_exporter",pid=513,fd=3))
Thêm node_exporter vào Prometheus Server
Tiếp theo, bạn sẽ cần thêm node_exporter vào tệp cấu hình Prometheus. Bạn có thể thực hiện bằng cách chỉnh sửa tệp cấu hình mặc định của Prometheus:
Mã:
nano /etc/prometheus/prometheus.yml
Mã:
- job_name: 'node_exporter' static_configs: - targets: ['localhost:9100']
Mã:
systemctl restart prometheus
Xác minh Prometheus và node_exporter
Bây giờ, hãy đến bảng điều khiển Prometheus và nhấp vào Trạng thái => Mục tiêu. Bạn sẽ thấy node_exporter của mình trong màn hình sau:Bây giờ, hãy quay lại trang chủ Prometheus và nhập node_memory_MemAvailable_bytes vào trường truy vấn và nhấp vào nút 'Thực thi'. Bạn sẽ nhận được kết quả sau:
Bạn cũng có thể lấy dữ liệu số liệu node_exporter bằng cách sử dụng URL như hiển thị bên dưới: