Increase number of replicas automatically

10/12/2019

I am running a service in Docker Swarm on a single machine. This is what I did to deploy the service:

docker swarm init
docker stack deploy -c docker-compose.yml MyApplication

Content of docker-compose.yml:

version: "3"
services:
    web:
        image: myimage:1.0
        ports:
            - "9000:80"
            - "9001:443"
        deploy:
            replicas: 3
            resources:
                limits:
                    cpus: "0.5"
                    memory: 256M
                restart_policy:
                    condition: on-failure

Is Docker Swarm able to increase number of replicas automatically based on current traffic? If yes, how to configure it to do so? If no, how can I achieve it, maybe use Kubernetes?

-- Bill
docker
docker-compose
docker-swarm
kubernetes

1 Answer

10/15/2019

Based on CPU utilization of Pods it is possible to autoscale Deployments. You need to use kubectl autoscale command, which creates a HorizontalPodAutoscaler object that targets a specified resource and scales it as needed. The HPA periodically adjusts the number of replicas of the scale target to match the average CPU utilization that you specify.

When using kubectl autoscale, you need to specify a maximum and minimum number of replicas for your application, as well as a CPU utilization target.

Take a look for the example: to set the maximum number of replicas to five and the minimum to two, with a CPU utilization target of 60% utilization, run the following command:

$ kubectl autoscale deployment my-app --max 5 --min 2 --cpu-percent 60

Please, found more about it in the documentation and in the following article. I hope it will helps you.

-- muscat
Source: StackOverflow