Kubernetes环境使用Prometheus Operator自发现监控SpringBoot

2020年9月21日14:40:33 发表评论 5,504 ℃

SpringBoot迁移kubernetes的工作已经完成得差不多,已经能基于原来的SpringBoot架构实现无感迁移,各项测试访问,服务间调用都正常,这几天准备迁移Prometheus监控到kubernetes集群。

经过一番调研,发现有开源项目可以一键部署prometheus到kubernetes集群,并且一些基础的监控规则都已经配置好了,项目地址https://github.com/prometheus-operator/kube-prometheus ,部署也非常简单,把项目下载到有管理kubernetes集群的服务器,然后执行:

kubectl apply -f ./manifests/setup/
kubectl apply -f ./manifests/

等自动部署就可以了,由于大部分镜像使用的都是国外源,可以手动改成国内的。

等部署完成要实现外部访问,需要添加ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: grafana-ingress
  namespace: monitoring
spec:
  rules:
  - host: grafana.amd5.cn
    http:
      paths:
      - path: /
        backend:
          serviceName: grafana
          servicePort: 3000
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: prometheus-ingress
  namespace: monitoring
spec:
  rules:
  - host: prometheus.amd5.cn
    http:
      paths:
      - path: /
        backend:
          serviceName: prometheus-k8s
          servicePort: 9090

然后解析好域名或者本地更改host,访问测试:

Kubernetes环境使用Prometheus Operator自发现监控SpringBoot

发现prometheus监控已经正常运行,默认便有一些监控项,如果我们新增自己的监控,你会发现没有配置文件可以更改。

经过一番的查找,在promethus-operator官方文档(https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/api.md#PrometheusSpec)找到了相关介绍,它已经给我们预留了一个属性additionalScrapeConfigs,我们只需将自定义的配置,通过这个配置就能追到上去。

下面介绍下具体操作步骤:

1、创建prometheus-additional.yaml文件:

  - job_name: "www.amd5.cn-test-jmx"
    scheme: http
    metrics_path: '/actuator/prometheus'
    basic_auth:
      username: your_username
      password: your_password
    consul_sd_configs:
      - server: "eureka-0.eureka-service.blog.svc.cluster.local:8761"
        scheme: http
        services:
    relabel_configs:
      - source_labels: [__meta_consul_service_id]
        target_label: instance

注意:如果你的springboot服务还未接入prometheus监控,可以参考:SpringBoot1.5.X接入prometheus监控基于Eureka服务自发现

2、创建secret:

kubectl create secret generic www.amd5.cn-additional-configs --from-file=prometheus-additional.yaml -n monitoring

如果更改了prometheus-additional.yaml 文件,需要删除secret 再重新创建:

kubectl delete secret additional-configs -n monitoring

3、修改prometheus-prometheus.yaml文件,文件最后新增

  additionalScrapeConfigs:
    name: www.amd5.cn-additional-configs #对应secret名
    key: prometheus-additional.yaml #对应文件名

如果你的springboot版本是1.5.x,需要修改prometheus版本为v2.16.0,具体原因可以参考:升级Prometheus报错Error refreshing servic Unexpected response code: 404解决办法

完整的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
  #image: prom/prometheus:v2.21.0
  image: prom/prometheus:v2.16.0
  nodeSelector:
    kubernetes.io/os: linux
  podMonitorNamespaceSelector: {}
  podMonitorSelector: {}
  replicas: 2
  resources:
    requests:
      memory: 400Mi
  ruleSelector:
    matchLabels:
      prometheus: k8s
      role: alert-rules
  securityContext:
    fsGroup: 2000
    runAsNonRoot: true
    runAsUser: 1000
  serviceAccountName: prometheus-k8s
  serviceMonitorNamespaceSelector: {}
  serviceMonitorSelector: {}
  version: v2.16.0
  additionalScrapeConfigs:
    name: www.amd5.cn-additional-configs
    key: prometheus-additional.yaml

4、重新部署prometheus。

kubectl apply -f prometheus-prometheus.yaml

等部署成功,再次访问prometheus.amd5.cn/targets查看targets

Kubernetes环境使用Prometheus Operator自发现监控SpringBoot

注意:生产环境还需要对prometheus数据持久化存储。

5、访问grafana.amd5.cn导入视图id 4701,查看监控效果。

Kubernetes环境使用Prometheus Operator自发现监控SpringBoot

【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: