redis-3.2.6
redis3 安装,启动,停止,连接
redis3主从复制安装配置
建议最少配3个哨兵,
如果只有两台服务器,(一个服务器也可装多个redis,端口不同)
主redis服务器,1个哨兵 (防火墙需要开通6379,26379端口)
从redis服务器,2个哨兵(防火墙需要开通6380,26380,26381端口)
(哨兵应该安装在3个服务器上,要不有可能主从切换失败)
java端配置连接池,连接多个哨兵
jedis-2.2.1可以这样使用
sentinels.add("192.168.2.11:26379");
sentinels.add("192.168.2.11:26380");
sentinels.add("192.168.2.11:26381");
也可以在一台机器上测试
sentinel26379.conf
#保护模式
protected-mode no
#端口
port 26379
#守护进程
#daemonize yes
#设置pid文件路径
pidfile /opt/redis/sentinel_26379.pid
#工作路径,不用改
dir /tmp
#设置日志文件路径
#logfile /opt/redis/logs/sentinel26379.log
# 哨兵监控这个master,在至少quorum个哨兵实例都认为master down后把master标记为odown
# (objective down客观down;相对应的存在sdown,subjective down,主观down)状态。
# slaves是自动发现,不用指定slaves。
se
sentinel26380.conf
#保护模式
protected-mode no
#端口
port 26380
#守护进程
#daemonize yes
#设置pid文件路径
pidfile /opt/redis/sentinel_26380.pid
#工作路径,不用改
dir /tmp
#设置日志文件路径
#logfile /opt/redis/logs/sentinel26380.log
# 哨兵监控这个master,在至少quorum个哨兵实例都认为master down后把master标记为odown
# (objective down客观down;相对应的存在sdown,subjective down,主观down)状态。
# slaves是自动发现,不用指定slaves。
sentinel monitor mymaster 192.168.2.11 6379 2
# master或slave多长时间(默认30秒)不能使用后标记为s_down状态。
sentinel down-after-milliseconds mymaster 30000
#选项指定了在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步, 这个数字越小, 完成故障转移所需的时间就越长。
sentinel parallel-syncs mymaster 1
#若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。
sentinel failover-timeout mymaster 180000
#设置master和slaves的密码
sentinel auth-pass mymaster redis2016
sentinel26381.conf
#保护模式
protected-mode no
#端口
port 26381
#守护进程
#daemonize yes
#设置pid文件路径
pidfile /opt/redis/sentinel_26381.pid
#工作路径,不用改
dir /tmp
#设置日志文件路径
#logfile /opt/redis/logs/sentinel26381.log
# 哨兵监控这个master,在至少quorum个哨兵实例都认为master down后把master标记为odown
# (objective down客观down;相对应的存在sdown,subjective down,主观down)状态。
# slaves是自动发现,不用指定slaves。
sentinel monitor mymaster 192.168.2.11 6379 2
# master或slave多长时间(默认30秒)不能使用后标记为s_down状态。
sentinel down-after-milliseconds mymaster 30000
#选项指定了在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步, 这个数字越小, 完成故障转移所需的时间就越长。
sentinel parallel-syncs mymaster 1
#若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。
sentinel failover-timeout mymaster 180000
#设置master和slaves的密码
sentinel auth-pass mymaster redis2016
sentinel monitor mymaster 192.168.2.11 6379 2
这个要配局域网IP,否则远程连不上,
最后的quorum最好配成2,不要配成1,否则容易切换失败
测试时可以把
sentinel down-after-milliseconds mymaster 30000 参数设短点,减少等待时间
daemonize no
logfile ""
这样方便看日志,好停止
启动
/opt/redis/bin/redis-sentinel /opt/redis/conf/sentinel26379.conf
/opt/redis/bin/redis-sentinel /opt/redis/conf/sentinel26380.conf
/opt/redis/bin/redis-sentinel /opt/redis/conf/sentinel26381.conf
连接
/opt/redis/bin/redis-cli -h 192.168.2.11 -p 26379 -a redis2016
停止
/opt/redis/bin/redis-cli -h 192.168.2.11 -p 26379 -a redis2016 shutdown
模拟
结束redis,sentinel26379进程
查看reids6380日志
查看sentinel26380,sentinel26381日志
可查看主从切换过程
结束进程
[root@dev ~]# ps -ef|grep redis
root 23703 1012 0 17:49 pts/1 00:00:00 /opt/redis/bin/redis-server *:6379
root 23706 2392 0 17:49 pts/5 00:00:00 /opt/redis/bin/redis-server *:6380
root 23723 2437 1 17:49 pts/6 00:00:00 /opt/redis/bin/redis-sentinel *:26379 [sentinel]
root 23726 14438 1 17:49 pts/10 00:00:00 /opt/redis/bin/redis-sentinel *:26380 [sentinel]
root 23743 2493 1 17:49 pts/8 00:00:00 /opt/redis/bin/redis-sentinel *:26381 [sentinel]
root 23753 15605 0 17:50 pts/9 00:00:00 grep redis
[root@dev ~]# kill -9 23703 23723
1.主redis日志
23703:M 17 Jan 17:49:27.318 # Server started, Redis version 3.2.6
23703:M 17 Jan 17:49:27.319 # 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.
23703:M 17 Jan 17:49:27.320 # 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.
23703:M 17 Jan 17:49:27.320 * DB loaded from disk: 0.000 seconds
23703:M 17 Jan 17:49:27.320 * The server is now ready to accept connections on port 6379
23703:M 17 Jan 17:49:30.262 * Slave 192.168.2.11:6380 asks for synchronization
23703:M 17 Jan 17:49:30.263 * Full resync requested by slave 192.168.2.11:6380
23703:M 17 Jan 17:49:30.263 * Starting BGSAVE for SYNC with target: disk
23703:M 17 Jan 17:49:30.275 * Background saving started by pid 23709
23709:C 17 Jan 17:49:30.343 * DB saved on disk
23709:C 17 Jan 17:49:30.345 * RDB: 6 MB of memory used by copy-on-write
23703:M 17 Jan 17:49:30.349 * Background saving terminated with success
23703:M 17 Jan 17:49:30.350 * Synchronization with slave 192.168.2.11:6380 succeeded
已杀死
2.从redis6380日志
23706:S 17 Jan 17:49:30.257 # Server started, Redis version 3.2.6
23706:S 17 Jan 17:49:30.258 # 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.
23706:S 17 Jan 17:49:30.258 # 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.
23706:S 17 Jan 17:49:30.258 * DB loaded from disk: 0.001 seconds
23706:S 17 Jan 17:49:30.258 * The server is now ready to accept connections on port 6380
23706:S 17 Jan 17:49:30.259 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:49:30.259 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:49:30.260 * Non blocking connect for SYNC fired the event.
23706:S 17 Jan 17:49:30.260 * Master replied to PING, replication can continue...
23706:S 17 Jan 17:49:30.262 * Partial resynchronization not possible (no cached master)
23706:S 17 Jan 17:49:30.288 * Full resync from master: f43a3a2b02420a869d003c5394e07ee2ec145f84:1
23706:S 17 Jan 17:49:30.350 * MASTER <-> SLAVE sync: receiving 168 bytes from master
23706:S 17 Jan 17:49:30.350 * MASTER <-> SLAVE sync: Flushing old data
23706:S 17 Jan 17:49:30.350 * MASTER <-> SLAVE sync: Loading DB in memory
23706:S 17 Jan 17:49:30.351 * MASTER <-> SLAVE sync: Finished with success
23706:S 17 Jan 17:50:52.645 # Connection with master lost.
23706:S 17 Jan 17:50:52.646 * Caching the disconnected master state.
23706:S 17 Jan 17:50:52.837 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:50:52.837 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:50:52.838 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:50:53.848 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:50:53.849 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:50:53.849 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:50:54.879 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:50:54.880 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:50:54.880 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:50:55.891 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:50:55.891 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:50:55.892 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:50:56.898 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:50:56.898 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:50:56.899 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:50:57.906 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:50:57.907 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:50:57.907 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:50:58.917 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:50:58.918 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:50:58.918 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:50:59.927 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:50:59.932 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:50:59.932 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:51:00.940 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:51:00.941 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:51:00.941 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:51:01.949 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:51:01.950 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:51:01.950 # Error condition on socket for SYNC: Connection refused
23706:S 17 Jan 17:51:02.959 * Connecting to MASTER 192.168.2.11:6379
23706:S 17 Jan 17:51:02.960 * MASTER <-> SLAVE sync started
23706:S 17 Jan 17:51:02.960 # Error condition on socket for SYNC: Connection refused
23706:M 17 Jan 17:51:03.125 * Discarding previously cached master state.
23706:M 17 Jan 17:51:03.125 * MASTER MODE enabled (user request from 'id=5 addr=192.168.2.11:52242 fd=9 name=sentinel-8a2828fe-cmd age=70 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=0 qbuf-free=32768 obl=36 oll=0 omem=0 events=r cmd=exec')
23706:M 17 Jan 17:51:03.131 # CONFIG REWRITE executed with success.
3.sentinel26379日志
23723:X 17 Jan 17:49:44.032 # Sentinel ID is 87db9542e66f3da13a24426ec16f971e683ab33c
23723:X 17 Jan 17:49:44.033 # +monitor master mymaster 192.168.2.11 6379 quorum 2
23723:X 17 Jan 17:49:44.036 * +slave slave 192.168.2.11:6380 192.168.2.11 6380 @ mymaster 192.168.2.11 6379
23723:X 17 Jan 17:49:55.791 * +sentinel sentinel 8a2828fe4def373111d21ef4fefecb60ebaca83e 192.168.2.11 26380 @ mymaster 192.168.2.11 6379
23723:X 17 Jan 17:50:00.623 * +sentinel sentinel 18c682862d62ebe411ad11a99daa4758464a8d04 192.168.2.11 26381 @ mymaster 192.168.2.11 6379
已杀死
4.sentinel26380日志
23726:X 17 Jan 17:49:53.837 # Sentinel ID is 8a2828fe4def373111d21ef4fefecb60ebaca83e
23726:X 17 Jan 17:49:53.838 # +monitor master mymaster 192.168.2.11 6379 quorum 2
23726:X 17 Jan 17:49:53.841 * +slave slave 192.168.2.11:6380 192.168.2.11 6380 @ mymaster 192.168.2.11 6379
23726:X 17 Jan 17:49:54.222 * +sentinel sentinel 87db9542e66f3da13a24426ec16f971e683ab33c 192.168.2.11 26379 @ mymaster 192.168.2.11 6379
23726:X 17 Jan 17:50:00.623 * +sentinel sentinel 18c682862d62ebe411ad11a99daa4758464a8d04 192.168.2.11 26381 @ mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:02.748 # +sdown master mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:02.748 # +sdown sentinel 87db9542e66f3da13a24426ec16f971e683ab33c 192.168.2.11 26379 @ mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:02.820 # +odown master mymaster 192.168.2.11 6379 #quorum 2/2
23726:X 17 Jan 17:51:02.820 # +new-epoch 1
23726:X 17 Jan 17:51:02.820 # +try-failover master mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:02.868 # +vote-for-leader 8a2828fe4def373111d21ef4fefecb60ebaca83e 1
23726:X 17 Jan 17:51:02.908 # 18c682862d62ebe411ad11a99daa4758464a8d04 voted for 8a2828fe4def373111d21ef4fefecb60ebaca83e 1
23726:X 17 Jan 17:51:02.960 # +elected-leader master mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:02.960 # +failover-state-select-slave master mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:03.061 # +selected-slave slave 192.168.2.11:6380 192.168.2.11 6380 @ mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:03.062 * +failover-state-send-slaveof-noone slave 192.168.2.11:6380 192.168.2.11 6380 @ mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:03.124 * +failover-state-wait-promotion slave 192.168.2.11:6380 192.168.2.11 6380 @ mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:03.302 # +promoted-slave slave 192.168.2.11:6380 192.168.2.11 6380 @ mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:03.302 # +failover-state-reconf-slaves master mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:03.331 # +failover-end master mymaster 192.168.2.11 6379
23726:X 17 Jan 17:51:03.331 # +switch-master mymaster 192.168.2.11 6379 192.168.2.11 6380
23726:X 17 Jan 17:51:03.332 * +slave slave 192.168.2.11:6379 192.168.2.11 6379 @ mymaster 192.168.2.11 6380
23726:X 17 Jan 17:51:13.372 # +sdown slave 192.168.2.11:6379 192.168.2.11 6379 @ mymaster 192.168.2.11 6380
5.sentinel26381日志
23743:X 17 Jan 17:49:58.700 # Sentinel ID is 18c682862d62ebe411ad11a99daa4758464a8d04
23743:X 17 Jan 17:49:58.701 # +monitor master mymaster 192.168.2.11 6379 quorum 2
23743:X 17 Jan 17:49:58.704 * +slave slave 192.168.2.11:6380 192.168.2.11 6380 @ mymaster 192.168.2.11 6379
23743:X 17 Jan 17:49:59.885 * +sentinel sentinel 8a2828fe4def373111d21ef4fefecb60ebaca83e 192.168.2.11 26380 @ mymaster 192.168.2.11 6379
23743:X 17 Jan 17:50:00.305 * +sentinel sentinel 87db9542e66f3da13a24426ec16f971e683ab33c 192.168.2.11 26379 @ mymaster 192.168.2.11 6379
23743:X 17 Jan 17:51:02.692 # +sdown master mymaster 192.168.2.11 6379
23743:X 17 Jan 17:51:02.692 # +sdown sentinel 87db9542e66f3da13a24426ec16f971e683ab33c 192.168.2.11 26379 @ mymaster 192.168.2.11 6379
23743:X 17 Jan 17:51:02.885 # +new-epoch 1
23743:X 17 Jan 17:51:02.907 # +vote-for-leader 8a2828fe4def373111d21ef4fefecb60ebaca83e 1
23743:X 17 Jan 17:51:03.360 # +config-update-from sentinel 8a2828fe4def373111d21ef4fefecb60ebaca83e 192.168.2.11 26380 @ mymaster 192.168.2.11 6379
23743:X 17 Jan 17:51:03.360 # +switch-master mymaster 192.168.2.11 6379 192.168.2.11 6380
23743:X 17 Jan 17:51:03.361 * +slave slave 192.168.2.11:6379 192.168.2.11 6379 @ mymaster 192.168.2.11 6380
23743:X 17 Jan 17:51:13.396 # +sdown slave 192.168.2.11:6379 192.168.2.11 6379 @ mymaster 192.168.2.11 6380
主备切换等操作,redis会修改配置文件