Servicemonitor监控自带metrics接口和无metrics接口

前言:

servicemonitor监控存在两种情况:

1、有metrics ,创建service+servicemonitor配置

2、无metrics  配置exporter,exporter进行采集

 

一、监控自带metrics接口

①、部署ServiceMonitor

$ vi prometheus-serviceMonitorEtcd.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: etcd-k8s
  namespace: monitoring
  labels:
    k8s-app: etcd-k8s
spec:
  jobLabel: k8s-app
  endpoints:
  - port: port      #匹配service端口
    interval: 15s
  selector:
    matchLabels:
      k8s-app: etcd
  namespaceSelector:
    matchNames:
    - kube-system

上面我们在 monitoring 命名空间下面创建了名为 etcd-k8s 的 ServiceMonitor 对象,基本属性和前面介绍的一致,匹配 kube-system 这个命名空间下面的具有 k8s-app=etcd 这个 label 标签的 Service,jobLabel 表示用于检索 job 任务名称的标签,由于 etcd 的 metrics 接口在 2381 端口下面,不需要 https 安全认证,所以用默认的配置即可。然后我们直接创建这个 ServiceMonitor 对象即可

②、创建 Service & Endpoints

因为 ETCD 是独立于集群之外的,所以我们需要创建一个 Endpoints 将其代理到 Kubernetes 集群,然后创建一个 Service 绑定 Endpoints,然后 Kubernetes 集群的应用就可以访问 ETCD 集群了

$ vi prometheus-etcdService.yaml
apiVersion: v1
kind: Service
metadata:
  name: etcd-k8s
  namespace: kube-system
  labels:
    k8s-app: etcd
spec:
  type: ClusterIP
  clusterIP: None  #设置为None,不分配Service IP
  ports:
  - name: port     #匹配service端口
    port: 2381
---
apiVersion: v1
kind: Endpoints
metadata:
  name: etcd-k8s
  namespace: kube-system
  labels:
    k8s-app: etcd
subsets:
- addresses:
  - ip: 172.16.1.11   # 指定etcd节点地址,如果是集群则继续向下添加
    nodeName: etc-master
  ports:
  - name: port
    port: 2381         # Etcd 端口号

二、监控无metrics接口

监控kafka-exporter为例

apiVersion: v1
kind: Service
metadata:
  name: kafka-exporter
  namespace: monitoring
  labels:
    k8s-app: kafka-exporter
spec:
  selector:
    k8s-app: kafka-exporter
  ports:
  - name: kafka-exporter
    port: 9308
    protocol: TCP
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kafka-exporter
  namespace: monitoring
  labels:
    k8s-app: kafka-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: kafka-exporter
  template:
    metadata:
      labels:
        k8s-app: kafka-exporter
    spec:
      containers:
      - name: kafka-exporter
        image: danielqsj/kafka-exporter:latest
        imagePullPolicy: IfNotPresent
        args: ["--kafka.server=kafka-0.kafka-headless.public-service:9092"]
        resources:
          requests:
            memory: 100M
        volumeMounts:
        - name: tz-config
          mountPath: /etc/localtime
          readOnly: true
      nodeSelector:
        monitor: "true"
      volumes:
        - name: tz-config
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
  • 创建了kafka_exporter的service和deployment,service用于和Prometheus servicemonitor进行匹配,采集数据
  • args:Kafka_exporter启动参数,为k8s集群中kafka的地址
posted @ 2022-09-25 00:51  南昌拌粉的成长  阅读(596)  评论(0编辑  收藏  举报