在 k8s 集群中安装 Grafana

本文演示如何在 K8s 集群中安装 Grafana, 对 K8s 集群中的 CPU, Memory 进行监控。

建立一个新的 NFS 目录

建立一个共享目录

1
sudo mkdir /var/nfs/grafana -p

改变目录所有者

1
sudo chown nobody:nogroup /var/nfs/grafana

配置 nfs

1
sudo vi /etc/exports

添加如下内容

1
/var/nfs/grafana    192.168.11.0/24(rw,sync,no_subtree_check)

注意:用你实际的IP替换上面IP

保存以后执行

1
sudo exportfs -arvf
1
sudo systemctl restart nfs-kernel-server

查看列表

1
showmount -e

部署 grafana

新建名为 grafana.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
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
apiVersion: v1
kind: PersistentVolume
metadata:
name: "grafana-data-pv"
labels:
name: grafana-data-pv
release: stable
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /var/nfs/grafana
server: 192.168.11.16
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: grafana-data-pvc
namespace: ns-monitor
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
selector:
matchLabels:
name: grafana-data-pv
release: stable
---
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
app: grafana
name: grafana
namespace: ns-monitor
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: grafana
template:
metadata:
labels:
app: grafana
spec:
securityContext:
runAsUser: 0
containers:
- name: grafana
image: grafana/grafana:latest
imagePullPolicy: IfNotPresent
env:
- name: GF_AUTH_BASIC_ENABLED
value: "true"
- name: GF_AUTH_ANONYMOUS_ENABLED
value: "false"
readinessProbe:
httpGet:
path: /login
port: 3000
volumeMounts:
- mountPath: /var/lib/grafana
name: grafana-data-volume
ports:
- containerPort: 3000
protocol: TCP
volumes:
- name: grafana-data-volume
persistentVolumeClaim:
claimName: grafana-data-pvc
---
kind: Service
apiVersion: v1
metadata:
labels:
app: grafana
name: grafana-service
namespace: ns-monitor
spec:
ports:
- port: 3000
targetPort: 3000
selector:
app: grafana
type: NodePort

注意:用你实际的IP和目录替换上面IP和目录

保存以后执行

1
kubectl apply -f grafana.yaml

验证

1
kubectl get pod -n ns-monitor
1
2
3
4
5
6
NAME                          READY   STATUS    RESTARTS   AGE
grafana-66568fb745-bg2lf 1/1 Running 0 3m15s
node-exporter-hk4rv 1/1 Running 0 91m
node-exporter-jjkrc 1/1 Running 0 91m
node-exporter-qdx8d 1/1 Running 0 91m
prometheus-7fbcbcf664-2g82n 1/1 Running 0 32m
1
kubectl get svc -n ns-monitor
1
2
3
grafana-service         NodePort   10.100.110.149   <none>        3000:31004/TCP   3m43s
node-exporter-service NodePort 10.103.175.254 <none> 9100:31672/TCP 94m
prometheus-service NodePort 10.106.108.178 <none> 9090:32426/TCP 32m

然后就可以通过浏览器进行访问了

1
浏览器访问: http://主机ip:31004  默认用户名和密码:admin/admin

登陆以后改变密码

修改 prometheus配置成数据源 :

1
http://prometheus-service.ns-monitor:9090

接着,加入一个新的 Dashboard

“+” -> “Import”

把 kubernetes的Dashboard的模板导入进来

直接导入 json 文件

大功告成

本文标题:在 k8s 集群中安装 Grafana

文章作者:Morning Star

发布时间:2021年09月03日 - 17:09

最后更新:2022年01月03日 - 11:01

原始链接:https://www.mls-tech.info/microservice/k8s/k8s-install-grafana/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。