ToiVietBlog - Bài viết này mình sẽ hướng dẫn các bạn cài đặt Prometheus và Grafana trên Ubuntu 18.04.
Với những bạn chưa biết 2 công cụ này là gì?
Prometheus và Grafana là gì?
Mình xin tóm tắt:
- Prometheus là một công cụ giám sát và cảnh báo mã nguồn mở. Phát triển bởi SoundCloud từ 2012, nó hỗ trợ dạng data time serie, tương tự như InfluxDB.
- Grafana là một công cụ “đồ thị hóa”, nó cho phép bạn truy vấn và vẽ các dạng bảng/biểu đồ trực quan dựa trên các dữ liệu có sẵn.
Vậy thì khi kết hợp Prometheus và Grafana ta sẽ được gì? Prometheus thu thập dữ liệu monitoring từ các endpoint (server, switch, firewall, website…). Grafana sẽ hiển thị biểu đồ dựa trên dữ liệu của Prometheus.

Chuẩn bị máy chủ Ubuntu 18
Để bắt đầu thì bạn có thể sử dụng máy chủ Ubuntu server 18 với cấu hình như sau:
- 1 vCPU
- 1 GB RAM
- 40 GB HDD
- OS: Ubuntu server 18.04
- User: root
Đồi với những hệ thống lớn hơn thì bạn cần tùy chỉnh cấu hình theo mức độ sử dụng hệ thống.
Cài đặt ban đầu cho máy chủ
Bạn sẽ cần thực hiện vài lệnh trước khi cài đặt các tool chính.
Update các package.
apt update
Thiết lập timezone.
timedatectl set-timezone "Asia/Ho_Chi_Minh" timedatectl set-ntp true systemctl restart systemd-timesyncd
Cài đặt firewall IPtables (nếu bạn muốn).
apt install iptables-persistent netfilter-persistent -y
Cài đặt Prometheus trên Ubuntu 18
Đầu tiên bạn cần xác định phiên bản mới nhất cần sử dụng theo link này.
export RELEASE="2.20.1"
Tải và giải nén gói cài đặt.
wget https://github.com/prometheus/prometheus/releases/download/v${RELEASE}/prometheus-${RELEASE}.linux-amd64.tar.gz tar xvf prometheus-${RELEASE}.linux-amd64.tar.gz cd prometheus-${RELEASE}.linux-amd64/
Tạo user cho dịch vụ Prometheus.
groupadd --system prometheus grep prometheus /etc/group useradd -s /sbin/nologin -r -g prometheus prometheus
Tạo thư mục chứa file cấu hình Prometheus.
mkdir -p /etc/prometheus/{rules,rules.d,files_sd} /var/lib/prometheus
Chuyển các file thực thi binary vào thư mục local bin và copy thư mục consoles, file config.
cp prometheus promtool /usr/local/bin/ cp -r consoles/ console_libraries/ /etc/prometheus/ cp prometheus.yml /etc/prometheus/
Gán quyền cho user prometheus.
chown -R prometheus:prometheus /etc/prometheus/ /var/lib/prometheus/ chmod -R 775 /etc/prometheus/ /var/lib/prometheus/
Tạo file systemd cho dịch vụ prometheus.
nano /etc/systemd/system/prometheus.service
Copy nội dung dưới vào file.
[Unit] Description=Prometheus systemd service unit Wants=network-online.target After=network-online.target [Service] Type=simple User=prometheus Group=prometheus ExecReload=/bin/kill -HUP $MAINPID 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 \ --web.listen-address=0.0.0.0:9090 \ --storage.tsdb.retention.time=1y SyslogIdentifier=prometheus Restart=always [Install] WantedBy=multi-user.target
Enable và khởi động dịch vụ prometheus.
systemctl daemon-reload systemctl start prometheus systemctl enable prometheus
Bây giờ bạn có thể truy cập trên trình duyệt IP:9090
để vào giao diện chương trình prometheus. Ví dụ: http://192.168.1.10:9090
. Mặc định thì giao diện chương trình prometheus sẽ không có bất kì lớp khóa/login bảo vệ nào cả.
Cài đặt Grafana trên Ubuntu 18
Bây giờ, ta sẽ cài đặt Grafana và việc này đơn giản hơn nhiều so với cài prometheus.
Đầu tiên, bạn thêm repository của Grafana vào máy chủ.
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add
Tiếp theo, bạn update các gói phần mềm từ repository này và cài đặt Grafana.
apt-get update apt-get install grafana -y
Sau khi cài đặt xong, bạn kích hoạt và khởi động dịch vụ.
systemctl start grafana-server systemctl enable grafana-server.service
Bây giờ, bạn có thể mở trình duyệt lên và truy cập IP:3000
. Ví dụ: http://192.168.1.10:3000
. Mặc định tài khoản đăng nhập grafana là admin/admin
. Ở lần truy cập đầu tiên thì bạn sẽ phải đổi mật khẩu mới cho user admin.
Kết luận
Vậy là mình đã hướng dẫn các bạn cài đặt xong 2 công cụ Prometheus và Grafana trên server Ubuntu 18.04. Trong các bài viết tiếp theo, mình sẽ hướng dẫn các bạn những thiết lập cần thiết và cách sử dụng 2 công cụ này.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.