One of the most requested features in Kamal 2 is the ability to easily deploy multiple applications on a single server. While Kamal makes this process straightforward, running multiple apps on shared hardware comes with an important consideration: resource management. You need to distribute CPU and memory wisely to ensure each application gets its fair share without starving others.
Kamal uses Docker under the hood, specifically the docker run
command to manage containers. This means we can leverage Docker’s resource limitation options, but the configuration method differs between:
For your main application and accessories, add options key to your deploy.yml
:
# config/deploy.yml
servers:
host: HOST
options:
cpus: "1" # Use 1 CPU core
memory: 512M # Limit memory to 512MB
accessories:
redis:
options:
cpus: "0.5" # Half a CPU core
memory: 256M # 256MB memory limit
The proxy container (Kamal proxy) requires a different approach. We need to use the Kamal CLI to set Docker options:
# Set CPU and memory limits for proxy
kamal proxy boot_config set --docker-options="cpus=0.5 memory=256M"
# Verify current proxy settings
kamal proxy boot_config get
# Restart the proxy to apply the changes
kamal proxy reboot
Need to learn more? Check out: