Kubernetes環境使用Prometheus Operator自發現監控SpringBoot

2020年9月21日14:40:33 發表評論 7,479 ℃

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: "m.maowutv.com-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 m.maowutv.com-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: m.maowutv.com-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: m.maowutv.com-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: