prometheus-operator用于监控kubernetes集群
1.下载prometheus-operator文件包
https://github.com/prometheus-operator/kube-prometheus/archive/main.zip
下载完成后解压kube-prometheus-main.zip
# 解压
unzip kube-prometheus-main.zip
# 进入到manifests
cd kube-prometheus-main && cd manifests
2.准备工作替换国内镜像源
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' setup/prometheus-operator-deployment.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' prometheus-prometheus.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' alertmanager-alertmanager.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' kube-state-metrics-deployment.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' node-exporter-daemonset.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' prometheus-adapter-deployment.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' blackbox-exporter-deployment.yaml
sed -i 's#k8s.gcr.io/kube-state-metrics/kube-state-metrics#bitnami/kube-state-metrics#g' kube-state-metrics-deployment.yaml
改完如下图,k8s.gcr.io
和quay.io
国内被墙,需要替代方案,
修改prometheus和grafana的持久化方式,默认持久化方式为emptyDir
,最好修改为pvc
,参考该作者博客
修改
grafana-deployment.yaml
的grafana-storage
,创建一个pvc
替换上去
修改prometheus-prometheus.yaml
的CRD
添加PVC
存储
3. 修改promethes,alertmanager,grafana的service类型为NodePort类型
prometheus-service.yaml
[root@master1 manifests]# cat prometheus-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.28.0
prometheus: k8s
name: prometheus-k8s
namespace: monitoring
spec:
type: NodePort #新增
ports:
- name: web
port: 9090
targetPort: web
selector:
app: prometheus
app.kubernetes.io/component: prometheus
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
prometheus: k8s
sessionAffinity: ClientIP
alertmanager-service.yaml
[root@master1 manifests]# cat alertmanager-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
alertmanager: main
app.kubernetes.io/component: alert-router
app.kubernetes.io/name: alertmanager
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 0.22.2
name: alertmanager-main
namespace: monitoring
spec:
type: NodePort #新增
ports:
- name: web
port: 9093
targetPort: web
selector:
alertmanager: main
app: alertmanager
app.kubernetes.io/component: alert-router
app.kubernetes.io/name: alertmanager
app.kubernetes.io/part-of: kube-prometheus
sessionAffinity: ClientIP```
grafana-service.yaml
[root@master1 manifests]# cat grafana-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 8.0.3
name: grafana
namespace: monitoring
spec:
type: NodePort #新增
ports:
- name: http
port: 3000
targetPort: http
selector:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
4. 安装kube-prometheus和CRD
安装prometheus-operator
kubectl apply -f setup/
查看状态,到Running
再进行下一步
kubectl get pods -n monitoring
安装prometheus, alertmanager, grafana, kube-state-metrics, node-exporter等资源
kubectl apply -f .
5.访问web页面
grafana 帐号密码 admin/admin