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

6
我想将Redis配置为LRU缓存。我希望避免在内存限制方面重复自己。如果我这样做:
command: redis-server --maxmemory-policy allkeys-lru 
deploy:
  resources:
    limits:
      memory: 1.5G

这样做是否足够,或者我真的需要像这样添加内容吗(我对操作系统等使用了稍低的尺寸)?
command: redis-server --maxmemory-policy allkeys-lru --maxmemory 1.4G
deploy:
  resources:
    limits:
      memory: 1.5G
1个回答

6

通过检查redis-cli info memory,它会显示总内存为整个系统的大小,因此如果没有maxmemory,它将使用该值。

但是,使用从容器内获取内存限制和一些bash算术运算,您可以创建一个命令来完成工作,因此操作只需要调整一个数字而不是拼凑command。并结合Redis的适当健康检查

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

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接