Open mangreen opened 4 years ago
k8s 监控(一)安装 Prometheus https://juejin.cn/post/6844903908251451406
k8s 監控(二)監控集群組件和 pod https://juejin.cn/post/6844903921207492621
k8s 監控(三)prometheus-adapter https://juejin.cn/post/6844903967218991117
k8s 監控(四)監控宿主機 https://juejin.cn/post/6844904057098731534
additionalServiceMonitors:
- name: myapp-monitor
selector:
matchLabels:
app.kubernetes.io/instance: myapp
app.kubernetes.io/name: myapp
namespaceSelector:
# any: true
matchNames:
- myapp
endpoints:
- port: http
interval: 10s
path: /actuator/prometheus
rules:
default: false
custom:
- seriesQuery: 'myapp_http_requests_count'
resources:
template: <<.Resource>>
name:
matches: "^(.*)_count"
as: "${1}_per_second"
metricsQuery: sum(rate(<<.Series>>{<<.LabelMatchers>>}[2m])) by (<<.GroupBy>>)
記得覆寫 prometheus.url 為 http://\<kube-prometheus-stack service>.\<namespace>.svc.cluster.local
helm install <adapter-name> -n <namespace> --set prometheus.url=http://kube-prometheus-stack.monitoring.svc.cluster.local --set prometheus.port=9090 --set prometheus.path=/
...
prometheus:
url: http://prometheus-opreated.monitoring.svc
port: 9090
path: /
...
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: myapp-custom-metrics-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp-deploy-v1.0
minReplicas: 2
maxReplicas: 10
metrics:
- type: Pods
pods:
metric:
name: myapp_http_requests_count
target:
type: AverageValue
averageValue: 5000m
自訂資源指標含義是基於 myapp-deploy-v1.0 這個 deployment 的全部 pod 來計算http_requests的平局值,如果達到 5000m就進行擴容。
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: myapp-custom-metrics-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp-deploy-v1.0
minReplicas: 2
maxReplicas: 10
metrics:
- type: Object
object:
target:
kind: Service
# 這裡是你自己的APP的service
name: myapp-svc
metric:
name: http_requests
target:
type: AverageValue
averageValue: 200
--
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
labels:
appname: myapp-svc
annotations:
prometheus.io/scrape: "true" # 新增內容
prometheus.io/port: "5555" # 新增內容
spec:
type: ClusterIP
ports:
- name: http
port: 5555
targetPort: 5555
selector:
appname: myapp
然後 prometheus-adapter 規則,增加如下內容:
rules:
default: false
custom:
- seriesQuery: '{__name__=~"^http_requests_.*",kubernetes_name!="",kubernetes_namespace!=""}'
seriesFilters: []
resources:
overrides:
kubernetes_namespace:
resource: namespace
kubernetes_name:
resource: service
name:
matches: ^(.*)_(total)$
as: "${1}"
metricsQuery: sum(rate(<<.Series>>{<<.LabelMatchers>>}[1m])) by (<<.GroupBy>>)
https://dev.to/mjace/horizontal-pod-autoscale-with-custom-prometheus-metrics-5gem