3.1 Prometheus環境搭建

 

  • 項目摘要:

本文是搭建一套prometheus環境的教程。

前期準備:準備三臺虛擬機,本文以centos7爲例。

  • 項目具體實施:
  1. 分別進入每臺虛擬機設置hostname:

# hostnamectl set-hostname prometheus.demo

# hostnamectl set-hostname agent.demo

# hostnamectl set-hostname grafana.demo

 

  1. 在每臺虛擬機的/etc/hosts文件中加入如下內容(ip改成真實的ip地址):

ip agent.demo

ip grafana.demo

ip prometheus.demo

 

  1. 每臺虛擬機同步時間(前兩條命令不一定需要):

# mount /dev/sr0/mnt

# yum install ntpdate -y

# ntpdate -u cn.pool.ntp.org

  1. 每臺禁用SELINUX

# vim /etc/selinux/config

  將SELINUX=enforcing 修改爲:SELINUX=disabled

 

  1. 每臺關閉防火牆

# systemctl stop firewalld

# systemctl disable firewalld

# iptables -F

 

  1. 安裝prometheus(登錄到prometheus主機)

下載後,解壓就能用,不需要編譯。

  1. 下載:

# wget https://github.com/prometheus/prometheus/releases/download/v2.5.0/prometheus-2.5.0.linux-amd64.tar.gz

  1. 解壓、移動/重命名:
  2. # tar xf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local

# mv /usr/local/prometheus-2.5.0.linux-amd64/ /usr/local/prometheus

# mkdir /etc/prometheus

# cp /usr/local/prometheus/prometheus.yml  /etc/prometheus/

  1. 啓動:

# /usr/local/prometheus/prometheus --config.file=" /etc/prometheus/

/prometheus.yml" &

 解釋:&:表示後臺運行

  默認端口是9090

 查看端口占用情況:

# lsof -i:9090 或者 ss -naltp | grep 9090

此時,瀏覽器輸入http://ip:9090即可訪問到prometheus的主界面:

  1. 安裝node_exporter監控遠程Linux主機(登錄到agent機器)
  1. 下載:

# wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz

  1. 解壓:# tar xf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
  2. 重命名: # mv  /usr/local/node_exporter-0.16.0.linux-amd64/ /usr/local/node_exporter
  3. 啓動:# nohup /usr/local/node_exporter/node_exporter &
  4. 查看啓動情況:# lsof -i:9100
  5. 打開瀏覽器輸入:http://ip:9100/metrics即可看到監控信息。
  6. 將該監控添加到prometheus監控軟件,登錄到prometheus機器。

修改prometheus.yml

# vim /etc/prometheus/prometheus.yml

末尾添加如下內容:

 - job_name: 'agent'

  static_configs: 

  - targets: ['ip:9100']

  # ps -aux | grep prome

  # kill -HUP 進程號

去控制檯查看。(選擇statusàtargets查看)

  1. 在agent機器添加一個mysql監控
  1. 下載:

# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz

  1. 解壓:tar xf mysqld_exporter-0.11.0.linux-amd64.tar.gz -C /usr/local/重命名:# mv /usr/local/mysqld_exporter-0.11.0.linux-amd64/ /usr/local/mysqld_exporter
  2. 安裝mysql數據庫:

參考:https://www.jianshu.com/p/fe476f511485

創建一個mysql配置文件,寫上連接的用戶名與密碼(和上面的授權的用戶名 和密碼要對應)

# vim /usr/local/mysqld_exporter/.my.cnf

[client]

user=用戶名

password=密碼

# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &

確認端口(9104)

# lsof -i:9104

瀏覽器輸入:http://ip:9104/metrics即可看到監控信息。

將該監控添加到prometheus監控軟件,登錄到prometheus機器。

編輯如下內容:

# vim /etc/prometheus/prometheus.yml

末尾添加如下內容:

 - job_name: 'agent_mysql'

  static_configs: 

  - targets: ['ip:9104']

  # kill -HUP 進程號

再次去瀏覽器頁面觀察是否添加成功。

  1. 安裝grafana
  1. 下載:# wget https://dl.grafana.com/oss/release/grafana-5.3.4-1.x86_64.rpm
  2. 安裝:# rpm -ivh grafana-5.3.4-1.x86_64.rpm
  3. 啓動:

# systemctl start grafana-server

# systemctl enable grafana-server

  1. 檢查端口: # lsof -i:3000
  2. 打開瀏覽器輸入:http://ip:3000查看是否成功運行。

至此,環境搭建完成。

資源下載地址(1份與本文內容一致的文檔和兩份講義):https://download.csdn.net/download/qq_25337221/12437489

  • 參考鏈接:

1. mysqlId安:https://blog.csdn.net/an88411980/article/details/86610043

2.prometheus安裝: https://www.cnblogs.com/fatyao/p/11007357.html

3.grafana安裝:https://grafana.com/grafana/download/5.3.4

本內容由安康學院“雨季”原創。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章