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

<pre><code>docker swarm init
docker stack deploy -c docker-compose.yml MyApplication
</code></pre>

<p>Content of <strong>docker-compose.yml:</strong></p>

<pre><code>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
</code></pre>

<p>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?</p>

<p>Based on CPU utilization of Pods it is possible to autoscale Deployments. You need to use <code>kubectl autoscale</code> 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.</p>

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

<p>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:</p>

<pre><code>$ kubectl autoscale deployment my-app --max 5 --min 2 --cpu-percent 60
</code></pre>

<p>Please, found more about it in the <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#autoscale" rel="nofollow noreferrer">documentation</a> and in the <a href="https://dzone.com/articles/how-to-use-kubernetes-for-autoscaling" rel="nofollow noreferrer">following article</a>. I hope it will helps you.</p>
