grafana高可用部署
# mysql安装
在数据库主机执行,docker-compose.yaml文件
version: '3.9'
services:
mysql:
image: docker.cnb.cool/zzppjj/docker-images/mysql:8.0.39
container_name: mysql8
restart: always
ports:
- "3306:3306"
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: "2023WqGhsd"
MYSQL_DATABASE: app_db
MYSQL_USER: app_user
MYSQL_PASSWORD: "App123456"
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
volumes:
- ./data:/var/lib/mysql
- ./logs:/var/log/mysql
- ./conf:/etc/mysql/conf.d
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-p2023WqGhsd"]
interval: 10s
timeout: 5s
retries: 10
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
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
创建数据库
docker exec -it mysql8 mysql -uroot -p'2023WqGhsd'
create database grafana;
1
2
2
将宿主机(物理机/虚拟机)上的 MySQL 提供给集群内部 Pod 使用
推荐方案Service + Endpoints(企业最常用)
这是最标准、最稳定的方法。
原理:
Pod --> Kubernetes Service --> Endpoints --> 外部 MySQL IP
1
这样你的应用只需要连接:
mysql.default.svc.cluster.local
1
不用关心真实 IP。
创建service
apiVersion: v1
kind: Service
metadata:
name: external-mysql
namespace: default
spec:
ports:
- port: 3306
targetPort: 3306
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
创建Endpoints
apiVersion: v1
kind: Endpoints
metadata:
name: external-mysql
namespace: default
subsets:
- addresses:
- ip: 192.168.51.51
ports:
- port: 3306
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 创建headless service
#先创建命名空间
kubectl create ns monitoring
#headless.yaml
apiVersion: v1
kind: Service
metadata:
name: monitoring-grafana-headless
namespace: monitoring
spec:
clusterIP: None
ports:
- name: http-web
port: 3000
protocol: TCP
targetPort: 3000
- name: alert
port: 9094
protocol: TCP
targetPort: 9094
selector:
app.kubernetes.io/instance: grafana
app.kubernetes.io/name: grafana
type: ClusterIP
# 执行安装
kubectl apply -f headless.yaml
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
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
# helm chart
replicas: 2
image:
repository: grafana/grafana
registry: "docker.1ms.run"
tag: latest
pullPolicy: IfNotPresent
defaultDashboardsTimezone: browser
grafana.ini:
unified_alerting:
enabled: true
ha_peers: 'monitoring-grafana-headless.monitoring.svc.cluster.local:9094'
alerting:
enabled: false
database:
url: 'mysql://root:2023WqGhsd@external-mysql.default.svc.cluster.local:3306/grafana'
server:
root_url: "https://grafana.zzppjj.top"
paths:
data: /var/lib/grafana/
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
#持久化部署文件
replicas: 2
image:
repository: grafana/grafana
registry: "docker.1ms.run"
tag: latest
pullPolicy: IfNotPresent
initChownData:
enabled: true
image:
repository: busybox
registry: "docker.1ms.run"
tag: "1.31.1" # 明确指定版本,与 Helm chart 默认保持一致
defaultDashboardsTimezone: browser
grafana.ini:
unified_alerting:
enabled: true
ha_peers: 'monitoring-grafana-headless.monitoring.svc.cluster.local:9094'
alerting:
enabled: false
database:
url: 'mysql://root:2023WqGhsd@external-mysql.default.svc.cluster.local:3306/grafana'
server:
root_url: "https://grafana.zzppjj.top"
paths:
data: /var/lib/grafana/
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
persistence:
enabled: true
type: pvc
storageClassName: nfs-grafana
accessModes:
- ReadWriteOnce
size: 5Gi
finalizers:
- kubernetes.io/pvc-protection
extraVolumes:
- name: plugins-dir
emptyDir: {}
extraVolumeMounts:
- name: plugins-dir
mountPath: /var/lib/grafana/plugins
env:
GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS: victoriametrics-logs-datasource
GF_UNIFIED_ALERTING_ENABLED: "true"
GF_ALERTING_ENABLED: "false"
securityContext:
runAsUser: 472
runAsGroup: 472
fsGroup: 472
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 安装
先更新helm的repo
helm repo add grafana "https://helm-charts.itboon.top/grafana" --force-update
helm repo update
1
2
2
执行安装
helm install grafana grafana/grafana -n monitoring --create-namespace -f values.yaml
1
登录
获取密码
kubectl get secret -n monitoring grafana \
-o jsonpath="{.data.admin-password}" | base64 -d
1
2
2

# 添加VictoriaLogs数据源

添加URL地址http://vlc-victoria-logs-cluster-vmauth.tools.svc.cluster.local:8427


仪表盘模版导入,地址:https://d.frps.cn/file/tools/grafana/VictoriaLogs-Dashboard.json

升级操作命令
helm upgrade --install grafana grafana/grafana -n monitoring -f values.yaml
1
# 配置访问入口
gateway.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: grafana-gateway
namespace: monitoring
spec:
gatewayClassName: istio
listeners:
- name: http
port: 80
hostname: grafana.zzppjj.top
protocol: HTTP
allowedRoutes:
namespaces:
from: Same
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
httproute.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: grafana-route
namespace: monitoring
spec:
parentRefs:
- name: grafana-gateway
namespace: monitoring
sectionName: http
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: grafana
port: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
执行
kubectl apply -f gateway.yaml
kubectl apply -f httproute.yaml
1
2
2
上次更新: 2026/05/27, 14:26:52
|