一、监控基础
一、监控简介:
监控模型:
端监控
业务层监控
应用层监控
中间件监控
系统层监控
1、监控概述
web监控:打开速度、URL打开状态码、API接口可用性
业务监控:订单交易量、活跃用户量、支付量
中间件监控:数据库、redis、kafka、MQ、API等
系统层监控:CPU、内存、网卡、磁盘利用率、带宽利用率、延迟、丢包率、交换机、路由器、防火墙等基础设施监控
2、监控规划
3、监控方案
(1)Cacti:
https://www.cacti.net/
https://github.com/Cacti/cacti
用于网络性能监测
(2)Nagios:
https://www.nagios.org/
Nagios用来监视系统和网络;适合各企业的业务监控,可通过Web页面显示对象状态、日志、告警信息,
分层告警机制及自定义监控相对薄弱。
(3)SmokePing:
https://oss.oetiker.ch/smokeping/
http://blogs.studylinux.net/?p=794
用于网络性能监测
(4)Open-falcon:
https://www.open-falcon.org/
https://github.com/XiaoMi/open-falcon
小米公司开源出来的监控软件open-falcon(鹰隼),监控能力和性能较强。
(5)Nightingale夜莺:
https://n9e.didiyun.com/
一款经过大规模生产环境验证的、分布式高性能的运维监控系统,由滴滴基于open-falcon二次开发后开源出来的分布式监控系统。
(6)Zabbix:
https://www.zabbix.com/cn/
目前使用较多的开源监控软件,可横向扩展、自定义监控项、支持多种监控方式、可监控网络与服务等。
(7)商业监控解决方案:
监控宝:https://www.jiankongbao.com/
听云:https://www.tingyun.com/
(8)Prometheus:
见下
二、prometheus基础
官方文档:https://prometheus.io/docs/introduction/overview/
github地址:https://github.com/prometheus
对比zabbix
容器监控的实现方对比虚拟机或者物理机来说有较大的区别,比如容器在k8s环境中可以任意横向扩容与缩容,那么就需要监控服务能够自动对新创建的容器进行监控,当容器删除后又能够及时的从监控服务中删除,
而传统的zabbix的监控方式需要在每一个容器中安装启动agent,并且在容器自动发现注册及模板关联方面并没有比较好的实现方式。
监控组件:
k8s的早期版本基于组件heapster实现对pod和node节点的监控功能
k8s1.8版本开始使用metrics API的方式监控,并在1.11版本正式将heapster替换
后期的k8s监控主要是通过metrics Server提供核心监控指标,比如Node节点的CPU和内存使用率,其他的监控交由另外一个组件Prometheus完成。
1、prometheus简介
Prometheus是基于go语言开发的一套开源的监控、报警和时间序列数据库的组合,是由SoundCloud公司开发的开源监控系统,
Prometheus于2016年加入CNCF,是CNCF继kubernetes 后毕业的第二个项目,prometheus在容器和微服务领域中得到了广泛的应用,其特点主要如下:
使用key-value的多维度格式保存数据
数据不使用MySQL这样的传统数据库,而是使用时序数据库,目前是使用的TSDB:https://www.aliyun.com/product/hitsdb
支持第三方dashboard实现更高的图形界面,如grafana(Grafana 2.5.0版本及以上)功能组件化
不需要依赖存储,数据可以本地保存也可以远程保存
服务自动化发现
强大的数据查询语句功能(PromQL)
支持不同语言开发的客户端
官方和社区推出很多exporter:https://prometheus.io/docs/instrumenting/exporters/
2、prometheus组件
prometheus server:主服务,接受外部http请求,收集、存储与查询数据等
prometheus targets:静态收集的目标服务数据
service discovery:动态发现服务
prometheus alerting:报警通知
pushgateway:数据收集代理服务器(类似于zabbix proxy)
data visualization and export:数据可视化与数据导出(访问客户端)
3、Prometheus安装
prometheus server的监听端囗为9090
方式一:安装在k8s集群内
#在master把Prometheus运行到node节点上,提前准备数据目录
[root@k8s-node2 ~]#mkdir -p /data/prometheusdata
[root@k8s-node2 ~]#chmod 777 /data/prometheusdata/
#提前创建账户及授权
[root@k8s-master1 case]#kubectl create serviceaccount monitor -n monitoring #创建监控账号
serviceaccount/monitor created
[root@k8s-master1 case]#kubectl create clusterrolebinding monitor-clusterrolebinding -n monitoring --clusterrole=cluster-admin --serviceaccount=monitoring:monitor #对monitoring账号授权
clusterrolebinding.rbac.authorization.k8s.io/monitor-clusterrolebinding created
[root@k8s-master1 case]#kubectl get serviceaccount -n monitoring
NAME SECRETS AGE
default 1 5h57m
monitor 1 105s
#yaml部署prometheus-server
#部署配置文件
[root@k8s-master1 case]#kubectl apply -f case3-1-prometheus-cfg.yaml
configmap/prometheus-config created
[root@k8s-master1 case]#kubectl get Configmap -n monitoring
NAME DATA AGE
kube-root-ca.crt 1 6h2m
prometheus-config 1 30s
[root@k8s-master1 case]#kubectl describe configmap prometheus-config -n monitoring #查看配置
#运行Prometheus pod
[root@k8s-master1 case]#vim case3-2-prometheus-deployment.yaml
nodeName: 192.168.150.162
[root@k8s-master1 case]#kubectl apply -f case3-2-prometheus-deployment.yaml
deployment.apps/prometheus-server created
[root@k8s-master1 case]#kubectl get pod -n monitoring -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
prometheus-server-6879d8686f-54fkj 1/1 Running 0 119s 10.200.169.156 192.168.150.162 <none> <none>
#运行svc
[root@k8s-master1 case]#kubectl apply -f case3-3-prometheus-svc.yaml
service/prometheus created
[root@k8s-master1 case]#kubectl get svc -n monitoring
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
prometheus NodePort 10.100.39.105 <none> 9090:30090/TCP 14s
#验证web页面:192.168.150.162:30090
#文件解释:
case3-1-prometheus-cfg.yaml #configmap编写配置文件
case3-2-prometheus-deployment.yaml #部署Prometheus,提前创建数据目录用hostpath或nfs存储数据
case3-3-prometheus-svc.yaml #service 9090:30090
scrape_configs:
- job_name: 'kubernetes-endpoints' #job
kubernetes_sd_configs: #基于kubernetes_sd_configs实现服务发现
- role: endpoints #发现对象endpoint/node/pod/
scheme: https #当前job使用的发现协议
tls_config: #证书配置
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs: #重新修改标签
- source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: keep #保留 ;drop取反
regex: default;kubernetes;https
- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port] #源标签
action: replace #替换
target_label: __address__ #action处理后的新标签
regex: ([^:]+)(?::\d+)?;(\d+) #正则,匹配源标签
replacement: $1:$2 #匹配新标签
#修改配置后重新apply configmap-pod
[root@k8s-master1 case]#kubectl apply -f case3-1-prometheus-cfg.yaml
#重建Prometheus-pod后配置生效
[root@k8s-master1 case]#kubectl delete -f case3-2-prometheus-deployment.yaml && kubectl apply -f case3-2-prometheus-deployment.yaml
方式二:安装在k8s集群外
二进制部署/docker部署–>编辑配置文件(基于static_configs进行服务发现)–>重启服务
配置文件见博客:https://blog.csdn.net/weixin_44515412/article/details/123843092
(1)在线安装(apt或yum)
[root@k8s-harbor1 ~]#apt install prometheus -y
[root@k8s-harbor1 ~]#cd /etc/prometheus/
[root@k8s-harbor1 prometheus]#ll
total 16
drwxr-xr-x 4 root root 69 Mar 24 11:58 ./
drwxr-xr-x 104 root root 8192 Mar 24 11:58 ../
drwxr-xr-x 2 root root 29 Mar 24 11:58 console_libraries/
drwxr-xr-x 2 root root 29 Mar 24 11:58 consoles/
-rw-r--r-- 1 root root 1429 Jan 13 2020 prometheus.yml
[root@k8s-harbor1 prometheus]#cat prometheus.yml |grep -v "#" |grep -v "^$"
#验证端口:
[root@k8s-harbor1 ~]#ss -ntl |grep 9090
LISTEN 0 4096 *:9090 *:*
#验证访问:
http://192.168.150.154:9090/graph
(2)二进制安装
https://prometheus.io/download/
[root@k8s-ha1 ~]#cd /apps
[root@k8s-ha1 apps]#tar xvf prometheus-2.32.1.linux-amd64.tar.gz
[root@k8s-ha1 apps]#ln -sv /apps/prometheus-2.32.1.linux-amd64 /apps/prometheus
[root@k8s-ha1 apps]#cd prometheus
[root@k8s-ha1 prometheus]#ll
total 193448
drwxr-xr-x 4 3434 3434 132 Dec 18 06:21 ./
drwxr-xr-x 3 root root 105 Mar 24 14:35 ../
-rw-r--r-- 1 3434 3434 11357 Dec 18 06:18 LICENSE
-rw-r--r-- 1 3434 3434 3646 Dec 18 06:18 NOTICE
drwxr-xr-x 2 3434 3434 38 Dec 18 06:18 console_libraries/
drwxr-xr-x