---
# System prepended metadata

title: '[k8s] Horizontal Pod Autoscaling'
tags: [hpa, kubernetes]

---

[k8s] Horizontal Pod Autoscaling
============
###### tags: `kubernetes` `hpa`

HPA will increase and decrease the number of replicas to maintain an average CPU utilization across all Pods as per the defined value (e.g.: 50%)

![](https://i.imgur.com/Bou6KSB.png =400x)

```yaml=
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploymentnginx
  namespace: default
spec:
  replicats: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata: 
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: "250m"
            
---
apiVersion: v1
kind: Service
metadata:
  name: nginxservice
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
    - protocal: TCP
      port: 80
      nodePort: 31050
      targetPort: nginxwebport

---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: hpanginx
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: deploymentnginx
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50 
```
