Horizontal Pod Autoscaler for two deployments and same pod

137 Views Asked by At

I have setup like this:

  • one container "cont1" in Azure ACR with one image and one tag (image1:1.0.0),
  • AKS cluster with one custom defined namespace: testns,
  • two separate deployments: deploy1, deploy2.

Those deployments share same (mentioned above) image. I have separate defined resources for each deplyment wit different CPU and Memory parameteres. My trouble is that I would like to have two separate HPA for every deployment (separatly). If I make two separated HPAs Kubernetes will combine metrics from two deplyments' pods. There is also information that two HPA cannot contol one image. How to do this?

1

There are 1 best solutions below

3
On

Don't think HPA cobines metrics from two deployments, unless you have used same labels for the pods. Here is an example based on https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/

Deployment 1

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache1
spec:
  selector:
    matchLabels:
      run: php-apache1
  template:
    metadata:
      labels:
        run: php-apache1
    spec:
      containers:
      - name: php-apache1
        image: registry.k8s.io/hpa-example
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
  name: php-apache1
  labels:
    run: php-apache1
spec:
  ports:
  - port: 80
  selector:
    run: php-apache1

Deploy it on namespace testns

kubectl apply -f php-apache1.yaml -n testns

Deployment 2

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache2
spec:
  selector:
    matchLabels:
      run: php-apache2
  template:
    metadata:
      labels:
        run: php-apache2
    spec:
      containers:
      - name: php-apache2
        image: registry.k8s.io/hpa-example
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m
---
apiVersion: v1
kind: Service
metadata:
  name: php-apache2
  labels:
    run: php-apache2
spec:
  ports:
  - port: 80
  selector:
    run: php-apache2

Deploy it on namespace testns

kubectl apply -f php-apache2.yaml -n testns

Then run a load on the first deployment

kubectl run -i --tty load-generator --rm --image=busybox:1.28 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache1.testns.svc.cluster.local:80; done"

Then we can observer HPA triggers correctly on specific deployment.

NAME          REFERENCE                TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
php-apache1   Deployment/php-apache1   57%/50%   1         10        7          15m
php-apache2   Deployment/php-apache2   0%/50%    1         10        1          8m8s