架构
网络规划
主机名 | IP | 服务端口 | 主/从 |
---|---|---|---|
redis1 | 192.168.62.31 | 6381 | 6381-Master |
redis1 | 192.168.62.31 | 6384 | 6384-Slave |
redis2 | 192.168.62.32 | 6383 | 6383-Master |
redis2 | 192.168.62.32 | 6386 | 6386-Slave |
redis3 | 192.168.62.33 | 6385 | 6385-Master |
redis3 | 192.168.62.33 | 6382 | 6382-Slave |
目录规划
主机名 | 服务端口 | 软件安装目录 |
---|---|---|
redis1 | 6381 | /redis6381 |
redis1 | 6384 | /redis6384 |
redis2 | 6383 | /redis6383 |
redis2 | 6386 | /redis6386 |
redis3 | 6385 | /redis6385 |
redis3 | 6382 | /redis6382 |
创建用户
useradd redis
echo "redis"|passwd --stdin redis
创建目录
#192.168.62.31
mkdir /redis6381
mkdir /redis6384
chown -R redis:redis /redis
chmod 775 /redis
#192.168.62.32
mkdir /redis6383
mkdir /redis6386
chown -R redis:redis /redis
chmod 775 /redis
#192.168.62.32
mkdir /redis6385
mkdir /redis6382
chown -R redis:redis /redis
chmod 775 /redis
安装tcl包
yum install tcl tcl-devel
解压
tar zxvf /home/cpcn/redis-6.2.10.tar.gz
编译测试
192.168.62.31/32/33编译测试
cd redis-6.2.10
make && make test
make PREFIX=/usr/local/redis install
编辑配置
根据服务器IP修改bind参数
根据端口号修改port、pidfile、logfile、cluster-config-file参数
bind 192.168.62.31
protected-mode yes
masterauth Redis
port 6381
tcp-backlog 511
timeout 3600
tcp-keepalive 300
daemonize yes
supervised no
pidfile /redis6381/redis_6381.pid
loglevel notice
logfile /redis6381/redis_6381.log
databases 1
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump6381.rdb
dir /usr/local/redis6381
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 50000
maxmemory 1g
maxmemory-policy noeviction
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
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6381.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-require-full-coverage yes
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
requirepass Redis
启动服务
合计6个redis服务
nohup /usr/local/redis/bin/redis-server /redis6381/redis.conf
添加集群节点
cluster meet 192.168.56.{31,32,33} {6382,6383,6384,6385,6386}
指派槽位
#192.168.62.31
./bin/redis-cli -c -h 192.168.62.31 -p 6381 cluster addslots {0..5460}
#192.168.62.32
./bin/redis-cli -c -h 192.168.62.32 -p 6383 cluster addslots {5461..10922}
#192.168.62.33
./bin/redis-cli -c -h 192.168.62.33 -p 6385 cluster addslots {10923..16383}
配置主从关系
192.168.62.31
192.168.62.33:6381> cluster replicate 6eca1d1d4a178ebd93162b3fe1eb46be39227eab
OK
192.168.62.33:6381> cluster nodes
9a5c5379c99242de67f8a325669c296935cbd5fe 192.168.62.33:6385 master - 0 1599826755103 5 connected 10923-16383
6eca1d1d4a178ebd93162b3fe1eb46be39227eab 192.168.62.32:6383 master - 0 1599826753084 3 connected 5461-10922
2cb5b8971d08b6e8ac800f2d1c718edd57856bb9 192.168.62.31:6381 master - 0 1599826758127 2 connected 0-5460
7a0fee134d6d63316a98c67abb3270f6558337d5 192.168.62.32:6386 myself,master - 0 0 4 connected
dc88fb3227e1b689b3c2582f87257755affd4049 192.168.62.33:6382 master - 0 1599826756617 0 connected
121bd383dc1e65511929e07afce4a98e1d33ff2d 192.168.62.31:6384 slave 6eca1d1d4a178ebd93162b3fe1eb46be39227eab 0 1599826757120 3 connected
192.168.62.32
192.168.62.32:6386> cluster replicate 9a5c5379c99242de67f8a325669c296935cbd5fe
OK
192.168.62.32:6386> cluster nodes
9a5c5379c99242de67f8a325669c296935cbd5fe 192.168.62.33:6385 master - 0 1599826755103 5 connected 10923-16383
6eca1d1d4a178ebd93162b3fe1eb46be39227eab 192.168.62.32:6383 master - 0 1599826753084 3 connected 5461-10922
2cb5b8971d08b6e8ac800f2d1c718edd57856bb9 192.168.62.31:6381 master - 0 1599826758127 2 connected 0-5460
7a0fee134d6d63316a98c67abb3270f6558337d5 192.168.62.32:6386 myself,slave - 0 0 4 connected
dc88fb3227e1b689b3c2582f87257755affd4049 192.168.62.33:6382 master - 0 1599826756617 0 connected
121bd383dc1e65511929e07afce4a98e1d33ff2d 192.168.62.31:6384 slave 6eca1d1d4a178ebd93162b3fe1eb46be39227eab 0 1599826757120 3 connected
192.168.62.33
192.168.62.33:6382> cluster replicate 9a5c5379c99242de67f8a325669c296935cbd5fe
OK
192.168.62.33:6382> cluster nodes
121bd383dc1e65511929e07afce4a98e1d33ff2d 192.168.62.31:6384 slave 6eca1d1d4a178ebd93162b3fe1eb46be39227eab 0 1599826795383 3 connected
2cb5b8971d08b6e8ac800f2d1c718edd57856bb9 192.168.62.31:6381 master - 0 1599826797399 2 connected 0-5460
dc88fb3227e1b689b3c2582f87257755affd4049 192.168.62.33:6382 myself,slave - 0 0 0 connected
6eca1d1d4a178ebd93162b3fe1eb46be39227eab 192.168.62.32:6383 master - 0 1599826796391 3 connected 5461-10922
7a0fee134d6d63316a98c67abb3270f6558337d5 192.168.62.32:6386 slave 9a5c5379c99242de67f8a325669c296935cbd5fe 0 1599826793366 5 connected
9a5c5379c99242de67f8a325669c296935cbd5fe 192.168.62.33:6385 master - 0 1599826794375 5 connected 10923-16383
主从切换
从库执行
CLUSTER FAILOVER
CLUSTER FAILOVER FORCE
CLUSTER FAILOVER TAKEOVER