欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

redis环境搭建之主从复制集群+哨兵集群

程序员文章站 2022-05-07 15:50:49
...

redis环境搭建之主从复制集群+哨兵集群

环境:redis-5.0.5 + CentOS 6.x
单机伪集群搭建:6379为主节点,6380、6381为从节点

1、准备配置文件

1-1、准备主节点配置文件-6379.conf

mkdir /opt/test
vi /opt/test/6379.conf

6379.conf文件全配置

bind 127.0.0.1
# 端口号
port 6379
# 是否后台启动
daemonize no
# pid启动文件
pidfile /var/run/redis_6379.pid
loglevel notice
# 日志文件
#logfile /opt/redis5/log/redis_6379.log
# 数据存储目录
dir /opt/test/data/6379
protected-mode yes
supervised no
tcp-backlog 511
timeout 0
tcp-keepalive 300
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

1-2、准备从节点配置文件-6380.conf

cp /opt/test/6379.conf /opt/test/6380.conf
vi /opt/test/6380.conf
# 修改端口号
port 6380
# 修改pid启动文件
pidfile /var/run/redis_6380.pid
# 修改数据存储目录
dir /opt/test/data/6380

6380.conf完整配置

bind 127.0.0.1
# 端口号
port 6380
# 是否后台启动
daemonize no
# pid启动文件
pidfile /var/run/redis_6380.pid
loglevel notice
# 日志文件
#logfile /opt/redis5/log/redis_6380.log
# 数据存储目录
dir /opt/test/data/6380
protected-mode yes
supervised no
tcp-backlog 511
timeout 0
tcp-keepalive 300
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

1-3、准备从节点配置文件-6381.conf

cp /opt/test/6379.conf /opt/test/6381.conf
vi /opt/test/6381.conf
# 修改端口号
port 6381
# 修改pid启动文件
pidfile /var/run/redis_6381.pid
# 修改数据存储目录
dir /opt/test/data/6381

6381.conf完整配置

bind 127.0.0.1
# 端口号
port 6381
# 是否后台启动
daemonize no
# pid启动文件
pidfile /var/run/redis_6381.pid
loglevel notice
# 日志文件
#logfile /opt/redis5/log/redis_6381.log
# 数据存储目录
dir /opt/test/data/6381
protected-mode yes
supervised no
tcp-backlog 511
timeout 0
tcp-keepalive 300
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

1-4、创建数据存储目录

mkdir -p /opt/test/data/6379
mkdir -p /opt/test/data/6380
mkdir -p /opt/test/data/6381

2、启动伪集群

# 启动主节点
redis-server /opt/test/6379.conf
# 启动从节点并追随主节点
redis-server /opt/test/6380.conf --replicaof 127.0.0.1 6379
redis-server /opt/test/6381.conf --replicaof 127.0.0.1 6379

2-1、6379启动

redis-server /opt/test/6379.conf
redis环境搭建之主从复制集群+哨兵集群

2-2、6380启动

redis-server /opt/test/6380.conf --replicaof 127.0.0.1 6379
redis环境搭建之主从复制集群+哨兵集群

6380启动后,6379的日志显示
redis环境搭建之主从复制集群+哨兵集群

2-3、6381启动

redis-server /opt/test/6381.conf --replicaof 127.0.0.1 6379
redis环境搭建之主从复制集群+哨兵集群

6381启动后,6379的日志显示
redis环境搭建之主从复制集群+哨兵集群

3、验证集群

3-1、连接6379端口,设置key zctest

redis环境搭建之主从复制集群+哨兵集群

3-2、连接6380端口,获取key zctest

redis环境搭建之主从复制集群+哨兵集群

3-3、连接6381端口,获取key zctest

redis环境搭建之主从复制集群+哨兵集群

4、模拟主节点宕机及手动故障转移

4-1、ctrl + c 关闭6379的redis服务

redis环境搭建之主从复制集群+哨兵集群

观察6380日志
redis环境搭建之主从复制集群+哨兵集群

观察6381日志
redis环境搭建之主从复制集群+哨兵集群

4-2、手动将6380手动切为新的主节点

redis-cli 连接6380

redis -cli -p 6380
# 将6380置为新的主节点
REPLICAOF no one

redis环境搭建之主从复制集群+哨兵集群

查看6380的日志
redis环境搭建之主从复制集群+哨兵集群

4-3、手动将6381切为6380的从节点

redis -cli -p 6381
# 追随6380节点
REPLICAOF 127.0.0.1 6380

redis环境搭建之主从复制集群+哨兵集群

查看6381日志
redis环境搭建之主从复制集群+哨兵集群

查看6380日志
redis环境搭建之主从复制集群+哨兵集群

5、将主从切换交给哨兵

搭建一个redis哨兵集群,端口分别为:26379、26380、26381
监控的redis主节点为:127.0.0.1 的 6379 端口

5-1、准备哨兵配置文件

# 配置文件26379.conf
vi /opt/test/26379.conf
# 文件添加如下内容
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2

# 配置文件26380.conf
cp /opt/test/26379.conf /opt/test/26380.conf
vi /opt/test/26380.conf
# 修改端口为23680
port 26380

# 配置文件26381.conf
cp /opt/test/26379.conf /opt/test/26381.conf
vi /opt/test/26381.conf
# 修改端口为23681
port 26381

5-2、启动redis集群

# 启动主节点
redis-server /opt/test/6379.conf
# 启动从节点并追随主节点
redis-server /opt/test/6380.conf --replicaof 127.0.0.1 6379
redis-server /opt/test/6381.conf --replicaof 127.0.0.1 6379

具体参照 :第 2 章

5-3、启动哨兵集群

启动26379
redis-server /opt/test/26379.conf --sentinel
redis环境搭建之主从复制集群+哨兵集群

启动26380
redis-server /opt/test/26380.conf --sentinel
redis环境搭建之主从复制集群+哨兵集群

启动26381
redis-server /opt/test/26381.conf --sentinel
redis环境搭建之主从复制集群+哨兵集群

5-4、手动模拟redis主节点6379宕机

1、ctrl + c 关闭6379的redis服务

redis环境搭建之主从复制集群+哨兵集群

2、等待一会儿,查看哨兵日志

26379日志

redis环境搭建之主从复制集群+哨兵集群

26380日志

redis环境搭建之主从复制集群+哨兵集群

26381日志

redis环境搭建之主从复制集群+哨兵集群
**
由日志: +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6381
可见,主节点由6379 切换为了 6381

3、查看redis日志

1、查看redis 6380日志

redis环境搭建之主从复制集群+哨兵集群

2、查看redis 6381日志

redis环境搭建之主从复制集群+哨兵集群

6、特殊说明

1、redis中文文档更新有些滞后,比如 SLAVEOF 命令在Redis 5 之后替换为了 REPLICAOF,具体可根据help命令查看
redis环境搭建之主从复制集群+哨兵集群
2、如需搭建真正的redis集群或sentinel集群,只需在不同主机操作及相应配置文件修改IP及端口即可(文章只是为了方便演示所以搭建的伪分布式集群)

7、参考

1、主从复制:http://www.redis.cn/topics/replication.html
2、Redis 的 Sentinel 文档:http://www.redis.cn/topics/sentinel.html