Xin chào ! Nếu đây là lần đầu tiên bạn đến với diễn đàn, xin vui lòng danh ra một phút bấm vào đây để đăng kí và tham gia thảo luận cùng VnPro.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Giải thích chi tiết từng bước triển khai prometheus giám sát switch

    Đầu tiên mình triển khai Prometheus trên Ubuntu 20.04.

    1. Cài đặt Prometheus

    Bước 1: Tạo user riêng cho Prometheus
    useradd --no-create-home --shell /bin/false prometheus

    Giải thích:
    • Tạo một user chuyên biệt (prometheus) để chạy dịch vụ, tăng tính bảo mật.
    • --no-create-home: Không tạo thư mục home
    • --shell /bin/false: Không cho phép user này đăng nhập shell
    Bước 2: Tải gói và cài Prometheus
    - Tải gói bằng dòng lệnh:
    curl -LO https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz

    ​- Giải nén gói: tar -xvf prometheus-2.52.0.linux-amd64.tar.gz
    - Di chuyển vào thư mục: cd prometheus-2.52.0.linux-amd64​​
    - Copy 2 file thực thi:
    • cp prometheus /usr/local/bin/
    • cp promtool /usr/local/bin/
    - Giải thích:
    prometheus là chương trình chính, promtool dùng để kiểm tra file cấu hình.

    Bước 3: Tạo thư mục cấu hình và dữ liệu
    mkdir -p /etc/prometheus /var/lib/prometheus

    - Giải thích:
    • /etc/prometheus: chứa file cấu hình như prometheus.yml
    • /var/lib/prometheus: nơi lưu dữ liệu time-series do Prometheus thu thập
    Bước 4: Tạo file dịch vụ Prometheus
    nano /etc/systemd/system/prometheus.service
    Thêm vào file với nội dung sau:
    [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


    Giải thích:
    • Cấu hình Prometheus chạy như một dịch vụ hệ thống (systemd).
    • ExecStart: chỉ định file chạy chính và các tham số cần thiết.
    • User, Group: chỉ định Prometheus chạy bằng user đã tạo.
    Bước 5: Khởi động Prometheus
    Mình khởi động dịch vụ bằng những câu lệnh sau

    sudo systemctl daemon-reload
    sudo systemctl enable prometheus
    sudo systemctl start Prometheus
    sudo systemctl status Prometheus

    Click image for larger version  Name:	Picture2(1).png Views:	0 Size:	53.8 KB ID:	432232
    Để lên được web Prometheus, mình cần phải mở port trên tường lửa.
    Dùng lệnh:
    uwf allow 9090
    uwf enable


    ​​Click image for larger version  Name:	Picture3.png Views:	0 Size:	26.8 KB ID:	432233
    Truy cập: http://<IP-của-server>:9090 và ra được như hình bên dưới là đã cài đặt Prometheus thành công.

    Click image for larger version  Name:	Picture7.png Views:	0 Size:	29.8 KB ID:	432241

    2. Cài đặt Node Exporter để giám sát Server
    Bước 1: Tạo user riêng
    useradd --no-create-home --shell /bin/false node_exporter
    Tương tự Prometheus – bảo mật tốt hơn khi mỗi dịch vụ dùng một user riêng.
    Bước 2: Cài Node Exporter

    - Tải gói: curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
    ​- Giải nén gói
    tar -xvf node_exporter-*.tar.gz
    p node_exporter /usr/local/bin/


    Bước 3: Tạo file dịch vụ Node Exporter
    Tương tự Prometheus – cấu hình chạy tự động bằng systemd.

    nano /etc/systemd/system/node_exporter.service

    Thêm vào file nội dung sau:
    [Unit]
    Description=Node Exporter
    Wants=network-online.target
    After=network-online.target

    [Service]
    User=node_exporter
    Group=node_exporter
    Type=simple
    ExecStart=/usr/local/bin/node_exporter

    [Install]
    WantedBy=default.target


    Bước 4: Khởi động dịch vụ

    sudo systemctl daemon-reload
    sudo systemctl enable node_exporter
    sudo systemctl start node_exporter
    sudo systemctl status node_exporter

    Node Exporter sẽ mở port 9100 và bắt đầu phục vụ dữ liệu metrics tại.
    Vì vậy cần mở port 9110 trên tường lửa.

    Bước 5: Cấu hình file prometheus.yml để thu thập dữ liệu từ node_exporter
    nano /etc/prometheus/prometheus.yml
    Thêm vào file với nội dung sau: ( cần điền đúng định dạng file thì mới có thể thu thập được)
    scrape_configs:
    - job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9100']


    Giải thích:
    • scrape_configs: cấu hình Prometheus đi thu thập dữ liệu (scrape job)
    • job_name: tên job, có thể tùy ý đặt
    • targets: danh sách IP:port hoặc hostname:port của exporter
    Sau khi thêm thì mình khởi động lại Prometheus bằng lệnh sau:
    systemctl restart prometheus


    3. Kiểm tra hệ thống
    Truy cập trình duyệt: http://<IP-của-server>:9090
    • Kiểm tra exporter:
      • Vào Status > Targets
      • Xem các exporter có trạng thái UP không
    Click image for larger version  Name:	Picture6.png Views:	0 Size:	32.2 KB ID:	432237

    4. Mẹo nhỏ cho SysAdmin / DevOps
    Prometheus không khởi động Kiểm tra log: journalctl -u prometheus
    Thay đổi file .yml không áp dụng Phải restart Prometheus sau mỗi lần sửa
    Bị lỗi YAML Kiểm tra tại: https://www.yamllint.com/
    Exporter không hiển thị Kiểm tra port, firewall, và thử curl trực tiếp


    Attached Files
    Last edited by My~Hang; 2 days ago.
Working...
X