如果我对 Redis 服务器的容器设置了内存限制,是否还需要设置 maxmemory?

Arc*_*ano 6 redis docker

我想将 Redis 配置为 LRU 缓存。我想避免因内存限制而重复自己。如果我做

command: redis-server --maxmemory-policy allkeys-lru 
deploy:
  resources:
    limits:
      memory: 1.5G
Run Code Online (Sandbox Code Playgroud)

就足够了还是我真的需要放入这样的东西(我为操作系统等使用了稍小的尺寸)

command: redis-server --maxmemory-policy allkeys-lru --maxmemory 1.4G
deploy:
  resources:
    limits:
      memory: 1.5G
Run Code Online (Sandbox Code Playgroud)

Arc*_*ano 6

检查redis-cli info memory它显示总内存是完整的系统大小,因此如果没有 maxmemory,它可能会使用该值。

然而,使用从容器内部获取内存限制和一些bash算术,您可以创建一个命令来完成这项工作,这样操作只需要调整一个数字,而不是乱搞command. 并与Redis 的适当的 HEALTHCHECK相结合

services:
 cache:
    image: redis:6
    command:
      - bash
      - -c
      - redis-server --appendonly yes --maxmemory $$(( $$( cat /sys/fs/cgroup/memory/memory.limit_in_bytes 2>/dev/null || cat /sys/fs/cgroup/memory.max ) - 100000000)) --maxmemory-policy volatile-lru
    healthcheck:
      test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
    networks:
      - default
    deploy:
      resources:
        limits:
          memory: 512M
Run Code Online (Sandbox Code Playgroud)

这会将 maxmemory 设置为 compose 文件中定义的值减去 100MB 的开销。

cat中间的两个尝试使用CGroupV2 和 CGroup V1 位置来确定内存限制