使用Grafana、Prometheus和cAdvisor监控kubernetes中的container
创建ns
kubectl create ns monitoring
提前准备好镜像gcr.io/cadvisor/cadvisor:v0.39.3
部署 cAdvisor模式
kubectl apply -f case1-daemonset-deploy-cadvisor.yaml
[root@k8s-master 20220605]# cat case1-daemonset-deploy-cadvisor.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: cadvisor
namespace: monitoring
spec:
selector:
matchLabels:
app: cAdvisor
template:
metadata:
labels:
app: cAdvisor
spec:
tolerations: #污点容忍,忽略master的NoSchedule
- effect: NoSchedule
key: node-role.kubernetes.io/master
hostNetwork: true
restartPolicy: Always # 重启策略
containers:
- name: cadvisor
image: gcr.io/cadvisor/cadvisor:v0.39.3
imagePullPolicy: IfNotPresent # 镜像策略
ports:
- containerPort: 8080
volumeMounts:
- name: root
mountPath: /rootfs
- name: run
mountPath: /var/run
- name: sys
mountPath: /sys
- name: docker
mountPath: /var/lib/docker
volumes:
- name: root
hostPath:
path: /
- name: run
hostPath:
path: /var/run
- name: sys
hostPath:
path: /sys
- name: docker
hostPath:
path: /var/lib/docker
kubectl get pods -n moniroting查看下
先部署好prometheus,修改下prometheus.yml添加配置
- job_name: "container-cadvisor"
static_configs:
- targets: ["10.211.55.20:8080","10.211.55.21:8080","10.211.55.22:8080"]
配置热加载
curl -X POST http://10.211.55.26:9090/-/reload
访问prometheus,可以发现已经有数据了
安装Grafana
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.5.4-1.x86_64.rpm
yum install grafana-enterprise-8.5.4-1.x86_64.rpm
systemctl restart grafana-server.service
systemctl enable grafana-server.service
添加Prometheus数据源
导入14282模板
也可以使用node-exporter方式部署
[root@k8s-master]# kubectl apply -f case2-daemonset-deploy-node-exporter.yaml
[root@k8s-master]# cat case2-daemonset-deploy-node-exporter.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: monitoring
labels:
k8s-app: node-exporter
spec:
selector:
matchLabels:
k8s-app: node-exporter
template:
metadata:
labels:
k8s-app: node-exporter
spec:
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/master
containers:
- image: prom/node-exporter:v1.3.1
imagePullPolicy: IfNotPresent
name: prometheus-node-exporter
ports:
- containerPort: 9100
hostPort: 9100
protocol: TCP
name: metrics
volumeMounts:
- mountPath: /host/proc
name: proc
- mountPath: /host/sys
name: sys
- mountPath: /host
name: rootfs
args:
- --path.procfs=/host/proc
- --path.sysfs=/host/sys
- --path.rootfs=/host
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
- name: rootfs
hostPath:
path: /
hostNetwork: true
hostPID: true
kubectl get 查看
Grafana导入模版11074