准备三台服务器 192.168.1.27、192.168.1.28、192.168.1.29
#安装并发包 wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz tar -xzvf tcl8.6.1-src.tar.gz cd /usr/local/tcl8.6.1/unix/ ./configure make && make install #下载及编译 wget http://download.redis.io/releases/redis-5.0.3.tar.gz tar -zxvf redis-5.0.3.tar.gz mv redis-5.0.3 /usr/local/redis cd /usr/local/redis/ make && make test && make install #启动脚本 cd utils/ cp redis_init_script /etc/init.d/redis_6379 #配置文件 cd /usr/local/redis cp redis.conf /etc/redis/6379.conf chmod 777 redis_6379 #启动 ./redis_6379 start #查看进程 ps -ef|grep redis
#设置192.168.1.27 为 master节点,配置如下 bind 129.168.1.27 #注释掉这部分,这是限制redis只能本地访问 protected-mode no #默认yes,开启保护模式,限制为本地访问 daemonize no#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方式启动redis失败 databases 16 #数据库个数(可选),我修改了这个只是查看是否生效。。 #创建/var/redis/6379目录 dir "/var/redis/6379" #输入本地redis数据库存放文件夹(可选) appendonly yes #redis持久化(可选) requirepass "redis-pass" #配置redis访问密码 #192.168.1.28 为slave节点 bind 129.168.1.28 #注释掉这部分,这是限制redis只能本地访问 protected-mode no #默认yes,开启保护模式,限制为本地访问 daemonize no#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方式启动redis失败 databases 16 #数据库个数(可选),我修改了这个只是查看是否生效。。 #创建/var/redis/6379目录 dir "/var/redis/6379" #输入本地redis数据库存放文件夹(可选) appendonly yes #redis持久化(可选) requirepass "redis-pass" #配置redis访问密码 #设置master节点 replicaof 192.168.1.27 6379 #192.168.1.29 为slave节点 bind 129.168.1.29 #注释掉这部分,这是限制redis只能本地访问 protected-mode no #默认yes,开启保护模式,限制为本地访问 daemonize no#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方式启动redis失败 databases 16 #数据库个数(可选),我修改了这个只是查看是否生效。。 #创建/var/redis/6379目录 dir "/var/redis/6379" #输入本地redis数据库存放文件夹(可选) appendonly yes #redis持久化(可选) requirepass "redis-pass" #配置redis访问密码 #设置master节点 replicaof 192.168.1.27 6379
[root@sword-01 redis]# redis-cli -h 192.168.1.27 -p 6379 192.168.1.27:6379> auth redis-pass OK 192.168.1.27:6379> info replication # Replication role:slave master_host:192.168.1.27 #master节点 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:876195 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:4e89742a8782641ad1cdca8c72b7602d3fdec98e master_replid2:0000000000000000000000000000000000000000 master_repl_offset:876195 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:397777 repl_backlog_histlen:478419
哨兵默认用26379端口, 这里更改为5000
mkdir /etc/sentinel #配置文件目录 mkdir -p /var/sentinel/5000 #工作目录 #复制 cp /usr/local/redis/sentinel.conf /etc/sentinel/5000.conf #3台同样此设置 #更改192.168.1.27 配置 port 5000 bind 192.168.1.27 protected-mode no dir /var/sentinel/5000 sentinel monitor mymaster 192.168.1.27 6379 2 sentinel auth-pass mymaster redis-pass #设置密码 redis-pass[自定义] sentinel down-after-milliseconds mymaster 30000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 logfile "/var/logs/sentinel/5000" #日志目录 查看日志方便 #更改192.168.1.28 配置 port 5000 bind 192.168.1.28 protected-mode no dir /var/sentinel/5000 sentinel monitor mymaster 192.168.1.27 6379 2 sentinel auth-pass mymaster redis-pass #设置密码 redis-pass[自定义] sentinel down-after-milliseconds mymaster 30000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 logfile "/var/logs/sentinel/5000" #日志目录 查看日志方便 #更改192.168.1.29 配置 port 5000 bind 192.168.11.29 protected-mode no dir /var/sentinel/5000 sentinel monitor mymaster 192.168.1.27 6379 2 sentinel auth-pass mymaster redis-pass #设置密码 redis-pass[自定义] sentinel down-after-milliseconds mymaster 30000 sentinel failover-timeout mymaster 60000 sentinel parallel-syncs mymaster 1 logfile "/var/logs/sentinel/5000" #日志目录 查看日志方便
配置完,启动3台服务
#启动命令 redis-sentinel /etc/sentinel/5000.conf #检查哨兵状态 [root@sword-02 sentinel]# redis-cli -h 192.168.1.29 -p 5000 192.168.1.29:5000> ping PONG 192.168.1.29:5000> sentinel master mymaster #查看master节点信息 1) "name" 2) "mymaster" 3) "ip" 4) "192.168.1.27" 5) "port" 6) "6379" 7) "runid" 8) "a9cddc491c36ff561ee97201819106884c912283" 9) "flags" 10) "s_down,master,disconnected" 11) "link-pending-commands" 12) "3" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "120260" 17) "last-ok-ping-reply" 18) "120791" 19) "last-ping-reply" 20) "120791" 21) "s-down-time" 22) "90229" 23) "down-after-milliseconds" 24) "30000" 25) "info-refresh" 26) "123936" 27) "role-reported" 28) "master" 29) "role-reported-time" 30) "585799" 31) "config-epoch" 32) "0" 33) "num-slaves" 34) "2" 35) "num-other-sentinels" 36) "0" 37) "quorum" 38) "2" 39) "failover-timeout" 40) "60000" 41) "parallel-syncs" 42) "1" 192.168.1.29:5000> sentinel slaves mymaster #查看slaves节点信息 1) 1) "name" 2) "192.168.1.29:6379" 3) "ip" 4) "192.168.1.29" 5) "port" 6) "6379" 7) "runid" 8) "db4bd0c73bf7259fd05dfe88cba72a77227825a7" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "728" 19) "last-ping-reply" 20) "728" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "728" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "649376" 29) "master-link-down-time" 30) "183000" 31) "master-link-status" 32) "err" 33) "master-host" 34) "192.168.1.27" 35) "master-port" 36) "6379" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "302075" 2) 1) "name" 2) "192.168.1.28:6379" 3) "ip" 4) "192.168.1.28" 5) "port" 6) "6379" 7) "runid" 8) "219c05030fd02d1d3f9dbee684ccc5f4109730a9" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "728" 19) "last-ping-reply" 20) "728" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "728" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "649377" 29) "master-link-down-time" 30) "183000" 31) "master-link-status" 32) "err" 33) "master-host" 34) "192.168.1.27" 35) "master-port" 36) "6379" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "302075" 192.168.1.29:5000> SENTINEL get-master-addr-by-name mymaster #获取master节点 1) "192.168.1.27" 2) "6379"
手工杀死master节点,看哨兵模式是否生效
[root@sword-01 sentinel]# ps -ef |grep redis root 1742 1 0 16:16 ? 00:00:12 /usr/local/bin/redis-server 192.168.1.27:6379 root 1785 1 0 17:02 ? 00:00:02 redis-sentinel 192.168.1.27:5000 [sentinel] root 1801 1389 0 17:11 pts/0 00:00:00 grep --color=auto redis [root@sword-01 sentinel]# kill -9 1742 #查看哨兵日志 1944:X 19 Jun 2021 18:05:02.328 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1944:X 19 Jun 2021 18:05:02.328 # Sentinel ID is 7dff065f997df354b439b81e176a40a17647bf11 1944:X 19 Jun 2021 18:05:02.328 # +monitor master mymaster 192.168.1.27 6379 quorum 2 1944:X 19 Jun 2021 18:09:29.294 * +sentinel sentinel 7dff065f923df354b439b81e176a40a17647bf11 192.168.1.28 5000 @ mymaster 192.168.1.27 6379 1944:X 19 Jun 2021 18:10:09.601 * +sentinel sentinel 7dff065f997df352b439b81e176a40a17647bf11 192.168.1.29 5000 @ mymaster 192.168.1.27 6379 1944:X 19 Jun 2021 18:12:29.538 # +sdown master mymaster 192.168.1.27 6379 1944:X 19 Jun 2021 18:12:29.544 # +new-epoch 1 1944:X 19 Jun 2021 18:12:29.546 # +vote-for-leader 7dff065f997df352b439b81e176a40a17647bf11 1 1944:X 19 Jun 2021 18:12:29.638 # +odown master mymaster 192.168.1.27 6379 #quorum 3/2 1944:X 19 Jun 2021 18:12:29.638 # Next failover delay: I will not start a failover before Sat Jun 19 18:14:29 2021 1944:X 19 Jun 2021 18:12:30.077 # +config-update-from sentinel 7dff065f997df352b439b81e176a40a17647bf11 192.168.1.29 5000 @ mymaster 192.168.1.27 6379 1944:X 19 Jun 2021 18:12:30.078 # +switch-master mymaster 192.168.1.27 6379 192.168.1.29 6379 1944:X 19 Jun 2021 18:12:30.078 * +slave slave 192.168.1.28:6379 192.168.1.28 6379 @ mymaster 192.168.1.29 6379 1944:X 19 Jun 2021 18:12:30.078 * +slave slave 192.168.1.27:6379 192.168.1.27 6379 @ mymaster 192.168.1.29 6379 1944:X 19 Jun 2021 18:13:00.116 # +sdown slave 192.168.1.27:6379 192.168.1.27 6379 @ mymaster 192.168.1.29 6379 1944:X 19 Jun 2021 18:13:51.951 # -sdown slave 192.168.1.27:6379 192.168.1.27 6379 @ mymaster 192.168.1.29 6379