基于redis 5的redis cluster 部署
Posted y_zilong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于redis 5的redis cluster 部署相关的知识,希望对你有一定的参考价值。
1、创建redis cluster集群的环境准备
每个redis节点采用相同的redis版本、相同的密码、硬件配置
每个redis服务器必须没有任何数据
准备六台主机,地址如下
10.0.0.6
10.0.0.16
10.0.0.26
10.0.0.36
10.0.0.46
10.0.0.56
2、启动redis cluster配置
#所有6台主机都执行以下配置
#安装redis 5.0.9脚本
[root@cent7_6 ~]# cat install_redis.sh
#!/bin/bash
VERSION=redis-5.0.9
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
#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@cent7_6 ~]# redis-cli --version
redis-cli 5.0.9
#每个节点修改redis配置,必须开启cluster功能的参数
[root@cent7_6 ~]# sed -i -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
[root@cent7_6 ~]# systemctl enable --now redis
#验证当前redis服务状态
[root@cent7_6 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 511 *:16379 *:*
LISTEN 0 511 *:6379 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
#注意进程有[cluster]状态
[root@cent7_6 ~]# ps -ef |grep redis
redis 10066 1 0 09:26 ? 00:00:00 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]
root 10074 5773 0 09:26 pts/2 00:00:00 grep --color=auto redis
root 10075 2008 0 09:26 pts/1 00:00:00 grep --color=auto redis
3、创建集群
#命令redis-cli的选项 --cluster-replicas 1 表示每个master对应一个slave节点
[root@cent7_6 ~]# redis-cli -a 123456 --cluster create 10.0.0.6:6379 10.0.0.16:6379 10.0.0.26:6379 10.0.0.36:6379 10.0.0.46:6379 10.0.0.56:6379 --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 10.0.0.46:6379 to 10.0.0.6:6379
Adding replica 10.0.0.56:6379 to 10.0.0.16:6379
Adding replica 10.0.0.36:6379 to 10.0.0.26:6379
M: 3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379 #带M的为master
slots:[0-5460] (5461 slots) master #当前master的槽位起始和结束位
M: cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379
slots:[5461-10922] (5462 slots) master
M: 37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379
slots:[10923-16383] (5461 slots) master
S: db58e59352e7ea4386e20d9f042724b5560606b9 10.0.0.36:6379 #带S的slave
replicates 37d2503603fe9e06182e6053d302f8fc7a66f262
S: 433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379
replicates 3feffd6b2f7959a74af852238237575daede4c96
S: 471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379
replicates cdb9f3a8f54c47381d401c1fee103547e0d3922a
Can I set the above configuration? (type 'yes' to accept): yes#输入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 10.0.0.6:6379)
M: 3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379
slots:[0-5460] (5461 slots) master #已经分配的槽位
1 additional replica(s) #分配了一个slave
M: cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: db58e59352e7ea4386e20d9f042724b5560606b9 10.0.0.36:6379
slots: (0 slots) slave #slave没有分配槽位
replicates 37d2503603fe9e06182e6053d302f8fc7a66f262 #对应的master的10.0.0.26的ID
S: 433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379
slots: (0 slots) slave
replicates 3feffd6b2f7959a74af852238237575daede4c96 #对应的master的10.0.0.6的ID
M: 37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379
slots: (0 slots) slave
replicates cdb9f3a8f54c47381d401c1fee103547e0d3922a #对应的master的10.0.0.16的ID
[OK] All nodes agree about slots configuration. #所有节点槽位分配完成
>>> Check for open slots... #检查打开的槽位
>>> Check slots coverage... #检查插槽覆盖范围
[OK] All 16384 slots covered. #所有槽位(16384个)分配完成
#观察以上结果,可以看到3组master/slave
master:10.0.0.6---slave:10.0.0.46
master:10.0.0.16---slave:10.0.0.56
master:10.0.0.26---slave:10.0.0.36
4、查看主从状态
[root@cent7_6 ~]# redis-cli -a 123456 -c info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.46,port=6379,state=online,offset=1162,lag=0
master_replid:049cc6cdb873eeb50e3f8a7658f6be63bbfc97b9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1162
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1162
[root@cent7_16 ~]# redis-cli -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.56,port=6379,state=online,offset=1302,lag=0
master_replid:69dfd2e401cfc85fb1a8f8745f2b61d40a3f3192
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1302
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1302
[root@cent7_26 ~]# redis-cli -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.36,port=6379,state=online,offset=1302,lag=1
master_replid:616ac5baf09e828e600c5c10e7afac040aef6a1f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1302
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1302
[root@cent7_36 ~]# redis-cli -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.26
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:1302
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:616ac5baf09e828e600c5c10e7afac040aef6a1f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1302
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1302
[root@centos7_46 ~]# redis-cli -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.6
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:1302
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:049cc6cdb873eeb50e3f8a7658f6be63bbfc97b9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1302
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1302
[root@cent7_56 ~]# redis-cli -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.16
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:1302
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:69dfd2e401cfc85fb1a8f8745f2b61d40a3f3192
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1302
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1302
#查看指定master节点的slave节点信息
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning cluster nodes
cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379@16379 master - 0 1625320272198 2 connected 5461-10922
db58e59352e7ea4386e20d9f042724b5560606b9 10.0.0.36:6379@16379 slave 37d2503603fe9e06182e6053d302f8fc7a66f262 0 1625320270182 4 connected
3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379@16379 myself,master - 0 1625320270000 1 connected 0-5460
433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379@16379 slave 3feffd6b2f7959a74af852238237575daede4c96 0 1625320270000 5 connected
37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379@16379 master - 0 1625320270000 3 connected 10923-16383
471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379@16379 slave cdb9f3a8f54c47381d401c1fee103547e0d3922a 0 1625320269175 6 connected
#注意:重启之后发现
master上:
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning info replication
# Replication
role:master
connected_slaves:0 #已连接master上的从为0
master_replid:314d78c1ebda47f13a28f18d77fcd42c8e66acf6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
#查看从上面的错误日志
[root@centos7_46 ~]# tail /apps/redis/log/redis-6379.log
1453:S 04 Jul 2021 04:15:13.867 * MASTER <-> REPLICA sync started
1453:S 04 Jul 2021 04:15:14.869 # Error condition on socket for SYNC: No route to host #没有到主机的路由
1453:S 04 Jul 2021 04:15:14.874 * Connecting to MASTER 10.0.0.6:6379
1453:S 04 Jul 2021 04:15:14.874 * MASTER <-> REPLICA sync started
#查看防火墙:
[root@cent7_6 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-07-04 03:57:52 EDT; 19min ago
Docs: man:firewalld(1)
Main PID: 743 (firewalld)
CGroup: /system.slice/firewalld.service
└─743 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
[root@cent7_6 ~]#
#关闭防火墙
[root@cent7_6 ~]# systemctl stop firewalld
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning info replication
# Replication
role:master
connected_slaves:1 #从已连接上主
slave0:ip=10.0.0.46,port=6379,state=online,offset=56,lag=0
master_replid:1cf92c3740574b41aa6d72d6e6e2dc6af1d09b57
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
5、验证集群状态
[root@cent7_6 ~]# 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:8
cluster_my_epoch:1
cluster_stats_messages_ping_sent:3057
cluster_stats_messages_pong_sent:594
cluster_stats_messages_fail_sent:5
cluster_stats_messages_auth-ack_sent:2
cluster_stats_messages_sent:3658
cluster_stats_messages_ping_received:594
cluster_stats_messages_pong_received:579
cluster_stats_messages_fail_received:4
cluster_stats_messages_auth-req_received:2
cluster_stats_messages_received:1179
#查看任意节点的集群状态
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning --cluster info 10.0.0.36:6379
10.0.0.36:6379 (db58e593...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.6:6379 (3feffd6b...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.56:6379 (471aa637...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
6、查看集群node对应关系
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning cluster nodes
cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379@16379 slave 471aa637ee8fe618d46d294afa10e77c0461bde4 0 1625387490772 7 connected
db58e59352e7ea4386e20d9f042724b5560606b9 10.0.0.36:6379@16379 master - 0 1625387488000 8 connected 10923-16383
3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379@16379 myself,master - 0 1625387490000 1 connected 0-5460
433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379@16379 slave 3feffd6b2f7959a74af852238237575daede4c96 0 1625387488000 5 connected
471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379@16379 master - 0 1625387490000 7 connected 5461-10922
37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379@16379 slave db58e59352e7ea4386e20d9f042724b5560606b9 0 1625387489000 8 connected
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning --cluster check 10.0.0.36:6379
10.0.0.36:6379 (db58e593...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.6:6379 (3feffd6b...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.56:6379 (471aa637...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.0.36:6379)
M: db58e59352e7ea4386e20d9f042724b5560606b9 10.0.0.36:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379
slots: (0 slots) slave
replicates db58e59352e7ea4386e20d9f042724b5560606b9
M: 471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379
slots: (0 slots) slave
replicates 471aa637ee8fe618d46d294afa10e77c0461bde4
S: 433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379
slots: (0 slots) slave
replicates 3feffd6b2f7959a74af852238237575daede4c96
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
7、验证集群写入key
7.1redis cluster写入key
#经过算法计算,当前key的槽位需要写入指定的node
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning cluster nodes
37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379@16379 slave db58e59352e7ea4386e20d9f042724b5560606b9 0 1625391528906 8 connected
cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379@16379 slave 471aa637ee8fe618d46d294afa10e77c0461bde4 0 1625391527897 7 connected
3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379@16379 myself,master - 0 1625391527000 1 connected 0-5460
471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379@16379 master - 0 1625391526890 7 connected 5461-10922
db58e59352e7ea4386e20d9f042724b5560606b9 10.0.0.36:6379@16379 master - 0 1625391526000 8 connected 10923-16383
433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379@16379 slave 3feffd6b2f7959a74af852238237575daede4c96 0 1625391528000 5 connected
#观察以上结果,可以看到3组master/slave
master:10.0.0.6---slave:10.0.0.46
master:10.0.0.36---slave:10.0.0.26
master:10.0.0.56---slave:10.0.0.16
#经过算法计算,当前key的槽位需要写入指定的node
[root@cent7_6 ~]# redis-cli -a 123456 -h 10.0.0.6 set key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) MOVED 9189 10.0.0.56:6379 #槽位不在当前node所以无法写入
#指定槽位对应node可写入
[root@cent7_6 ~]# redis-cli -a 123456 -h 10.0.0.56 set key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
#对应的slave节点可以keys *,但get key1失败,可以到master上执行key1
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.16 keys "*"
1) "key1"
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.16 get key1
(error) MOVED 9189 10.0.0.56:6379
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.56 get key1
"values1"
7.2redis cluster计算key所属的slot
[root@cent7_6 ~]# redis-cli -h 10.0.0.6 -a 123456 --no-auth-warning cluster nodes
37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379@16379 slave db58e59352e7ea4386e20d9f042724b5560606b9 0 1625392209060 8 connected
cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379@16379 slave 471aa637ee8fe618d46d294afa10e77c0461bde4 0 1625392209000 7 connected
3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379@16379 myself,master - 0 1625392208000 1 connected 0-5460
471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379@16379 master - 0 1625392208052 7 connected 5461-10922
db58e59352e7ea4386e20d9f042724b5560606b9 10.0.0.36:6379@16379 master - 0 1625392209000 8 connected 10923-16383
433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379@16379 slave 3feffd6b2f7959a74af852238237575daede4c96 0 1625392210066 5 connected
#计算得到hello对应的slot
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.6 cluster keyslot hello
(integer) 866
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning set hello yzl
OK
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.6 cluster keyslot yzil
(integer) 12883
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.6 set name yzil
(error) MOVED 5798 10.0.0.56:6379
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.56 set name yzil
OK
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.56 get name
"yzil"
#使用选项 -c 以集群模式连接
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.6 -c
10.0.0.6:6379> cluster keyslot linux
(integer) 12299
10.0.0.6:6379> set linux love
-> Redirected to slot [12299] located at 10.0.0.36:6379
OK
10.0.0.36:6379> get linux
"love"
10.0.0.36:6379> exit
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.36 get linux
"love"
8、python脚本实现redis cluster集群写入
[root@cent7_6 ~]# yum -y install python3
[root@cent7_6 ~]# pip3 install redis-py-cluster
[root@cent7_6 ~]# cat redis_cluster_test.py
#!/usr/bin/env python3
from rediscluster import RedisCluster
if __name__ == '__main__':
startup_nodes = [
{"host":"10.0.0.6", "port":6379},
{"host":"10.0.0.16", "port":6379},
{"host":"10.0.0.26", "port":6379},
{"host":"10.0.0.36", "port":6379},
{"host":"10.0.0.46", "port":6379},
{"host":"10.0.0.56", "port":6379}]
try:
redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456', decode_responses=True)
except Exception as e:
print(e)
for i in range(0, 10000):
redis_conn.set('key'+str(i),'value'+str(i))
print('key'+str(i)+':',redis_conn.get('key'+str(i)))
[root@cent7_6 ~]# chmod +x redis_cluster_test.py
[root@cent7_6 ~]# ./redis_cluster_test.py
key9998: value9998
key9999: value9999
#验证数据
[root@cent7_6 ~]# redis-cli -a 123456 -h 10.0.0.6 --no-auth-warning
10.0.0.6:6379> dbsize
(integer) 3332
10.0.0.6:6379> get key1
(error) MOVED 9189 10.0.0.56:6379
10.0.0.6:6379> get key2
"value2"
10.0.0.6:6379> get key3
"value3"
10.0.0.6:6379> keys *
....
3330) "key6055"
3331) "key7789"
3332) "key8912"
10.0.0.6:6379>
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.16 dbsize
(integer) 3341
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.56 dbsize
(integer) 3341
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.46 get key5
(error) MOVED 9057 10.0.0.56:6379
[root@cent7_6 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.56 get key5
"value5"
9、模拟master故障,对应的slave节点自动提升为新master
#主从节点
master:10.0.0.6---slave:10.0.0.46
master:10.0.0.36---slave:10.0.0.26
master:10.0.0.56---slave:10.0.0.16
#模拟master 10.0.0.36出现故障,需要相应的数秒故障转移时间
[root@cent7_36 ~]# tail -f /apps/redis/log/redis-6379.log
[root@cent7_36 ~]# redis-cli -a 123456 --no-auth-warning
127.0.0.1:6379> shutdown
not connected> exit
[root@cent7_36 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::]:111 [::]:*
#可以看到slave10.0.0.26成为新的master
[root@cent7_36 ~]# redis-cli -a 123456 --cluster info 10.0.0.6:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 10.0.0.36:6379: Connection refused
10.0.0.6:6379 (3feffd6b...) -> 3332 keys | 5461 slots | 1 slaves.
10.0.0.26:6379 (37d25036...) -> 3330 keys | 5461 slots | 0 slaves. #10.0.0.26为新的master
10.0.0.56:6379 (471aa637...) -> 3341 keys | 5462 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
[root@cent7_36 ~]# redis-cli -a 123456 --cluster check 10.0.0.6:6379 --no-auth-warning
Could not connect to Redis at 10.0.0.36:6379: Connection refused
10.0.0.6:6379 (3feffd6b...) -> 3332 keys | 5461 slots | 1 slaves.
10.0.0.26:6379 (37d25036...) -> 3330 keys | 5461 slots | 0 slaves.
10.0.0.56:6379 (471aa637...) -> 3341 keys | 5462 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.0.6:6379)
M: 3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379
slots:[10923-16383] (5461 slots) master
S: cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379
slots: (0 slots) slave
replicates 471aa637ee8fe618d46d294afa10e77c0461bde4
M: 471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379
slots: (0 slots) slave
replicates 3feffd6b2f7959a74af852238237575daede4c96
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#恢复故障节点 10.0.0.36自动成为10.0.0.26 slave节点
[root@cent7_36 ~]# systemctl start redis
#查看自动生成的匹配文件,可以查看10.0.0.36自动成为slave节点
[root@cent7_36 ~]# tail -f /apps/redis/log/redis-6379.log
1758:S 04 Jul 2021 06:49:36.559 # Cluster state changed: ok
1758:S 04 Jul 2021 06:49:37.566 * Connecting to MASTER 10.0.0.26:6379
1758:S 04 Jul 2021 06:49:37.566 * MASTER <-> REPLICA sync started
[root@cent7_36 ~]# redis-cli -a 123456 --no-auth-warning -h 10.0.0.26 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.36,port=6379,state=online,offset=142068,lag=0
master_replid:648486d0180717355403e7c3985ec84bc4bfc009
master_replid2:f8553e466dd78adb2ad1af417fcfbcbafd998d15
master_repl_offset:142068
second_repl_offset:141579
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:142068
#模拟slave 10.0.0.46出现故障
[root@centos7_46 ~]# redis-cli -a 123456 --no-auth-warning
127.0.0.1:6379> shutdown
not connected> exit
[root@centos7_46 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
#另一窗口同时监控日志
[root@centos7_46 ~]# tail -f /apps/redis/log/redis-6379.log
2843:S 04 Jul 2021 06:32:10.680 # User requested shutdown...
2843:S 04 Jul 2021 06:32:10.680 * Saving the final RDB snapshot before exiting.
2843:S 04 Jul 2021 06:32:10.686 * DB saved on disk
2843:S 04 Jul 2021 06:32:10.686 * Removing the pid file.
2843:S 04 Jul 2021 06:32:10.686 # Redis is now ready to exit, bye bye...
[root@centos7_46 ~]# redis-cli -a 123456 --cluster info 10.0.0.6:6379 --no-auth-warning
Could not connect to Redis at 10.0.0.46:6379: Connection refused
10.0.0.6:6379 (3feffd6b...) -> 3332 keys | 5461 slots | 0 slaves.
10.0.0.56:6379 (471aa637...) -> 3341 keys | 5462 slots | 1 slaves.
10.0.0.36:6379 (db58e593...) -> 3330 keys | 5461 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
[root@centos7_46 ~]# redis-cli -a 123456 --cluster info 10.0.0.6:6379 --no-auth-warning
Could not connect to Redis at 10.0.0.46:6379: Connection refused
10.0.0.6:6379 (3feffd6b...) -> 3332 keys | 5461 slots | 0 slaves.
10.0.0.56:6379 (471aa637...) -> 3341 keys | 5462 slots | 1 slaves.
10.0.0.36:6379 (db58e593...) -> 3330 keys | 5461 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
[root@centos7_46 ~]# redis-cli -a 123456 -h 10.0.0.6 --no-auth-warning cluster nodes
37d2503603fe9e06182e6053d302f8fc7a66f262 10.0.0.26:6379@16379 slave db58e59352e7ea4386e20d9f042724b5560606b9 0 1625395005000 8 connected
cdb9f3a8f54c47381d401c1fee103547e0d3922a 10.0.0.16:6379@16379 slave 471aa637ee8fe618d46d294afa10e77c0461bde4 0 1625395005000 7 connected
3feffd6b2f7959a74af852238237575daede4c96 10.0.0.6:6379@16379 myself,master - 0 1625395006000 1 connected 0-5460
471aa637ee8fe618d46d294afa10e77c0461bde4 10.0.0.56:6379@16379 master - 0 1625395007006 7 connected 5461-10922
db58e59352e7ea4386e20d9f042724b5560606b9 10.0.0.36:6379@16379 master - 0 1625395008016 8 connected 10923-16383
433703e6f3e6fb66831726da95e5ee9548184643 10.0.0.46:6379@16379 slave,fail 3feffd6b2f7959a74af852238237575daede4c96 1625394730789 1625394729383 5 disconnected
#恢复故障slave 10.0.0.46,重新成为10.0.0.6的从
[root@centos7_46 ~]# redis-cli -a 123456 --cluster info 10.0.0.6:6379 --no-auth-warning
10.0.0.6:6379 (3feffd6b...) -> 3332 keys | 5461 slots | 1 slaves.
10.0.0.56:6379 (471aa637...) -> 3341 keys | 5462 slots | 1 slaves.
10.0.0.36:6379 (db58e593...) -> 3330 keys | 5461 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
以上是关于基于redis 5的redis cluster 部署的主要内容,如果未能解决你的问题,请参考以下文章