docker 搭建prometheusgranfana
Posted X糊涂仙儿
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker 搭建prometheusgranfana相关的知识,希望对你有一定的参考价值。
一、docker prometheus安装
docker版本
[root@k03 ~]# docker -v
Docker version 20.10.8, build 3967b7d
下载镜像
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
访问url:9100/metrics,有数据显示
启动prome
mkdir /data/prometheus
cd /data/prometheus/
#
[root@k03 prometheus]# cat 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 #jobname唯一
static_configs: #静态发现列表
- targets: [\'192.168.1.63:9100\'] #本机ip地址,可配置多个ip:port并且以,分隔
labels: #打标签,便于归类发现
instance: localhost
执行docker run启动
docker run -d -p 9090:9090 -v /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
访问url
http://192.168.1.63:9090/graph
http://192.168.1.63:9090/targets
二、docker grafana 安装
创建granfana存储目录
mkdir -p /data/grafana-data
chmod 777 /data/grafana-data
如果目录不加权限,报错:mkdir: can\'t create directory \'/var/lib/grafana/plugins\': Permission denied
docker启动grafana
docker run -d \\
-p 3000:3000 \\
--name=grafana \\
-v /data/grafana-data:/var/lib/grafana \\
grafana/grafana
访问grafana
http://192.168.1.63:3000
admin/admin 登陆,首次登陆提示修改密码
添加数据源
添加成功
返回首页添加数据
导入监控模板
如下:
三、添加多台主机
ansible 多台主机部署node-exporter
ansible中的hosts配置,etcd和k8s
[etcd]
192.168.1.61
192.168.1.62
192.168.1.63
[k8s]
192.168.1.31
192.168.1.32
192.168.1.81
192.168.1.82
# playbook脚本用于安装docker exporter
vim node-exporter_install.yml
---
- hosts: k8s
tasks:
- name: "node-exporter install"
shell: docker pull prom/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
ansible-playbook node-exporter_install.yml
#配置文件加入主机
vim /data/prometheus/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.1.63:9100\']
labels:
instance: localhost
- job_name: 1.31
static_configs:
- targets: [\'192.168.1.31:9100\']
labels:
instance: km01
- job_name: 1.32
static_configs:
- targets: [\'192.168.1.32:9100\']
labels:
instance: km02
- job_name: 1.81
static_configs:
- targets: [\'192.168.1.81:9100\']
labels:
instance: node01
- job_name: 1.82
static_configs:
- targets: [\'192.168.1.82:9100\']
labels:
instance: node02
#重启
docker container ls
docker restart cde3c91bbf62
#通过api查询监控主机是否上线
[root@km01 ~]# curl \'http://192.168.1.63:9090/api/v1/query?query=up\' | python -m json.tool | grep -A 1 "up"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 618 100 618 0 0 237k 0 --:--:-- --:--:-- --:--:-- 301k
"__name__": "up",
"instance": "km01",
--
"__name__": "up",
"instance": "km02",
--
"__name__": "up",
"instance": "localhost",
--
"__name__": "up",
"instance": "node01",
--
"__name__": "up",
"instance": "node02",
--
"__name__": "up",
"instance": "prometheus",
------------------------
#target显示主机状态
http://192.168.1.63:9090/targets
#http api详情
https://prometheus.io/docs/prometheus/latest/querying/api/#expression-query-result-formats
-----------------------------------------
#推荐模板id
https://www.jianshu.com/p/367d52fe1171
以上是关于docker 搭建prometheusgranfana的主要内容,如果未能解决你的问题,请参考以下文章