0%

k8s部署kube-prometheus

kube-prometheus是coreos专门为kubernetes封装的一套高可用的监控预警,方便用户直接安装使用。

Prometheus Operator官网

kube-prometheus GitHub地址

安装

下载源码

1
git clone https://github.com/prometheus-operator/kube-prometheus.git

部署kube-prometheus

1
2
3
4
kubectl create -f manifests/setup

until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done
kubectl create -f manifests/

部署后kubectl -n monitoring get all

遇到的问题

官方镜像下载异常

解决办法:替换镜像地址

  • prometheus-adapter的镜像地址由k8s.gcr.io/prometheus-adapter/prometheus-adapter:v0.9.1改为registry.cn-beijing.aliyuncs.com/zhaohongye/prometheus-adapter:v0.9.1
  • kube-state-metrics的镜像地址由k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.3.0改为registry.cn-hangzhou.aliyuncs.com/google_containers/kube-state-metrics:v1.9.5,由于阿里镜像版本低,最终改为registry.cn-beijing.aliyuncs.com/zhaohongye/kube-state-metrics:v2.3.0

访问服务

Prometheus

port-forward

1
kubectl --namespace monitoring port-forward svc/prometheus-k8s 9090

ingress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 由于默认svc是配置的9090指向9090,需再创建svc,80指向9090
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.32.1
name: ops-prometheus-k8s
namespace: monitoring
spec:
ports:
- name: web
port: 80
protocol: TCP
targetPort: 9090
selector:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
type: ClusterIP
status:
loadBalancer: {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ops-prometheus.zhaohongye.com
namespace: monitoring
spec:
rules:
- host: ops-prometheus.zhaohongye.com
http:
paths:
- backend:
service:
name: prometheus-k8s
port:
number: 9090
path: /
pathType: ImplementationSpecific
tls:
- hosts:
- ops-prometheus.zhaohongye.com
secretName: izuche-2022-https

Alertmanager

Port-forward

1
kubectl --namespace monitoring port-forward svc/alertmanager-main 9093

ingress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ops-alert.zhaohongye.com
namespace: monitoring
spec:
rules:
- host: ops-alert.zhaohongye.com
http:
paths:
- backend:
service:
name: ops-alertmanager-main
port:
number: 80
path: /
pathType: ImplementationSpecific
tls:
- hosts:
- oops-alert.zhaohongye.com
secretName: izuche-2022-https
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ops-alert.zhaohongye.com
namespace: monitoring
spec:
rules:
- host: ops-alert.zhaohongye.com
http:
paths:
- backend:
service:
name: ops-alertmanager-main
port:
number: 80
path: /
pathType: ImplementationSpecific
tls:
- hosts:
- oops-alert.zhaohongye.com
secretName: izuche-2022-https

Grafana

Port-forward

1
kubectl --namespace monitoring port-forward svc/grafana 3000

ingress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.3.3
name: ops-grafana
namespace: monitoring
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 3000
selector:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ops-grafana.zhaohongye.com
namespace: monitoring
spec:
rules:
- host: ops-grafana.zhaohongye.com
http:
paths:
- backend:
service:
name: ops-grafana
port:
number: 80
path: /
pathType: ImplementationSpecific
tls:
- hosts:
- ops-grafana.zhaohongye.com
secretName: izuche-2022-https

grafana导入Dashboard

https://grafana.com/grafana/dashboards/13105