#
#  Author: Hari Sekhon
#  Date: 2019-11-27 12:19:45 +0000 (Wed, 27 Nov 2019)
#
#  vim:ts=2:sts=2:sw=2:et
#
#  https://github.com/harisekhon/kubernetes-templates
#
#  License: see accompanying Hari Sekhon LICENSE file
#
#  If you're using my code you're welcome to connect with me on LinkedIn
#  and optionally send me feedback to help improve or steer this or other code I publish
#
#  https://www.linkedin.com/in/HariSekhon
#

# ============================================================================ #
#               H o r i z o n t a l   P o d   A u t o s c a l e r
# ============================================================================ #

# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/

# https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/

# Metrics Server needs to be deployed for HPA to query to make scaling decisions
#
# Metrics Server is automatically available on GKE but may need to be added on EKS

---
apiVersion: autoscaling/v1
#apiVersion: autoscaling/v2beta2  # k8s 1.6+ - scale using the largest of multiple metrics
kind: HorizontalPodAutoscaler
metadata:
  name: APP-hpa
  namespace: NAMESPACE
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: APP-deployment
  minReplicas: 3
  maxReplicas: 5
  targetCPUUtilizationPercentage: 70

  # multiple metrics using autoscaling/v2beta2 - remove targetCPUUtilizationPercentage if using this
#  metrics:
#    - type: Resource
#      resource:
#        name: cpu
#        target:
#          type: Utilization
#          averageUtilization: 70
#    - type: Resource
#      resource:
#        name: memory
#        #targetAverageValue: 250M
#        target:
#          type: AverageValue
#          averageValue: 250Mi

  # Kubernetes 1.20+ - container level resource metrics - check the important container in the pod
# type: ContainerResource
# containerResource:
#   name: cpu
#   container: application
#   target:
#     type: Utilization
#     averageUtilization: 60
