Trước khi băt đầu bước vào phần cài đặt Prometheus hãy cùng mình tìm hiểu qua Prometheus là gì nhé!
Prometheus là gì?
Khi nhắc đến các công cụ Monitor cho các quản trị hệ thống thì không ai không biết đến Zabbix, OpenNMS và vừa rồi mình có viết một số bài về Zabbix bạn có thể xem qua ở link bên dưới. Ngoài zabbix ra thì có một công cụ cực kỳ mạnh mẽ và mình đang tìm hiểu để triển khai đó chính là Prometheus.
Prometheus là một bộ công cụ mã nguồn mở ban đầu được xây dựng tại SoundCloud để theo dõi và cảnh báo. Bộ công cụ hiện là một dự án mã nguồn mở độc lập và được duy trì độc lập với bất kỳ công ty nào.
Prometheus thu thập số liệu từ các mục tiêu được giám sát bằng cách loại bỏ các điểm cuối HTTP trên các mục tiêu đó. Hầu hết các thành phần Prometheus đều có trong Go. Một số được viết bằng Java, Python và Ruby.
Prometheus hoạt động tốt để ghi lại bất kỳ chuỗi thời gian số nào. Nó phù hợp với cả giám sát tập trung vào máy móc và giám sát kiến trúc hướng dịch vụ năng động cao. Ngoài việc giúp chạy và tích hợp vào môi trường của bạn dễ dàng hơn, Prometheus còn cung cấp một mô hình dữ liệu phong phú và ngôn ngữ truy vấn.
Cài đặt Prometheus lên máy chủ CentOS
Để cài đặt bạn cần truy cập vào máy chủ thông qua giao thức SSH và thực hiện các bước như sau.
Bước 1: Cập nhật hệ thống và tắt SELINUX
Bạn hãy câp nhật hệ thống lên phiên bản mới và tắt SELINUX nếu đang bật bằng lệnh sau.
yum update -y vi /etc/sysconfig/selinux
Sau đó Change “SELINUX=enforcing
” thành “SELINUX=disabled
”.
Bước 2: Download Prometheus package
Bạn hãy truy cập vào trang chủ của Prometheus và tải phiên bản .tar về cài đặt.
Bạn hãy copy url file và sử dụng lệnh wget để tải về
wget https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz
Bước 3: Configure Prometheus
Bạn hãy tạo một user mới tên là prometheus như sau
useradd --no-create-home --shell /bin/false prometheus
- Sau đó tạo thư mục
mkdir /etc/prometheus mkdir /var/lib/prometheus
- Set owner cho thư mục vừa tạo
chown prometheus:prometheus /etc/prometheus chown prometheus:prometheus /var/lib/prometheus
- Giải nén Prometheus đã tải và di chuyển vào thư mục vừa tạo.
tar -xvzf prometheus-2.20.1.linux-amd64.tar.gz
- Đổi tên thư mục vừa giải nén
mv prometheus-2.20.1.linux-amd64 prometheuspackage
- Copy “prometheus” và “promtool” từ prometheuspackage vào thư mục “/usr/local/bin”
cp prometheuspackage/prometheus /usr/local/bin/ cp prometheuspackage/promtool /usr/local/bin/
- Thay đổi ownership đến user Prometheus
chown prometheus:prometheus /usr/local/bin/prometheus chown prometheus:prometheus /usr/local/bin/promtool
- Copy “consoles” và “console_libraries” từ thư mục prometheuspackage sang thư mục /etc/prometheus
cp -r prometheuspackage/consoles /etc/prometheus cp -r prometheuspackage/console_libraries /etc/prometheus
- Thay đổi ownership user Prometheus
chown -R prometheus:prometheus /etc/prometheus/consoles chown -R prometheus:prometheus /etc/prometheus/console_libraries
Thêm và sửa file cấu hình Prometheus.
Bạn hãy mở file cấu hình Prometheus tại đường dẫn ‘/etc/prometheus/prometheus.yml
‘ sau đó thêm vào các dòng sau
vi /etc/prometheus/prometheus.yml
global: scrape_interval: 10s scrape_configs: - job_name: 'prometheus_master' scrape_interval: 5s static_configs: - targets: ['localhost:9090']
- Sau khi thêm và save lại bạn cần change ownership cho file
chown prometheus:prometheus /etc/prometheus/prometheus.yml
- Cấu hình file dịch vụ prometheus.service
Bạn mở file /etc/systemd/system/prometheus.service
và thêm vào các đoạn sau
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
- Bây giờ bạn hãy lưu lại và reload lại service
systemctl daemon-reload
- Start dịch vụ
systemctl start prometheus
- Kiểm tra trạng thái dịch vụ
systemctl status prometheus
Sau khi đã start dịch vụ bạn hãy mở port firewalld để truy cập. Ở đây mình dùng firewalld nên sử dụng lệnh sau để. Nếu máy chủ bạn sử dụng csf hoặc không có firewall thì bỏ qua bước này.
firewall-cmd --zone=public --add-port=9090/tcp --permanent systemctl reload firewalld
Bước: 4 Truy cập Prometheus từ trình duyệt.
Như vậy đã hoàn tất bước cài đặt, bạn hãy mở trình duyệt và truy cập với http://my_ip:9090
Như vậy mình vừa hoàn tất hướng dẫn cài đặt Prometheus lên máy chủ CentOS 7. Hi vọng qua bài viết này có thể giúp bạn cài đặt thành công Prometheus mà không gặp phải lỗi nào. Hãy tiếp tục theo dõi blog nhé, ở các bài tiếp theo mình sẽ tiếp tục viết các bước cài đặt giám sát hệ thống Linux với Prometheus này.