k8s 中通过 consul 实现 prometheus 联邦功能

栏目: 服务器 · 发布时间: 6年前

k8s 中通过 consul 实现 prometheus 联邦功能

目录

背景介绍

架构

部署

验证

背景介绍

应项目需要,对于平台的监控数据需要做统一存储和展示,对于原生的prometheus联邦功能需要进行静态配置联邦部分,无法实现动态添加prometheus的功能,因此我们讨论以后计划使用consul来达到动态配置的效果。这样新加入的prometheus只需要自动注册到consul,负责存储展示的prometheus只需要从consul拿到prometheus实例列表即可。

架 构

为了方便描述,基于上面的情况我们暂且先将参与的prometheus分一下角色。负责统一存储展示的Prometheus称为Data Center。各个环境下的prometheus称为 Data Node吧。大致架构如下:

k8s 中通过 consul 实现 prometheus 联邦功能

如上图所示, Data Center我们使用了Prometheus-Operator,至于Prometheus-Operator的优点我就不介绍了,大家可以移步官网查看。图中的Data Node节点都是部署的原生的Prometheus。

  • 部署consul(这里consul可以部署在任意k8s集群中,只要Data Node注册时能拿到改地址即可)

  • 部署Data Node,并注册到consul

  • 部署Prometheus-Operator,从consul发现已经注册的prometheus

注意: 经过采坑,推荐大家使用Prometheus-Operator v0.24.0及以后的版本。原因在于operator在v0.24.0之前对于additionalScrapeConfigs的解析有误。

部  署

  • 部署Data Node prometheus.yaml

apiVersion: v1
kind: "Service"
metadata:
  name: prometheus
  namespace: wisecloud-agent
  labels:
    name: prometheus-service
spec:
  ports:
  - name: prometheus-server
    protocol: TCP
    port: 9090
    targetPort: 9090
    nodePort: 32001
  selector:
    app: prometheus-server
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    name: prometheus
    app: prometheus-server
    com.wise2c.service: prometheus
    com.wise2c.stack: wisecloud-agent
  name: prometheus
  namespace: wisecloud-agent
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: prometheus
        com.wise2c.service: prometheus
        com.wise2c.stack: wisecloud-agent
        app: prometheus-server
    spec:
      hostNetwork: true
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: io.wise2c.service.prometheus
                operator: Exists
      serviceAccountName: wisecloud-agent
      containers:
      - name: service-register
        image: registry.cn-hangzhou.aliyuncs.com/tder/service-register
        imagePullPolicy: IfNotPresent
        env:
          - name: CONSUL_URL
            value: 192.168.5.11:8500
          - name: LISTEN_PORT
            value: "8089"
          - name: SERVICE_NAME
            value: wisecloud-agent-prometheus
          - name: SERVICE_PORT
            value: "9090"
          - name: SERVICE_HEALTH_CHECK_PATH
            value: "/health"
      - name: prometheus
        image: prom/prometheus:v1.7.1
        imagePullPolicy: IfNotPresent
        command:
        - "/bin/prometheus"
        args:
        - "-config.file=/etc/prometheus/prometheus.yml"
        - "-storage.local.path=/prometheus"
        - "-storage.local.retention=180h"
        ports:
        - containerPort: 9090
          protocol: TCP
        volumeMounts:
        - name: data
          mountPath: "/prometheus"
        - name: config-volume
          mountPath: "/etc/prometheus"
        - name: alert-roles
          mountPath: "/etc/prometheus-rules"
        - name: local-timezone
          mountPath: "/etc/localtime"
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 500m
            memory: 2500Mi
      tolerations:
      - key: "node-role.kubernetes.io/master"
        operator: "Exists"
        effect: "NoSchedule"
      volumes:
      - name: data
        hostPath:
          path: /prometheus
      - name: alert-roles
        hostPath:
          path: /etc/prometheus-rules
      - name: config-volume
        configMap:
          name: prometheus-config
      - name: local-timezone
        hostPath:
          path: /etc/localtime
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: wisecloud-agent
data:
  prometheus.yml: |
    global:
      scrape_interval: 30s
      scrape_timeout: 10s
      evaluation_interval: 30s
      external_labels:
        environment: k8s_test
    rule_files:
      - "/etc/prometheus-rules/*.rules"
    scrape_configs:
    - job_name: 'cadvisor'
      honor_labels: true
      metrics_path: /metrics/cadvisor
      scheme: https
      kubernetes_sd_configs:
      - role: endpoints
        namespaces:
          names:
          - kube-system
      bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      tls_config:
        insecure_skip_verify: true
      relabel_configs:
      - source_labels: [__meta_kubernetes_service_label_k8s_app]
        separator: ;
        regex: kubelet
        replacement: $1
        action: keep
      - source_labels: [__meta_kubernetes_endpoint_port_name]
        separator: ;
        regex: https-metrics
        replacement: $1
        action: keep
      - source_labels: [__meta_kubernetes_namespace]
        separator: ;
        regex: (.*)
        target_label: namespace
        replacement: $1
        action: replace
      - source_labels: [__meta_kubernetes_pod_name]
        separator: ;
        regex: (.*)
        target_label: pod
        replacement: $1
        action: replace
      - source_labels: [__meta_kubernetes_service_name]
        separator: ;
        regex: (.*)
        target_label: service
        replacement: $1
        action: replace
      - source_labels: [__meta_kubernetes_service_name]
        separator: ;
        regex: (.*)
        target_label: job
        replacement: ${1}
        action: replace
      - source_labels: [__meta_kubernetes_service_label_k8s_app]
        separator: ;
        regex: (.+)
        target_label: job
        replacement: ${1}
        action: replace
      - separator: ;
        regex: (.*)
        target_label: endpoint
        replacement: https-metrics
        action: replace

针对上述Yaml我挑重要的部分做一下说明。

- name: service-register
  image: registry.cn-hangzhou.aliyuncs.com/tder/service-register
  imagePullPolicy: IfNotPresent 
  env:
    - name: CONSUL_URL
      value: 192.168.5.11:8500
    - name: LISTEN_PORT
      value: "8089"
    - name: SERVICE_NAME
      value: wisecloud-agent-prometheus
    - name: SERVICE_PORT
      value: "9090"
    - name: SERVICE_HEALTH_CHECK_PATH
      value: "/health"

k8s 中通过 consul 实现 prometheus 联邦功能

如果上述参数不对可能导致实例注册不到consul。上面的ConfigMap为prometheus的监控配置文件,大家可以自行配置。注意这里的配置使用的是k8s中内置的cadvisor exporter。如果是自己部署的cadvisor需要修改ConfigMap中data的定义。

部署yaml kubectl apply -f prometheus.yaml -n xxx。效果如下:

k8s 中通过 consul 实现 prometheus 联邦功能

  • 部署Data Center。 为了简化Prometheus-Operator的部署可以使用

    https://github.com/alanpeng/install-prometheus-operator (喜欢的可以star哦)。启动以后效果如下:

k8s 中通过 consul 实现 prometheus 联邦功能

如果用户为了方便调试可以把svc列表中的grafana和prometheus端口暴露成NodePort。

目前为止Prometheus-Operator部署已经告一段落。接下来开始配置prometheus-operator的动态发现功能了。Prometheus-Operator虽然提供了ServiceMonitor来生成Prometheus需要的配置,但对于复杂的场景并不支持的很好,所以Operator中提出了AdditionalScrapeConfigs的属性。基于AdditionalScrapeConfigs配置需要对已经部署的Prometheus-Operator做一下修改

  • 创建secrets(additional.yaml)

apiVersion: v1
data:
  prometheus-additional.yaml: LSBqb2JfbmFtZTogJ2ZlZGVyYXRlJwogIHNjcmFwZV9pbnRlcnZhbDogMTVzCgogIGhvbm9yX2xhYmVsczogdHJ1ZQogIG1ldHJpY3NfcGF0aDogJy9mZWRlcmF0ZScKCiAgcGFyYW1zOgogICAgJ21hdGNoW10nOgogICAgLSAne2pvYj0iazhzLWFwaS1leHBvcnRlciJ9JwogICAgLSAne2pvYj0ia3ViZS1zdGF0ZS1tZXRyaWNzIn0nCiAgICAtICd7am9iPSJrdWJlbGV0In0nCgogIGNvbnN1bF9zZF9jb25maWdzOgogICAgICAtIHNlcnZlcjogeHgueHgueHgueHg6ODUwMAogICAgICAgIHNlcnZpY2VzOiBbIndpc2VjbG91ZC1hZ2VudC1wcm9tZXRoZXVzIl0=
kind: Secret
metadata:
  labels:
    managed-by: prometheus-operator
  name: prometheus-k8s-new
  namespace: monitoring

针对面的prometheus-additional.yaml属性我要多介绍一下。把上面的base64编码进行解码,得到一下内容

- job_name: 'federate'
  scrape_interval: 15s

  honor_labels: true
  metrics_path: '/federate'

  params:
    'match[]':
    - '{job="k8s-api-exporter"}'
    - '{job="kube-state-metrics"}'
    - '{job="kubelet"}'

  consul_sd_configs:
  - server: xx.xx.xx.xx:8500
    services:
    - wisecloud-agent-prometheus

k8s 中通过 consul 实现 prometheus 联邦功能

  • 修改prometheus-prometheus.yaml

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  labels:
    prometheus: k8s
  name: k8s
  namespace: monitoring
spec:
  alerting:
    alertmanagers:
    - name: alertmanager-main
      namespace: monitoring
      port: web
  baseImage: 192.168.5.14/library/prometheus
  nodeSelector:
    beta.kubernetes.io/os: linux
  replicas: 1
  resources:
    requests:
      memory: 100Mi
  ruleSelector:
    matchLabels:
      prometheus: k8s
      role: alert-rules
  serviceAccountName: prometheus-k8s
  serviceMonitorNamespaceSelector: {}
  serviceMonitorSelector: {}
  version: v2.3.1
  additionalScrapeConfigs:
    name: prometheus-k8s-new
    key: prometheus-additional.yaml

注意: Prometheus-Operator部署以后本身已经对该集群的相关资源进行了监控,如果prometheus和operator部署在同一环境最好删掉operator中的相关ServiceMonitor

kubectl delete servicemonitor alertmanager coredns kube-apiserver kube-controller-manager kube-scheduler kube-state-metrics kubelet  prometheus prometheus-operator -n monitoring

部署上述文件

additional.yaml prometheus-prometheus.yaml

kubectl apply -f additional.yaml -f prometheus-prometheus.yaml

验 证

访问operator中的prometheus可以看到如下效果:

  • target一览

k8s 中通过 consul 实现 prometheus 联邦功能

  • 查询metrics

k8s 中通过 consul 实现 prometheus 联邦功能

上图的environment=k8s-test就是在Data Node中定义的external-labels。看到上面的效果,表示联邦已经生效了。

k8s 中通过 consul 实现 prometheus 联邦功能

关于睿云智合

深圳睿云智合科技有限公司成立于2012年,总部位于深圳,并分别在成都、深圳设立了研发中心,北京、上海设立了分支机构,核心骨干人员全部为来自金融、科技行业知名企业资深业务专家、技术专家。早期专注于为中国金融保险等大型企业提供创新技术、电子商务、CRM等领域专业咨询服务。

自2016年始,在率先将容器技术引进到中国保险行业客户后,公司组建了专业的容器技术产品研发和实施服务团队,旨在帮助中国金融行业客户将容器创新技术应用于企业信息技术支持业务发展的基础能力改善与提升,成为中国金融保险行业容器技术服务领导品牌。

此外,凭借多年来在呼叫中心领域的业务经验与技术积累,睿云智合率先在业界推出基于开源软交换平台FreeSwitch的微服务架构多媒体数字化业务平台,将语音、视频、webchat、微信、微博等多种客户接触渠道集成,实现客户统一接入、精准识别、智能路由的CRM策略,并以容器化治理来支持平台的全应用生命周期管理,显著提升了数字化业务处理的灵活、高效、弹性、稳定等特性,为帮助传统企业向“以客户为中心”的数字化业务转型提供完美的一站式整体解决方案。

大家快来扫我!

这里有你想了解的行业资讯和你所需要的技术干货!!

k8s 中通过 consul 实现 prometheus 联邦功能

大家关注【Wise2C】后回复【进群】,睿云小助手会第一时间把拉你进入【 Docker企业落地实践群】,我们分享的各个企业案例项目的技术专家与用户代表,正在敬候您的光临!

若需要了解更多有关Wise系列PaaS产品的详情,请与我们的市场团队联系:contact@wise2c.com


以上所述就是小编给大家介绍的《k8s 中通过 consul 实现 prometheus 联邦功能》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

具体数学(英文版第2版)

具体数学(英文版第2版)

[美] Ronald L. Graham、Donald E. Knuth、Oren Patashnik / 机械工业出版社 / 2002-8 / 49.00元

This book introduces the mathematics that supports advanced computer Programming and the analysis of algorithms. The primary aim of its well-known authors is to provide a solid and relevant base of ma......一起来看看 《具体数学(英文版第2版)》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具