Kubernetes環(huán)境使用Prometheus Operator自發(fā)現(xiàn)監(jiān)控SpringBoot

2020年9月21日14:40:33 發(fā)表評(píng)論 7,333 ℃

SpringBoot遷移kubernetes的工作已經(jīng)完成得差不多,已經(jīng)能基于原來(lái)的SpringBoot架構(gòu)實(shí)現(xiàn)無(wú)感遷移,各項(xiàng)測(cè)試訪問(wèn),服務(wù)間調(diào)用都正常,這幾天準(zhǔn)備遷移Prometheus監(jiān)控到kubernetes集群。

經(jīng)過(guò)一番調(diào)研,發(fā)現(xiàn)有開源項(xiàng)目可以一鍵部署prometheus到kubernetes集群,并且一些基礎(chǔ)的監(jiān)控規(guī)則都已經(jīng)配置好了,項(xiàng)目地址https://github.com/prometheus-operator/kube-prometheus ,部署也非常簡(jiǎn)單,把項(xiàng)目下載到有管理kubernetes集群的服務(wù)器,然后執(zhí)行:

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

等自動(dòng)部署就可以了,由于大部分鏡像使用的都是國(guó)外源,可以手動(dòng)改成國(guó)內(nèi)的。

等部署完成要實(shí)現(xiàn)外部訪問(wèn),需要添加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,訪問(wèn)測(cè)試:

Kubernetes環(huán)境使用Prometheus Operator自發(fā)現(xiàn)監(jiān)控SpringBoot

發(fā)現(xiàn)prometheus監(jiān)控已經(jīng)正常運(yùn)行,默認(rèn)便有一些監(jiān)控項(xiàng),如果我們新增自己的監(jiān)控,你會(huì)發(fā)現(xiàn)沒(méi)有配置文件可以更改。

經(jīng)過(guò)一番的查找,在promethus-operator官方文檔(https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/api.md#PrometheusSpec)找到了相關(guān)介紹,它已經(jīng)給我們預(yù)留了一個(gè)屬性additionalScrapeConfigs,我們只需將自定義的配置,通過(guò)這個(gè)配置就能追到上去。

下面介紹下具體操作步驟:

1、創(chuàng)建prometheus-additional.yaml文件:

  - job_name: "www.zhongjima.net-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服務(wù)還未接入prometheus監(jiān)控,可以參考:SpringBoot1.5.X接入prometheus監(jiān)控基于Eureka服務(wù)自發(fā)現(xiàn)

2、創(chuàng)建secret:

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

如果更改了prometheus-additional.yaml 文件,需要?jiǎng)h除secret 再重新創(chuàng)建:

kubectl delete secret additional-configs -n monitoring

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

  additionalScrapeConfigs:
    name: www.zhongjima.net-additional-configs #對(duì)應(yīng)secret名
    key: prometheus-additional.yaml #對(duì)應(yīng)文件名

如果你的springboot版本是1.5.x,需要修改prometheus版本為v2.16.0,具體原因可以參考:升級(jí)Prometheus報(bào)錯(cuò)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.zhongjima.net-additional-configs
    key: prometheus-additional.yaml

4、重新部署prometheus。

kubectl apply -f prometheus-prometheus.yaml

等部署成功,再次訪問(wèn)prometheus.amd5.cn/targets查看targets

Kubernetes環(huán)境使用Prometheus Operator自發(fā)現(xiàn)監(jiān)控SpringBoot

注意:生產(chǎn)環(huán)境還需要對(duì)prometheus數(shù)據(jù)持久化存儲(chǔ)。

5、訪問(wèn)grafana.amd5.cn導(dǎo)入視圖id 4701,查看監(jiān)控效果。

Kubernetes環(huán)境使用Prometheus Operator自發(fā)現(xiàn)監(jiān)控SpringBoot

【騰訊云】云服務(wù)器、云數(shù)據(jù)庫(kù)、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評(píng)論

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