使用Prometheus发现在Kubernetes上运行的应用程序
Prometheus支持抓取應用程序的多個實例。 由于其IP地址會發(fā)生變化,因此需要動態(tài)發(fā)現(xiàn)在協(xié)調環(huán)境中運行的應用程序。 可以將Prometheus配置為使用Kubernetes API動態(tài)地發(fā)現(xiàn)正在運行的實例列表中的更改。
下面顯示了一個簡單的Prometheus示例,該示例對具有多個Pod實例的應用程序進行檢測。
普羅米修斯配置
我們配置Prometheus來發(fā)現(xiàn)我們的config-example應用程序的pod。
global: scrape_interval: 15s external_labels: monitor: 'example-monitor' scrape_configs: - job_name: 'example-metric' scrape_interval: 5s metrics_path: /metrics/ scheme: https basic_auth: username: admin password: adminadmin tls_config: insecure_skip_verify: true kubernetes_sd_configs: - role: endpoints namespaces: names: - default relabel_configs: - source_labels: [__meta_kubernetes_service_label_app] separator: ; regex: config-example replacement: $ 1 action: keep - source_labels: [__meta_kubernetes_endpoint_port_name] separator: ; regex: https 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 - separator: ; regex: (.*) target_label: endpoint replacement: https action: replace我們需要調整app標簽(在此處為config-example )和端口名稱( https ),在該端口下可以使用監(jiān)視端點。
儀表應用
本示例使用在Open Liberty之上運行的MicroProfile Metrics端點。 該應用程序在端口9443和路徑/metrics/下以Prometheus格式公開/metrics/ 。
在這里,您可以看到示例服務和部署:
kind: Service apiVersion: v1 metadata: name: config-example labels: app: config-example spec: selector: app: config-example ports: - port: 9443 name: https - port: 9080 name: http kind: Deployment apiVersion: apps/v1beta1 metadata: name: config-example spec: replicas: 2 template: metadata: labels: app: config-example spec: containers: - name: config-example image: sdaschner/config-example: 1 # ...您可以在GitHub上找到完整的示例,包括正在運行的應用程序。
Prometheus設置和RBAC
為了使該示例適用于RBAC Kubernetes設置,Prometheus服務帳戶需要具有正確的權限。 因此,我們將在此處創(chuàng)建服務帳戶和相應的群集角色,如YAML定義中所述。 同樣,我們在創(chuàng)建Prometheus部署時指定服務帳戶。
我們?yōu)镻rometheus實例創(chuàng)建部署和服務。
kind: Service apiVersion: v1 metadata: name: prometheus labels: app: prometheus spec: selector: app: prometheus ports: - port: 9090 name: http kind: Deployment apiVersion: apps/v1beta1 metadata: name: prometheus spec: replicas: 1 template: metadata: labels: app: prometheus version: v1 spec: serviceAccountName: prometheus containers: - name: prometheus image: prom/prometheus:v2. 7.1 ports: 9090 - containerPort: 9090 volumeMounts: - name: prometheus-config-volume mountPath: /etc/prometheus/prometheus.yml subPath: prometheus.yml volumes: - name: prometheus-config-volume configMap: name: prometheus-config restartPolicy: Always前面顯示的配置可以從配置映射中注入實例。
kind: ConfigMap apiVersion: v1 metadata: name: prometheus-config data: prometheus.yml: | global: scrape_interval: 15s # content as shown earlier ...有關Prometheus配置的完整說明,請參閱文檔 。
訪問目標
現(xiàn)在,正在運行的Prometheus實例可以發(fā)現(xiàn)兩個正在運行的配置示例應用程序,它們都作為Kubernetes容器運行:
gt; kubectl get pods NAME READY STATUS RESTARTS AGE config-example-69974cbc96-dqd96 gt; kubectl get pods NAME READY STATUS RESTARTS AGE config-example-69974cbc96-dqd96 1 / 1 Running 0 4m config-example-69974cbc96-zstg7 1 / 1 Running 0 4m grafana-8694db9d4f-nvn5s 1 / 1 Running 0 3m prometheus-594dd9cdb8-95ftz 1 / 1 Running 0 3m我們可以在Prometheus配置下看到實際目標,包括其IP地址。
看一下GitHub上的完整示例。 您可能還會看到以下視頻,了解如何使用MicroProfile在Java EE應用程序中實現(xiàn)業(yè)務指標。
對于定義了更多應用程序的更復雜的微服務示例,使用純Prometheus配置的這種方法可能有點麻煩。 為了減少樣板代碼,開發(fā)人員可以使用抽象化較低層配置的解決方案,例如Prometheus Operator 。 在下一篇文章中,我們將看到Prometheus Operator如何促進應用程序的配置。
監(jiān)控愉快!
翻譯自: https://www.javacodegeeks.com/2019/02/applications-running-kubernetes-prometheus.html
總結
以上是生活随笔為你收集整理的使用Prometheus发现在Kubernetes上运行的应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海南省面积 海南省有多大面积
- 下一篇: 最多是什么意思 最多的意思