Redis 主从复制群集和哨兵模式配置

奋斗吧
奋斗吧
擅长邻域:未填写

标签: Redis 主从复制群集和哨兵模式配置 博客 51CTO博客

2023-03-26 18:23:29 108浏览

Redis 主从复制群集和哨兵模式配置,Redis主从复制和哨兵模式

Redis 主从复制群集和哨兵模式配置_redis

推荐步骤:

➢ 在 Centos01 配置 ntp 服务器,Centos02 和 Centos02 和 Centos03 以及 Centos04 配置同步时间,在 Centos01

和 Centos02 以及 Centos03 和 Centos04 安装 Redis 服务器

➢ 配置 Redis 主从复制群集,在 Centos01 配置主 Redis 节点,Centos02 为第一台从 Redis 节点,Centos03 为第

二台从节点,验证主从复制

➢ 在 Centos04 上安装 Redis 配置 Redis 哨兵模式,监控主从 Redis 节点运行状态,主 Redis 故障自动切换到新主

Redis 服务器

实验步骤:

一、在 Centos01 配置 ntp 服务器,Centos02 和 Centos02 和 Centos03 以及

Centos04 配置同步时间,在 Centos01 和 Centos02 以及 Centos03 和 Centos04

安装 Redis 服务器

​1、配置时间服务器

1)修改时间服务器配置文件

[root@centos01 ~]# vim /etc/ntp.conf

restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap

server 127.127.1.0
fudge 127.127.1.0 stratum 8

2)重新启动时间服务器设置开机自动启动

[root@centos01 ~]# systemctl restart ntpd

[root@centos01 ~]# systemctl enable ntpd

3)从 redis 服务器同步时间

3)从 redis 服务器同步时间

[root@centos02 ~]# ntpdate 192.168.100.10

14 Mar 01:06:14 ntpdate[1143]: adjust time server 192.168.100.10 offset 0.147980 sec

[root@centos03 ~]# ntpdate 192.168.100.10

14 Mar 01:06:22 ntpdate[1143]: adjust time server 192.168.100.10 offset -0.227113

sec

[root@centos04 ~]# ntpdate 192.168.100.10

14 Mar 01:06:22 ntpdate[1143]: adjust time server 192.168.100.10 offset -0.227113

sec

2、在 Centos01 安装 Redis 服务器

1)解压移动 Redis 安装位置

​[root@centos01 ~]# mount /dev/cdrom /mnt/

mount: /dev/sr0 写保护,将以只读方式挂载

[root@centos01 ~]# tar zxf /mnt/redis-3.2.9.tar.gz -C /usr/src/

[root@centos01 ~]# mv /usr/src/redis-3.2.9/ /usr/local/redis

2)配置安装 Redis 服务器

[root@centos01 redis]# make && make install

3)初始化配置 Redis 服务器

[root@centos01 redis]# cd ./utils/

[root@centos01 utils]# ./install_server.sh {一直回车}
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@centos01 utils]# cd

​4)停止服务

[root@centos01 ~]# /etc/init.d/redis_6379 stop

Stopping ...

Redis stopped

3、在 Centos02 安装 Redis 服务器

1)解压移动 Redis 安装位置

[root@centos02 ~]# mount /dev/cdrom /mnt/

mount: /dev/sr0 写保护,将以只读方式挂载

[root@centos02 ~]# tar zxf /mnt/redis-3.2.9.tar.gz -C /usr/src/

[root@centos02 ~]# mv /usr/src/redis-3.2.9/ /usr/local/redis

2)配置安装 Redis 服务器

[root@centos02 redis]# make && make install

3)初始化配置 Redis 服务器

[root@centos02 redis]# cd ./utils/

[root@centos02 utils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]

Selected default - /etc/redis/6379.confPlease select the redis log file name [/var/log/redis_6379.log]

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

Selected config:

Port : 6379

Config file : /etc/redis/6379.conf

Log file : /var/log/redis_6379.log

Data dir : /var/lib/redis/6379

Executable : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

[root@centos02 utils]# cd

4)停止服务

[root@centos02 ~]# /etc/init.d/redis_6379 stop

Stopping ...

Redis stopped

4、在 Centos03 安装 Redis 服务器

1)解压移动 Redis 安装位置

[root@centos03 ~]# mount /dev/cdrom /mnt/

mount: /dev/sr0 写保护,将以只读方式挂载

[root@centos03 ~]# tar zxf /mnt/redis-3.2.9.tar.gz -C /usr/src/

[root@centos03 ~]# mv /usr/src/redis-3.2.9/ /usr/local/redis

2)配置安装 Redis 服务器

[root@centos03 redis]# make && make install

3)初始化配置 Redis 服务器

[root@centos03 redis]# cd ./utils/

[root@centos03 utils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

Selected config:

Port : 6379Config file : /etc/redis/6379.conf

Log file : /var/log/redis_6379.log

Data dir : /var/lib/redis/6379

Executable : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!
[root@centos03 utils]# cd

4)停止服务

[root@centos03 ~]# /etc/init.d/redis_6379 stop

Stopping ...

Redis stopped

5、在 Centos04 安装 Redis 服务器

1)解压移动 Redis 安装位置

[root@centos04 ~]# mount /dev/cdrom /mnt/

mount: /dev/sr0 写保护,将以只读方式挂载

[root@centos04 ~]# tar zxf /mnt/redis-3.2.9.tar.gz -C /usr/src/

[root@centos04 ~]# mv /usr/src/redis-3.2.9/ /usr/local/redi

2)配置安装 Redis 服务器

[root@centos04 redis]# make && make install

3)初始化配置 Redis 服务器

[

root@centos04 redis]# cd ./utils/

[root@centos04 utils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

Selected config:

Port : 6379

Config file : /etc/redis/6379.conf

Log file : /var/log/redis_6379.log

Data dir : /var/lib/redis/6379

Executable : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

[root@centos04 utils]# cd

4)停止服务

[root@centos04 ~]# /etc/init.d/redis_6379 stop

Stopping ...

Redis stopped

二、配置 Redis 主从复制群集,在 Centos01 配置主 Redis 节点,Centos02 为

第一台从 Redis 节点,Centos03 为第二台从节点,验证主从复制

1、修改主 Redis 配置文件

1)修改主 Redis 服务器配置文件

[root@centos01 ~]# vim /etc/redis/6379.conf

63 bind 192.168.100.10

86 port 6379

130 daemonize yes

483 requirepass pwd@123

2)启动服务

[root@centos01 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@centos01 ~]# netstat -anptu | grep redis-servertcp 0 0 192.168.100.10:6379 0.0.0.0:* LISTEN 4640/redis
server 1

2、修改第一台从 Redis 服务器配置文件

1)修改第一台从 Redis 主配置文件

[root@centos02 ~]# vim /etc/redis/6379.conf

62 bind 192.168.100.20

85 port 6379

129 daemonize yes

482 requirepass pwd@123

483 masterauth pwd@123

484 slaveof 192.168.100.10 6379

2)启动 Redis 服务

[root@centos02 ~]# netstat -anptu | grep redis-server

tcp 0 0 192.168.100.20:6379 0.0.0.0:* LISTEN 4584/redis
server 1

tcp 0 0 192.168.100.20:33924 192.168.100.10:6379 ESTABLISHED

4584/redis-server 1

3、修改第二台从 Redis 服务器配置文件

1)修改第二台从 Redis 主配置文件

[root@centos02 ~]# vim /etc/redis/6379.conf

62 bind 192.168.100.30

85 port 6379

129 daemonize yes

482 requirepass pwd@123483 masterauth pwd@123

484 slaveof 192.168.100.10 6379

2)启动 Redis 服务

[root@centos03 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@centos03 ~]# netstat -anptu | grep redis-server

tcp 0 0 192.168.100.30:6379 0.0.0.0:* LISTEN 4624/redis
server 1

tcp 0 0 192.168.100.30:34356 192.168.100.10:6379 ESTABLISHED

4624/redis-server 1

4、查看配置主从复制

1)登录主 Redis 查看复制群集

[root@centos01 ~]# redis-cli -h 192.168.100.10 -a pwd@123 -p 6379

2)查看配置 Redis 复制只群集

192.168.100.10:6379> info replication

# Replication

role:master

connected_slaves:2

slave0:ip=192.168.100.20,port=6379,state=online,offset=267,lag=0

slave1:ip=192.168.100.30,port=6379,state=online,offset=267,lag=1

master_repl_offset:267

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:266192.168.100.10:6379> quit

三、在 Centos04 上安装 Redis 配置 Redis 哨兵模式,监控主从 Redis 节点运

行状态,主 Redis 故障自动切换到新主 Redis 服务器

1、修改配置配置哨兵模式

1)修改 Redis 配置文件

[root@centos04 ~]# vim /usr/local/redis/sentinel.conf

16 bind 0.0.0.0

22 port 26379

70 sentinel monitor mymaster 192.168.100.10 6379 1

73 sentinel auth-pass mymaster pwd@123

100 sentinel down-after-milliseconds mymaster 30000

2)启动哨兵默认服务

[root@centos04 ~]# redis-sentinel /usr/local/redis/sentinel.conf &> 

/var/log/redis_sentinel.log &

[1] 4697

3)查看哨兵模式运行状态

[root@centos04 ~]# netstat -anptu | grep redis-sentinel

tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 4697/redis
sentinel

tcp 0 0 192.168.100.40:34325 192.168.100.30:6379 ESTABLISHED

4697/redis-sentinel

tcp 0 0 192.168.100.40:41415 192.168.100.30:6379 ESTABLISHED

4697/redis-sentinel tcp 0 0 192.168.100.40:51628 192.168.100.20:6379 ESTABLISHED

4697/redis-sentinel

tcp 0 0 192.168.100.40:59584 192.168.100.20:6379 ESTABLISHED

4697/redis-sentinel

tcp 0 0 192.168.100.40:40574 192.168.100.10:6379 ESTABLISHED

4697/redis-sentinel

tcp 0 0 192.168.100.40:45760 192.168.100.10:6379 ESTABLISHED

4697/redis-sentinel

2、模拟主 redis 数据库故障

1)停止主 Redis 服务

[root@centos01 ~]# redis-cli -h 192.168.100.10 -a pwd@123 -p 6379 shutdown

2)查看 redis-sentinel 监控

[root@centos04 ~]# tail -f /var/log/redis_sentinel.log

|`-._`-._ `-.__.-' _.-'_.-'|

| `-._`-._ _.-'_.-' |

`-._ `-._`-.__.-'_.-' _.-'

`-._ `-.__.-' _.-'

`-._ _.-'

`-.__.-'

4697:X 15 Mar 05:22:36.314 # WARNING: The TCP backlog setting of 511 cannot be 

enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

4697:X 15 Mar 05:22:36.314 # Sentinel ID is 54d4b56816bbd0a5f6316c90ecbd21b956b268d7

4697:X 15 Mar 05:22:36.314 # +monitor master mymaster 192.168.100.10 6379 quorum 14697:X 15 Mar 05:25:54.596 # +sdown master mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:54.596 # +odown master mymaster 192.168.100.10 6379 #quorum 1/1

4697:X 15 Mar 05:25:54.597 # +new-epoch 1

4697:X 15 Mar 05:25:54.597 # +try-failover master mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:54.598 # +vote-for-leader

54d4b56816bbd0a5f6316c90ecbd21b956b268d7 1

4697:X 15 Mar 05:25:54.598 # +elected-leader master mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:54.598 # +failover-state-select-slave master mymaster

192.168.100.10 6379

4697:X 15 Mar 05:25:54.659 # +selected-slave slave 192.168.100.20:6379

192.168.100.20 6379 @ mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:54.659 * +failover-state-send-slaveof-noone slave

192.168.100.20:6379 192.168.100.20 6379 @ mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:54.733 * +failover-state-wait-promotion slave

192.168.100.20:6379 192.168.100.20 6379 @ mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:54.810 # +promoted-slave slave 192.168.100.20:6379

192.168.100.20 6379 @ mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:54.810 # +failover-state-reconf-slaves master mymaster

192.168.100.10 6379

4697:X 15 Mar 05:25:54.884 * +slave-reconf-sent slave 192.168.100.30:6379

192.168.100.30 6379 @ mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:55.738 * +slave-reconf-inprog slave 192.168.100.30:6379

192.168.100.30 6379 @ mymaster 192.168.100.10 63794697:X 15 Mar 05:25:55.739 * +slave-reconf-done slave 192.168.100.30:6379

192.168.100.30 6379 @ mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:55.807 # +failover-end master mymaster 192.168.100.10 6379

4697:X 15 Mar 05:25:55.807 # +switch-master mymaster 192.168.100.10 6379

192.168.100.20 6379

4697:X 15 Mar 05:25:55.807 * +slave slave 192.168.100.30:6379 192.168.100.30 6379 @

mymaster 192.168.100.20 6379

4697:X 15 Mar 05:25:55.807 * +slave slave 192.168.100.10:6379 192.168.100.10 6379 @

mymaster 192.168.100.20 6379

4697:X 15 Mar 05:26:25.808 # +sdown slave 192.168.100.10:6379 192.168.100.10 6379 @

mymaster 192.168.100.20 6379

3)登录到备份节点查看状态

[root@centos02 ~]# redis-cli -h 192.168.100.20 -a pwd@123 -p 6379

192.168.100.20:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.100.30,port=6379,state=online,offset=11402,lag=0

master_repl_offset:11402

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:11401

192.168.100.20:6379>

3、模拟故障节点启动

1)启动故障节点的 redis

[root@centos01 ~]# netstat -anptu | grep redis-server

tcp 0 0 192.168.100.10:6379 0.0.0.0:* LISTEN 5051/redis
server 1

tcp 0 0 192.168.100.10:6379 192.168.100.40:38356 ESTABLISHED

5051/redis-server 1

tcp 0 0 192.168.100.10:6379 192.168.100.40:52097 ESTABLISHED

5051/redis-server 1

2)查看主从信息

[root@centos01 ~]# redis-cli -h 192.168.100.10 -a pwd@123 -p 6379

192.168.100.10:6379> info replication

# Replication

role:slave

master_host:192.168.100.20

master_port:6379

master_link_status:down

master_last_io_seconds_ago:-1

master_sync_in_progress:0

slave_repl_offset:1

master_link_down_since_seconds:1678829565

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0


好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695