本次实验通过redis原生命令方式手工一步步完成redis集群的部署和实践,这个过程非常有利于理解redis集群的工作原理,为直接使用redis的工具cluster自动实现redis集群做好基础概念和知识
本次实验通过redis原生命令方式手工一步步完成redis集群的部署和实践,这个过程非常有利于理解redis集群的工作原理,为直接使用redis的工具cluster自动实现redis集群做好基础概念和知识储备。
部署步骤及过程:在所有节点安装redis,并配置开启cluster功能 ---> 各个节点执行meet,实现所有节点的相互通信 ---> 为各个master 节点指派槽位范围 ---> 指定各个节点的主从关系
1. 实验架构
下图是本实验的架构和槽位等分配示意图
下面两幅图是三主三从的工作原理图
需要准备六台虚拟机完成本实验
时间同步,确保NTP或Chrony服务正常运行。
禁用SELinux和防火墙(或放通需要的端口)
2. 手动部署Redis集群
原生命令手动部署过程:
第一步:在所有节点安装redis,并配置开启cluster功能
第二步:各个节点执行meet,实现所有节点的相互通信
第三步:为各个master 节点指派槽位范围
第四步:指定各个节点的主从关系
2.1 所有节点安装redis,开启cluster
将 install_redis7.sh 脚本文件上传到6台节点主机上,强烈建议redis的关闭尽量不要kill 的方式。
脚本内容:
#### 脚本内容[root@CentOS84-IP172-18 ]#cat install_redis7.sh
#
#******************************************************************<strong>
#Date: 2022-04-26
#FileName: install_redis_for_centos.sh
#Description: The test script
#Copyright (C): 2022 All rights reserved
#</strong>******************************************************************
VERSION=redis-7.0.0
PASSWORD=123456
INSTALL_DIR=/apps/redis
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
install() {
yum -y install gcc jemalloc-devel || { color "安装软件包失败,请检查网络配置" 1 ; exit; }
wget http://download.redis.io/releases/${VERSION}.tar.gz || { color "Redis 源码下载失败" 1 ; exit; }
tar xf ${VERSION}.tar.gz
cd ${VERSION}
make -j 4 PREFIX=${INSTALL_DIR} install && color "Redis 编译安装完成" 0 || { color "Redis 编译安装失败" 1 ;exit ; }
ln -s ${INSTALL_DIR}/bin/redis-* /usr/bin/
mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
cp redis.conf ${INSTALL_DIR}/etc/
sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${INSTALL_DIR}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf
if id redis &> /dev/null ;then
color "Redis 用户已存在" 1
else
useradd -r -s /sbin/nologin redis
color "Redis 用户创建成功" 0
fi
chown -R redis.redis ${INSTALL_DIR}
cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
sysctl -p
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
/etc/rc.d/rc.local
cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
#ExecStop=/bin/kill -s QUIT \$MAINPID
ExecStop=/usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a $PASSWORD shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now redis &> /dev/null && color "Redis 服务启动成功,Redis信息如下:" 0 || { color "Redis 启动失败" 1 ;exit; }
sleep 2
redis-cli -a $PASSWORD INFO Server 2> /dev/null
}
install
[root@CentOS84-IP172-18 ]#
通过SSH终端软件的特性,一次性完成规划的六台主机的redis安装和配置,具体界面如下图:
# 修改前先备份 /apps/redis/etc/redis.conf 文件
[root@CentOS84-IP172-18 ]#cp /apps/redis/etc/redis.conf{,.bak}
[root@CentOS84-IP172-18 ]#ll /apps/redis/etc/
total 216
-rw-r--r-- 1 redis redis 106612 May 3 21:20 redis.conf
-rw-r--r-- 1 root root 106612 May 3 23:37 redis.conf.bak
####################################################################################
# 脚本编译安装后,需要修改 redis.conf 文件开启 cluster
[root@CentOS84-IP172-18 ]#sed -i.bak -e '/masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf
# 修改完成后的 /apps/redis/etc/redis.conf 有效配置行
[root@CentOS84-IP172-18 ]#egrep -v "^#|^$" /apps/redis/etc/redis.conf
bind 0.0.0.0 -::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /apps/redis/run/redis_6379.pid
loglevel notice
logfile /apps/redis/log/redis-6379.log
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /apps/redis/data/
masterauth 123456
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-diskless-sync-max-replicas 0
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appenddirname "appendonlydir"
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
aof-timestamp-enabled no
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-require-full-coverage no
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-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
jemalloc-bg-thread yes
[root@CentOS84-IP172-18 ]#
## 验证集群启动
# 没有重新启动redis前的信息
[root@CentOS84-IP172-18 ]#ps aux | grep redis
redis 39431 0.1 0.1 91608 7176 ? Ssl May03 0:13 /apps/redis/bin/redis-server 0.0.0.0:6379
root 49166 0.0 0.0 12136 1040 pts/0 S+ 00:10 0:00 grep --color=auto redis
[root@CentOS84-IP172-18 ]#
# 启动或者重新启动 rendis 后,验证启动状态
[root@CentOS84-IP172-18 ]#systemctl enable --now redis
[root@CentOS84-IP172-18 ]#systemctl restart redis
[root@CentOS84-IP172-18 ]#systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2022-05-04 00:01:54 CST; 8s ago
Process: 109533 ExecStop=/usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456 shutdown (code=exited, status=0/SUCCESS)
Main PID: 109536 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 23544)
Memory: 2.6M
CGroup: /system.slice/redis.service
└─109536 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]
[root@CentOS84-IP172-18 ]#
# 重新启动redis服务后的信息 看到有 [cluster] 标签了
[root@CentOS84-IP172-18 ]#ps aux | grep redis
redis 117808 0.1 0.2 94168 8280 ? Ssl 00:01 0:00 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]
root 117896 0.0 0.0 12136 1176 pts/0 S+ 00:09 0:00 grep --color=auto redis
[root@CentOS84-IP172-18 ]#
# 进入redis,查看 Cluster 的信息
[root@CentOS84-IP172-18 ]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info cluster
# Cluster
cluster_enabled:1
127.0.0.1:6379>
2.2 执行meet 操作实现所有节点相互通信
# 执行meet前查看下 cluser nodes 情况[root@CentOS84-IP172-18 ]#redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
d5462f6961c0f45ecbdf12d6606e6993c33e3e29 :6379@16379 myself,master - 0 0 0 connected
[root@CentOS84-IP172-18 ]#
# 在任一节点上完成meet操作,将其和其它所有节点进行meet通信,进而实现所有节点间的通信
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.18 -a 123456 --no-auth-warning cluster meet 172.16.0.28 6379
OK
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
5163e9abbf42bd3540d9c04f6fb384ea23a1f58e 172.16.0.28:6379@16379 master - 0 1651594704395 1 connected
d5462f6961c0f45ecbdf12d6606e6993c33e3e29 172.16.0.18:6379@16379 myself,master - 0 0 0 connected
[root@CentOS84-IP172-18 ]#
# 上面是完成和一个节点meet后的情况,下面将剩余的meet工作全部完成,并查看节点间的meet关系信息
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.18 -a 123456 --no-auth-warning cluster meet 172.16.0.38 6379
OK
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.18 -a 123456 --no-auth-warning cluster meet 172.16.0.118 6379
OK
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.18 -a 123456 --no-auth-warning cluster meet 172.16.0.128 6379
OK
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.18 -a 123456 --no-auth-warning cluster meet 172.16.0.138 6379
OK
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4c429a48054a771cbc154319182a3d16cf4ce7a1 172.16.0.38:6379@16379 master - 0 1651594832000 2 connected
3bbdbc3ab34b67161655974fed9de5667def8ed0 172.16.0.128:6379@16379 master - 0 1651594832000 4 connected
d5462f6961c0f45ecbdf12d6606e6993c33e3e29 172.16.0.18:6379@16379 myself,master - 0 1651594829000 0 connected
5163e9abbf42bd3540d9c04f6fb384ea23a1f58e 172.16.0.28:6379@16379 master - 0 1651594834859 1 connected
a7583f69703921c6b3d14a97f54b3015966155ab 172.16.0.118:6379@16379 master - 0 1651594832852 3 connected
3d69cddc61df9443ff7de9850c220fc9e9187c03 172.16.0.138:6379@16379 master - 0 1651594833856 5 connected
[root@CentOS84-IP172-18 ]#
# 查看端口监听 16379
[root@CentOS84-IP172-18 ]#ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 511 0.0.0.0:16379 0.0.0.0:* LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* LISTEN 0 511 [::1]:16379 [::]:* LISTEN 0 511 [::1]:6379 [::]:*
[root@CentOS84-IP172-18 ]#ss -lntp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 511 0.0.0.0:16379 0.0.0.0:* users:(("redis-server",pid=117808,fd=9)) LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* users:(("redis-server",pid=117808,fd=6)) LISTEN 0 511 [::1]:16379 [::]:* users:(("redis-server",pid=117808,fd=10)) LISTEN 0 511 [::1]:6379 [::]:* users:(("redis-server",pid=117808,fd=7)) [root@CentOS84-IP172-18 ]#
# 查看节点间建立的联系
[root@CentOS84-IP172-18 ]#ss -nt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process ESTAB 0 0 172.16.0.18:58989 172.16.0.118:16379 ESTAB 0 0 172.16.0.18:62839 172.16.0.128:16379 ESTAB 0 0 172.16.0.18:16379 172.16.0.128:24965 ESTAB 0 0 172.16.0.18:38449 172.16.0.138:16379 ESTAB 0 0 172.16.0.18:16379 172.16.0.28:25481 ESTAB 0 0 172.16.0.18:62363 172.16.0.38:16379 ESTAB 0 0 172.16.0.18:16379 172.16.0.38:34755 ESTAB 0 96 172.16.0.18:22 172.16.0.254:1112 ESTAB 0 0 172.16.0.18:19803 172.16.0.28:16379 ESTAB 0 0 172.16.0.18:16379 172.16.0.118:41535 ESTAB 0 0 172.16.0.18:16379 172.16.0.138:52871 [root@CentOS84-IP172-18 ]#
# 进入redis-cli 可以看到 cluster_state:fail 是因为还没创建槽位
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:fail # 槽位没分配,所以提示失败
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:0
cluster_stats_messages_ping_sent:383
cluster_stats_messages_pong_sent:393
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:781
cluster_stats_messages_ping_received:393
cluster_stats_messages_pong_received:388
cluster_stats_messages_received:781
total_cluster_links_buffer_limit_exceeded:0
[root@CentOS84-IP172-18 ]#
# 由于没有槽位无法创建key
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 set name shone
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) CLUSTERDOWN Hash slot not served
[root@CentOS84-IP172-18 ]#
2.3 给各个master 节点指派槽位范围
利用 addslot.sh 脚本给各个master 节点指派槽位范围,并实践修改指派错了的槽位。
# 导入 addslot.sh 脚本[root@CentOS84-IP172-18 ]#vim addslot.sh
#
#******************************************************************<strong>
#FileName: addslot.sh
#Description: The test script
#Copyright (C): 2020 All rights reserved
#</strong>******************************************************************
host=$1
port=$2
start=$3
end=$4
pass=123456
for slot in `seq ${start} ${end}`;do
echo slot:$slot
redis-cli -h ${host} -p $port -a ${pass} --no-auth-warning cluster addslots ${slot}
done
[root@CentOS84-IP172-18 ]#
[root@CentOS84-IP172-18 ]#chmod +x addslot.sh
[root@CentOS84-IP172-18 ]#./addslot.sh 172.16.0.18 6379 0 5461
#### 查看已经分配的槽位
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 --no-auth-warning cluster nodes
4c429a48054a771cbc154319182a3d16cf4ce7a1 172.16.0.38:6379@16379 master - 0 1651595968085 2 connected
3bbdbc3ab34b67161655974fed9de5667def8ed0 172.16.0.128:6379@16379 master - 0 1651595969088 4 connected
d5462f6961c0f45ecbdf12d6606e6993c33e3e29 172.16.0.18:6379@16379 myself,master - 0 1651595967000 0 connected 0-5461
5163e9abbf42bd3540d9c04f6fb384ea23a1f58e 172.16.0.28:6379@16379 master - 0 1651595968000 1 connected
a7583f69703921c6b3d14a97f54b3015966155ab 172.16.0.118:6379@16379 master - 0 1651595969000 3 connected
3d69cddc61df9443ff7de9850c220fc9e9187c03 172.16.0.138:6379@16379 master - 0 1651595970092 5 connected
[root@CentOS84-IP172-18 ]#
####################################################################################
####################################################################################
#### 这个地方特别容易犯错,如果需要彻底删除掉,需要去修改或者删除 /apps/redis/data/nodes-6379.conf 文件内的内容。这个文件内容还包括meet的关系,删除就需要重新建立meet关系。
# 下面这个命令就把应该在IP28的机器槽位搞到了IP18上去了,可以修改和删除 /apps/redis/data/nodes-6379.conf文件方式,也可以通过脚本删除多分配的槽位
# 下面模拟了错误分配槽位
[root@CentOS84-IP172-18 ]#./addslot.sh 172.16.0.18 6379 5462 10922
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 --no-auth-warning cluster nodes
4c429a48054a771cbc154319182a3d16cf4ce7a1 172.16.0.38:6379@16379 master - 0 1651596170000 2 connected
3bbdbc3ab34b67161655974fed9de5667def8ed0 172.16.0.128:6379@16379 master - 0 1651596171000 4 connected
d5462f6961c0f45ecbdf12d6606e6993c33e3e29 172.16.0.18:6379@16379 myself,master - 0 1651596171000 0 connected 0-10922
5163e9abbf42bd3540d9c04f6fb384ea23a1f58e 172.16.0.28:6379@16379 master - 0 1651596172876 1 connected
a7583f69703921c6b3d14a97f54b3015966155ab 172.16.0.118:6379@16379 master - 0 1651596170000 3 connected
3d69cddc61df9443ff7de9850c220fc9e9187c03 172.16.0.138:6379@16379 master - 0 1651596171873 5 connected
[root@CentOS84-IP172-18 ]#
####################################################################################
#### 通过在所有的节点上修改文件 /apps/redis/data/nodes-6379.conf 将10922 修改成 5461,重新启动redis服务器后,就可以纠正槽位分配错误的问题。
[root@CentOS84-IP172-18 ]#vim /apps/redis/data/nodes-6379.conf
[root@CentOS84-IP172-18 ]#systemctl restart redis
####################################################################################
####################################################################################
## 回归到正常的实验途径上,继续完成IP28 IP38两个主节点上的槽位
[root@CentOS84-IP172-18 ]#./addslot.sh 172.16.0.28 6379 5462 10922
[root@CentOS84-IP172-18 ]#./addslot.sh 172.16.0.38 6379 10923 16383
#### 上面的脚本运行过程中看到,再另外一个窗口上可以看到槽位数字在不停增加
[root@CentOS84-IP172-28 ]#redis-cli -a 123456 --no-auth-warning cluster nodes
#### 全部分配完成后的槽位信息
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 --no-auth-warning cluster nodes
a7583f69703921c6b3d14a97f54b3015966155ab 172.16.0.118:6379@16379 master - 0 1651598124428 3 connected
5163e9abbf42bd3540d9c04f6fb384ea23a1f58e 172.16.0.28:6379@16379 master - 0 1651598123000 1 connected 5462-10922
3d69cddc61df9443ff7de9850c220fc9e9187c03 172.16.0.138:6379@16379 master - 0 1651598123423 5 connected
4c429a48054a771cbc154319182a3d16cf4ce7a1 172.16.0.38:6379@16379 master - 0 1651598124000 2 connected 10923-16383
3bbdbc3ab34b67161655974fed9de5667def8ed0 172.16.0.128:6379@16379 master - 0 1651598123000 4 connected
d5462f6961c0f45ecbdf12d6606e6993c33e3e29 172.16.0.18:6379@16379 myself,master - 0 1651598122000 0 connected 0-5461
[root@CentOS84-IP172-18 ]#
#### 下面是槽位全部分配完成后 redis-cli 看到的集群情况
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 --no-auth-warning 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:5
cluster_my_epoch:0
cluster_stats_messages_ping_sent:913
cluster_stats_messages_pong_sent:917
cluster_stats_messages_sent:1830
cluster_stats_messages_ping_received:917
cluster_stats_messages_pong_received:913
cluster_stats_messages_received:1830
total_cluster_links_buffer_limit_exceeded:0
[root@CentOS84-IP172-18 ]#
2.4 指定各个节点的主从关系
规划的是三主三从的架构,主从关系需要通过 ID 来完成指定。
## 指定主从关系[root@CentOS84-IP172-18 ]#redis-cli -a 123456 --no-auth-warning cluster nodes
a7583f69703921c6b3d14a97f54b3015966155ab 172.16.0.118:6379@16379 master - 0 1651598189000 3 connected
5163e9abbf42bd3540d9c04f6fb384ea23a1f58e 172.16.0.28:6379@16379 master - 0 1651598189663 1 connected 5462-10922
3d69cddc61df9443ff7de9850c220fc9e9187c03 172.16.0.138:6379@16379 master - 0 1651598187655 5 connected
4c429a48054a771cbc154319182a3d16cf4ce7a1 172.16.0.38:6379@16379 master - 0 1651598190000 2 connected 10923-16383
3bbdbc3ab34b67161655974fed9de5667def8ed0 172.16.0.128:6379@16379 master - 0 1651598190666 4 connected
d5462f6961c0f45ecbdf12d6606e6993c33e3e29 172.16.0.18:6379@16379 myself,master - 0 1651598188000 0 connected 0-5461
[root@CentOS84-IP172-18 ]#
# 通过上面cluster nodes,查看master的ID信息,执行下面操作,将对应的slave 指定相应的master节点,实现三对主从节点
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.118 -a 123456 --no-auth-warning cluster replicate d5462f6961c0f45ecbdf12d6606e6993c33e3e29
OK
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.128 -a 123456 --no-auth-warning cluster replicate 5163e9abbf42bd3540d9c04f6fb384ea23a1f58e
OK
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.138 -a 123456 --no-auth-warning cluster replicate 4c429a48054a771cbc154319182a3d16cf4ce7a1
OK
[root@CentOS84-IP172-18 ]#
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 --no-auth-warning cluster nodes
a7583f69703921c6b3d14a97f54b3015966155ab 172.16.0.118:6379@16379 slave d5462f6961c0f45ecbdf12d6606e6993c33e3e29 0 1651598551144 0 connected
5163e9abbf42bd3540d9c04f6fb384ea23a1f58e 172.16.0.28:6379@16379 master - 0 1651598547129 1 connected 5462-10922
3d69cddc61df9443ff7de9850c220fc9e9187c03 172.16.0.138:6379@16379 slave 4c429a48054a771cbc154319182a3d16cf4ce7a1 0 1651598549136 2 connected
4c429a48054a771cbc154319182a3d16cf4ce7a1 172.16.0.38:6379@16379 master - 0 1651598550140 2 connected 10923-16383
3bbdbc3ab34b67161655974fed9de5667def8ed0 172.16.0.128:6379@16379 slave 5163e9abbf42bd3540d9c04f6fb384ea23a1f58e 0 1651598547000 1 connected
d5462f6961c0f45ecbdf12d6606e6993c33e3e29 172.16.0.18:6379@16379 myself,master - 0 1651598549000 0 connected 0-5461
[root@CentOS84-IP172-18 ]#
#### 三组主从关系确定好后看到的信息
####################################################################################
# IP18 上看到的 info replication 信息
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 --no-auth-warning info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.0.118,port=6379,state=online,offset=574,lag=1
master_failover_state:no-failover
master_replid:d7028580afb352b417a1062245640c456b56b8ed
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:574
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:574
[root@CentOS84-IP172-18 ]#
[root@CentOS84-IP172-118 ]#redis-cli -a 123456 --no-auth-warning info replication
# Replication
role:slave
master_host:172.16.0.18
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_read_repl_offset:658
slave_repl_offset:658
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:d7028580afb352b417a1062245640c456b56b8ed
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:658
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:658
[root@CentOS84-IP172-118 ]#
####################################################################################
# IP28 上看到的 info replication 信息
[root@CentOS84-IP172-28 ]#redis-cli -a 123456 --no-auth-warning info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.0.128,port=6379,state=online,offset=560,lag=0
master_failover_state:no-failover
master_replid:298ddc200843ba7a7110928c2d4e9ca0b1c7a551
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:560
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:560
[root@CentOS84-IP172-28 ]#
[root@CentOS84-IP172-128 ]#redis-cli -a 123456 --no-auth-warning info replication
# Replication
role:slave
master_host:172.16.0.28
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_read_repl_offset:560
slave_repl_offset:560
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:298ddc200843ba7a7110928c2d4e9ca0b1c7a551
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:560
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:560
[root@CentOS84-IP172-128 ]#
####################################################################################
# IP38 上看到的 info replication 信息
[root@CentOS84-IP172-38 ]#redis-cli -a 123456 --no-auth-warning info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.0.138,port=6379,state=online,offset=672,lag=0
master_failover_state:no-failover
master_replid:348918a6f1e9fc1b9eab3289584760767ed883b4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:672
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:672
[root@CentOS84-IP172-38 ]#
[root@CentOS84-IP172-138 ]#redis-cli -a 123456 --no-auth-warning info replication
# Replication
role:slave
master_host:172.16.0.38
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:672
slave_repl_offset:672
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:348918a6f1e9fc1b9eab3289584760767ed883b4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:672
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:672
[root@CentOS84-IP172-138 ]#
####################################################################################
# 查看主从节关系及槽位信息
[root@CentOS84-IP172-18 ]#redis-cli -a 123456 --no-auth-warning cluster slots
1) 1) (integer) 0
2) (integer) 5461
3) 1) "172.16.0.18"
2) (integer) 6379
3) "d5462f6961c0f45ecbdf12d6606e6993c33e3e29"
4) (empty array)
4) 1) "172.16.0.118"
2) (integer) 6379
3) "a7583f69703921c6b3d14a97f54b3015966155ab"
4) (empty array)
2) 1) (integer) 5462
2) (integer) 10922
3) 1) "172.16.0.28"
2) (integer) 6379
3) "5163e9abbf42bd3540d9c04f6fb384ea23a1f58e"
4) (empty array)
4) 1) "172.16.0.128"
2) (integer) 6379
3) "3bbdbc3ab34b67161655974fed9de5667def8ed0"
4) (empty array)
3) 1) (integer) 10923
2) (integer) 16383
3) 1) "172.16.0.38"
2) (integer) 6379
3) "4c429a48054a771cbc154319182a3d16cf4ce7a1"
4) (empty array)
4) 1) "172.16.0.138"
2) (integer) 6379
3) "3d69cddc61df9443ff7de9850c220fc9e9187c03"
4) (empty array)
[root@CentOS84-IP172-18 ]#
2.5 验证 redis cluster 访问
#### 验证 redis cluster 访问# -c 表示以集群方式连接
[root@CentOS84-IP172-18 ]#
[root@CentOS84-IP172-18 ]#redis-cli -c -h 172.16.0.18 -a 123456 --no-auth-warning set name shone
OK
[root@CentOS84-IP172-18 ]#redis-cli -c -h 172.16.0.18 -a 123456 --no-auth-warning get name
"shone"
# 不带 -c 访问会提示到哪个节点去,可以看到信息
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.18 -a 123456 --no-auth-warning get name
(error) MOVED 5798 172.16.0.28:6379
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.28 -a 123456 --no-auth-warning get name
"shone"
[root@CentOS84-IP172-18 ]#redis-cli -h 172.16.0.38 -a 123456 --no-auth-warning get name
(error) MOVED 5798 172.16.0.28:6379
[root@CentOS84-IP172-18 ]#