Docker部署PostgreSQL14.1以及postgres_exporter+prometheus+grafana監控
一、Docker軟體部署
1.構建yum源
在/etc/yum.repos.d下建立docker-ce.repo
docker-ce.repok可在下載
檢視全部倉庫中全部docker版本,並選擇特定版本安裝
[root@node1 yum.repos.d]# yum makecache Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. docker-ce-stable | 3.5 kB 00:00:00 Loading mirror speeds from cached hostfile docker-ce-stable/7/x86_64/primary_db | 70 kB 00:00:00 docker-ce-stable/7/x86_64/filelists_db | 29 kB 00:00:00 docker-ce-stable/7/x86_64/other_db | 122 kB 00:00:00 Metadata Cache Created [root@node1 yum.repos.d]# yum list |grep docker containerd.io.x86_64 1.4.12-3.1.el7 docker-ce-stable docker-ce.x86_64 3:20.10.12-3.el7 docker-ce-stable docker-ce-cli.x86_64 1:20.10.12-3.el7 docker-ce-stable docker-ce-rootless-extras.x86_64 20.10.12-3.el7 docker-ce-stable docker-ce-selinux.noarch 17.03.3.ce-1.el7 docker-ce-stable docker-scan-plugin.x86_64 0.12.0-3.el7 docker-ce-stable
2.安裝docker-ce
[root@node1 yum.repos.d]# yum install docker-ce -y
解決安裝報錯:
只需要安裝container-selinux包,就可以了
[root@localhost ~]# yum install -y container-selinux Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile - base: mirrors.bfsu.edu.cn - extras: mirrors.bfsu.edu.cn - updates: mirrors.bfsu.edu.cn Resolving Dependencies --> Running transaction check ---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed --> Processing Dependency: selinux-policy-targeted >= 3.13.1-216.el7 for package: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch --> Processing Dependency: selinux-policy-base >= 3.13.1-216.el7 for package: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch --> Processing Dependency: selinux-policy >= 3.13.1-216.el7 for package: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch --> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch --> Running transaction check
3.啟動docker
systemctl start docker systemctl enable docker systemctl status docker
二、Docker拉取PG14映象並構建PG14容器
1.拉取pg14.1映象
[root@localhost ~]# docker pull postgres:14.1 14.1: Pulling from library/postgres a2abf6c4d29d: Pull complete e1769f49f910: Pull complete 33a59cfee47c: Pull complete 461b2090c345: Pull complete 8ed8ab6290ac: Pull complete 495e42c822a0: Pull complete 18e858c71c58: Pull complete 594792c80d5f: Pull complete 794976979956: Pull complete eb5e1a73c3ca: Pull complete 6d6360292cba: Pull complete 131e916e1a28: Pull complete b84c0e55930b: Pull complete Digest: sha256:17286f7ca2590bdfdfa3afd3305d839780c33c4f9af167a45e537f22d3cafcb0 Status: Downloaded newer image for postgres:14.1 docker.io/library/postgres:14.1
2.檢視本地PG映象
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE postgres 14.1 cdbebe091601 2 days ago 374MB
3.對映目錄
建立postgresql資料庫容器的資料目錄路徑掛載到的宿主機路徑,保證容器銷燬後資料還在
[root@localhost ~]# mkdir -p /docker/postgresql/data/
4.執行PG容器
[root@localhost ~]# docker run -d --name postgresql --restart always -v /docker/postgresql/data/:/var/lib/postgresql/data -e POSTGRES_PASSWORD=123456 -p 15432:5432 postgres:14.1 09b45ee81642527ebcdf25c48a25a9e2524068562f889b8a103a915444363c78
-d 表示啟動後在後臺執行,不啟動日誌
-name 容器名 表示給容器命名
–restart always 表示如果容器死掉,會自動拉起,可以不配置
-v 宿主機路徑:容器內路徑 把容器內路徑掛載到宿主機路徑,保證容器銷燬後資料還在
-p 宿主機埠:容器埠 把宿主機埠和容器內埠進行對映
-e POSTGRES_PASSWORD=123456 設定環境變數值,這裡設定了資料庫的密碼
5.檢視啟動的容器狀態
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 09b45ee81642 postgres:14.1 "docker-entrypoint.s…" 8 seconds ago Up 6 seconds 0.0.0.0:15432->5432/tcp, :::15432->5432/tcp postgresql
(檢視所有的容器用docker ps -a)
6.進入容器檢視PG資料庫版本
[root@localhost ~]# docker exec -it postgresql bash root@09b45ee81642:/# su - postgres postgres@09b45ee81642:~$ psql psql (14.1 (Debian 14.1-1.pgdg110+1)) Type "help" for help. postgres=# select version(); version ----------------------------------------------------------------------------------------------------------------------------- PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit (1 row)
-i: 以互動模式執行容器,通常與 -t 同時使用
-t: 為容器重新分配一個偽輸入終端,通常與 -i 同時使用;
PG14的docker部署完成
三、Docker部署prometheus容器
1.搜尋prometheus映象
搜尋可用的prometheus映象,並選擇使用數量最多的
[root@localhost ~]# docker search prometheus
2.拉取prometheus映象
[root@localhost ~]# docker pull prom/prometheus Using default tag: latest latest: Pulling from prom/prometheus 3cb635b06aa2: Pull complete 34f699df6fe0: Pull complete 33d6c9635e0f: Pull complete f2af7323bed8: Pull complete c16675a6a294: Pull complete 827843f6afe6: Pull complete 3d272942eeaf: Pull complete 7e785cfa34da: Pull complete 05e324559e3b: Pull complete 170620261a59: Pull complete ec35f5996032: Pull complete 5509173eb708: Pull complete Digest: sha256:cb9817249c346d6cfadebe383ed3b3cd4c540f623db40c4ca00da2ada45259bb Status: Downloaded newer image for prom/prometheus:latest docker.io/prom/prometheus:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE postgres 14.1 cdbebe091601 2 days ago 374MB prom/prometheus latest a3d385fc29f9 6 days ago 201MB
3.開啟prometheus容器
[root@localhost ~]# docker run --name prometheus --privileged=true -di -p 9090:9090 prom/prometheus 708e0cb45bdb16c9ccc5b6d89204c361c6145f03ff7bf3e8be78f2941b707e6a [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 708e0cb45bdb prom/prometheus "/bin/prometheus --c…" 5 minutes ago Up 5 minutes 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus 09b45ee81642 postgres:14.1 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:15432->5432/tcp, :::15432->5432/tcp postgresql
4.bash報錯,可以用/bin/sh替代
[root@localhost ~]# docker exec -it prometheus bash OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "bash": executable file not found in $PATH: unknown [root@localhost ~]# docker exec -it prometheus /bin/sh /prometheus $ find / -name prometheus /bin/prometheus /etc/prometheus find: /root: Permission denied /usr/share/prometheus find: /proc/tty/driver: Permission denied /prometheus
5.拉起prometheus容器
/prometheus $ /bin/prometheus --web.listen-address="0.0.0.0:9090" --config.file="/etc/prometheus/prometheus.yml" --log.level=info ts=2021-12-24T10:17:39.879Z caller=main.go:478 level=info msg="No time or size retention was set so using the default time retention" duration=15d ts=2021-12-24T10:17:39.880Z caller=main.go:515 level=info msg="Starting Prometheus" version="(version=2.32.1, branch=HEAD, revision=41f1a8125e664985dd30674e5bdf6b683eff5d32)" ts=2021-12-24T10:17:39.880Z caller=main.go:520 level=info build_context="(go=go1.17.5, user=root@54b6dbd48b97, date=20211217-22:08:06)" ts=2021-12-24T10:17:39.880Z caller=main.go:521 level=info host_details="(Linux 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 708e0cb45bdb (none))" ts=2021-12-24T10:17:39.880Z caller=main.go:522 level=info fd_limits="(soft=1048576, hard=1048576)" ts=2021-12-24T10:17:39.880Z caller=main.go:523 level=info vm_limits="(soft=unlimited, hard=unlimited)" ts=2021-12-24T10:17:39.883Z caller=web.go:570 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090 /prometheus $ ps -ef | grep prometheus 1 nobody 0:00 /bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.console.libraries=/usr/share/prometheus/console_libraries --web.console.templates=/usr/share/prometheus/consoles /prometheus $ netstat -altp | grep 9090 tcp 0 0 localhost:58478 localhost:9090 ESTABLISHED 1/prometheus tcp 0 0 :::9090 :::* LISTEN 1/prometheus tcp 0 0 ::ffff:127.0.0.1:9090 ::ffff:127.0.0.1:58478 ESTABLISHED 1/prometheus
6.去瀏覽器驗證
我的地址為172.20.10.5,所以訪問的地址為172.20.10.5:9090
如果配置exporter的話,可以透過如下介面獲取exporter的狀態,可以點選endpoint,來檢視當前抽取到的一些指標,這些指標監控項可以在Graph介面查詢值和隨時間變化的趨勢,可以根據表示式進行處理,或者加入到grafana上邊,形成更加直觀的介面。
四、Docker部署postgres_exporter容器
1.拉取postgres_exporter
[root@localhost ~]# docker pull wrouesnel/postgres_exporter Using default tag: latest latest: Pulling from wrouesnel/postgres_exporter 45b42c59be33: Pull complete 4634a89d50c2: Pull complete fbcf7c278f83: Pull complete Digest: sha256:54bd3ba6bc39a9da2bf382667db4dc249c96e4cfc837dafe91d6cc7d362829e0 Status: Downloaded newer image for wrouesnel/postgres_exporter:latest docker.io/wrouesnel/postgres_exporter:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE postgres 14.1 cdbebe091601 2 days ago 374MB prom/prometheus latest a3d385fc29f9 6 days ago 201MB wrouesnel/postgres_exporter latest 9fe9d3d02141 10 months ago 88.7MB
2.資料庫配置
需在監控的資料庫上建立監控使用者並執行一下以下命令 。以非超級使用者身份執行時候為了能夠從pg_stat_activity和pg_stat_replication 作為非超級使用者收集指標,必須以超級使用者的身份建立功能和檢視,並分別為它們分配許可權。
postgres=# CREATE USER postgres_exporter WITH PASSWORD 'postgres_exporter123'; CREATE ROLE postgres=# ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog; ALTER ROLE postgres=# CREATE SCHEMA postgres_exporter AUTHORIZATION postgres_exporter; CREATE SCHEMA postgres=# CREATE FUNCTION postgres_exporter.f_select_pg_stat_activity() RETURNS setof pg_catalog.pg_stat_activity LANGUAGE sql SECURITY DEFINER AS $$ SELECT * from pg_catalog.pg_stat_activity; $$; CREATE FUNCTION postgres=# CREATE FUNCTION postgres_exporter.f_select_pg_stat_replication() RETURNS setof pg_catalog.pg_stat_replication LANGUAGE sql SECURITY DEFINER AS $$ SELECT * from pg_catalog.pg_stat_replication; $$; CREATE FUNCTION postgres=# CREATE VIEW postgres_exporter.pg_stat_replication AS SELECT * FROM postgres_exporter.f_select_pg_stat_replication(); CREATE VIEW postgres=# CREATE VIEW postgres_exporter.pg_stat_activity AS SELECT * FROM postgres_exporter.f_select_pg_stat_activity(); CREATE VIEW postgres=# GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter; GRANT postgres=# GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter; GRANT
修改pg_hba.conf,使能訪問資料庫,注意密碼校驗的時候password_encryption引數以及pg_authid裡密碼的加密方式。
postgres=# show password_encryption; password_encryption --------------------- scram-sha-256 (1 row) postgres=# select * from pg_authid where rolname='postgres_exporter'; -[ RECORD 1 ]--+-------------------------------------------------------------------------------------------------------------------------------------- oid | 16384 rolname | postgres_exporter rolsuper | f rolinherit | t rolcreaterole | f rolcreatedb | f rolcanlogin | t rolreplication | f rolbypassrls | f rolconnlimit | -1 rolpassword | SCRAM-SHA-256$4096:1VfD4I+8uxNHv+qDEZACCg==$b/2Wgt6zGMkHPCMk9RIN+aPci8lK0oSYc87Eu8wXfNk=:f8lituzDMMMdb6rMA/5pdiFWC4j/MuS4cAVy/awMjJA= rolvaliduntil |
pg_hba.conf加的部分如下
host all postgres_exporter 172.20.10.5/32 scram-sha-256
注意docker容器最簡模式裡可能使用不了vim或者ps,可以用如下方法解決。如果容器沒有許可權執行,可以在進入容器時候加上-u 0以root許可權進入,再執行如下操作
root@09b45ee81642:/# apt-get update Get:1 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB] Get:2 [116 kB] Get:3 http://security.debian.org/debian-security bullseye-security/main amd64 Packages [102 kB] Get:4 [39.4 kB] Get:5 [86.7 kB] Get:6 [8,183 kB] Get:7 [234 kB] Get:8 [2,592 B] Fetched 8,808 kB in 3min 55s (37.5 kB/s) Reading package lists... Done root@09b45ee81642:/# apt-get install -y vim Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libgpm2 vim-common vim-runtime xxd Suggested packages: gpm ctags vim-doc vim-scripts The following NEW packages will be installed: libgpm2 vim vim-common vim-runtime xxd 0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded. Need to get 8,174 kB of archives. After this operation, 36.9 MB of additional disk space will be used. Get:1 [192 kB] Get:2 [226 kB] Get:3 [35.6 kB] Get:4 [6,226 kB] Get:5 [1,494 kB] Fetched 8,174 kB in 7s (1,187 kB/s) root@09b45ee81642:/# apt-get install -y procps
注意:在對docker裡資料庫進行配置修改後,需要重啟生效時,建議直接將容器重啟,不僅節約時間,也避免容器內重啟庫帶來問題。例如:
[root@localhost ~]# docker restart postgresql
postgresql
3.執行容器使postgres_exporter可以抽取資料庫指標
[root@localhost ~]# docker run --name postgres_exporter --net=host -d -e DATA_SOURCE_NAME="postgresql://postgres:123456@172.20.10.5:15432/postgres?sslmode=disable" wrouesnel/postgres_exporter fdff211e4514859b29fae3d7bf5ecdf2c10083034f2254d522753f2bc20d0dca [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES be590925e24b wrouesnel/postgres_exporter "/postgres_exporter" 5 seconds ago Up 4 seconds 0.0.0.0:9187->9187/tcp, :::9187->9187/tcp postgres_exporter 708e0cb45bdb prom/prometheus "/bin/prometheus --c…" About an hour ago Up About an hour 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus 09b45ee81642 postgres:14.1 "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:15432->5432/tcp, :::15432->5432/tcp postgresql
4.修改prometheus裡的yml檔案,加上postgres_exporter部分
[root@localhost ~]# docker exec -it prometheus /bin/sh /prometheus $ cd /etc/prometheus/ /etc/prometheus $ ls console_libraries consoles prometheus.yml /etc/prometheus $ vi prometheus.yml
最下邊黃框部分是我加的postgres_exporter部分
5.重啟prometheus叢集
[root@localhost ~]# docker restart prometheus prometheus
6.瀏覽器驗證
依舊是登入到prometheus的9090埠
點選檢視metrics裡指標,部分指標如下。
五、Docker部署grafana容器
1.拉取grafana映象
[root@localhost ~]# docker pull grafana/grafana:5.1.0 5.1.0: Pulling from grafana/grafana 2a72cbf407d6: Pull complete 89f824064239: Pull complete b90280be2e0d: Pull complete Digest: sha256:a6b37f9afdd9782f1e6264acaf3b09519aad454f34ca6b0e863dd9873e11fa67 Status: Downloaded newer image for grafana/grafana:5.1.0 docker.io/grafana/grafana:5.1.0
如果下載太慢的話可以執行參考下邊操作再下載,速度會遠遠加快
[root@localhost ~]# vim /etc/docker/daemon.json [root@localhost ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["] } [root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl restart docker
2.拉起grafana容器
[root@localhost ~]# docker run --name grafana -d -p 3000:3000 grafana/grafana:5.1.0 37fa23cf597ef65a3fadfc22002b083bda6c917e0c1caadbceb54a78b92be10f [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 37fa23cf597e grafana/grafana:5.1.0 "/run.sh" 11 seconds ago Up 9 seconds 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp grafana f5e8d696c988 wrouesnel/postgres_exporter "/postgres_exporter" About an hour ago Up 45 minutes 0.0.0.0:9187->9187/tcp, :::9187->9187/tcp postgres_exporter 708e0cb45bdb prom/prometheus "/bin/prometheus --c…" 4 hours ago Up 44 minutes 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus 09b45ee81642 postgres:14.1 "docker-entrypoint.s…" 5 hours ago Up 54 minutes 0.0.0.0:15432->5432/tcp, :::15432->5432/tcp postgresql
可以看到最終有四個容器。
3.環境檢查及網頁驗證
(1).登入介面
如下是docker的程式,PostgreSQL14資料庫的容器埠為5432,對映到了宿主機15432的埠。postgres_expoeter的埠宿主機和容器對映為同樣的9187,prometheus的埠宿主機和容器對映為同樣的9090,grafana的埠宿主機和容器對映為同樣的3000。
我本地的ip為172.20.10.5,訪問172.20.10.5:3000
初始使用者名稱密碼為admin/admin
進入之後介面為
可以建立登入使用者
(2).配置資料來源
點選Data Sources配置資料來源
點選增加
(3).匯入皮膚的json檔案或者皮膚id
點選import
我這裡選擇的是填寫皮膚id
之後會出現皮膚的一些資訊,點選import
(4).驗證
然後選擇上邊的資料來源,例項,資料庫。監控介面如圖所示。
搭建完畢。
六、小結
本文章是用的postgres_exporter的docker映象搭建的,沒有實現指標的自定義。
如果想自定義監控項:
1.可以透過編寫ymal檔案,並在啟動postgres_exporter時候指定 --extend.query-path=""選項,使其根據你的檔案獲取監控項。
2.使用node_exporter,透過–collector.textfile.directory引數,指定自定義監控項的prom路徑,需要配合指令碼及crontab,將資料輸出到該目錄下,併產生.prom檔案(需要注意檔案許可權)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69990629/viewspace-2849346/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- docker部署監控Prometheus+GrafanaDockerPrometheusGrafana
- Docker部署zabbix3.2監控系統Docker
- 6.prometheus監控--監控dockerPrometheusDocker
- docker監控方案Docker
- Docker監控PrometheusDockerPrometheus
- Docker 監控實戰Docker
- Docker——11——Docker的監控——(待發)Docker
- Sentry 監控 - 私有 Docker Compose 部署與故障排除詳解Docker
- 【監控】open-falcon v0.2 部署、小米開源監控部署
- ORACLE監控之OSW部署Oracle
- Zabbix監控安裝部署
- docker-compose部署prometheus+grafana進行伺服器監控DockerPrometheusGrafana伺服器
- 效能測試之Docker監控Docker
- JD價格監控【docker版】Docker
- Docker 容器監控系統初探Docker
- Docker監控:google/cadvisorDockerGo
- zabbix系統監控部署(上)
- ganglia監控的搭建部署
- Ubuntu2404安裝以及部署dockerUbuntuDocker
- mysql 主從錯誤以及監控MySql
- 前端監控基礎篇 — Docker + Sentry 搭建前端監控系統前端Docker
- Docker 快速搭建主從 + 哨兵監控Docker
- docker08容器監控工具-WeaveScopeDocker
- Docker 之 執行狀態監控Docker
- prometheus-監控docker伺服器PrometheusDocker伺服器
- SpringBoot 監控、專案部署Spring Boot
- 初識docker以及node應用的部署Docker
- 使用 Sysdig 監控您的Docker容器Docker
- prometheus之docker監控與告警系列(一)PrometheusDocker
- prometheus之docker監控與告警系列(二)PrometheusDocker
- prometheus之docker監控與告警系列(三)PrometheusDocker
- 如何在 Ubuntu 上使用 Grafana 監控 DockerUbuntuGrafanaDocker
- Docker容器視覺化監控中心搭建Docker視覺化
- Prometheus監控系統入門與部署Prometheus
- 【Gin-API系列】部署和監控(九)API
- 部署Sentry日誌監控系統
- MySQL伺服器部署nagios監控MySql伺服器iOS
- 伺服器監控系統部署文件伺服器