Horizontal Pod Auto Scaling

 The Horizontal Pod Autoscaler automatically scales the number of Pods in a replication controller, deployment, replica set or stateful set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics). Note that Horizontal Pod Autoscaling does not apply to objects that can't be scaled, for example, DaemonSets.

1. Create a Deployment

apiVersion: apps/v1

kind: Deployment

metadata:

  name: nginx-deploy

  labels:

    app: nginx-app

spec:

  replicas: 1

  template:

    metadata:

      labels:

        app: nginx-app

    spec:

      serviceAccountName: appsa

      containers:

      - name: nginx-container

        image: nginx:1.7.9

        ports:

        - containerPort: 80

        resources:


          limits:


             cpu: 50m


          requests:


             cpu: 50m

  selector:

    matchLabels:

      app: nginx-app

---

apiVersion: v1

kind: Service

metadata:

  name: my-service

  labels:

    app: nginx-app

spec:

  selector:

    app: nginx-app

  type: NodePort

  ports:

  - nodePort: 31111

    port: 80

    targetPort: 80

2. Create HPA


kubectl autoscale deployment nginx-deploy --cpu-percent=20 --min=1 --max=5

OR you can  create below 

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
     name: nginx
spec:
    scaleTargetRef:
      apiVersion: apps/v1
      kind: Deployment
      name: nginx-deploy
    minReplicas: 1
    maxReplicas: 10
    targetCPUUtilizationPercentage: 10

3. Pods will be autoscaled as per cpu utilization
4. kubectl get hpa
5. while true; do wget -q -O- http://localhost:31111; done

Comments