-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Horizontal Pod Autoscale with Custom Prometheus Metrics #26
Comments
k8s 监控(一)安装 Prometheus k8s 監控(二)監控集群組件和 pod k8s 監控(三)prometheus-adapter k8s 監控(四)監控宿主機 |
基于Custom-metrics-apiserver实现Kubernetes的HPA部署 kube-prometheus-stack (prometheus-operator) 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 部署 prometheus-adapterrules:
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: /
... 部署 APP 和 Service建立 HPA基於 PODapiVersion: 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就進行擴容。
基於 ServiceapiVersion: 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
The text was updated successfully, but these errors were encountered: