环境:

本文档所有操作均基于自建的kubernetes 集群,集群版本:v1.21.0

master 节点IP:192.168.57.20

prometheus 端口:32500

grafana端口:32300

如果没有k8s 集群请参考:kubeadm方式安装kubernetes集群

安装prometheus及相关组件:

1.在master节点安装git

yum install -y git

2.下载prometheus 及相关组件的yml文件

#1.下载
git clone https://github.com/coreos/kube-prometheus.git

3.安装组件

#1.创建命名空间:
kubectl create ns monitoring

#2.创建prometheus-operator
kubectl apply -f /kube-prometheus/manifests/setup/

#3.创建prometheus及相关组件:
kubectl apply -f /kube-prometheus/manifests/

4.修改grafana,prometheus 访问方式为nodeport

#1.查看service
kubectl get svc -A

#以上结果展示了集群pod访问的方式

修改grafana:(标注的地方修改即可)

#1.编辑grafana
[root@master-01 ]# kubectl edit svc/grafana -n monitoring




# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app.kubernetes.io/component":"grafana","app.kubernetes.io/name":"grafana","app.kubernetes.io/part-of":"kube-prometheus","app.kubernetes.io/version":"7.5.4"},"name":"grafana","namespace":"monitoring"},"spec":{"ports":[{"name":"http","port":3000,"targetPort":"http"}],"selector":{"app.kubernetes.io/component":"grafana","app.kubernetes.io/name":"grafana","app.kubernetes.io/part-of":"kube-prometheus"}}}
creationTimestamp: "2021-05-07T03:07:55Z"
labels:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 7.5.4
name: grafana
namespace: monitoring
resourceVersion: "75077"
uid: c2a96af7-afa8-4130-9a4c-4d650094beff
spec:
clusterIP: 10.200.167.128
clusterIPs:
- 10.200.167.128
externalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: http
nodePort: 32300                     ###新增nodePort: 32300 端口可自定义,默认端口范围30000-32767
port: 3000
protocol: TCP
targetPort: http
selector:
app.kubernetes.io/component: grafana
app.kubernetes.io/name: grafana
app.kubernetes.io/part-of: kube-prometheus
sessionAffinity: None
type: NodePort                      ###这里由集群ClusterIP 方式改为NodePort方式
status:
loadBalancer: {}


###以上两处修改完成后保存退出,即可生效。

如下图:

修改prometheus:

[root@master-01 ]# kubectl edit svc/prometheus-k8s -n monitoring

##修改部分同上面grafana
问题:
#1.检查prometheus 组件运行状态
kubectl get po -n monitoring

###通常会出现 kube-state-metrics 组件处于pullimageError 的状态,这是因为镜像没法pull导致的,手动pull 一个kube-state-metrics镜像即可:

#2.手动pull kube-state-metrics镜像:
docker pull bitnami/kube-state-metrics

#3.修改 kube-state-metrics 
 kubectl edit deploy/kube-state-metrics -n monitoring

#将原来的的镜像名字替换成你pull 好的,或者把你pull 的改成和文件中镜像同名同版本即可。
访问:

通过nodeip+nodeport方式访问prometheus,  我的地址为 192.168.57.20:32500

依次点击: status –> targets 检查监控指标是否全部正常,如图

更多配置信息如告警,告警规则等请自行查看。

通过nodeip+nodeport方式访问。190.168.57.20:32300

grafana 默认登录用户名 admin  密码为 admin

依次点击  +  –> import   模板ID分别为

13105     POD资源监控
11074   Node资源监控

导入后就可以正常使用啦

POD资源监控界面 node资源监控界面:

打赏

0 条评论

发表评论