I have a docker compose file with around 15-20 containers.
In idle or during usual traffic it uses around 5-10% of cpu total (just 10% of one core of 8-core vm).
On a deploy (simultanious restart of containers) or right after VM restart the CPU load peaks to 300-400% for a 30sec-90sec, Load Average peaks to 6-8 and some user requests may timeout.
Why it's happening? What to do?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The issue you're experiencing is not uncommon, especially in containerized environments with multiple services. Let's break down the possible causes and potential solutions:
Causes:
Container startup overhead: When you restart or deploy containers simultaneously, each container needs to initialize, load configurations, and start its services. This process can cause a spike in CPU usage, especially if there are many containers.
Docker daemon overhead: The Docker daemon manages container lifecycles, network settings, and volumes. During a container restart or deployment, the Docker daemon may experience increased load, contributing to the CPU spike.
Service dependencies and initialization: Some services might depend on others to be fully operational before they can start. If these dependencies are not properly managed, it can lead to a cascade of startups, causing a CPU spike.
Resource constraints: Although your VM has an 8-core CPU, the Docker containers might not be properly configured to utilize multiple cores efficiently, leading to a bottleneck.
Solutions and optimizations:
Staggered container restarts: Instead of restarting all containers simultaneously, consider staggering their restarts to prevent a massive CPU spike. You can achieve this by using a tool like docker-compose with a depends_on directive or by creating a custom restart script.
Adjust Docker daemon configuration: You can tweak the Docker daemon's configuration to improve performance:
Increase the oom-kill-disable value to prevent the OOM (Out-of-Memory) killer from interfering with your containers.
Adjust the exec-opts value to optimize container execution.
Optimize service initialization: Review your services' initialization processes and optimize them to reduce startup time. This might involve:
Caching frequently accessed data.
Preloading dependencies.
Optimizing database queries.
Resource allocation and limits: Ensure that your containers have proper resource limits and reservations:
Set cpuset constraints to pin containers to specific CPU cores.
Configure cpu-shares to allocate CPU resources fairly among containers.
Use docker-composedeploy options to set resource limits.
Monitoring and auto-scaling: Implement monitoring tools (e.g., Prometheus, Grafana) to track CPU usage, Load Average, and other key metrics. Consider setting up auto-scaling rules to dynamically adjust resource allocation or container counts based on demand.
Consider using a more efficient container runtime: If you're using the traditional Docker daemon, you might want to explore alternative container runtimes like containerd or cri-o, which are designed for better performance and scalability.
Example docker-compose configuration adjustments:
yaml
version: '3'
services:
your_service:
# ...
deploy:
resources:
limits:
cpus: '0.5' # Limit to 50% of a CPU core
memory: 512M
reservations:
cpus: '0.25' # Reserve 25% of a CPU core
memory: 256M
depends_on:
- other_service
restart:
unless-stopped: true
In this example, the your_service container has resource limits and reservations set, and it depends on other_service to ensure staggered startup.
By implementing these strategies, you should be able to mitigate the CPU spike and Load Average issues during deployments or VM restarts. Monitor your system and adjust the configurations as needed to achieve optimal performance.
User
Limiting CPU in compose doesnt help - still CPU peaks on deploy. Maybe less peaks but still high load. Even to healthchecks timeout.
Assistant
Limiting CPU in docker-compose can help, but it might not completely eliminate the CPU peak during deployments. Let's explore additional strategies to mitigate the issue:
Healthcheck timeouts:
Increase healthcheck timeouts: You can increase the timeout value for healthchecks in your docker-compose file to give containers more time to recover:
Disable Docker's OOM killer: You can disable Docker's OOM killer by setting oom-kill-disable to true in your daemon.json file:
json
{
"oom-kill-disable": true
}
Adjust Docker's CPU CFS (Completely Fair Scheduler): You can adjust Docker's CPU CFS by setting cpu-cfs-quota and cpu-cfs-period in your daemon.json file:
version: '3'
services:
your_service:
# ...
deploy:
resources:
limits:
cpus: '0.5' # Limit to 50% of a CPU core
memory: 512M
reservations:
cpus: '0.25' # Reserve 25% of a CPU core
memory: 256M
healthcheck:
test: ["CMD-SHELL", "curl --fail http://localhost:8080/health || exit 1"]
interval: 10s
timeout: 30s
retries: 3
restart: on-failure
depends_on:
- other_service
By implementing these strategies, you should be able to further mitigate the CPU peak and Load Average issues during deployments. Monitor your system and adjust the configurations as needed to achieve optimal performance.