Redis Sentinel 集群搭建

1. redis环境

1.1 安装redis

从redis官网下载redis ,https://redis.io/download

$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
$ tar xzf redis-4.0.10.tar.gz
$ cd redis-4.0.10
$ make

1.2 error: jemalloc/jemalloc.h: No such file or directory

yum install epel-release
yum -y install jemalloc

1.3 配置redis

master节点配置

bind 10.0.116.144
port 6379
daemonize yes //启动redis后台运行
logfile "./logs/redis/redis.log" #需要手动在redis安装目录下新建logs文件夹,也可用绝对路径
masterauth 123456 
requirepass 123456

slave节点同上,但多出一个slaveof的配置

slaveof 10.0.116.144 6379

1.4 启动redis

$ ./src/redis-server redis.conf

1.5 测试主从同步

在master上执行

$ redis-cli -h 10.0.116.144 -p 6379 
10.0.116.144:6379> set name abc
OK
10.0.116.144:6379> get name
"abc"

在slave上执行

$ redis-cli -h 10.0.116.13 -p 6379 
10.0.116.13:6379> get name
"abc"

1.6 安装过程中可能会出现warning

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

修改/etc/sysctl.conf文件,增加一行
net.core.somaxconn= 1024
然后执行命令
sysctl -p


补充:
net.core.somaxconn是linux中的一个kernel参数,表示socket监听(listen)的backlog上限。
backlog是socket的监听队列,当一个请求(request)尚未被处理或建立时,他会进入backlog。
而socket server可以一次性处理backlog中的所有请求,处理后的请求不再位于监听队列中。
当server处理请求较慢,以至于监听队列被填满后,新来的请求会被拒绝。
所以说net.core.somaxconn限制了接收新 TCP 连接侦听队列的大小。

对于一个经常处理新连接的高负载 web服务环境来说,默认的 128 太小了。大多数环境这个值建议增加到 1024 或者更多。

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.

修改/etc/sysctl.conf文件,增加一行
vm.overcommit_memory = 1
然后执行命令
sysctl -p


补充: 
overcommit_memory参数说明:
设置内存分配策略(可选,根据服务器的实际情况进行设置)
/proc/sys/vm/overcommit_memory
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
注意:redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)。

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.

分别执行如下语句:

echo 511 > /proc/sys/net/core/somaxconn
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled

/etc/rc.local的最后添加
echo never > /sys/kernel/mm/transparent_hugepage/enabled


2. 搭建Sentinel系统

bind 10.0.116.144
protected-mode no




查看主从信息

redis-cli -h 10.0.116.144 -p 6379 -a 123456 info Replication
启动sentinel
nohup redis-sentinel ./sentinel.conf > ./logs/sentinel.log 2>&1 &


参考 :https://www.jianshu.com/p/d1853dc1b70a

http://debugo.com/redis-sentinel/

猜你喜欢

转载自blog.csdn.net/liufei198613/article/details/80826480