Redis搭建主从,哨兵,集群

Redis 搭建主从,哨兵,集群

准备

服务器

192.168.1.102 # master
192.168.1.103 # slave
192.168.1.104 # slave

安装 gcc
1
2
3
4
5
gcc -v

yum -y install centos-release-scl
yum -y install devtoolset-7-gcc*
scl enable devtoolset-7 bash
安装 redis
1
2
3
4
5
wget http://download.redis.io/releases/redis-6.0.0.tar.gz
mkdir -p /data/software
tar -zxvf redis-6.0.0.tar.gz -C /data/software
cd /data/software/redis-6.0.0
make
开放端口
1
2
3
4
5
6
# 开放端口  
firewall-cmd --zone=public --add-port=6379/tcp --add-port=16379/tcp --add-port=26379/tcp --add-port=6666/tcp --add-port=16666/tcp --add-port=6667/tcp --add-port=16667/tcp --permanent
# 重载入
firewall-cmd --reload
# 查看开放的端口
firewall-cmd --list-ports

主从

主节点修改配置文件

vim redis.onf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 关闭保护模式
protected-mode no
# 外部可访问
bind 0.0.0.0
# 后台运行
daemonize yes
# 密码
requirepass 123456
# 非只读
replica-read-only no
# 主节点密码,主备切换并重启旧的主节点后,不加密码可能导致连不上当前主节点
masterauth 123456
# 禁用一些命令
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command KEYS ""
rename-command CONFIG ""
从节点修改配置文件
1
2
3
4
5
6
7
8
9
10
protected-mode no
bind 0.0.0.0
daemonize yes
requirepass 123456
# 只读
replica-read-only yes
# 主节点信息
replicaof 192.168.1.102 6379
# 主节点密码
masterauth 123456
测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 查看配置信息
192.168.1.102:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.104,port=6379,state=online,offset=728,lag=0
slave1:ip=192.168.1.103,port=6379,state=online,offset=728,lag=0
master_replid:12398d4f5835d83b667f6edc54b3880cf0c05ecc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:728
master_repl_meaningful_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:728

# 主节点添加数据
192.168.1.102:6379> set key1 value1
OK

# 从节点查看数据
192.168.1.103:6379> get key1
"value1"

# 从节点添加数据
192.168.1.103:6379> set a b
(error) READONLY You can't write against a read only replica.

哨兵

哨兵核心功能
  • 监控:哨兵会不断地检查主节点和从节点是否运作正常
  • 自动故障转移:当主节点不能正常工作时,哨兵会开始自动故障转移操作,它会将失效主节点的其中一个从节点升级为新的主节点,并让其他从节点改为复制新的主节点
  • 配置提供者:客户端在初始化时,通过连接哨兵来获得当前Redis服务的主节点地址
  • 通知:哨兵可以将故障转移的结果发送给客户端
哨兵选举过程
  1. 多个 sentinel 发现并确认 master 有问题
  2. 选出一个 sentinel 作为领导
  3. 选出一个 slave 作为 master
  4. 通知其余 slave 成为新的 master 的 slave
  5. 通知客户端主从变化
  6. 等待老的 master 复活成为新 master 的 slave
全部节点修改配置文件

vim sentinel.conf

1
2
3
4
5
6
7
8
9
10
# 监控主节点,2 代表至少需要 2 个哨兵节点同意,才能判定主节点故障并进行故障转移
sentinel monitor mymaster 192.168.1.102 6379 2
# 在指定的时间(毫秒),不能接受 ping 应答,则哨兵认为该节点处于宕机状态
sentinel down-after-milliseconds mymaster 5000
# 指定故障切换允许的毫秒数,超过这个时间,就认为故障切换失败
sentinel failover-timeout mymaster 60000
# 重新选举后,有几个从节点能同时从新的主节点同步缓存
sentinel parallel-syncs mymaster 1
# 主节点密码
sentinel auth-pass mymaster 123456
测试
  1. 启动三台机器的 redis 和 sentinel
1
2
src/redis-server redis.conf
src/redis-sentinel sentinel.conf
  1. 查看配置信息
1
2
3
4
5
6
7
8
192.168.1.102:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.102:6379,slaves=2,sentinels=3
  1. 关闭主节点 redis,几秒过后查看 sentinel 日志
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# +monitor master mymaster 192.168.1.102 6379 quorum 2
* +slave slave 192.168.1.103:6379 192.168.1.103 6379 @ mymaster 192.168.1.102 6379
* +slave slave 192.168.1.104:6379 192.168.1.104 6379 @ mymaster 192.168.1.102 6379
* +sentinel sentinel 79d36f7254730a17297c52c0022c0d5058cef66f 192.168.1.103 26379 @ mymaster 192.168.1.102 6379
* +sentinel sentinel 185b25bf694a703f8f685434da03e6b9dfd68d8d 192.168.1.104 26379 @ mymaster 192.168.1.102 6379
# +sdown master mymaster 192.168.1.102 6379
# +new-epoch 1
# +vote-for-leader 79d36f7254730a17297c52c0022c0d5058cef66f 1
# +odown master mymaster 192.168.1.102 6379 #quorum 3/2
# Next failover delay: I will not start a failover before Thu Oct 8 22:44:10 2020
# +config-update-from sentinel 79d36f7254730a17297c52c0022c0d5058cef66f 192.168.1.103 26379 @ mymaster 192.168.1.102 6379
# +switch-master mymaster 192.168.1.102 6379 192.168.1.103 6379
* +slave slave 192.168.1.104:6379 192.168.1.104 6379 @ mymaster 192.168.1.103 6379
* +slave slave 192.168.1.102:6379 192.168.1.102 6379 @ mymaster 192.168.1.103 6379
# +sdown slave 192.168.1.102:6379 192.168.1.102 6379 @ mymaster 192.168.1.103 6379
  1. 再次查看主节点,发现已经改变
1
2
3
192.168.1.102:26379> sentinel get-master-addr-by-name mymaster
1) "192.168.1.103"
2) "6379"
  1. 重新启动之前的主节点,可以看到已经变成从节点了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
192.168.1.102:6379> info replication
# Replication
role:slave
master_host:192.168.1.102
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:427857
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b557e5253593be5b9d2dc52491f5b37b461b8e94
master_replid2:99e95a6c4b78e0a0b64090b43cf4546f5786b4d1
master_repl_offset:427857
master_repl_meaningful_offset:427857
second_repl_offset:125759
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:427815

集群

安装 redis
1
2
3
4
5
6
7
wget http://download.redis.io/releases/redis-6.0.0.tar.gz
mkdir -p /data/software/redisCluster
tar -zxvf redis-6.0.0.tar.gz -C /data/software/redisCluster
cd /data/software/redisCluster
mv redis-6.0.0 6666
make
cp -r ../6666 ../6667
修改配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vim /data/software/redisCluster/6666/redis.conf
port 6666 # 端口
protected-mode no # 关闭保护模式
bind 0.0.0.0 # 外部可访问
daemonize yes # 后台运行
requirepass 123456 # 密码
replica-read-only no # 非只读
pidfile /var/run/redis_6666.pid
cluster-enabled yes # 开启集群
cluster-config-file nodes-6666.conf # 集群节点配置文件
cluster-node-timeout 5000 # 集群超时时间

vim /data/software/redisCluster/6667/redis.conf
port 6667 # 端口
protected-mode no # 关闭保护模式
bind 0.0.0.0 # 外部可访问
daemonize yes # 后台运行
requirepass 123456 # 密码
replica-read-only no # 非只读
pidfile /var/run/redis_6667.pid
cluster-enabled yes # 开启集群
cluster-config-file nodes-6667.conf # 集群节点配置文件
cluster-node-timeout 5000 # 集群超时时间
复制文件到其他服务器
1
2
scp -r /data/software/redisCluster root@192.168.1.103:/data/software/redisCluster
scp -r /data/software/redisCluster root@192.168.1.104:/data/software/redisCluster
启动全部节点后执行命令创建集群
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# --cluster-replicas 1:为集群中的每个节点创建一个从节点
# -a 123456:指定密码
[root@localhost 6667]# src/redis-cli --cluster create 192.168.1.102:6666 192.168.1.103:6666 192.168.1.104:6666 192.168.1.102:6667 192.168.1.103:6667 192.168.1.104:6667 --cluster-replicas 1 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.1.103:6667 to 192.168.1.102:6666
Adding replica 192.168.1.104:6667 to 192.168.1.103:6666
Adding replica 192.168.1.102:6667 to 192.168.1.104:6666
M: 9b4d69b96870637bf09450c45eaacf423aedafb6 192.168.1.102:6666
slots:[0-5460] (5461 slots) master
M: 588f93f38d8e8937d2e458f32f130c8c5ca7a9f7 192.168.1.103:6666
slots:[5461-10922] (5462 slots) master
M: ecb366aec1d0b7a709a55230d0488ca11bf56403 192.168.1.104:6666
slots:[10923-16383] (5461 slots) master
S: cd237e96d59187e0472d103ce22d655bc23ca570 192.168.1.102:6667
replicates ecb366aec1d0b7a709a55230d0488ca11bf56403
S: 382d9791ab78fca6b1e4b150324bad138b312848 192.168.1.103:6667
replicates 9b4d69b96870637bf09450c45eaacf423aedafb6
S: e13a50aa9068480aa65b258791bf2ec885505f41 192.168.1.104:6667
replicates 588f93f38d8e8937d2e458f32f130c8c5ca7a9f7
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.1.102:6666)
M: 9b4d69b96870637bf09450c45eaacf423aedafb6 192.168.1.102:6666
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 588f93f38d8e8937d2e458f32f130c8c5ca7a9f7 192.168.1.103:6666
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e13a50aa9068480aa65b258791bf2ec885505f41 192.168.1.104:6667
slots: (0 slots) slave
replicates 588f93f38d8e8937d2e458f32f130c8c5ca7a9f7
M: ecb366aec1d0b7a709a55230d0488ca11bf56403 192.168.1.104:6666
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: cd237e96d59187e0472d103ce22d655bc23ca570 192.168.1.102:6667
slots: (0 slots) slave
replicates ecb366aec1d0b7a709a55230d0488ca11bf56403
S: 382d9791ab78fca6b1e4b150324bad138b312848 192.168.1.103:6667
slots: (0 slots) slave
replicates 9b4d69b96870637bf09450c45eaacf423aedafb6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[root@localhost 6667]# redis-cli --cluster check 192.168.1.102:6666 -a 123456
测试
1
2
3
4
5
6
7
8
9
10
[root@localhost 6667]# src/redis-cli -h 192.168.1.102 -p 6666 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.102:6666> set chen kaixin
OK

[root@localhost 6667]# src/redis-cli -h 192.168.1.103 -p 6666 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.103:6666> get chen
-> Redirected to slot [2419] located at 192.168.1.102:6666
"kaixin"