prometheus-operator 监控 Rabbitmq集群

  首先我们监控服务需要知道prometheus-operator是如何去工作的,才好去写相关的yaml配置,这里我划分成了5个部分,如果容器服务本身就以k8s来编排的,那就只需要三步,这里因为我的rabbitmq是部署在外面所以需要5步,它们分别是:

 1.创建 Endpoints  # 将外部服务映射进来
 2.创建 Service   # 创建servicName,嫌麻烦也可以不写,这里1,2步只是为了隐藏真实的地址
 3.创建采集工具使用deploy部署
 4.创建采集工具Service ##clusterIP: None
 5.创建ServiceMonitor

我的MQ管理地址为 192.168.100.192 : 15672

vim prometheus-rabbitmq.yaml

apiVersion: v1
kind: Endpoints
metadata:
  name: rabbitmq-management
  labels:
    k8s-app: rabbitmq-management
subsets:
- addresses:
  - ip: 192.168.100.192
  ports:
  - name: api
    port: 15672
    protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: rabbitmq-management
  labels:
    k8s-app: rabbitmq-management
spec:
  type: ClusterIP
  ports:
  - name: api
    port: 15672
    protocol: TCP
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: rabbitmq-exporter
spec:
  replicas: 1
  template:
    metadata:
      labels:
        k8s-app: rabbitmq-exporter
    spec:
      containers:
      - name: rabbitmq-exporter
        image: kbudde/rabbitmq-exporter
        env:
         - name: PUBLISH_PORT
           value: "9099"
         - name: RABBIT_CAPABILITIES
           value: "bert,no_sort"
         - name: RABBIT_USER
           value: "you user"
         - name: RABBIT_PASSWORD
           value: "you password"
         - name: RABBIT_URL
           value: http://rabbitmq-management:15672
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9099
---
apiVersion: v1
kind: Service
metadata:
  name: rabbitmq-exporter
  labels:
    k8s-app: rabbitmq-exporter
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    k8s-app: rabbitmq-exporter
  ports:
  - name: api
    port: 9099
    protocol: TCP

 kuctl apply -f prometheus-rabbitmq.yaml -n 指定命名空间 PS:我的是以项目+环境来区分的

 

上述可以进行测试

访问 curl http://10.244.6.145:9099/metrics

创建ServiceMonitor 让其能够自动发现并注册

vim prometheus-rabbitmq-k8s-monitoring.yaml

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: rabbitmq-exporter
  namespace: monitoring
  labels:
    k8s-app: rabbitmq-exporter
    namespace: monitoring
spec:
  jobLabel: k8s-app
  endpoints:
  - port: api
    interval: 30s
    scheme: http
  selector:
    matchLabels:
      k8s-app: rabbitmq-exporter
  namespaceSelector:
    matchNames:
    - default    #你采集的数据在哪里,就写它的namespaces

最后 kubectl apply -f prometheus-rabbitmq-k8s-monitoring.yaml

模版是获取的官网新增了namespaces变量,这样就方便多了。

至于规则告警,可以根据自己想要监测的数据来进行报警就不多说了。

 

posted @ 2018-10-25 14:44  清风徐来#  阅读(1367)  评论(2编辑  收藏  举报