Centos7上安裝部署Prometheus + Grafana (prometheus筆記01)

一、目標

在Centos7.6上安裝部署Prometheus 2.17.1 + Grafana 6.7.2-1。(無坑版)

prometheus默認端口9090,node_exporter默認端口9100

二、平臺

[[email protected] ~]# uname -a
Linux client 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
grafana-6.7.2-1,Prometheus 2.17.1

三、前言

1.Grafana官網https://grafana.com/
2.Grafana軟件包下載頁面和安裝方法https://grafana.com/grafana/download
3.Grafana默認的端口號:3000
4.Prometheus官網https://prometheus.io
5.Prometheus和node_exporter的下載地址https://prometheus.io/download/

四、拓撲

主機名       ip                          主要安裝的軟件                角色(作用)
mycat34    192.168.73.34      prometheus、grafana      prometheus和grafana服務端(收集數據並展示)
mycat33    192.168.73.33      node_exporter                  prometheus客戶端(被收集數據的客戶端)

五、在centos7上安裝Prometheus 2.17.1

囉嗦:本大段都在服務端mycat34上安裝。

1.基本操作,關閉centos防火牆和selinux

yum install -y wget ntp curl vim net-tools
tar -zcvf /etc/yum.repos.d/yumRepo.bak /etc/yum.repos.d/*.repo
rm -rf /etc/yum.repos.d/*.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install epel-release -y
yum clean all && yum makecache
timedatectl set-timezone Asia/Shanghai
timedatectl set-local-rtc 0
sed -i 's/server 0.centos.pool.ntp.org iburst/server ntp1.aliyun.com iburst/g' /etc/ntp.conf
sed -i 's/server 1.centos.pool.ntp.org iburst/server ntp2.aliyun.com iburst/g' /etc/ntp.conf
sed -i 's/server 2.centos.pool.ntp.org iburst/server 1.centos.pool.ntp.org iburst/g' /etc/ntp.conf
sed -i 's/server 3.centos.pool.ntp.org iburst/server 2.centos.pool.ntp.org iburst/g' /etc/ntp.conf
systemctl restart ntpd
systemctl enable ntpd
systemctl stop firwalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

2.下載最新版的Prometheus 2.17.1

wget -P /usr/local/src https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz

3.解壓Prometheus
現在的Prometheus都是編譯過的,無需安裝go語言環境,直接解壓即可使用。

tar -zxvf /usr/local/src/prometheus-2.17.1.linux-amd64.tar.gz -C /usr/local

4.將Prometheus做成軟連接的形式(可選,如果你對linux不熟悉,建議跟着做下去)

ln -s /usr/local/src/prometheus-2.17.1.linux-amd64 /usr/local/prometheus

5.創建Prometheus數據存儲目錄

mkdir -p /var/lib/prometheus
chown -R prometheus /var/lib/prometheus

6.創建用於運行Prometheus的組和用戶

groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus

7.給Prometheus主目錄賦用戶Prometheus權限

chown -R prometheus:prometheus /usr/local/prometheus/

8.將Prometheus加入到系統管理程序中

cat >/etc/systemd/system/prometheus.service <<EOF

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
# --storage.tsdb.path是可選項,默認數據目錄在運行目錄的./dada目錄中
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

至此,最基本的Prometheus服務端就部署完畢了。下面開始把客戶端mycat33給添加到Prometheus的配置文件中

9.(可選)將客戶端加入到Prometheus監控中
注意:將配置文件中的ip地址改成你的被監控客戶端的ip,(node_exporter的)端口號默認是9100

cp /usr/local/prometheus/prometheus.yml{,.bak}
cat >> /usr/local/prometheus/prometheus.yml <<EOF


  - job_name: 'xnode1-mycat33'
    scrape_interval: 10s
    static_configs:
    - targets: ['192.168.73.33:9100']
      labels:
        instance: xnode1-instance
EOF

10.啓動Prometheus並設置其開機自啓
注意:我們寫的自啓程序較爲簡單,啓動成功與否不能只看systemctl start沒報錯是不行的。要看systemctl status prometheus或ss -ntl |grep 9090纔行

systemctl start prometheus.service
systemctl enable prometheus.service

11.驗證prometheus的Web頁面,prometheus默認的端口號是9090,瀏覽器輸入http://192.168.73.34:9090/

12.在prometheus的web頁面上查看主機監控狀態
在prometheus的web頁面上依次點擊 “Status”---“Targets”,你會發現只監控到prometheus服務器自己。這就對了

13.使用prometheus的web方式查看主機的監控值
就如上圖中的那個地址所寫的一樣,格式:http://prometheus服務器ip:9090/metrics 。看到的一堆數據,存在形式是【key {數量} value】

14.拿一個監控的key去用圖形方式查看(文字描述的很彆扭)

在第13部中找個已經產生數值的key,等會便於查看數據的變化情況。如【go_goroutines】
在最上面的文本框中輸入key,如【go_goroutines】,然後點【Execute】,然後點【Graph】就能看到key[go_goroutines]被繪製出的圖形了。

六、在客戶端安裝部署node_exporter

感覺這個node_exporter有點想elk中的各種監控插件

本步需要在被監控端mycat33上執行。

1.下載最新版的node_exporter(這玩意比較小,8.2M很快就下載完了)

wget -P /usr/local/src https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.0/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz

2.解壓node_exporter

tar -zxvf /usr/local/src/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /usr/local

3.(可選)創建軟連接(如果你對linux不熟悉,建議跟着繼續做)

ln -s /usr/local/node_exporter-1.0.0-rc.0.linux-amd64/ /usr/local/node_exporter

4.創建用於運行node_exporter的用戶

groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus

5.給node_exporter主目錄賦權限

chown -R prometheus:prometheus /usr/local/node_exporter/

6.將node_exporter加入到系統服務當中

cat >/usr/lib/systemd/system/node_exporter.service <<EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

7.啓動node_exporter並將其設置開機自啓

systemctl start node_exporter
systemctl enable node_exporter

8.檢查node_exporter是否已啓動
node_exporter默認的端口是9100

systemctl status node_exporter
ss -ntl |grep 9100

9.(囉嗦)將9100在防火牆中放行,或者直接關閉防火牆

systemctl stop firewalld
systemctl disable firewalld

10.在prometheus的web上檢查是否監控到了本機
在【五9】中已經修改過配置文件,將本客戶端加入到了prometheus服務器的配置文件中了。

登錄prometheus的web,依次點擊【Status---Targets】,正常的話就會看得到被監控端mycat33主機了。

七、在Grafana上展示prometheus

關於Grafana的安裝,請參考《Centos7上安裝Grafana》,Grafana默認的端口是3000。

1.瀏覽器打開Grafana主頁 http://192.168.73.34:3000,默認用戶名和密碼都是admin。

2.添加數據源
依次點開左側的齒輪狀圖標【Configuration】---【Data Source】,再新頁面中點【Add data source】

3.點一下數據源類型Prometheus

4.爲Grafana數據源prometheus添加數據源參數
Name:隨便輸入
Default:設置爲開啓狀態
URL:http://192.168.73.34:9090/ ,寫你的prometheus主頁地址。
Access:Server(default)
Scrape interval:15s ,因爲我們這是測試環境,儘量把刷新數據的時間寫小點。

點【Save & Test】 後,能彈出綠色的【Data source is working】就說明我們的prometheus數據源添加成功了。

5.在Grafana上查看默認的prometheus儀表盤。

然後點一下【Prometheus Stats】就能看到默認的儀表盤了。

------------------END--------------2020年4月16日22:58:55------------------------------------------------------

------------------最後照例送上雞湯一碗:好男兒,人窮志不缺,天道也酬勤。---------------------------------

 

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