为了搭建一个基于prometheus和grafana的基础监控环境,用于应用监控的预研,找到《【监控】Prometheus+Grafana监控简介》一文,学习后进行了练习总结,本文补充原文后半部分例子的搭建步骤,补充内容包括:
学习并总结了一个docker工程docker-prometheus-scaffold,欢迎指导!
导出grafana.ini
假设工作目录为 ~/Develop/prometheus/docker
原文中“还有Grafana的配置(如果找不到配置,或者容器无法将配置映射出来,那么直接进入容器拷贝一份出来即可)”,这一句话的操作步骤:
# 从Grafana容器中导出grafana.ini
$ docker exec -it <Grafana-container-ID> cat /etc/grafana/grafana.ini > grafana.ini
# 将其放到宿主机目录,并映射给Grafana容器
$ mkdir -p ~/Develop/prometheus/docker/grafana/config
$ mv grafana.ini ~/Develop/prometheus/docker/grafana/config
需要调整的内容如下(其他部分保持不变)
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url properties.
type = mysql
host = mysql:3306
name = grafana
user = grafana
password =grafana
url = mysql://grafana:grafana@mysql:3306/grafana
[session]
# Either "memory", "file", "redis", "mysql", "postgres", default is "file"
#provider = file
provider = mysql
provider_config = grafana:grafana@tcp(mysql:3306)/grafana
mysql容器创建成功,同时自动创建grafana库和用户
参考自《如何让docker中的mysql启动时自动执行sql语句》
step1:准备目录和初始脚本
# 存放mysql容器构建时自动运行的shell脚本
mkdir -p ~/Develop/prometheus/docker/mysql/auto
# 存放初始SQL脚本
mkdir -p ~/Develop/prometheus/docker/mysql/init
touch ~/Develop/prometheus/docker/mysql/auto/init.sh
touch ~/Develop/prometheus/docker/mysql/init/init_grafana.sql
touch ~/Develop/prometheus/docker/mysql/init/check_grafana.sql
step2:init.sh内容,这个脚本目的在于控制sql执行顺序
#!/bin/bash
mysql -uroot -p$MYSQL_ROOT_PASSWORD << EOF
source $INIT_SQL_PATH/$INIT_SQL_0;
source $INIT_SQL_PATH/$INIT_SQL_1;
step3:init_grafana.sql内容,创建grafanak库和用户
create database grafana DEFAULT CHARACTER SET utf8mb4 ;
GRANT ALL ON grafana.* TO grafana@'%' IDENTIFIED BY 'grafana' WITH GRANT OPTION ;
step4:check_grafana.sql内容 ,这个check脚本只是为了演示如何指定SQL的执行顺序,也可以都在一个里面
-- 检查有没有grafana用户的表空间
use mysql ;
select Host,User from user ;
step5:修改docker-compose.yml,修改db部分即可,... 的地方保持不动
...
db:
image: mysql:5.7
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=root # mysql的root用户秘密
- INIT_SQL_PATH=/usr/local/init # mysql容器构建好后,执行init.sh脚本时会到这个目录找需要执行的sql脚本
- INIT_SQL_0=init_grafana.sql # 需要初始化的脚本,初始化grafana的库和用户并赋权
- INIT_SQL_1=check_grafana.sql # 需要初始化的脚本,检查init_grafana.sql的执行结果
hostname: mysql
restart: always
volumes:
- ~/Develop/prometheus/docker/mysql/auto:/docker-entrypoint-initdb.d # 挂载mysql容器构建完成后自动执行的脚本
- ~/Develop/prometheus/docker/mysql/init:/usr/local/init # 挂载初始化sql脚本到容器
- ~/Develop/prometheus/docker/mysql/config:/etc/mysql
- ~/Develop/prometheus/docker/mysql/data:/var/lib/mysql
- ~/Develop/prometheus/docker/mysql/log:/var/log/mysql
ports:
- "3306:3306"
expose:
- "3306"
networks:
- monitor
...
执行docker-compose up 启动后,做如下检查,判断是否成功:
查看grafana容器日志:
$ docker logs grafana
应该有
t=2020-05-09T16:42:53+0000 lvl=info msg="HTTP Server Listen" logger=http.server address=0.0.0.0:3000 protocol=http subUrl= socket=
查看prometheus容器日志:
$ docker logs prometheus
应该有
level=info ts=2020-05-09T16:42:31.355Z caller=main.go:623 msg="Server is ready to receive web requests."
检查一下MySQL数据库中是否有Grafana资源
$ docker exec -it mysql /bin/bash
> mysql -uroot -proot
> show databases ;
能看见grafana库,成功!