参考官网:redis from source
测试机器(sentine+redis)
redis server端口:6380
密码:*********
10.10.10.3 (master)
10.10.10.2 (slave)
10.10.10.1 (slave)
sentinel端口:26380
sentinel列表:10.10.10.3 10.10.10.2 10.10.10.1
安装步骤
-
下载
get https://download.redis.io/redis-stable.tar.gz
-
编译
tar -xzvf redis-stable.tar.gz cd redis-stable make
-
安装
# To install these binaries in /usr/local/bin make install
-
启动测试
redis-server
-
主从配置
cd /data/apps/redis-6.2.6 mkdir config mkdir logs vim config/redis-6380.conf
master配置文件
port 6380 pidfile /data/apps/redis-6.2.6/redis_6380.pid requirepass ******** protected-mode no daemonize yes bind 0.0.0.0 #从配置加上以下 #replicaof 10.10.10.3 6380 #masterauth **********
持久化策略配置
#RDB的默认策略 #表示在 3600 秒内,至少更新了 1 条数据,Redis 自动触发 BGSAVE 命令,将数据保存到硬盘。 save 3600 1 #表示在 300 秒内,至少更新了 100 条数据,Redis 自动触 BGSAVE 命令,将数据保存到硬盘。 save 300 100 #表示 60 秒内,至少更新了 10000 条数据,Redis 自动触发 BGSAVE 命令,将数据保存到硬盘。 save 60 10000 #开启aof,默认no appendonly yes
-
启动服务
# 分别启动主从三台机器
/data/apps/redis-6.2.6/src/redis-server /data/apps/redis-6.2.6/config/redis-6380.conf &
-
验证主从同步
#master 127.0.0.1:6380> info # Replication role:master connected_slaves:2 slave0:ip=10.10.10.2,port=6380,state=online,offset=84,lag=0 slave1:ip=10.0.10.1,port=6380,state=online,offset=84,lag=0 master_failover_state:no-failover 127.0.0.1:6380> set test test OK #slave 127.0.0.1:6380> get test "test"
配置Sentinel
-
新建配置文件
cd /data/apps/redis-6.2.6 cp sentinel.conf config/sentinel-26380.conf
配置文件内容
sentinel三台节点增加以下配置
scp sentinel-26380.conf root@node2:/data/apps/redis-6.2.6/config
scp sentinel-26380.conf root@node3:/data/apps/redis-6.2.6/config
port 26380 daemonize yes pidfile "/data/apps/redis-6.2.6/redis-sentinel-26380.pid" logfile "/data/apps/redis-6.2.6/sentinel-26380.log" sentinel monitor mymaster 10.10.10.3 6380 2 sentinel auth-pass mymaster ****** sentinel down-after-milliseconds mymaster 30000 sentinel failover-timeout mymaster 180000
quorum指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),
master才算真正失效
-
启动
/data/apps/redis-6.2.6/src/redis-sentinel /data/apps/redis-6.2.6/config/sentinel-26380.conf &
-
测试验证
-
sentinel主从节点配置文件自动生成以下内容
# Generated by CONFIG REWRITE protected-mode no user default on nopass sanitize-payload ~* &* +@all dir "/data/apps/redis-6.2.6/config" sentinel myid effab961c271f376dc9756f48fe7eb65344bb044 sentinel config-epoch mymaster 0 sentinel leader-epoch mymaster 0 sentinel current-epoch 0 sentinel known-sentinel mymaster 10.10.10.3 26380 06ff1d8a30bcd1d8c49f4c0c012a7a49c23cc2ce sentinel known-replica mymaster 10.10.10.1 6380 sentinel known-sentinel mymaster 10.10.10.2 26380 926466d6701e9c412684b06eb971720ccb03486a sentinel known-replica mymaster 10.10.10.2 6380
-
查看info
[root@]# redis-cli -p 26380 127.0.0.1:26380> info # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=10.10.10.3:6380,slaves=2,sentinels=3
-
验证redis master宕机,主从自动切换
#在redismaster 上执行stop redis,30s后查看 127.0.0.1:26380> info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=10.10.10.2:6380,slaves=2,sentinels=3 #redis master已经切换为10.10.10.2为主,查看master info 127.0.0.1:6380> info replication # Replication role:master connected_slaves:1 slave0:ip=10.10.10.1,port=6380,state=online,offset=139145,lag=0 master_failover_state:no-failover master_replid:15a89a2d2953d142a8e3f1e07cd849190ad3eada master_replid2:e8ddb137361138d8048d91f988758e01a366d02b master_repl_offset:139159 second_repl_offset:111543 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:139159
-
配置supervisor管理
注意:需要设置redis和sentinel为前台启动daemonize no
-
vim /etc/supervisor.d/redis.conf
[program:redis] command=/data/apps/redis-6.2.6/src/redis-server /data/apps/redis-6.2.6/config/redis-6380.conf priority=100 process_name=%(program_name)s user=root autostart=true autorestart=true startsecs=3 stdout_logfile=/data/apps/redis-6.2.6/logs/redis_stdout.log stdout_logfile_maxbytes=100MB stdout_logfile_backups=5 stderr_logfile=/data/apps/redis-6.2.6/logs/redis_std_error.log stderr_logfile_maxbytes=100MB stderr_logfile_backups=5
-
vim /etc/supervisor.d/redis_sentinel.conf
[program:redis_sentinel] command=/data/apps/redis-6.2.6/src/redis-sentinel /data/apps/redis-6.2.6/config/sentinel-26380.conf priority=100 process_name=%(program_name)s user=root autostart=true autorestart=true startsecs=3 stdout_logfile=/data/apps/redis-6.2.6/logs/sentinel_stdout.log stdout_logfile_maxbytes=100MB stdout_logfile_backups=5 stderr_logfile=/data/apps/redis-6.2.6/logs/sentinel_std_error.log stderr_logfile_maxbytes=100MB stderr_logfile_backups=5
错误记录
-
make
编译报错fatal error: systemd/sd-daemon.h: No such file or directory
yum install systemd-devel
-
make test
报错You need tcl 8.5 or newer in order to run the Redis test
解决:
yum install tcl.x86_64
-
supervisor配置启动失败
解决:
#修改启动方式为前台启动 daemonize no
参考文献
安装:https://redis.io/docs/getting-started/installation/install-redis-from-source/
搭建集群:https://www.cnblogs.com/kevingrace/p/9004460.html