How to set up prometheus on kubernetes

前置準備

  • kubernetes
  • Helm 3

加入 prometheus stack helm repo

1
2
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

建置 prometheus stack 和 grafana

抓取 default values

1
helm show values prometheus-community/kube-prometheus-stack > values.yaml

更改 values

Grafana

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# values.yaml

grafana:
enabled: true

## Grafana's primary configuration
## NOTE: values in map will be converted to ini format
## ref: http://docs.grafana.org/installation/configuration/
##
grafana.ini:
paths:
data: /var/lib/grafana/data
logs: /var/log/grafana
plugins: /var/lib/grafana/plugins
provisioning: /etc/grafana/provisioning
analytics:
check_for_updates: true
log:
mode: console
grafana_net:
url: https://grafana.net
server:
domain: gfn.seancheng.space
root_url: "https://gfn.seancheng.space"

## Enable persistence using Persistent Volume Claims
## ref: http://kubernetes.io/docs/user-guide/persistent-volumes/
##
persistence:
type: pvc
enabled: true
storageClassName: standard
accessModes:
- ReadWriteOnce
size: 10Gi
# annotations: {}
finalizers:
- kubernetes.io/pvc-protection
# subPath: ""
# existingClaim:

# Enable ingress
ingress:
## If true, Grafana Ingress will be created
##
enabled: true

## Annotations for Grafana Ingress
##
annotations:
kubernetes.io/ingress.class: nginx
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"

## Labels to be added to the Ingress
##
labels:
app: grafana

## Hostnames.
## Must be provided if Ingress is enable.
##
# hosts:
# - grafana.domain.com
hosts:
- gfn.seancheng.space

## Path for grafana ingress
path: /

## TLS configuration for grafana Ingress
## Secret must be manually created in the namespace
##
tls: []
# - secretName: grafana-general-tls
# hosts:
# - grafana.example.com

Prometheus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# values.yaml

prometheus:
enabled: true

## Prometheus StorageSpec for persistent data
## ref: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/storage.md
##
storageSpec:
## Using PersistentVolumeClaim
##
volumeClaimTemplate:
spec:
storageClassName: standard
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 50Gi

執行 helm install

1
2
3
4
5
6
ns=monitoring
name=ops
repo=prometheus-community/kube-prometheus-stack
value=values.yaml

helm --namespace=$(ns) upgrade --install $(name) $(repo) -f $(value)