1.prometheus介绍、安装及node监控、grafana
容器监控与报警:
容器监控的实现方对比虚拟机或者物理机来说比大的区别,比如容器在k8s环境中可以任意横向扩容与缩容,那么
就需要监控服务能够自动对新创建的容器进行监控,当容器删除后又能够及时的从监控服务中删除,而传统的
zabbix的监控方式需要在每一个容器中安装启动agent,并且在容器自动发现注册方面并没有比好的实现方式
Prometheus:
k8s的早期版本基于组件heapster实现对pod和node节点的监控功能,但是从k8s 1.8版本开始使用metrics API的方
式监控,并在1.11版本 正式将heapster替换,后期的k8s监控主要是通过metrics Server提供核心监控指标,比如
Node节点的CPU和内存使用率,其他的监控交由另外一个组件Prometheus 完成。
prometheus简介:
https://prometheus.io/docs/ #官方文档
https://github.com/prometheus #github地址
Prometheus是基于go语言开发的一套开源的监控、报警和时间序列数据库的组合,是由SoundCloud公司开发的 开源监控系统,Prometheus是CNCF(Cloud Native Computing Foundation,云原生计算基金会)继kubernetes 之 后毕业的第二个项目,prometheus在容器和微服务领域中得到了广泛的应用,其特点主要如下:
6.1.2:prometheus系统架构图:
6.1.3:prometheus 二进制安装:
https://prometheus.io/download/ #官方二进制下载地址,监听端口为9090 6.1.3.1:二进制方式安装:
使用key-value的多维度格式保存数据 数据不使用MySQL这样的传统数据库,而是使用时序数据库,目前是使用的TSDB 支持第三方dashboard实现更高的图形界面,如grafana(Grafana 2.5.0版本及以上) 功能组件化 不需要依赖存储,数据可以本地保存也可以远程保存 服务自动化发现 强大的数据查询语句功(PromQL,Prometheus Query Language)
prometheus server:主服务,接受外部http请求,收集、存储与查询数据等 prometheus targets: 静态收集的目标服务数据 service discovery:动态发现服务 prometheus alerting:报警通知 pushgateway:数据收集代理服务器(类似于zabbix proxy) data visualization and export: 数据可视化与数据导出(访问客户端)
prometheus 二进制安装:
https://prometheus.io/download/ #官方二进制下载地址,监听端口为9090

安装镜像
root@master:~# docker pull prom/prometheus
运行
root@master:~# docker run --rm -p 9090:9090 prom/prometheus
下载镜像
root@master:~# docker pull grafana/grafana:6.2.2
解压
root@master:/usr/local/src# tar xf prometheus-2.13.0.linux-amd64.tar.gz
做个软连接
root@master:/usr/local/src# ln -sv /usr/local/src/prometheus-2.13.0.linux-amd64 /usr/local/prometheus
创建prometheus启动脚本:
root@master:/usr/local/prometheus# vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target
[Service]
Restart=on-failure
WorkingDirectory=/usr/local/prometheus/
ExecStart=/usr/local/prometheus/prometheus --
config.file=/usr/local/prometheus/prometheus.yml
[Install]
WantedBy=multi-user.target
启动服务
root@master:/usr/local/prometheus# systemctl start prometheus
root@master:/usr/local/prometheus# systemctl enable prometheus
查看端口
root@master:/usr/local/prometheus# ss -tnl | grep 9090
LISTEN 0 128 *:9090 *:*

访问prometheus web界面:

node exporter:
收集各k8s node节点上的监控指标数据,监听端口为9100
二进制方式安装node exporter:
解压
root@node1:/usr/local/src tar xf node_exporter-0.18.1.linux-amd64.tar.gz
root@node1:/usr/local/src ln -sv /usr/local/src/node_exporter-0.18.1.linux-amd64 /usr/local/node_exporter
启动
root@node1:/usr/local/node_exporter# ./node_exporter
访问

创建node exporter启动脚本:
root@node1:/usr/local/node_exporter# vim /etc/systemd/system/node-exporter.service
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
拷贝到另一个node节点
root@node1:/usr/local/node_exporter# scp /etc/systemd/system/node-exporter.service 192.168.200.207:/etc/systemd/system/
root@node1:/usr/local/node_exporter# systemctl start node-exporter
systemctl enable node-exporter
修改主节点
root@master:~# vim /usr/local/src/prometheus-2.13.0.linux-amd64/prometheus.yml
23 - job_name: 'prometheus'
24 static_configs:
25 - targets: ['localhost:9090']
26
27 - job_name: 'prometheus-node_exporter'
28 static_configs:
29 - targets: ['192.168.200.206:9100','192.168.200.207:9100']
重启服务
root@master:~# systemctl restart prometheus


Grafana:
https://grafana.com/docs/
调用prometheus的数据,进行更专业的可视化
安装这个自动解决依赖关系
apt --fix-broken install -y
然后安装
root@master2:~# dpkg -i grafana_6.4.2_amd64.deb
打开短裤
root@master2:~# vim /etc/grafana/grafana.ini
38 http_port = 3000
root@master2:~# systemctl restart grafana-server
查看监听端口
root@master2:~# ss -tnl | grep 3000
LISTEN 0 128 *:3000 *:*






验证图形信息:
饼图插件未安装,需要提前安装
https://grafana.com/grafana/plugins/grafana-piechart-panel
安装插件
grafana-cli plugins install grafana-piechart-panel
重启服务
root@master2:~# service grafana-server restart