Horizontal Pod Autoscaler on Kubernetes

If you are using Minikube, enable Metrics Server addon as Horizontal Pod Autoscaler uses this API to collect metrics:

$ minikube addons enable metrics-server
The 'metrics-server' addon is enabled
Create the following manifest file hpa.yml:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hpa-example
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hpa-example
  template:
    metadata:
      labels:
        app: hpa-example
    spec:
      containers:
      - name: hpa-example
        image: gcr.io/google_containers/hpa-example
        ports:
        - name: http-port
          containerPort: 80
        resources:
          requests:
            cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
  name: hpa-example
spec:
  ports:
  - port: 31001
    nodePort: 31001
    targetPort: http-port
    protocol: TCP
  selector:
    app: hpa-example
  type: NodePort
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-example-autoscaler
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: hpa-example
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50
Apply it:
$ kubectl apply -f hpa.yml
Run the following command and wait until the metrics will be collected:
$ kubectl get hpa
Periodically check the number of pods:
$ kubectl get po
Increase the load - run a busybox Pod and inside of it - increase the load:
$ kubectl run -it --rm load-generator --image=busybox /bin/sh
/ # while true; do wget -q -O- http://hpa-example.default.svc.cluster.local:31001; done
Periodically check the stats:
$ kubectl get hpa
$ kubectl get po
After number of pods increased, stop the load and wait - the number of Pods will drop out as load decreases.