Skip to content

Commit

Permalink
Set memory limit for containers to disable swapping
Browse files Browse the repository at this point in the history
Settings a memory limit and swap limit equal to memory will disable swapping. Some containers need a memory limit because of memory leaks (workers, app). Other containers don't specifically need a limit but set ony anyway to ensure they don't start swapping memory.
  • Loading branch information
aequitas committed Jun 19, 2024
1 parent 6f7c310 commit 31c3d32
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
6 changes: 6 additions & 0 deletions docker/defaults.env
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ PUBLIC_SUFFIX_LIST_URL=
# configure built-in unbound resolver to forward to integration test resolver for internal network
INTEGRATION_TESTS=False

# Default memory limit for containers
DEFAULT_MEMORY_LIMIT=1G

# Memory limit for containers with high memory usage
HIGH_MEMORY_LIMIT=5G

# Amount of concurrent worker (green)threads
WORKER_CONCURRENCY=100
# limit worker memory so it won't grow into swap
Expand Down
77 changes: 69 additions & 8 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_WEBSERVER_INTERNAL
Expand Down Expand Up @@ -75,6 +78,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $HIGH_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $HIGH_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_APP_INTERNAL
Expand Down Expand Up @@ -216,10 +222,9 @@ services:
dockerfile: docker/Dockerfile
target: app
restart: unless-stopped
deploy:
resources:
limits:
memory: $WORKER_MEMORY_LIMIT
mem_limit: $WORKER_MEMORY_LIMIT
# disable swap by setting swap to the memory limit
memswap_limit: $WORKER_MEMORY_LIMIT
logging:
driver: $LOGGING_DRIVER
options:
Expand Down Expand Up @@ -336,6 +341,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
network_mode: "none"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
Expand All @@ -344,10 +352,9 @@ services:
worker-slow:
# copy all attributes from 'worker' service and overwrite specific settings below
<<: *worker
deploy:
resources:
limits:
memory: $WORKER_SLOW_MEMORY_LIMIT
mem_limit: $WORKER_SLOW_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $WORKER_SLOW_MEMORY_LIMIT

command: celery --app internetnl worker --without-gossip --pool=eventlet --time-limit=600 --concurrency=$WORKER_SLOW_CONCURRENCY
--queues slow_db_worker,batch_slow
Expand All @@ -365,6 +372,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
- internal
- public-internet
Expand Down Expand Up @@ -434,6 +444,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_REDIS_INTERNAL
Expand All @@ -457,6 +470,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
user: rabbitmq
networks:
internal:
Expand All @@ -482,6 +498,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $HIGH_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $HIGH_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_POSTGRES_INTERNAL
Expand All @@ -506,6 +525,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $HIGH_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $HIGH_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_ROUTINATOR_INTERNAL
Expand Down Expand Up @@ -542,6 +564,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_UNBOUND_INTERNAL
Expand Down Expand Up @@ -588,6 +613,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_RESOLVER_INTERNAL_PERMISSIVE
Expand Down Expand Up @@ -621,6 +649,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_RESOLVER_INTERNAL_VALIDATING
Expand Down Expand Up @@ -660,6 +691,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal: {}
public-internet: {}
Expand Down Expand Up @@ -717,6 +751,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_GRAFANA_INTERNAL
Expand All @@ -739,6 +776,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_PROMETHEUS_INTERNAL
Expand All @@ -757,6 +797,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
- internal

Expand All @@ -771,6 +814,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
- internal

Expand All @@ -786,6 +832,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
internal:
ipv4_address: $IPV4_IP_STATSD_INTERNAL
Expand All @@ -804,6 +853,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
- internal
depends_on:
Expand All @@ -829,6 +881,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
pid: host
networks:
- internal
Expand All @@ -848,6 +903,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
- internal
volumes:
Expand All @@ -866,6 +924,9 @@ services:
driver: $LOGGING_DRIVER
options:
tag: '{{.Name}}'
mem_limit: $DEFAULT_MEMORY_LIMIT
# disable swap by settings swap to the memory limit
memswap_limit: $DEFAULT_MEMORY_LIMIT
networks:
- internal
volumes:
Expand Down

0 comments on commit 31c3d32

Please sign in to comment.