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 具體操作。

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