Kube-Promethus配置Nacos监控

前置:Kube-Promethus安装监控k8s集群

一.判断Nacos开启监控配置

首先通过集群内部任一节点访问Nacos的这个地址<NacosIP>:端口号/nacos/actuator/prometheus,查看是否能够获取监控数据。

image-20240129154707849

如果没有数据则修改Nacos集群的application.properties配置文件,每个节点都要添加。

management.endpoints.web.exposure.include=*

如图:

image-20240129154851943

重启Nacos后在访问那个地址就能看到数据了。

二.修改Kube-Promethus的RBAC(Role-Based Access Control)配置文件

Kube-Promethus会在默认命名空间(default)、kube-system和monitoring 命名空间中配置了相同的权限规则,允许对 services、endpoints、pods 进行 get、list、watch 操作。以及对其资源的访问权限。

因为此次监控的Nacos在,名为nacos的命名空间内,故需要作以下修改。

#首先进入Kube-Promethus的安装目录,在manifests/prometheus找到并修改如下文件
vi prometheus-roleSpecificNamespaces.yaml

添加如下内容

- apiVersion: rbac.authorization.k8s.io/v1
  kind: Role
  metadata:
    name: prometheus-k8s
    namespace: nacos
  rules:
  - apiGroups:
    - ""
    resources:
    - services
    - endpoints
    - pods
    verbs:
    - get
    - list
    - watch
  - apiGroups:
    - extensions
    resources:
    - ingresses
    verbs:
    - get
    - list
    - watch

如图:

image-20240130175948089

vi prometheus-roleBindingSpecificNamespaces.yaml
#写入如下内容
- apiVersion: rbac.authorization.k8s.io/v1
  kind: RoleBinding
  metadata:
    name: prometheus-k8s
    namespace: nacos
  roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: Role
    name: prometheus-k8s
  subjects:
  - kind: ServiceAccount
    name: prometheus-k8s
    namespace: monitoring

如图:

image-20240130180116091

然后执行这两个文件:

kubectl apply -f prometheus-roleBindingSpecificNamespaces.yaml
kubectl apply -f prometheus-roleSpecificNamespaces.yaml

三.添加ServiceMonitor

#在/manifests/serviceMonitor文件夹下,增加文件:prometheus-nacos-serviceMonitor.yaml
vi prometheus-nacos-serviceMonitor.yaml
#写入如下内容
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    app: nacos  #标签
  name: nacos
  namespace: monitoring  #监控的命名空间
spec:
  endpoints:
  - interval: 30s
    path: /nacos/actuator/prometheus    #nacos监控数据指标地址
    port: server    #端口名称,与svc中一致
  jobLabel: app    #标签,与svc中一致
  namespaceSelector:
    matchNames:
    - nacos   #命名空间,与nacos的svc中一致
  selector:
    matchLabels:
      app: nacos-server    #选择的标签,与svc中一致

注意:matchNames:这里填的是nacos服务所在的命名空间,matchLabels是nacos的Service服务名称。如图:

image-20240131113857237

#执行该文件
kubectl apply -f prometheus-nacos-serviceMonitor.yaml
#查看是否创建成功
kubectl get servicemonitors -n monitoring

image-20240131134324555

也可以在Rancher上查看是否成功。

image-20240131135132197

四.关联prometheus与ServiceMonitor

因为我们之前创建的Nacos的service,它的lables不是k8s-app,而是app。因为kube-prometheus默认将k8s-app标签的服务都已经加入关联了。

#在/manifests/prometheus目录下,修改prometheus-prometheus.yaml
vi prometheus-prometheus.yaml
#添加如下内容
  serviceMonitorSelector:
    matchLabels:
      app: nacos

如图:

image-20240131140704404

#然后执行修改文件
kubectl replace -f prometheus-prometheus.yaml 

重启Promethus,然后查看target是否有Naocs的节点。

image-20240131142056206

五.Grafana导入模板

image-20240131142506197

image-20240131142533442

成品如下:

image-20240131142612290

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐