Prometheus 監控MySQL資料庫

2020-12-10 abcdocker運維博客

Prometheus 監控mysql容器

Prometheus

這裡我們演示中,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.03curl -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 infoClient:DebugMode:falseServer:Containers:1Running:0Paused:0Stopped:1Images:2ServerVersion:19.03.11StorageDriver: overlay2BackingFilesystem: extfsSupports d_type:trueNativeOverlayDiff:trueLoggingDriver: json-fileCgroupDriver: systemdPlugins:Volume:localNetwork: bridge host ipvlan macvlan null overlayLog: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslogSwarm: inactiveRuntimes: runcDefaultRuntime: runcInitBinary: docker-init containerd version:7ad184331fa3e55e52b890ea95e65ba581ae3429 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd init version: fec3683SecurityOptions: seccompProfile:defaultKernelVersion:5.6.7-1.el7.elrepo.x86_64OperatingSystem:CentOSLinux7(Core)OSType: linuxArchitecture: x86_64CPUs:1TotalMemory:3.846GiBName: abcdocker ID:7NVC:4GYW:CRGD:ABOX:V6T4:LYEH:MLLO:UUB3:X5ZB:QTRC:A2RW:DER4DockerRootDir:/var/lib/dockerDebugMode:falseRegistry: https://index.docker.io/v1/Labels:Experimental:falseInsecureRegistries:127.0.0.0/8RegistryMirrors: https://hjvrgh7a.mirror.aliyuncs.com/LiveRestoreEnabled:falsenode_exporter 安裝

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.0wget http://down.i4t.com/prometheus/node_exporter-1.0.0.linux-amd64.tar.gztar xf node_exporter-1.0.0.linux-amd64.tar.gz mkdir -p /usr/local/node_exportermv node_exporter /usr/local/node_exporter/node_exporter創建systemd服務

#創建prometheus用戶[root@abcdocker local]# useradd -s /sbin/nologin -m prometheus[root@abcdocker local]# id prometheusuid=1002(prometheus) gid=1002(prometheus) groups=1002(prometheus)cat >>/etc/systemd/system/node_exporter.service<<EOF[Unit]Description=node_exporterAfter=network.target[Service]Type=simpleUser=prometheusExecStart=/usr/local/node_exporter/node_exporterRestart=on-failure[Install]WantedBy=multi-user.targetEOF啟動服務

在通過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.206Zcaller=node_exporter.go:177 msg="Starting node_exporter"level=info ts=2020-06-08T15:56:43.207Zcaller=node_exporter.go:112 collector=zfs.........level=info ts=2020-06-08T15:56:43.207Zcaller=node_exporter.go:191 msg="Listening on" address=:9100level=info ts=2020-06-08T15:56:43.207Zcaller=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 summarygo_gc_duration_seconds{quantile="0"}0...go_memstats_alloc_bytes 1.256328e+06#這裡只要有數據即可上面是執行二進位node_exporter是否異常,接下來我們就退出啟動。通過systemd進行管理

systemctl daemon-reloadsystemctl enable node_exportersystemctl start node_exportersystemctl 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/mysqld_exporter安裝

項目地址:https://github.com/prometheus/mysqld_exporter

目前mysqld_exporter支持的版本為

MySQL> = 5.6MariaDB> = 10.1為了方便管理,這裡mysqld_exporter就是用docker進行運行,如果是使用二進位安裝參數基本上相同

這裡先到mysql授權mysqld_exporter用戶,mysqld_exporter實際上是通過select 查看mysql狀態獲取metric

#登錄mysql創建mysqld_exporter用戶,docker安裝就不要使用localhostCREATE 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 inset(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.1DATA_SOURCE_NAME exporter代表用戶名:abcdocker代表密碼(xxxx:3306 mysql授權地址)查看容器啟動狀態

[root@abcdocker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe7ba8c43597e registry.cn-beijing.aliyuncs.com/abcdocker/mysqld_exporter:0.12.1"/bin/mysqld_exporter"19 seconds ago Up18 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 summarygo_gc_duration_seconds{quantile="0"}0go_gc_duration_seconds{quantile="0.25"}0go_gc_duration_seconds{quantile="0.5"}0go_gc_duration_seconds{quantile="0.75"}0Prometheus Install

prometheus我們也安裝在容器中

首先創建prometheus數據存儲目錄

[root@abcdocker ~]# mkdir /data/prometheus -p#授權prometheus寫入數據[root@abcdocker ~]# chmod -R 777/data/prometheus接下來編輯prometheus配置文件

mkdir -p /etc/prometheuscat >>/etc/prometheus/prometheus.yml <<EOFglobal: 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:3306EOF#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 psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES08b7d3d2de18 registry.cn-beijing.aliyuncs.com/abcdocker/prometheus:v2.18.1"/bin/prometheus --c…"About a minute ago Up2 seconds 0.0.0.0:9090->9090/tcp prometheuse7ba8c43597e registry.cn-beijing.aliyuncs.com/abcdocker/mysqld_exporter:0.12.1"/bin/mysqld_exporter"39 minutes ago Up39 minutes 0.0.0.0:9104->9104/tcp nervous_tesla[root@abcdocker ~]# docker logs 08b7d3d2de18接下來我們就可以登錄到宿主機IP+9090查看prometheus web ui

接下來我們點擊Graph可以查看到圖表

只要有數據寫入說明prometheus沒有問題,接下來就安裝grafana

Grafana

grafana 是一個可視化麵包,有著非常漂亮的圖片和布局展示,功能齊全的度量儀錶盤和圖形化編輯器,支持Graphite、Zabbix、InfluxDB、Prometheus、OpenTSDB、Elasticasearch等作為數據源,比Prometheus自帶的圖標展示功能強大很多,更加靈活,有豐富的插件

同樣grafana使用容器進行運行

mkdir -p /data/grafana-storagechmod -R 777/data/grafana-storagedocker 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

接下來保存測試

連接正常後會有下面的截圖

接下來可以點擊BACK退出

接下來可以添加監控圖片,如果想自定義圖片可以使用下面的方法,點擊創建圖片即可

另外一種比較受歡迎的就是導入模板

接下來可以到grafana官網找合適的監控項 插件地址https://grafana.com/dashboards/

我們需要找到以下模板

node_exporter監控模板mysqld_exporter監控模板node_exporter監控模板使用最新的中文監控https://grafana.com/grafana/dashboards/8919mysqld_exporter監控模板使用 https://grafana.com/grafana/dashboards/10101

這裡導入node_exporter模板

這裡可以屬於上面整個url,或者直接輸入後面ID都可以

數據源選擇我們創建的,prometheus-1

導入完成後會出現下面的截圖

node_exporter到這裡監控完畢

相同方法導入mysqld_exporter mysqld_exporter監控模板使用 https://grafana.com/grafana/dashboards/10101

目前是監控項添加完畢,後面是添加alertmanager告警

相關焦點

  • Prometheus+Grafana+Alertmanager實現告警推送教程——圖文詳解
    Prometheus Server可以通過靜態配置管理監控目標,也可以配合使用Service Discovery的方式動態管理監控目標,並從這些監控目標中獲取數據。其次Prometheus Server需要對採集到的監控數據進行存儲,Prometheus Server本身就是一個時序資料庫,將採集到的監控數據按照時間序列的方式存儲在本地磁碟當中。
  • MySQL資料庫主從複製搭建
    本文介紹MySQL資料庫主從複製搭建。文末有主從複製步驟導圖資料庫批量初始化更多詳細操作,請參考之前的多實例的文章內容為保證數據的純淨,將資料庫中所有數據刪除,並重新初始化rm -rf /data/* mysqld --initialize-insecure --user=mysql --datadir=/data/3308/data --basedir=/application/mysql mysqld --initialize-insecure --user=mysql --datadir=/data/3309/data --basedir=/
  • 資料庫基礎:mysql主從集群搭建
    還好mysql資料庫提供了一種主從備份的機制,其實就是把主資料庫的所有的數據同時寫到備份的資料庫中。實現mysql資料庫的熱備份。 要想實現雙機的熱備,首先要了解主從資料庫伺服器的版本的需求。要實現熱備mysql的版本都高於3.2。還有一個基本的原則就是作為從資料庫的數據版本可以高於主伺服器資料庫的版本,但是不可以低於主伺服器的資料庫版本。
  • MySQL資料庫無完整備份刪庫,除了跑路還能怎麼辦?
    所以我們只能把dump文件恢復到本地資料庫。在ECS上建立一個mysql資料庫,然後就是dump文件恢復。mysqlbinlog --start-datetime=「2020-02-16 17:00:01」 --stop-datetime=「2020-02-20 17:00:00」 —database=xxxdb mysql-bin.000218 | mysql -f -u root xxxdb–database 指定了使用binlog中的哪個資料庫進行導入,否則,如果binlog中有多個庫的操作記錄
  • 記錄一次線上zabbix監控資料庫佔用硬碟過多處理
    最近有一次線上zabbix監控資料庫佔用硬碟過多,達到80%多,為了釋放硬碟空間,清理zabbix歷史數據是非常必要的,只要清理過程如下:清理腳本如下:#!/bin/bashUser="root"Passwd="123456"Date=`date -d $(date -d "-30 day" +%Y%m%d) +%s` #取30天之前的時間戳$(which mysql) -u${User} -p
  • 如何在ubuntu20.04安裝MySQL並修改資料庫密碼
    sudo apt install mysql-client-core-83、再次輸入mysql命令,提示不能連接上mysql server,由提示可知mysql服務端沒有安裝;輸入命令sudo cat /etc/mysql/debian.cnf
  • MySQL資料庫常用命令詳解
    (1)登錄MySQL資料庫用SSH客戶端連接CentOS伺服器,打開終端命令輸入窗口,在終端輸入窗口輸入命令:mysql -uroot –p 該命令用root帳號以密碼方式登錄MySQL,回車後提示輸入密碼,輸入MySQL的登錄密碼。登錄成功後,終端窗口會進入MySQL命令方式。如圖3-44所示。
  • MySQL 資料庫的安裝和密碼的設定
    MySQL資料庫是工作中經常需要使用的,因此了解資料庫的安裝及其基本的使用是非常有必要的。目前MySQL數據的安裝主要有兩個版本一個是.msi和zip壓縮包兩種形式。前者可以界面要求按步驟進行安裝,本次是以壓縮包的形式進行講解,希望對大家有用。
  • 「Mysql」資料庫主從搭建-基於docker
    :)通過Docker搭建主從伺服器首先我們需要拉取docker鏡像,我們使用5.7版本的MySQL:docker pull mysql:5.7然後使用此鏡像啟動容器,這裡需要分別啟動主從兩個容器創建Master(主資料庫):docker run -p 3339
  • MySQL雙機互備熱備自動切換
    【IT168 技術文檔】在實際的應用中,資料庫是非常重要和關鍵的一個環節。在保障資料庫安全的同時,提高應用性和縮短出故障後的恢復時間,也同等重要。特別是在一些持續性和實時性要求高的應用中,故障一小時,可能會讓你損失幾千到幾萬甚至更高。  本方案致力於資料庫實時備份,並且在故障發生後以最短的時間恢復和修復。
  • MySQL的主從備份
    資料庫的裸奔時代資料庫伺服器存在單點問題DB伺服器資源無法滿足增長的讀寫請求高峰時資料庫連接數經常超過上限>如何解決單點問題增加額外的資料庫伺服器,組建資料庫集群同一集群中的資料庫伺服器需要具有相同的數據集群中的任一伺服器宕機後,其他伺服器可以取代宕機伺服器MySQL主從複製架構
  • MySQL分支資料庫MariaDB之CentOS安裝教程
    MariaDB資料庫管理系統是MySQL的一個分支,由MySQL的創始人Michael Widenius主持開發。採用GPL授權許可 MariaDB的目的是完全兼容MySQL,包括API和命令行,在存儲引擎方面,使用XtraDB(英語:XtraDB)來代替MySQL的InnoDB。
  • 新手入門MYSQL資料庫命令大全
    一、命令行連接資料庫Windows作業系統進入CMD命令行,進入mysql.exe所在目錄,運行命令mysql.exe -h主機名 -u用戶名 -p密碼注意:參數名與值之間沒有空格 , 如:-h127.0.0.1二、資料庫命令1.
  • MySQL 資料庫的哈希表-愛可生
    數組在各個開發語言以及資料庫中都有類似的結構,類似下圖1:圖 1 展示了一個一維整數數組,數組的長度為 10,下標從 0-9, 每個下標對應不同的值。每種程式語言基本上都有數組,大部分資料庫也提供了數組或者是類似數組的結構,MySQL 也有數組,以下為 MySQL 的一維數組:mysql> select @a as "array",json_length(@a) as "array_size";+---------------------------------
  • 讓Python幫你搞定MySQL資料庫
    MySQL是常用的資料庫之一,也是面試工作必備技能之一。本文通過一個實戰,將Python與SQL語句結合起來使用,搞定MySQL資料庫。
  • MySQL資料庫的分組操作,語句拼接,列轉行操作
    本文介紹MySQL資料庫的分組操作,語句拼接,列轉行操作。select @@sql_mode;在帶有group by 子句的select中,select後的條件列(非主鍵列),要麼是group by 後面的列,要麼需要在函數中示例group by 錯誤select user,host from mysql.user
  • mysql資料庫行列矩陣調換位置(行與列調換)
    工作上的需求:根據一個表(stu_bysjytj)的數據,矩陣調換位置(即列與行調換位置),並保存至資料庫的視圖中。表(stu_bysjytj)的數據結構如下:解決方案如下:以下是mysql資料庫的具體操作代碼:解釋:id、leibie、jiangong、yuanlin
  • mysql主從配置詳細教程
    >3、關閉系統防火牆4、保證兩臺伺服器之間可以相互ping 通具體操作步驟如下:第一步:在主資料庫master 上編輯mysql配置文件,做如下操作:註:mysql安裝方式的不同會導致mysql的配置文件的位置不一樣,大家要根據自己的安裝位置來找配置文件
  • MySQL系列二 - 搭建MySQL主從集群
    124的資料庫搭建什麼的參照上一章步驟執行就行。MySQL系列一 - MySQL安裝軟體環境CentOS 7MySQL5.7虛擬機IP: 192.168.64.123 (Master) / 192.168.64.124 (Slave)環境搭建主節點配置 修改 /etc/my.cnf文件,文件中添加修改後的my.cnf重啟MySQL, 重啟後在 /usr/local/mysql
  • 選型必看:監控K8S和Docker的熱門開源工具
    本文將詳細講解六個最熱門的開源工具,專門用於容器化服務的監控和分析。Prometheus當討論開源監控解決方案時,首先想到的就是Prometheus。它在開發社區中非常流行,是CNCF的畢業項目。Prometheus最初由SoundCloud創建並開放原始碼。