吾八哥博客

您现在的位置是:首页 > 云原生 > Kubernetes > 正文

Kubernetes

k8s集群安装Prometheus监控以及Grafana面板的方法

吾八哥2020-12-17Kubernetes3867

环境准备

  • kubernetes集群(minikube开启的集群也行)

  • Helm3

操作步骤

使用helm安装kube-prometheus-stack

提示:helm安装方法请参考:https://helm.sh/docs/intro/install/

helm repo add prometheus-community  
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack  --version 9.4.10

因为我现在集群版本较低,所以这里安装的是9.4.10的版本,详细的版本区别可以参考:https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack

这里使用的是默认的配置,生产环境里根据实际需求调整下配置,可以修改values.yaml文件来达到调整配置的目的。

等待kube-prometheus-stack相关的组件的pod启动成功

# kubectl get pod
alertmanager-prometheus-kube-prometheus-alertmanager-0   2/2     Running             0          3m
prometheus-grafana-5dc99995dd-qpjm9                      2/2     Running             0          3m
prometheus-kube-prometheus-operator-77569bd5c7-74pl6     2/2     Running             0          3m
prometheus-kube-state-metrics-7b4b4b7b7f-897zr           1/1     Running             0          3m
prometheus-prometheus-kube-prometheus-prometheus-0       3/3     Running             1          3m
prometheus-prometheus-node-exporter-97dhd                1/1     Running             0          3m
prometheus-prometheus-node-exporter-klm98                1/1     Running             0          3m
prometheus-prometheus-node-exporter-lqkkb                1/1     Running             0          3m
prometheus-prometheus-node-exporter-r56fr                1/1     Running             0          3m
prometheus-prometheus-node-exporter-tvjf5                1/1     Running             0          3m
prometheus-prometheus-node-exporter-v9rkx                1/1     Running             0          3m
prometheus-prometheus-node-exporter-zhff7                1/1     Running             0          3m
prometheus-prometheus-node-exporter-zx65r                1/1     Running             0          3m

pod全部启动成功后,此时grafana面板只能通过clusterIP的方式进行访问,可以通过转发端口来在本机curl验证测试

# kubectl port-forward deployment/prometheus-grafana 3000
Forwarding from 127.0.0.1:3000 -> 3000

另开一个shell窗口来curl测试下

# curl -v http://localhost:3000
* About to connect() to localhost port 3000 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 302 Found
< Cache-Control: no-cache
< Content-Type: text/html; charset=utf-8
< Expires: -1
< Location: /login
< Pragma: no-cache
< Set-Cookie: redirect_to=%2F; Path=/; HttpOnly; SameSite=Lax
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
< X-Xss-Protection: 1; mode=block
< Date: Thu, 17 Dec 2020 10:43:28 GMT
< Content-Length: 29
<
<a href="/login">Found</a>.

* Connection #0 to host localhost left intact

出现这个302跳转到login就是代表部署成功了,可以配置外部访问了。

配置外部访问Grafana

安装完成后默认是ClusterIP的方式访问,可以通过如下方法配置外部访问:

  1. port-forward转发端口

  2. 配置ingress

  3. 改为NodePort方式访问

我这里使用的NodePort访问,端口为32222,配置后访问地址:http://任意Node节点IP:32222/  ,使用默认用户名和密码:admin/prom-operator,Nodes监控面板效果如下:


grafana.png