环境:
服务器 | 公网IP | 内网IP | 服务 |
---|---|---|---|
lb | 172.10.10.100 | 192.168.142.101 | node_export、cadvisor、Nginx(docker) |
web | 192.168.142.102 | node_export、cadvisor、grafana | |
server | 192.168.142.103 | node_export、cadvisor、prometheus |
就三台机器就不用ansible了。。(╯‵□′)╯︵┻━┻
192.168.142.101 lb
192.168.142.102 web
192.168.142.103 server
ssh-copy-id -i ~/.ssh/id_rsa.pub web
ssh-copy-id -i ~/.ssh/id_rsa.pub server
node_exporter.sh
#!/bin/bash
#解压并且移动到对应目录
tar xf node_exporter-0.17.0.linux-amd64.tar.gz
mv node_exporter-0.17.0.linux-amd64 /usr/local/node_exporter
#使用systemctl管理
cat <<EOF >/usr/lib/systemd/system/node_exporter.service
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/usr/local/node_exporter/node_exporter --collector.systemd --collector.systemd.unit-whitelist=(docker|kubelet|kube-proxy|flanneld).service
[Install]
WantedBy=multi-user.target
EOF
#刷新systemctl、设置开机自启动并启动node_exporter
systemctl daemon-reload
systemctl enable node_exporter
systemctl restart node_exporter
操作服务器:lb 192.168.142.101
pull.sh
#!/bin/bash
echo -e "\033[32m------1.拉取需要的镜像------\033[0m"
docker pull prom/prometheus
docker pull google/cadvisor
docker pull grafana/grafana
docker pull nginx:1.14.2
sleep 1
echo -e "\033[32m------2.启动nginx容器------\033[0m"
docker run -itd -p 80:80 --name=nginx-test nginx:1.14.2
sleep 1
echo -e "\033[32m------3.打包镜像------\033[0m"
docker save -o prom.tar.gz prom/prometheus
docker save -o cad.tar.gz google/cadvisor
docker save -o grafa.tar.gz grafana/grafana
echo -e "\033[32m------4.下载node_exporter------\033[0m"
wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
if [ -f `ls | grep node_exporter-0.17.0.linux-amd64.tar.gz` ];then
echo '下载成功'
else
echo '下载失败'
exit 0
fi
ls /root | grep *.tar.gz
sleep 1
echo -e "\033[32m------5.推送对应的镜像包、安装包以及脚本至web服务器------\033[0m"
scp node_exporter-0.17.0.linux-amd64.tar.gz web:/root
scp cad.tar.gz web:/root
scp grafa.tar.gz web:/root
scp node_exporter.sh web:/root
sleep 1
echo -e "\033[32m------6.推送对应的镜像包、安装包以及脚本至server服务器------\033[0m"
scp node_exporter-0.17.0.linux-amd64.tar.gz server:/root
scp cad.tar.gz server:/root
scp prom.tar.gz server:/root
scp node_exporter.sh server:/root
sleep 1
echo -e "\033[32m------7.执行本地、远程脚本器------\033[0m"
sh /root/node_exporter.sh
mv /root/*.tar.gz /tmp/
ssh web "sh /root/node_exporter.sh"
ssh web "mv /root/*.tar.gz /tmp/"
ssh server "sh /root/node_exporter.sh"
ssh server "mv /root/*.tar.gz /tmp/"
sleep 1
echo -e "\033[32m------8.执行本地、远程命令------\033[0m"
ps -ef |grep exporter|grep -v grep
echo -e "\033[33m-------server服务器有返回值即成功,没有即失败-----------\033[0m"
echo -e "\033[33m-------(╯‵□′)╯︵┻━┻----------华丽的分割线-----------\033[0m"
sleep 1
ssh web "ps -ef |grep exporter|grep -v grep"
echo -e "\033[33m-------server服务器有返回值即成功,没有即失败-----------\033[0m"
echo -e "\033[33m-------(╯‵□′)╯︵┻━┻----------华丽的分割线-----------\033[0m"
sleep 1
ssh server "ps -ef |grep exporter|grep -v grep"
echo -e "\033[33m-------server服务器有返回值即成功,没有即失败-----------\033[0m"
echo -e "\033[33m-------(╯‵□′)╯︵┻━┻----------华丽的分割线-----------\033[0m"
执行脚本:sh pull.sh
操作服务器:server 192.168.142.103
docker load -i /tmp/cad.tar.gz
docker load -i /tmp/prom.tar.gz
mkdir -p /data/prometheus
vim /data/prometheus/prometheus.yml
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
evaluation_interval: 15s # By default, scrape targets every 15 seconds.
rule_files:
- "rules/node.rules"
scrape_configs:
- job_name: 'Prometheus'
scrape_interval: 5s
static_configs:
- targets: ['192.168.142.103:9090']
- job_name: 'Docker'
scrape_interval: 8s
static_configs:
- targets: ['192.168.142.101:8080','192.168.142.102:8080','192.168.142.103:8080']
- job_name: 'Linux'
static_configs:
- targets: ['192.168.142.101:9100','192.168.142.102:9100','192.168.142.103:9100']
docker run -d \
--name=prometheus \
-p 9090:9090 \
-v /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
docker run -d \
-v /:/rootfs:ro \
-v /var/run:/var/run:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
-v /dev/disk/:/dev/disk:ro \
-p 8080:8080 \
-d --name=cadvisor-server \
google/cadvisor:latest
操作服务器:web 192.168.142.102
docker load -i /tmp/cad.tar.gz
docker load -i /tmp/grafa.tar.gz
#创建目录,拷贝配置文件,并授权,否则会报错
mkdir -p /data/grafana/grafana-etc && mkdir -p /data/grafana/grafana
docker run -d --name=grafana -p 3000:3000 grafana/grafana
docker cp grafana:/etc/grafana/grafana.ini /data/grafana/grafana-etc
chmod 777 /data/grafana && chmod 777 /data/grafana/grafana
#删除容器
docker rm -f grafana
#修改配置文件
vim /data/grafana/grafana-etc/grafana.ini
38行 http_port = 3000
41行 domain = 192.168.142.102
#重新挂载数据目录以及配置文件
docker run -d --name=grafana -p 3000:3000 \
-v /data/grafana/grafana:/var/lib/grafana \
-v /data/grafana/grafana-etc/grafana.ini:/etc/grafana/grafana.ini \
grafana/grafana
#也可以添加一下配置直接修改(未验证)
-e "GF_SERVER_ROOT_URL=http://grafana.server.com"
-e "GF_SECURITY_ADMIN_PASSWORD=jack2019"
docker run -d \
-v /:/rootfs:ro \
-v /var/run:/var/run:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
-v /dev/disk/:/dev/disk:ro \
-p 8080:8080 \
-d --name=cadvisor-web \
google/cadvisor:latest
#模板网址:https://grafana.com/grafana/dashboards/
#第一部分
监控容器模板
推荐ID:
Kubernetes Pods:3146
K8s Cluster Summary:8685
Cluster Monitoring for Kubernetes:10000
1. Kubernetes Deployment Statefulset Daemonset metrics:8588
Kubernetes cluster monitoring (via Prometheus):315
#第二部分
监控物理机/虚拟机(linux)模板
推荐ID:9276
监控物理机/虚拟机(windows)模板
推荐ID:10467、10171、2129
#第三部分
监控协议http/icmp/tcp/dns/模板
http监控某个网站、icmp监控某台机器、tcp监控某个端口、dns监控dns
推荐ID:9965
操作服务器:lb 192.168.142.101
mkdir -p /data/nginx/ && docker cp nginx-test:/etc/nginx/nginx.conf /data/nginx/
stream {
upstream grafana {
server 192.168.142.102:3000;
}
server {
listen 3000;
proxy_pass grafana;
}
upstream prometheus {
server 192.168.142.103:9090;
}
server {
listen 9090;
proxy_pass prometheus;
}
}
docker run -itd -p 80:80 -p 3000:3000 -p 9090:9090 \
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
--name nginx-lb nginx:1.14.2
#!/bin/bash
ng_name='nginx-lb'
nginx_t=`docker exec -it ${ng_name} /bin/bash -c 'nginx -t'| grep -o successful`
if
[[ $nginx_t == successful ]];then
docker exec -it ${ng_name} /bin/bash -c 'nginx -s reload'
echo -e "\033[33m---------------Nginx配置成功---------------\033[0m"
exit 0
else
docker exec -it ${ng_name} /bin/bash -c 'nginx -t'
echo -e "\033[33m---------------Nginx配置错误---------------\033[0m"
exit 0
fi
docker run -d \
-v /:/rootfs:ro \
-v /var/run:/var/run:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
-v /dev/disk/:/dev/disk:ro \
-p 8080:8080 \
-d --name=cadvisor-lb \
google/cadvisor:latest
192.168.142.101:3000
192.168.142.101:9090
#默认登入账号密码为admin,登入后会强制更改密码
#grafana账号密码
admin
sleep_2019
进入某块官网,选择喜欢的Docker模块
完工撒花!!!喜欢的可以关注~不定期更新技术帖子。