Scale an app based on custom metrics #
Links: https://cloud.google.com/kubernetes-engine/docs/tutorials/custom-metrics-autoscaling
Here we have nginx ingress controller deployed and we want to scale nginx ingress controller if request count hit more than 85 (nginx_ingress_controller_requests).
First we deployed ingress controller with metrics and add a additional container to export metrics to stack driver If you check nginx ingress helm you can see that metrics are export unde port 10254
extraContainers:
- name: prometheus-to-sd
image: gcr.io/google-containers/prometheus-to-sd:v0.2.3
command:
- /monitor
- --source=:http://localhost:10254
- --stackdriver-prefix=custom.googleapis.com
- --pod-id=$(POD_ID)
- --namespace-id=$(POD_NAMESPACE)
env:
- name: POD_ID
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.uid
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Now if you check stacdriver you should see nginx ingress metrics.
Step 1: Deploy Custom Metrics Stackdriver Adapter To grant GKE objects access to metrics stored in Stackdriver, you need to deploy the Custom Metrics Stackdriver Adapter. To run Custom Metrics Adapter, you must grant your user the ability to create required authorization roles by running the following
command:
kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole cluster-admin --user "$(gcloud config get-value account)"
To deploy the adapter in your cluster, run the following command:
kubectl create -f https://raw.githubusercontent.com/GoogleCloudPlatform/k8s-stackdriver/master/custom-metrics-stackdriver-adapter/deploy/production/adapter.yaml
hpa yaml for the nginx ingress controller
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: dp-nginx-public-nginx-ingress-controller
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1beta1
kind: Deployment
name: dp-nginx-public-nginx-ingress-controller
minReplicas: 1
maxReplicas: 5
metrics:
- type: Pods
pods:
metricName: nginx_ingress_controller_requests
targetAverageValue: 85