概述
监控指标
一个好的系统,主要监控以下内容
- 集群监控
- 节点资源利用率
- 节点数
- 运行Pods
- Pod监控
- 容器指标
- 应用程序【程序占用多少CPU、内存】
监控平台
使用普罗米修斯 【prometheus】 + Grafana 搭建监控平台
- prometheus【定时搜索被监控服务的状态】
- 开源的
- 监控、报警、数据库
- 以HTTP协议周期性抓取被监控组件状态
- 不需要复杂的集成过程,使用http接口接入即可
- Grafana
- 开源的数据分析和可视化工具
- 支持多种数据源
部署prometheus
首先需要部署一个守护进程
[root@k8s-master ~]$ cat > node-exporter.yaml << EOF
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: kube-system
labels:
k8s-app: node-exporter
spec:
template:
metadata:
labels:
k8s-app: node-exporter
spec:
containers:
- image: prom/node-exporter
name: node-exporter
ports:
- containerPort: 9100
protocol: TCP
name: http
---
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: node-exporter
name: node-exporter
namespace: kube-system
spec:
ports:
- name: http
port: 9100
nodePort: 31672
protocol: TCP
type: NodePort
selector:
k8s-app: node-exporter
EOF
然后执行下面命令
[root@k8s-master ~]$ kubectl create -f node-exporter.yaml
然后通过yaml的方式部署prometheus
文件地址https://wws.lanzous.com/ib2bLkwmbrc
密码:d93r
- configmap:定义一个configmap:存储一些配置文件【不加密】
- prometheus.deploy.yaml:部署一个deployment【包括端口号,资源限制】
- prometheus.svc.yaml:对外暴露的端口
- rbac-setup.yaml:分配一些角色的权限
先部署 rbac-setup.yaml
[root@k8s-master ~]$ kubectl create -f rbac-setup.yaml
然后分别部署
# 部署configmap
[root@k8s-master ~]$ kubectl create -f configmap.yaml
# 部署deployment
[root@k8s-master ~]$ kubectl create -f prometheus.deploy.yml
# 部署svc
[root@k8s-master ~]$ kubectl create -f prometheus.svc.yml
部署完成后,使用下面命令查看
[root@k8s-master ~]$ kubectl get pods -n kube-system
在我们部署完成后,即可看到 prometheus 的 pod了,然后通过下面命令,能够看到对应的端口
[root@k8s-master ~]$ kubectl get svc -n kube-system
通过这个,我们可以看到 prometheus 对外暴露的端口为 30003,访问页面即可对应的图形化界面
http://192.168.88.10:30003
在上面我们部署完prometheus后,我们还需要来部署grafana
[root@k8s-master ~]$ kubectl create -f grafana-deploy.yaml
然后执行完后,发现下面的问题
error: unable to recognize "grafana-deploy.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
我们需要修改如下内容
# 修改
apiVersion: apps/v1
# 添加selector
spec:
replicas: 1
selector:
matchLabels:
app: grafana
component: core
修改完成后,我们继续执行上述代码
# 创建deployment
[root@k8s-master ~]$ kubectl create -f grafana-deploy.yaml
# 创建svc
[root@k8s-master ~]$ kubectl create -f grafana-svc.yaml
# 创建 ing
[root@k8s-master ~]$ kubectl create -f grafana-ing.yaml
配置数据源
下面我们需要开始打开 Grafana,然后配置数据源,导入数据显示模板
[root@k8s-master ~]$ kubectl get svc -n kube-system