K8S部署Prometheus+Grafana监控集群

满堂花醉三千客,一剑霜寒十四洲。这篇文章主要讲述K8S部署Prometheus+Grafana监控集群相关的知识,希望能为你提供帮助。
利用Prometheus+Grafana可以轻松的搭建K8S集群监控系统,这套系统事实上也作为K8S监控的标准,本文借鉴了网上大量的文章资料并在他们的基础上总结所成。
说明: 文章中所使用的均为CentOS 7 操作系统
一. NFS文件服务器准备,用以存储Promethues和Grafana数据
1.安装NFS服务器(192.168.0.2)

yum -y install nfs-utils
systemctl enable nfs#开启自启动
systemctl start nfs#启动nfs服务

2.创建共享目录
mkdir -p /data/nfs/promethues,grafana,grafana
chown -R 65534.65534 /data/nfs/promethues#这里非常重要,否则promethues无法启动,即便是设置了777权限也会报错
chmod 777 /data/nfs/promethues,grafana,grafana#这里需要打开所有用户可读写

【K8S部署Prometheus+Grafana监控集群】3.设置NFS共享
echo "/data/nfs *(rw,no_root_squash,async,insecure)"> > /etc/exports
systemctl restart nfs

4.查看共享
showmount -e 127.0.0.1

[root@nfsserv ~]# showmount -e 127.0.0.1

Export list for 127.0.0.1:

/data/nfs *


二.安装node-exporter
1.node-exporter.yaml
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: kube-system
labels:
k8s-app: node-exporter
spec:
selector:
matchLabels:
k8s-app: node-exporter
template:
metadata:
labels:
k8s-app: node-exporter
spec:
tolerations:
- key: "disk"
operator: "Equal"
value: "ssd"
effect: "NoSchedule"
- key: "node-role.kubernetes.io/master"
operator: "Equal"
value: "true"
effect: "NoSchedule"
- key: "esnode"
operator: "Equal"
value: "es"
effect: "NoSchedule"
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

kubectl apply -f node-exporter.yaml



三.安装Promethues
1.创建promethues-pvc.yaml
apiVersion: v1
kind: Namespace
metadata:
name: kube-system

---

apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv-prometheus
labels:
pv: nfs-pv-prometheus
annotations:
volume.beta.kubernetes.io/mount-options: "noatime,nodiratime,noresvport,nolock,proto=udp,rsize=1048576,wsize=1048576,hard"
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /data/nfs/prometheus
server: 192.168.0.2

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nfs-pvc-prometheus
namespace: kube-system
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Gi
selector:
matchLabels:
pv: nfs-pv-prometheus

2.创建promethues-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: kube-system
data:
prometheus.yml: |
global:
scrape_interval:15s
evaluation_interval: 15s
scrape_configs:

- job_name: prometheus
honor_timestamps: true
scrape_interval: 15s
scrape_timeout: 15s
scheme: http
metrics_path: /metrics
static_configs:
- targets:
- localhost:9090

- job_name: kubernetes-apiservers
kubernetes_sd_configs:
- role: endpoints
scheme: https
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
regex: default; kubernetes; https
- job_name: kubernetes-nodes
kubernetes_sd_configs:
- role: node
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /api/v1/nodes/$1/proxy/metrics

- job_name: kubernetes-cadvisor
kubernetes_sd_configs:
- role: node
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /api/v1/nodes/$1/proxy/metrics/cadvisor

- job_name: kubernetes-service-endpoints
kubernetes_sd_configs:
- role: endpoints
relabel_configs:
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
action: replace
target_label: __scheme__
regex: (https?)
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: ([^:]+)(?::\\d+)?; (\\d+)
replacement: $1:$2
- action: labelmap
regex: __meta_kubernetes_service_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_service_name]
action: replace
target_label: kubernetes_name

- job_name: kubernetes-services
kubernetes_sd_configs:
- role: service
metrics_path: /probe
params:
module: [http_2xx]
relabel_configs:
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_probe]
action: keep
regex: true
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: blackbox-exporter.example.com:9115
- source_labels: [__param_target]
target_label: instance
- action: labelmap
regex: __meta_kubernetes_service_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_service_name]
target_label: kubernetes_name

- job_name: kubernetes-ingresses
kubernetes_sd_configs:
- role: ingress
relabel_configs:
- source_labels: [__meta_kubernetes_ingress_annotation_prometheus_io_probe]
action: keep
regex: true
- source_labels: [__meta_kubernetes_ingress_scheme,__address__,__meta_kubernetes_ingress_path]
regex: (.+); (.+); (.+)
replacement: $1://$2$3
target_label: __param_target
- target_label: __address__
replacement: blackbox-exporter.example.com:9115
- source_labels: [__param_target]
target_label: instance
- action: labelmap
regex: __meta_kubernetes_ingress_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_ingress_name]
target_label: kubernetes_name

- job_name: kubernetes-pods
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: ([^:]+)(?::\\d+)?; (\\d+)
replacement: $1:$2
target_label: __address__
- action: labelmap
regex: __meta_kubernetes_pod_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: kubernetes_pod_name

3.创建promethues-rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/proxy
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources:
- configmaps
verbs: ["get"]
- apiGroups:
- extensions
resources:
- ingresses
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: kube-system

4.创建promethues-deploy.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
name: prometheus-deployment
name: prometheus
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
containers:
- image: prom/prometheus:v2.27.1
name: prometheus
command:
- "/bin/prometheus"
args:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--storage.tsdb.path=/prometheus"
- "--storage.tsdb.retention=24h"
- "--web.enable-admin-api"
- "--web.enable-lifecycle"
ports:
- containerPort: 9090
protocol: TCP
volumeMounts:
- mountPath: "/prometheus"
name: data
- mountPath: "/etc/prometheus"
name: config-volume
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 500m
memory: 1024Mi
serviceAccountName: prometheus
volumes:
- name: data
persistentVolumeClaim:
claimName: nfs-pvc-prometheus
#需要chown 65534.65534 目录
- name: config-volume
configMap:
name: prometheus-config

5.创建promethues-svc.yaml
---
kind: Service
apiVersion: v1
metadata:
labels:
app: prometheus
name: prometheus
namespace: kube-system
spec:
type: NodePort
ports:
- port: 9090
targetPort: 9090
nodePort: 30003
selector:
app: prometheus

四 安装Grafana
1.创建grafana存储以及插件的存储grafana-pvc.yaml
apiVersion: v1
kind: Namespace
metadata:
name: kube-system

---

apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv-grafana
labels:
pv: nfs-pv-grafana
annotations:
volume.beta.kubernetes.io/mount-options: "noatime,nodiratime,noresvport,nolock,proto=udp,rsize=1048576,wsize=1048576,hard"
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /data/nfs/grafana
server: 192.168.0.2

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nfs-pvc-grafana
namespace: kube-system
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Gi
selector:
matchLabels:
pv: nfs-pv-grafana
---

apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv-grafana-plugin
labels:
pv: nfs-pv-grafana-plugin
annotations:
volume.beta.kubernetes.io/mount-options: "noatime,nodiratime,noresvport,nolock,proto=udp,rsize=1048576,wsize=1048576,hard"
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /data/nfs/grafana-plugin
server: 192.168.0.2

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nfs-pvc-grafana-plugin
namespace: kube-system
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Gi
selector:
matchLabels:
pv: nfs-pv-grafana-plugin

2.创建grafana-configmap.yaml (这里整合了grafana的datasource,dashboard)
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-datasources
namespace: kube-system
data:
data_source.yaml: |

"apiVersion": 1,
"datasources": [

"access": "proxy",
"editable": false,
"name": "Prometheus",
"orgId": 1,
"type": "prometheus",
"url": "http://prometheus:9090",
"version": 1

]


---
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-dashboards
namespace: kube-system
data:
dashboards.yaml: |-

"apiVersion": 1,
"providers": [

"folder": "Default",
"name": "0",
"options":
"path": "/grafana-dashboard-definitions/0"
,
"orgId": 1,
"type": "file"

]


---
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-k8s-pod-resource
namespace: kube-system
data:
Kubernetes-Pod-Resources.json: |

"annotations":
"list": [

"builtIn": 1,
"datasource": "-- Grafana --",
"enable":

    推荐阅读