前言
kubernetes集群安装完毕之后需要接入监控,本篇博客是介绍helm安装prometheus的,希望可以对大家有所帮助
一、部署
1、配置helm仓库
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
2、安装prometheus
helm -n prometheus-stack install kube-prometheus-stack prometheus-community/kube-prometheus-stac
3、查看prometheus版本
kubectl get prometheuses -n prometheus-stack
4、查看安装pod
kubectl get pods -n prometheus-stack
二、配置prometheus
1、配置自动发现规则
(1)编写自动发现配置
cat prometheus-additional.yaml
- job_name: 'kubernetes-endpoints'kubernetes_sd_configs:- role: endpointsrelabel_configs:- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]action: keepregex: true- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]action: replacetarget_label: __scheme__regex: (https?)- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]action: replacetarget_label: __metrics_path__regex: (.+)- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]action: replacetarget_label: __address__regex: ([^:]+)(?::\d+)?;(\d+)replacement: $1:$2- action: labelmapregex: __meta_kubernetes_service_label_(.+)- source_labels: [__meta_kubernetes_namespace]action: replacetarget_label: kubernetes_namespace- source_labels: [__meta_kubernetes_service_name]action: replacetarget_label: kubernetes_name- source_labels: [__meta_kubernetes_pod_name]action: replacetarget_label: kubernetes_pod_name
(2)存入集群的secret
kubectl create secret generic additional-configs --from-file=prometheus-additional.yaml -n prometheus-stack
(3)注入prometheus
kubectl edit prometheuses -n prometheus-stack
2、配置RBAC
kubectl apply -f prometheus-rbac.yml
#cat prometheus-rbac.yml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:name: prometheus-k8s
rules:- apiGroups:- ""resources:- nodes- services- endpoints- pods- nodes/proxyverbs:- get- list- watch- apiGroups:- ""resources:- configmaps- nodes/metricsverbs:- get- nonResourceURLs:- /metricsverbs:- get
3、登录prometheus网页查看
#使用port-forward映射端口
kubectl port-forward -n prometheus-stack --address 0.0.0.0 svc/kube-prometheus-stack-prometheus 9090:9090
4、应用配置服务发现自动接入prometheus
只需要添加这两行即可。
三、配置grafana
1、登录grafana
#grafana默认会安装好,映射端口登录
kubectl port-forward -n prometheus-stack --address 0.0.0.0 svc/kube-prometheus-stack-grafana 3000:80
2、导入监控模板