Kubernetes 1.13.3 部署 Prometheus+Grafana-7.5.2(最新版本踩坑)

本教程直接在 Kubernetes 1.13.3 版本上安装 Prometheus 和 Grafana-7.5.2,至于它们的原理和概念就不再赘述,这里就直接开始操作。

Git 下载相关 YAML 文件

[root@k8s-master01 ~]# git clone [email protected]:MagicBinary/kubernetes-prometheus.git

部署 Prometheus 组件

官方文档:https://prometheus.io/docs/introduction/overview/

[root@k8s-master01 kubernetes-prometheus]# ls
grafana  prometheus  README.md
[root@k8s-master01 kubernetes-prometheus]# kubectl apply -f prometheus/
configmap/prometheus-config created
daemonset.extensions/node-exporter created
service/node-exporter created
deployment.apps/prometheus created
service/prometheus created
clusterrole.rbac.authorization.k8s.io/prometheus created
serviceaccount/prometheus created
clusterrolebinding.rbac.authorization.k8s.io/prometheus created

image-20210719154846727

查看 Pod 以及 SVC 状态情况

[root@k8s-master01 kubernetes-prometheus]# kubectl get pod -n kube-system -o wide

image-20210719155033464

[root@k8s-master01 kubernetes-prometheus]# kubectl get svc -n kube-system -o wide

image-20210719155133702

浏览器访问 node-exporter

http://任意节点 IP 地址:31672/metrics

image-20210719155304631

浏览器访问 Prometheus

http://任意节点 IP 地址:30003/targets

image-20210719155547962

由于 YAML 文件里面指定镜像版本为latest,所以这里都是拉取最新版本的

image-20210719155833141

部署 Grafana 组件

官方文档:https://grafana.com/docs/grafana/latest/installation/kubernetes/

由于官方 YAML 中用到 K8s 持久化 PV 存储,如果你的环境有存储了,则不需要配置 NFS 存储来实现,直接使用即可,我这里的实验环境是没有存储的,所以需要通过 NFS 来实现持久化存储。

image-20210719160140029

配置 NFS 存储

所有节点都需要安装 nfs

[root@k8s-master01 ~]# yum install -y nfs-common nfs-utils 

拿 master 来作为 nfs-server,创建共享目录

[root@k8s-master01 ~]# mkdir /nfsdata

授权共享目录

[root@k8s-master01 ~]# chmod 666 /nfsdata

编辑 exports 文件,使配置生效

[root@k8s-master01 ~]# cat /etc/exports
/nfsdata *(rw,no_root_squash,no_all_squash,sync)

启动服务

[root@k8s-master01 ~]# systemctl start rpcbind
[root@k8s-master01 ~]# systemctl start nfs

在另一台 Node 上挂载测试

[root@k8s-node01 ~]# mkdir /test
[root@k8s-node01 ~]# mount -t nfs 192.168.115.21:/nfsdata /test/
[root@k8s-node01 ~]# cd /test/
[root@k8s-node01 test]# date > test.txt
[root@k8s-node01 test]# cat test.txt 
2021年 07月 19日 星期一 16:15:59 CST

image-20210719161621050

image-20210719161630221

测试完成,将其卸载

[root@k8s-node01 ~]# umount /test

构建 Grafana

创建 PV

[root@k8s-master01 grafana]# ls
grafana.yaml  nfs-pv1.yaml
[root@k8s-master01 grafana]# kubectl apply -f nfs-pv1.yaml 
persistentvolume/pv1 created
[root@k8s-master01 grafana]# kubectl get pv

image-20210719161937579

创建 Grafana 组件

官方的 YAML 文件需要修改两个地方,如下所示。

image-20210719162204820

image-20210719162358219

[root@k8s-master01 grafana]# kubectl apply -f grafana.yaml 
persistentvolumeclaim/grafana-pvc created
deployment.apps/grafana created
service/grafana created

故障排查

创建好了,发现 Pod 没有 Running 状态

image-20210719162633490

查看 Pod 的信息,容器一直重启失败,网上说要在 YAML 文件加 command 命令并不可靠

image-20210719162722014

于是查看 Pod 日志发现了问题所在,看来是权限问题

[root@k8s-master01 grafana]# kubectl log grafana-64b445bd65-tzgrx
log is DEPRECATED and will be removed in a future version. Use logs instead.
GF_PATHS_DATA='/var/lib/grafana' is not writable.
You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied

image-20210719163056215

由于我的是实验环境,所以 NFS 存储目录就直接给 777 权限了,如果是生产环境不建议这样操作

[root@k8s-master01 ~]# chmod 777 /nfsdata/

重启构建一下 grafana

[root@k8s-master01 grafana]# kubectl delete -f grafana.yaml
[root@k8s-master01 grafana]# kubectl apply -f grafana.yaml

image-20210719163410683

再次检查 Grafana Pod 状态,已经 running

image-20210719163506068

查看 svc 端口,就可以在浏览器访问 Grafana

image-20210719163548490

http://任意节点 IP 地址:31557

image-20210719163613775

image-20210719163841161

初始用户密码都是 admin;

到此就完成 Prometheus 和 Grafana 最新版本的部署,后续继续更新添加监控 Node 具体操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章