docker运行prometheus 监控mysql
时间:2021-06-24 17:29:11
收藏:0
阅读:0
安装docker
配置阿里云Docker Yum源 [root@xingdian ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 git [root@xingdian ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 安装Docker新版本(比如Docker 2010.2): [root@xingdian ~]# yum install docker-ce-20.10.2.ce -y 或者 [root@xingdian ~]# yum install docker-ce -y 启动Docker服务 [root@xingdian ~]# systemctl enable docker [root@xingdian ~]# systemctl start docker 查看docker版本状态 [root@xingdian ~]# docker -v Docker version 19.03.12, build 48a66213fe 查看docker运行状态: [root@xingdian ~]# docker info Client: Debug Mode: false Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 3 Server Version: 19.03.12 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs 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: 3.10.0-1062.12.1.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 7.637GiB Name: xingdian ID:DW7V:VN63:7P5F:X36D:UPXA:WSXK:NVXU:FG4E:UIKY:ISIZ:AVW3:3RXT 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 Live Restore Enabled: false 方法 2:配置镜像加速器 [root@xuegod63 ~]# tee /etc/docker/daemon.json << ‘EOF‘ { "registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com" } EOF 重启 docker 服务使配置生效 [root@xuegod63 ~]# systemctl daemon-reload && systemctl restart docker
prometheus安装
下载镜像包
docker pull prom/node-exporter docker pull prom/prometheus docker pull grafana/grafana
启动node-exporter
docker run -d -p 9100:9100 -v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro" -v "/:/rootfs:ro" --net="host" prom/node-exporter
等待几秒钟,查看端口是否起来了
root@ubuntu:~# netstat -anpt Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1147/sshd tcp 0 36 192.168.91.132:22 192.168.91.1:63648 ESTABLISHED 2969/0 tcp 0 0 192.168.91.132:22 192.168.91.1:63340 ESTABLISHED 1321/1 tcp6 0 0 :::9100 :::* LISTEN 3070/node_exporter
访问url:
http://192.168.91.132:9100/metrics
效果如下:
这些都是收集到数据,有了它就可以做数据展示了
启动prometheus
新建目录prometheus,编辑配置文件prometheus.yml
mkdir /opt/prometheus cd /opt/prometheus/ vim prometheus.yml
内容如下:
global: scrape_interval: 60s evaluation_interval: 60s scrape_configs: - job_name: prometheus static_configs: - targets: [‘localhost:9090‘] labels: instance: prometheus - job_name: linux static_configs: - targets: [‘192.168.91.132:9100‘] labels: instance: localhost
注意:修改IP地址,这里的192.168.91.132就是本机地址
启动prometheus
docker run -d -p 9090:9090 -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
等待几秒钟,查看端口状态
root@ubuntu:/opt/prometheus# netstat -anpt Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1147/sshd tcp 0 36 192.168.91.132:22 192.168.91.1:63648 ESTABLISHED 2969/0 tcp 0 0 192.168.91.132:22 192.168.91.1:63340 ESTABLISHED 1321/1 tcp6 0 0 :::9100 :::* LISTEN 3070/node_exporter tcp6 0 0 :::22 :::* LISTEN 1147/sshd tcp6 0 0 :::9090 :::* LISTEN 3336/docker-proxy
访问url:
http://192.168.91.132:9090/graph
效果如下:
访问targets,url如下:
http://192.168.91.132:9090/targets
效果如下:
如果状态没有UP起来,等待一会,就会UP了
启动grafana
新建空文件夹grafana-storage,用来存储数据
mkdir /opt/grafana-storage
设置权限
chmod 777 -R /opt/grafana-storage
因为grafana用户会在这个目录写入文件,直接设置777,比较简单粗暴!
启动grafana
docker run -d -p 3000:3000 --name=grafana -v /opt/grafana-storage:/var/lib/grafana grafana/grafana
等待几秒钟,查看端口状态
root@ubuntu:/opt/prometheus# netstat -anpt Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1147/sshd tcp 0 36 192.168.91.132:22 192.168.91.1:63648 ESTABLISHED 2969/0 tcp 0 0 192.168.91.132:22 192.168.91.1:63340 ESTABLISHED 1321/1 tcp6 0 0 :::9100 :::* LISTEN 3070/node_exporter tcp6 0 0 :::22 :::* LISTEN 1147/sshd tcp6 0 0 :::3000 :::* LISTEN 3494/docker-proxy tcp6 0 0 :::9090 :::* LISTEN 3336/docker-proxy tcp6 0 0 192.168.91.132:9100 172.17.0.2:55108 ESTABLISHED 3070/node_exporter
访问url:
http://192.168.91.132:3000/
默认会先跳转到登录页面,默认的用户名和密码都是admin
登录之后,它会要求你重置密码。你还可以再输次admin密码!
密码设置完成之后,就会跳转到首页
点击Add data source,由于使用的是镜像方式,所以版本比较新。和网络上的文章展示的图片不一样!
name名字写Prometheus
type 选择Prometheus,因为数据都从它那里获取
url 输入Prometheus的ip+端口
点击下面的Save & Test,如果出现绿色的,说明ok了
mysql所在机器操作:
安装mysql
mysql的官方网站:www.mysql.com mysql最新版本:8.0 mysql使用最多:5.7 默认情况下:centos的仓库里没有mysql,去官网下载mysql仓库安装包 1.下载mysql的yum仓库 [root@22e34e653991 /]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm 2.安装mysql的yum仓库 [root@22e34e653991 /]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm 3.修改安装版本(方法一) [root@22e34e653991 /]# yum repolist all | grep mysql 查看所有关于mysql的库 [root@22e34e653991 /]# yum -y install yum-utils yum的工具包 [root@22e34e653991 /]# yum-config-manager --enable mysql57-community 启动mysql57 [root@22e34e653991 /]# yum-config-manager --disable mysql80-community 禁用mysql80 4.安装数据库 [root@22e34e653991 /]# yum -y install mysql mysql-server 修改yum仓库(方法二) systemctl start mysqld 5.查看数据库的初始密码 [root@22e34e653991 /]# grep ‘password‘ /var/log/mysqld.log 2019-07-13T15:14:31.176905Z 1 [Note] A temporary password is generated for root@localhost: k12zPB1r;2Ta 6.使用密码登陆 [root@22e34e653991 /]# mysql -u root -p‘k12zPB1r;2Ta‘ 7.修改密码 方法一: mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘Cj@123‘; 方法二: [root@22e34e653991 /]# mysqladmin -u root -p‘k12zPB1r;2Ta‘ password ‘cj@123‘ 密码:大小写有特殊字符数字
cp -ar mysqld_exporter /usr/local/bin/
chmod +x /usr/local/bin/mysqld_exporter
2.登陆 mysql 为 mysql_exporter 创建账号并授权
# 创建数据库用户。
CREATE USER ‘exporter‘@‘localhost‘ IDENTIFIED BY ‘XXXXXXXX‘ WITH MAX_USER_CONNECTIONS 3;
# 对 mysql_exporter 用户授权
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO ‘exporter‘@‘localhost‘;
exit 退出 mysql
3.创建 mysql 配置文件、运行时可免密码连接数据库:
cd mysqld_exporter-0.10.0.linux-amd64 cat my.cnf [client] user=exporter password=Abcdef123!. host=127.0.0.1
host一定要写
4.启动 mysql_exporter 客户端
nohup ./mysqld_exporter --config.my-cnf=./my.cnf &
mysqld_exporter 的监听端口是 9104
5.修改 prometheus-alertmanager-cfg.yaml 文件,添加如下
vim /opt/prometheus/prometheus.yml - job_name: ‘mysql‘ static_configs: - targets: [‘192.168.40.180:9104‘]
重启prometheus
root@ubuntu:/opt/prometheus# docker ps |grep prometheus 0fa256f85750 prom/prometheus "/bin/prometheus --c…" About an hour ago Up 29 seconds 0.0.0.0:9090->9090/tcp wizardly_kirch root@ubuntu:/opt/prometheus# docker restart 0fa256f85750
grafana 导入 mysql 监控图表
mysql-overview_rev5.json
最后就有数据啦
评论(0)