本文來自 Rancher Labs
在過去的文章中,咱們花了至關大的篇幅來聊關於監控的話題。這是由於當你正在管理Kubernetes集羣時,一切都會以極快的速度發生變化。所以有一個工具來監控集羣的健康狀態和資源指標極爲重要。git
在Rancher 2.5中,咱們引入了基於Prometheus Operator的新版監控,它能夠提供Prometheus以及相關監控組件的原生Kubernetes部署和管理。Prometheus Operator可讓你監控集羣節點、Kubernetes組件和應用程序工做負載的狀態和進程。同時,它還可以經過Prometheus收集的指標來定義告警而且建立自定義儀表盤,經過Grafana能夠輕鬆地可視化收集到的指標。你能夠訪問下列連接獲取更多關於新版監控組件的細節:github
https://rancher.com/docs/ranc...golang
新版本的監控也採用prometheus-adapter,開發人員能夠利用其基於自定義指標和HPA擴展他們的工做負載。web
在本文中,咱們將探索如何利用Prometheus Operator來抓取自定義指標並利用這些指標進行高級工做負載管理。json
在Rancher 2.5中安裝Prometheus極爲簡單。僅需訪問Cluster Explorer -> Apps並安裝rancher-monitoring便可。api
你須要瞭解如下默認設置:app
prometheus-adapter
將會做爲chart安裝的一部分啓用ServiceMonitorNamespaceSelector
留爲空,容許 Prometheus 在全部命名空間中收集 ServiceMonitors安裝完成後,咱們能夠從Cluster Explorer訪問監控組件。ide
如今讓咱們部署一個從應用層暴露自定義指標的示例工做負載。該工做負載暴露了一個簡單的應用程序,該應用程序已經使用Prometheus client_golang庫進行了檢測,並在/metric
端點上提供了一些自定義指標。工具
它有兩個指標:ui
如下manifest部署了工做負載、相關服務以及訪問該工做負載的ingress:
apiVersion: apps/v1 kind: Deployment metadata: labels: app.kubernetes.io/name: prometheus-example-app name: prometheus-example-app spec: replicas: 1 selector: matchLabels: app.kubernetes.io/name: prometheus-example-app template: metadata: labels: app.kubernetes.io/name: prometheus-example-app spec: containers: - name: prometheus-example-app image: gmehta3/demo-app:metrics ports: - name: web containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: prometheus-example-app labels: app.kubernetes.io/name: prometheus-example-app spec: selector: app.kubernetes.io/name: prometheus-example-app ports: - protocol: TCP port: 8080 targetPort: 8080 name: web --- apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: prometheus-example-app spec: rules: - host: hpa.demo http: paths: - path: / backend: serviceName: prometheus-example-app servicePort: 8080
ServiceMonitor是一個自定義資源定義(CRD),可讓咱們聲明性地定義如何監控一組動態服務。
你能夠訪問如下連接查看完整的ServiceMonitor規範:
https://github.com/prometheus...
如今,咱們來部署ServiceMonitor,Prometheus用它來收集組成prometheus-example-app Kubernetes服務的pod。
kind: ServiceMonitor metadata: name: prometheus-example-app spec: selector: matchLabels: app.kubernetes.io/name: prometheus-example-app endpoints: - port: web
如你所見,如今用戶能夠在Rancher監控中瀏覽ServiceMonitor。
不久以後,新的service monitor和服務相關聯的pod應該會反映在Prometheus服務發現中。
咱們也可以在Prometheus中看到指標。
在Rancher 2.5中,監控可讓用戶將Grafana儀表盤存儲爲cattle-dashboards
命名空間中的ConfigMaps。
用戶或集羣管理員如今能夠在這一命名空間中添加更多的儀表盤以擴展Grafana的自定義儀表盤。
Dashboard ConfigMap Example
apiVersion: v1 kind: ConfigMap metadata: name: prometheus-example-app-dashboard namespace: cattle-dashboards labels: grafana_dashboard: "1" data: prometheus-example-app.json: | { "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations & Alerts", "type": "dashboard" } ] }, "editable": true, "gnetId": null, "graphTooltip": 0, "links": [], "panels": [ { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "fieldConfig": { "defaults": { "custom": {} }, "overrides": [] }, "fill": 1, "fillGradient": 0, "gridPos": { "h": 9, "w": 12, "x": 0, "y": 0 }, "hiddenSeries": false, "id": 2, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "percentage": false, "pluginVersion": "7.1.5", "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "rate(http_requests_total{code=\"200\",service=\"prometheus-example-app\"}[5m])", "instant": false, "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "http_requests_total_200", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } }, { "aliasColors": {}, "bars": false, "dashLength": 10, "dashes": false, "datasource": null, "description": "", "fieldConfig": { "defaults": { "custom": {} }, "overrides": [] }, "fill": 1, "fillGradient": 0, "gridPos": { "h": 8, "w": 12, "x": 0, "y": 9 }, "hiddenSeries": false, "id": 4, "legend": { "avg": false, "current": false, "max": false, "min": false, "show": true, "total": false, "values": false }, "lines": true, "linewidth": 1, "nullPointMode": "null", "percentage": false, "pluginVersion": "7.1.5", "pointradius": 2, "points": false, "renderer": "flot", "seriesOverrides": [], "spaceLength": 10, "stack": false, "steppedLine": false, "targets": [ { "expr": "rate(http_requests_total{code!=\"200\",service=\"prometheus-example-app\"}[5m])", "interval": "", "legendFormat": "", "refId": "A" } ], "thresholds": [], "timeFrom": null, "timeRegions": [], "timeShift": null, "title": "http_requests_total_not_200", "tooltip": { "shared": true, "sort": 0, "value_type": "individual" }, "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } } ], "schemaVersion": 26, "style": "dark", "tags": [], "templating": { "list": [] }, "time": { "from": "now-15m", "to": "now" }, "timepicker": { "refresh_intervals": [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ] }, "timezone": "", "title": "prometheus example app", "version": 1 }
如今,用戶應該可以在Grafana中訪問prometheus example app的儀表盤。
這一部分假設你已經將prometheus-adapter
做爲監控的一部分安裝完畢了。實際上,在默認狀況下,監控安裝程序會安裝prometheus-adapter。
用戶如今能夠建立一個HPA spec,以下所示:
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: prometheus-example-app-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: prometheus-example-app minReplicas: 1 maxReplicas: 5 metrics: - type: Object object: describedObject: kind: Service name: prometheus-example-app metric: name: http_requests target: averageValue: "5" type: AverageValue
你能夠查看如下連接獲取關於HPA的更多信息:
https://kubernetes.io/docs/ta...
咱們將使用自定義的http_requests_total指標來執行pod自動伸縮。
如今咱們能夠生成一個樣本負載來查看HPA的運行狀況。我可使用hey
進行一樣的操做。
hey -c 10 -n 5000 http://hpa.demo
在本文中,咱們探討了Rancher 2.5中新監控的靈活性。開發人員和集羣管理員能夠利用該堆棧來監控它們的工做負載,部署可視化,並利用Kubernetes內可用的高級工做負載管理功能。