主要参考自 https://segmentfault.com/a/1190000014091287 并做相应的修改

Redis

macOS上安装Redis一般使用hombrew 安装: brew install redis 启动: redis-server

这里主要介绍docker上运行,以及一些问题

拉取镜像并尝试启动,以作简单调试

#拉取镜像,默认最新
docker pull redis

如果简单调试,直接执行以下命令就可以了:

docker run --name redis -d -p 6379:6379 redis

执行redis-cli

docker exec -it <名称/id> redis-cli

远程连接

#远程连接 比如我docker宿主机ip为10.10.39.57,redis映射的端口为6379
redis-cli -h 10.10.39.57 -p 6379

启动警告分析

启动时可以看到基础警告,

root@aml:~/docker/redis# docker run --name redis  redis
1:C 22 May 2019 09:28:40.070 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 22 May 2019 09:28:40.070 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 22 May 2019 09:28:40.070 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 22 May 2019 09:28:40.076 * Running mode=standalone, port=6379.
1:M 22 May 2019 09:28:40.076 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 22 May 2019 09:28:40.076 # Server initialized
1:M 22 May 2019 09:28:40.076 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 22 May 2019 09:28:40.076 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 22 May 2019 09:28:40.076 * Ready to accept connections

有几处警告: https://blog.51cto.com/mashushu/1926941

TCP backlog

1:C 22 May 2019 09:28:40.070 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf

意思是:TCP backlog设置值,511没有成功,因为 /proc/sys/net/core/somaxconn这个设置的是更小的128. linux里的backlog详解

解决方法:

参考issue。docker启动redis时加参数--sysctl net.core.somaxconn=511,这种解决方式比较简单。

#发现启动时没有该错误了
docker run --name redis --rm  --sysctl net.core.somaxconn=511 redis

overcommit_memory

#WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

意思是:overcommit_memory参数设置为0!在内存不足的情况下,后台程序save可能失败。

overcommit_memory表示内存分配策略: 0 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程 1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。 2 表示内核允许分配超过所有物理内存和交换空间总和的内存

参考此处: https://stackoverflow.com/questions/43843197/how-to-fix-the-warnings-when-running-the-redisalpine-docker-image

vm.overcommit_memory不能直接用--sysctl来设置。

解决方法:

添加行vm.overcommit_memory=1至底部/etc/sysctl.conf (未解决有时间再研究)

THP

1:M 22 May 2019 09:28:40.076 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

意思是:你使用的是透明大页,可能导致redis延迟和内存使用问题。

解决方法,宿主机器(linux)执行如下命令:

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

基本的启动配置

创建redis目录,并在目录下添加data文件夹、config文件夹。

└── redis
    ├── config
    │   └── redis.conf
    └── data

下载配置文件到config文件夹

wget http://download.redis.io/redis-stable/redis.conf -O config/redis.conf

# 直接替换编辑(可选)
#sed -i 's/logfile ""/logfile "access.log"/' conf/redis.conf
#sed -i 's/# requirepass foobared/requirepass 123456/' conf/redis.conf
#sed -i 's/appendonly no/appendonly yes/' conf/redis.conf
# 这里可能还需配置一些 bind protected-mode
#protected-mode 是在没有显示定义 bind 地址(即监听全网断),又没有设置密码 requirepass时,protected-mode 只允许本地回环 127.0.0.1 访问。
#也就是说当开启了 protected-mode 时,如果你既没有显示的定义了 bind 监听的地址,同时又没有设置 auth 密码。那你只能通过 127.0.0.1 来访问 redis 服务。

编写启动脚本:

docker run --name redis --restart=always\
	-v `pwd`/redis/data:/data:rw  \
	-v `pwd`/redis/config/redis.conf:/etc/redis/redis.conf:ro\
	--privileged=true \
	--sysctl net.core.somaxconn=511 \
	-d -p 6379:6379  \
	redis redis-server /etc/redis/redis.conf
#参数解释
# :rw 为读写  :ro 为ro 为readonly
# --privileged=true \ # 给与一些权限
# --sysctl net.core.somaxconn=511 见启动警告说明

访问

#访问reids
docker exec -it <名称/id> bash
#访问cli 
docker exec -it <名称/id> redis-cli
#重新启动
docker restart <名称/id> 
#远程连接 比如我docker宿主机ip为10.10.39.57,redis映射的端口为6379
redis-cli -h 10.10.39.57 -p 6379