Skip to content

Instantly share code, notes, and snippets.

@sylr
Last active November 8, 2019 08:53
Show Gist options
  • Select an option

  • Save sylr/99210b71072b87c0a955691c17dcae10 to your computer and use it in GitHub Desktop.

Select an option

Save sylr/99210b71072b87c0a955691c17dcae10 to your computer and use it in GitHub Desktop.
k8s-prometheus-adapter deployment avg cpu usage / deployment limit ratio rule (based on cAdvisor and kube-state-metrics)
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta1
metadata:
name: {{ HPA }}
spec:
scaleTargetRef:
apiVersion: extensions/v1beta1
kind: Deployment
name: {{ DEPLOYMENT }}
minReplicas: 2
maxReplicas: 10
metrics:
- type: Object
object:
metricName: deployment_cpu_usage_limit_ratio
target:
apiVersion: extensions/v1beta1
kind: Deployment
name: {{ DEPLOYMENT }}
targetValue: 0.5
rules:
default: false
custom:
- seriesQuery: kube_deployment_created{namespace!="",deployment!=""}
resources:
overrides:
deployment:
resource: deployment
namespace:
resource: namespace
metricsQuery: |-
avg(
avg(label_replace(kube_pod_owner{namespace=~"<< index .LabelValuesByName "namespace" >>"}, "replicaset", "$1", "owner_name", "(.*)")) by (namespace, replicaset, pod)
* on (namespace, replicaset) group_left(deployment) avg(label_replace(kube_replicaset_owner{namespace=~"<< index .LabelValuesByName "namespace" >>",owner_name=~"<< index .LabelValuesByName "deployment" >>"}, "deployment", "$1", "owner_name", "(.*)")) by (namespace, replicaset, deployment)
* on (namespace, pod) group_left
label_replace(
(
avg(
rate(container_cpu_system_seconds_total{namespace=~"<< index .LabelValuesByName "namespace" >>",pod_name!="",container_name!~"(|POD)"}[2m])
+ rate(container_cpu_user_seconds_total{namespace=~"<< index .LabelValuesByName "namespace" >>",pod_name!="",container_name!~"(|POD)"}[2m])
) by (namespace,pod_name)
/
avg(
sum(
label_replace(
label_replace(kube_pod_container_resource_limits_cpu_cores{namespace=~"<< index .LabelValuesByName "namespace" >>"}, "pod_name", "$1", "pod", "(.*)"),
"container_name", "$1", "container", "(.*)"
)
) by (namespace, pod_name, container_name)
) by (namespace, pod_name)
),
"pod", "$1",
"pod_name", "(.*)"
)
) by (namespace, deployment)
name:
as: deployment_cpu_usage_limit_ratio
matches: ^kube_deployment_created$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment