1、准备最少三个redis作为集群中三个节点
192.168.2.24 6373(master) 192.168.2.24 6373(slave) 192.168.2.24 6373(slave)
在redis安装文件中将sentinel.conf拷贝到redis.conf所在目录
2、修改配置文件redis.conf
##以下master、slave节点都需要配置 port 6373 #端口6373,6374,6375 bind 0.0.0.0 #可通过外部访问 daemonize yes #redis后台运行 pidfile /var/run/redis_6373.pid #pidfile文件对应6373,6374,6375 dbfilename dump-6373.rdb #redis持久化时存储的文件名 requirepass 123456 #本节点密码 masterauth 123456 #master节点密码,建议都设置成一样的 dir /usr/local/redis/data/ #配置redis持久化时存储位置,此目录需要额外创建 ###只需要slave节点配置,如果发声主从切换,原slave节点会自动写入master节点的复制信息 replicaof 192.168.2.24 6373 //配置当前节点从哪个master节点复制
3、修改sentinel.conf
bind 0.0.0.0 daemonize yes dir "/usr/local/redis/data #端口26373,26374,26375 port 26373 #26373,26374,26375 pidfile "/var/run/sentinel-6373.pid" #26373,26374,26375 logfile "sentinel-26373.log" #mymaster:master节点名称,可自行定义 + master<ip> + master<port> + 2:当该master节点挂了后,需sentinel集群中必须至少2个sentinel同意才行,只要数量不达标就不会故障迁移 sentinel monitor mymaster 192.168.2.24 6373 2 sentinel auth-pass mymaster 123456
4、启动redis-server、redis-sentinel
#启动所有redis节点,启动顺序无要求 ./bin/redis-server ./redis_cluster/6373/redis.conf ./bin/redis-server ./redis_cluster/6374/redis.conf ./bin/redis-server ./redis_cluster/6375/redis.conf #启动sentinel集群,先启动master的哨兵,再启动slave哨兵 ./bin/redis-sentinel ./redis_cluster/6373/sentinel.conf ./bin/redis-sentinel ./redis_cluster/6374/sentinel.conf ./bin/redis-sentinel ./redis_cluster/6375/sentinel.conf
5、查看集群信息
##登录master节点查看集群信息,可以看到当前角色为master,有两个slave节点 [root@project-deve data]# ../bin/redis-cli -h 192.168.2.24 -p 6373 -a 123456 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.2.24,port=6374,state=online,offset=2278758,lag=0 slave1:ip=192.168.2.24,port=6375,state=online,offset=2278897,lag=0 master_failover_state:no-failover master_replid:22e45bc804b796f54b57b8145e80f69d483697a3 master_replid2:0248953553f40d7d8bd6cf0c463827469cbaba47 master_repl_offset:2278897 second_repl_offset:67095 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1230322 repl_backlog_histlen:1048576 ##登录slave节点 [root@project-deve data]# ../bin/redis-cli -h 192.168.2.24 -p 6375 -a 123456 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.2.24 master_port:6373 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_read_repl_offset:2295133 slave_repl_offset:2295133 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:22e45bc804b796f54b57b8145e80f69d483697a3 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2295133 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1246558 repl_backlog_histlen:1048576
6、此时一个redis哨兵集群就已经搭建好了,现模仿master节点down掉,直接kill掉6373
20845:X 09 Nov 2021 20:05:24.970 # +sdown master mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:25.042 # +odown master mymaster 192.168.2.24 6373 #quorum 2/2 20845:X 09 Nov 2021 20:05:25.042 # +new-epoch 3 20845:X 09 Nov 2021 20:05:25.042 # +try-failover master mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:25.185 # +vote-for-leader e775358d1a361f29c02aa5c81891820ca412d91b 3 20845:X 09 Nov 2021 20:05:25.203 # 2115307517329fc4551e65005def0f495823d1c4 voted for 2115307517329fc4551e65005def0f495823d1c4 3 20845:X 09 Nov 2021 20:05:25.271 # 716608d689335e4651eba4de32c7799b51f89271 voted for e775358d1a361f29c02aa5c81891820ca412d91b 3 20845:X 09 Nov 2021 20:05:25.300 # +elected-leader master mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:25.300 # +failover-state-select-slave master mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:25.383 # +selected-slave slave 192.168.2.24:6374 192.168.2.24 6374 @ mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:25.383 * +failover-state-send-slaveof-noone slave 192.168.2.24:6374 192.168.2.24 6374 @ mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:25.466 * +failover-state-wait-promotion slave 192.168.2.24:6374 192.168.2.24 6374 @ mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:26.318 # +promoted-slave slave 192.168.2.24:6374 192.168.2.24 6374 @ mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:26.318 # +failover-state-reconf-slaves master mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:26.318 * +slave-reconf-sent slave 192.168.2.24:6375 192.168.2.24 6375 @ mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:26.419 # -odown master mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:27.204 * +slave-reconf-inprog slave 192.168.2.24:6375 192.168.2.24 6375 @ mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:27.204 * +slave-reconf-done slave 192.168.2.24:6375 192.168.2.24 6375 @ mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:27.270 # +failover-end master mymaster 192.168.2.24 6373 20845:X 09 Nov 2021 20:05:27.270 # +switch-master mymaster 192.168.2.24 6373 192.168.2.24 6374 20845:X 09 Nov 2021 20:05:27.271 * +slave slave 192.168.2.24:6375 192.168.2.24 6375 @ mymaster 192.168.2.24 6374 20845:X 09 Nov 2021 20:05:27.271 * +slave slave 192.168.2.24:6373 192.168.2.24 6373 @ mymaster 192.168.2.24 6374 20845:X 09 Nov 2021 20:05:57.303 # +sdown slave 192.168.2.24:6373 192.168.2.24 6373 @ mymaster 192.168.2.24 6374
7、从日志看出已经选举了6374为master,从6375节点查看集群信息
[root@project-deve data]# ../bin/redis-cli -h 192.168.2.24 -p 6375 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.2.24
master_port:6374
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:2437128
slave_repl_offset:2437128
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:278763267d06775e33b169a96a1ed5b3620a12c1
master_replid2:04b3854e5644032fbdd4e8cad5bdfffbc6c2f935
master_repl_offset:2437128
second_repl_offset:2385896
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2351830
repl_backlog_histlen:85299