這裡我們演示中,prometheus以及mysqld_exporter都使用容器進行運行。這裡我的伺服器配置為阿里雲2h4g
由於Prometheus及相關的exporter會隨著版本發生變化,官方推薦都是最新的版本。這裡我為了保證後期文檔可用性,將mysqld_exporter和node_exporter都傳到我的倉庫中,下面有直接下載的方法
Docker Install安裝docker版本使用19.06,使用腳本一鍵安裝
19.03版本需要最新的Linux 5.x內核支持,如果達不到升級的條件可以使用18.09的版本,具體參考docker官方文章
export VERSION=19.03
curl -fsSL "https://get.docker.com/" | bash -s -- --mirror Aliyun
所有機器配置加速源並配置docker的啟動參數使用systemd,使用systemd是官方的建議,詳見 https://kubernetes.io/docs/setup/cri/
mkdir -p /etc/docker/
cat>/etc/docker/daemon.json<<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": [
"https://fz5yth0r.mirror.aliyuncs.com",
"https://dockerhub.mirrors.nwafu.edu.cn/",
"https://mirror.ccs.tencentyun.com",
"https://docker.mirrors.ustc.edu.cn/",
"https://reg-mirror.qiniu.com",
"http://hub-mirror.c.163.com/",
"https://registry.docker-cn.com"
],
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
}
}
EOF
啟動docker,檢查狀態是否正常
systemctl enable --now docker
查看docker info
[root@abcdocker ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 2
Server Version: 19.03.11
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 5.6.7-1.el7.elrepo.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 3.846GiB
Name: abcdocker
ID: 7NVC:4GYW:CRGD:ABOX:V6T4:LYEH:MLLO:UUB3:X5ZB:QTRC:A2RW:DER4
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://hjvrgh7a.mirror.aliyuncs.com/
Live Restore Enabled: false
node_exporter主要是用於監控宿主機的信息
項目地址https://github.com/prometheus/node_exporter
官方文檔提示: node_exporter設計用於監控主機系統。不建議將其部署為Docker容器,因為它需要訪問主機系統。請注意,您要監視的任何非根安裝點都需要綁定到容器中。如果啟動容器以進行主機監視,請指定path.rootfs參數。此參數必須與host root的bind-mount中的路徑匹配。node_exporter將path.rootfs用作訪問主機文件系統的前綴。
這裡我們就不以容器進行運行,使用二進位安裝的方式
官方下載地址https://prometheus.io/download/#node_exporter
#由於官方下載可能比較慢,我將壓縮包上傳到我的站點存儲,本次演示的版本為1.0.0
wget http://down.i4t.com/prometheus/node_exporter-1.0.0.linux-amd64.tar.gz
tar xf node_exporter-1.0.0.linux-amd64.tar.gz
mkdir -p /usr/local/node_exporter
mv node_exporter /usr/local/node_exporter/node_exporter
創建systemd服務
#創建prometheus用戶
[root@abcdocker local]# useradd -s /sbin/nologin -m prometheus
[root@abcdocker local]# id prometheus
uid=1002(prometheus) gid=1002(prometheus) groups=1002(prometheus)
cat >>/etc/systemd/system/node_exporter.service<<EOF
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
啟動服務
在通過systemd啟動之前我們先測試一下node_exporter是否正常,直接./usr/local/node_exporter/node_exporter運行成功即可
[root@abcdocker ~]# cd /usr/local/node_exporter/
[root@abcdocker node_exporter]# ./node_exporter
level=info ts=2020-06-08T15:56:43.206Z caller=node_exporter.go:177 msg="Starting node_exporter"
level=info ts=2020-06-08T15:56:43.207Z caller=node_exporter.go:112 collector=zfs
...
...
...
level=info ts=2020-06-08T15:56:43.207Z caller=node_exporter.go:191 msg="Listening on" address=:9100
level=info ts=2020-06-08T15:56:43.207Z caller=tls_config.go:170 msg="TLS is disabled and it cannot be enabled on the fly." http2=false
#直接執行沒有問題,我們可以訪問測一下。 需要單獨在開通一個窗口進行curl,測試完畢以後通過ctrl -c結束node_exporter進程
[root@abcdocker node_exporter]# curl localhost:9100/metrics
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
...
go_memstats_alloc_bytes 1.256328e+06
#這裡只要有數據即可
上面是執行二進位node_exporter是否異常,接下來我們就退出啟動。通過systemd進行管理
systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
systemctl status node_exporter
#溫馨提示: 需要退出剛剛啟動的進程,否則啟動失敗
到現在我們的node_exporter二進位已經安裝完畢,如果想使用的可以使用下面的命令
docker run -d \
--net="host" \
--pid="host" \
-v "/:/host:ro,rslave" \
quay.io/prometheus/node-exporter \
--path.rootfs=/host
#這裡的命令為官方提供,官方建議使用二進位,docker安裝參數查閱官方文檔即可
https://github.com/prometheus/node_exporter/
項目地址:https://github.com/prometheus/mysqld_exporter
目前mysqld_exporter支持的版本為
MySQL> = 5.6
MariaDB> = 10.1
為了方便管理,這裡mysqld_exporter就是用docker進行運行,如果是使用二進位安裝參數基本上相同
這裡先到mysql授權mysqld_exporter用戶,mysqld_exporter實際上是通過select 查看mysql狀態獲取metric
#登錄mysql創建mysqld_exporter用戶,docker安裝就不要使用localhost
CREATE USER 'exporter'@'10.26.33.104' IDENTIFIED BY 'abcdocker..' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'10.26.33.104';
FLUSH PRIVILEGES;
#查看exporter信息
MySQL [(none)]> select user,host from mysql.user;
++-+
| user | host |
++-+
| root | 127.0.0.1 |
| exporter | 10.26.33.104 |
++-+
9 rows in set (0.00 sec)
創建mysqld_exporter容器
docker run -d \
--net="host" \
--name mysqld_exporter \
-e DATA_SOURCE_NAME="exporter:abcdocker..@(10.26.33.104:3306)/" \
registry.cn-beijing.aliyuncs.com/abcdocker/mysqld_exporter:0.12.1
DATA_SOURCE_NAME exporter代表用戶名:abcdocker代表密碼(xxxx:3306 mysql授權地址)
查看容器啟動狀態
[root@abcdocker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7ba8c43597e registry.cn-beijing.aliyuncs.com/abcdocker/mysqld_exporter:0.12.1 "/bin/mysqld_exporter" 19 seconds ago Up 18 seconds 0.0.0.0:9104->9104/tcp nervous_tesla
#這裡我們看到已經獲取到metric
[root@abcdocker ~]# curl 10.26.33.104:9104/metrics
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
prometheus我們也安裝在容器中
首先創建prometheus數據存儲目錄
[root@abcdocker ~]# mkdir /data/prometheus -p
#授權prometheus寫入數據
[root@abcdocker ~]# chmod -R 777 /data/prometheus
接下來編輯prometheus配置文件
mkdir -p /etc/prometheus
cat >>/etc/prometheus/prometheus.yml <<EOF
global:
scrape_interval: 20s #表示prometheus抓取指標數據的頻率,默認是15s,我們可以覆蓋這個值
evaluation_interval: 20s #用來控制評估規則的頻率,prometheus使用規則產生新的時間序列數據或者產生警報
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['10.26.33.104:9090'] #mertics接口地址,相當於exporter地址
labels:
instance: prometheus
- job_name: linux #任務名稱
static_configs:
- targets: ['10.26.33.104:9100']
labels:
instance: localhost
- job_name: mysql
static_configs:
- targets: ['10.26.33.104:9104']
labels:
instance: localhost:3306
EOF
#targets的地址建議寫內網IP,寫localhost因為容器不通,localhost會讓prometheus以為是自己的埠,造成不通信的問題
創建prometheus容器
docker run -d \
-p 9090:9090 \
--restart=always \
--name prometheus \
-v /data/prometheus:/data/prometheus \
-v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
registry.cn-beijing.aliyuncs.com/abcdocker/prometheus:v2.18.1 \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/data/prometheus \ #數據存儲目錄
--web.enable-admin-api \ #動態更新
--web.enable-lifecycle \
--storage.tsdb.retention.time=30d #數據保留天數
查看prometheus容器狀態
[root@abcdocker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
08b7d3d2de18 registry.cn-beijing.aliyuncs.com/abcdocker/prometheus:v2.18.1 "/bin/prometheus --c…" About a minute ago Up 2 seconds 0.0.0.0:9090->9090/tcp prometheus
e7ba8c43597e registry.cn-beijing.aliyuncs.com/abcdocker/mysqld_exporter:0.12.1 "/bin/mysqld_exporter" 39 minutes ago Up 39 minutes 0.0.0.0:9104->9104/tcp nervous_tesla
[root@abcdocker ~]# docker logs 08b7d3d2de18
接下來我們就可以登錄到宿主機IP+9090查看prometheus web ui
接下來我們點擊Graph可以查看到圖表
只要有數據寫入說明prometheus沒有問題,接下來就安裝grafana
Grafanagrafana 是一個可視化麵包,有著非常漂亮的圖片和布局展示,功能齊全的度量儀錶盤和圖形化編輯器,支持Graphite、Zabbix、InfluxDB、Prometheus、OpenTSDB、Elasticasearch等作為數據源,比Prometheus自帶的圖標展示功能強大很多,更加靈活,有豐富的插件
同樣grafana使用容器進行運行
mkdir -p /data/grafana-storage
chmod -R 777 /data/grafana-storage
docker run -d \
-p 3000:3000 \
--name=grafana \
-v /data/grafana-storage:/var/lib/grafana \
registry.cn-beijing.aliyuncs.com/abcdocker/grafana:7.0.3
啟動完畢後我們可以到宿主機IP+3000查看服務
默認用戶名: admin
默認密碼: admin
在啟動grafana的時候可以設置密碼,後面修改也可以的。
第一次登陸會提示我們修改密碼
接下來配置prometheus源
選擇添加prometheus源
這裡只需要添加一個prometheus的地址就可以了,同樣建議使用內網IP
image_1eaafq81b44jl2q1npq94e1hjd3k.png-408.4kB接下來保存測試
連接正常後會有下面的截圖
接下來可以點擊BACK退出
接下來可以添加監控圖片,如果想自定義圖片可以使用下面的方法,點擊創建圖片即可
另外一種比較受歡迎的就是導入模板
接下來可以到grafana官網找合適的監控項
插件地址https://grafana.com/dashboards/
我們需要找到以下模板
node_exporter監控模板
mysqld_exporter監控模板
node_exporter監控模板使用最新的中文監控https://grafana.com/grafana/dashboards/8919
mysqld_exporter監控模板使用 https://grafana.com/grafana/dashboards/10101
這裡導入node_exporter模板
這裡可以屬於上面整個url,或者直接輸入後面ID都可以
image_1eaah0fp61lkk1e8h1b641p113136f.png-224kB數據源選擇我們創建的,prometheus-1
導入完成後會出現下面的截圖
node_exporter到這裡監控完畢
相同方法導入mysqld_exporter
mysqld_exporter監控模板使用 https://grafana.com/grafana/dashboards/10101
目前是監控項添加完畢,後面是添加alertmanager告警
原文:https://i4t.com/4741.html