Redis 6.0.8 集群搭建(三主三从)

/ 默认分类 / 没有评论 / 1147浏览

下载与安装

首先从 Redis 官网下载 Redis 源代码并解压,这里使用的是 redis-6.0.8.tar.gz 版本

# 创建目录
mkdir /usr/local/redis/redis-cluster
# 进入目录
cd /usr/local/redis/redis-cluster
# 下载安装包
wget http://download.redis.io/releases/redis-6.0.8.tar.gz
# 解压
tar -zxzf redis-6.0.8.tar.gz
#进入目录 
cd  redis-6.0.8
# 编译用 如果不安装会编译报错 
yum -y install gcc-c++ 
#加 MALLOC=libc 为了防止编译出错,具体百度 make MALLOC=libc
make MALLOC=libc
make install PREFIX=/apprun/redis # 目标安装路径

创建数据

#创建目录
cd /usr/local/redis/redis-cluster
mkdir logs conf data pid
#创建数据目录
cd data
mkdir data1111 data2222 data3333 data4444 data5555 data6666

修改配置文件

cd /usr/local/redis/redis-cluster/conf
cp /usr/local/redis/redis-cluster/redis/redis.conf .
mv redis.conf redis1111.conf
vim redis1111.conf
# 修改绑定地址
bind 192.168.5.199
protected-mode yes
# 修改端口号,但集群状态下需要注意,集群使用端口会在此端口上增加10000,所以redis自身端口+10000不能超过65535
port 1111
tcp-backlog 511
timeout 0
tcp-keepalive 300
# Redis是否后台运行
daemonize yes
supervised no
# 进程文件存储地址
pidfile /usr/local/redis/redis-cluster/pid/redis1111.pid
loglevel notice
# 日志文件存储地址
logfile "/usr/local/redis/redis-cluster/logs/redis1111.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes   # 持久化错误之后,停止写入Redis
rdbcompression yes   # 快照是否压缩存储(压缩消耗CPU、占用空间小)
rdbchecksum yes   # 存储的快照进行数据校验(增加约10%的性能消耗)
dbfilename dump.rdb
rdb-del-sync-files no
# 数据存放目录
dir /usr/local/redis/redis-cluster/data/data1111

# 主要是针对master对应的slave节点设置的,在slave节点数据同步的时候用到
masterauth redis123
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
# 对登录权限做限制,redis每个节点的requirepass可以是独立、不同的
requirepass redis123
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
oom-score-adj no
oom-score-adj-values 0 200 800
# aof日志开启,有需要就开启
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec # 每秒同步一次(always 每次修改同步一次)
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
# 开启集群模式
cluster-enabled yes
# 集群的配置文件,首次启动自动生成
cluster-config-file /usr/local/redis/redis-cluster/conf/nodes1111.conf

# 请求超时时间,默认15s
cluster-node-timeout 10000
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

复制配置

Redis 集群一般由多个节点组成,节点数量至少为 6 个,才能保证组成完整高可用的集群,所以再复制 5 份配置文件

cp redis1111.conf redis2222.conf
cp redis1111.conf redis3333.conf
cp redis1111.conf redis4444.conf
cp redis1111.conf redis5555.conf
cp redis1111.conf redis6666.conf

# vim 进去里边修改:比如:% s/1111/2222/

配置启动命令及启动集群

cd /usr/local/redis/redis-cluster/redis/src
# 这样可以直接使用该命令,不需要再进入该命令目录
cp redis-cli redis-server /usr/bin/

#编写统一启动脚本、启动
cd /usr/local/redis/redis-cluster
vi redis-start.sh

#!/bin/bash
redis-server /usr/local/redis/redis-cluster/conf/redis1111.conf
redis-server /usr/local/redis/redis-cluster/conf/redis2222.conf
redis-server /usr/local/redis/redis-cluster/conf/redis3333.conf
redis-server /usr/local/redis/redis-cluster/conf/redis4444.conf
redis-server /usr/local/redis/redis-cluster/conf/redis5555.conf
redis-server /usr/local/redis/redis-cluster/conf/redis6666.conf

# 赋予权限
chmod 755 redis-start.sh
./redis-start.sh
#启动集群
执行命令:redis-cli --cluster create 192.168.5.199:1111 192.168.5.199:2222 192.168.5.199:3333 192.168.5.199:4444 192.168.5.199:5555 192.168.5.199:6666 --cluster-replicas 1 -a redis123

参数说明:注意 redis-5.0.0 版本开始才支持 “ --cluster ”

create 表示创建一个redis集群。

--cluster-replicas 1 表示为集群中的每一个主节点指定一个从节点,即一比一的复制。

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.5.199:5555 to 192.168.5.199:1111
Adding replica 192.168.5.199:6666 to 192.168.5.199:2222
Adding replica 192.168.5.199:4444 to 192.168.5.199:3333
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 3c09b846d56efc9c3d3367b7db74317ff0c54980 192.168.5.199:1111
   slots:[0-5460] (5461 slots) master
M: f35e954bd158848b5dc09358752daf7db35c7d7e 192.168.5.199:2222
   slots:[5461-10922] (5462 slots) master
M: 2e54db41b7916b64d7689b22a0f456dd099086fc 192.168.5.199:3333
   slots:[10923-16383] (5461 slots) master
S: edf74be3f4784f0d560505ad0ca10bcd76d57d42 192.168.5.199:4444
   replicates 3c09b846d56efc9c3d3367b7db74317ff0c54980
S: 609822d35d216b3bea5a308f25fa537055d85089 192.168.5.199:5555
   replicates f35e954bd158848b5dc09358752daf7db35c7d7e
S: ee9838644056c360a88399b3acb5f905e17eebc5 192.168.5.199:6666
   replicates 2e54db41b7916b64d7689b22a0f456dd099086fc
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.5.199:1111)
M: 3c09b846d56efc9c3d3367b7db74317ff0c54980 192.168.5.199:1111
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: edf74be3f4784f0d560505ad0ca10bcd76d57d42 192.168.5.199:4444
   slots: (0 slots) slave
   replicates 3c09b846d56efc9c3d3367b7db74317ff0c54980
S: ee9838644056c360a88399b3acb5f905e17eebc5 192.168.5.199:6666
   slots: (0 slots) slave
   replicates 2e54db41b7916b64d7689b22a0f456dd099086fc
M: 2e54db41b7916b64d7689b22a0f456dd099086fc 192.168.5.199:3333
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 609822d35d216b3bea5a308f25fa537055d85089 192.168.5.199:5555
   slots: (0 slots) slave
   replicates f35e954bd158848b5dc09358752daf7db35c7d7e
M: f35e954bd158848b5dc09358752daf7db35c7d7e 192.168.5.199:2222
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

检测集群完整性

#执行命令
redis-cli --cluster check 192.168.121.128:1111 -a redis123
[root@localhost ~]# redis-cli --cluster check 192.168.5.199:1111 -a "redis123"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.5.199:1111 (3c09b846...) -> 0 keys | 5461 slots | 1 slaves.
192.168.5.199:3333 (2e54db41...) -> 1 keys | 5461 slots | 1 slaves.
192.168.5.199:2222 (f35e954b...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 2 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.5.199:1111)
M: 3c09b846d56efc9c3d3367b7db74317ff0c54980 192.168.5.199:1111
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: edf74be3f4784f0d560505ad0ca10bcd76d57d42 192.168.5.199:4444
   slots: (0 slots) slave
   replicates 3c09b846d56efc9c3d3367b7db74317ff0c54980
S: ee9838644056c360a88399b3acb5f905e17eebc5 192.168.5.199:6666
   slots: (0 slots) slave
   replicates 2e54db41b7916b64d7689b22a0f456dd099086fc
M: 2e54db41b7916b64d7689b22a0f456dd099086fc 192.168.5.199:3333
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 609822d35d216b3bea5a308f25fa537055d85089 192.168.5.199:5555
   slots: (0 slots) slave
   replicates f35e954bd158848b5dc09358752daf7db35c7d7e
M: f35e954bd158848b5dc09358752daf7db35c7d7e 192.168.5.199:2222
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

登录验证

#执行命令
redis-cli -h 192.168.5.199 -p 1111 -a "redis123"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.5.199:1111> info cluster
# Cluster
cluster_enabled:1
192.168.5.199:1111> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:67776
cluster_stats_messages_pong_sent:71182
cluster_stats_messages_sent:138958
cluster_stats_messages_ping_received:71177
cluster_stats_messages_pong_received:67776
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:138958
192.168.5.199:1111>