Detailed explanation. Redis Cluster working principle and cluster creation and use

Manually deploy redis cluster using native commands

Redis Cluster working principle

In the sentinel mechanism, the redis high availability problem can be solved, that is, when the master fails, the slave can be automatically promoted to the master, which can ensure the normal use of the redis service, but cannot solve the bottleneck problem of the redis single-machine write, that is, the single-machine redis write The input performance is limited by factors such as the memory size, concurrent number, and network card speed of a single machine.

Early Redis distributed cluster deployment plan:

  1. Client partition: The client program determines the key write allocation and write redis node, but the client needs to handle write allocation, high availability management, and failover by itself
  2. Proxy solution: based on third-party software to implement redis proxy, the client first connects to the proxy layer, and the proxy layer realizes the key write distribution. It is relatively simple for the client, but it is relatively troublesome for the cluster management node to increase or decrease. The agent itself is also a single point and performance bottleneck.

After redis version 3.0, the redis cluster mechanism with no central architecture was introduced. In a central redis cluster, each node saves the current node data and the entire cluster state, and each node is connected to all other nodes.

The features of Redis Cluster are as follows:

  1. All Redis nodes use (PING mechanism) to interconnect
  2. Whether a node in the cluster fails is monitored by more than half of the nodes in the entire cluster. It can be considered as a true failure.
  3. The client can directly connect to redis without a proxy, and all redis server IPs need to be configured in the application
  4. Redis cluster maps all redis nodes to 0--16383 slots on average, and reads and writes need to be performed on the specified redisnode. Therefore, how many redis nodes are equivalent to how many times redis concurrency is expanded, each redis node assumes 16384/N slots
  5. Redis cluster pre-allocates 16384 (slot) slots. When a key-value needs to be written in the redis cluster, the value after CRC16(key) mod 16384 is used to determine which slot to write the key into the value. Decide which Redis node to write to, so as to effectively solve the stand-alone bottleneck.
    Environment introduction: 6 servers, master/slave respectively, suitable for production environment, (experimental)
    Detailed explanation. Redis Cluster working principle and cluster creation and use
    #集群节点
    10.0.0.8
    10.0.0.18
    10.0.0.28
    10.0.0.38
    10.0.0.48
    10.0.0.58
    #预留服务器扩展使用
    10.0.0.68
    10.0.0.78

Use six centos8 servers

[root@localhost ~]#yum -y install redis #在所有6个节点上安装redis服务
[ root@localhost ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 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' /etc/redis.conf #修改所有节点的配置文件,按顺序排序分别是:修改redis监听端口;修改主节点密码;修改自身登录密码;开启cluster模式;配置cluster的配置文件路径,记录主从关系和槽位信息;关闭集群请求槽位全覆盖,该选项开启的话,如果集群中有一个主库宕机且无备库就会使整个集群不对外提供服务
[ root@localhost ~]#systemctl enable --now redis #启动redis并设置为开机启动
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[ root@localhost ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.18 6379 #使用meet使所有节点实现互相通信,登录任意一台节点来meet其他所有要加入到本集群中的节点
OK
[ root@localhost ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.28 6379
OK
[ root@localhost ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.38 6379
OK
[ root@localhost ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.48 6379
OK
[ root@localhost ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.58 6379
OK
[ root@localhost ~]#vi addslot.sh #编写一个分配槽位的脚本,并利用脚本分配槽位,redis cluster的槽位范围为0-16364

#!/bin/bash
# 
#********************************************************************
#Author:            rzx
#QQ:                970707452
#Date:              2020-10-25
#FileName:         addslot.sh
#URL:               https://www.cnblogs.com/rzx-006/
#Description:      The test script
#Copyright (C):    2020 All rights reserved
#********************************************************************
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@localhost ~]#bash addslot.sh 10.0.0.8 6379 0 5461 #利用脚本为10.0.0.8分配槽位
[ root@localhost ~]#redis-cli -h 10.0.0.8 -a 123456 cluster info #查看10.0.0.8的集群状态
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:5462 #整个集群当中已经分配的槽位数量
cluster_slots_ok:5462
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:1
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1342
cluster_stats_messages_pong_sent:1341
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:2688
cluster_stats_messages_ping_received:1341
cluster_stats_messages_pong_received:1347
cluster_stats_messages_received:2688
[ root@localhost ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:6379@16379 master - 0 1603608771000 5 connected
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:6379@16379 master - 0 1603608772000 3 connected
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:6379@16379 master - 0 1603608773663 0 connected
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:6379@16379 myself,master - 0 1603608773000 1 connected 0-5461 #分配到的槽位号
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:6379@16379 master - 0 1603608771616 4 connected
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:6379@16379 master - 0 1603608772647 2 connected

[ root@localhost ~]#bash addslot.sh 10.0.0.18 6379 5462 10922 #利用脚本为10.0.0.18分配槽位
[ root@localhost ~]#bash addslot.sh 10.0.0.28 6379 10923 16383 #利用脚本为10.0.0.28分配槽位

[ root@localhost ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:6379@16379 master - 0 1603609539000 5 connected
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:6379@16379 master - 0 1603609540900 3 connected
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:6379@16379 master - 0 1603609541915 0 connected
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:6379@16379 myself,master - 0 1603609536000 1 connected 0-5461
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:6379@16379 master - 0 1603609539884 4 connected 10923-16383
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:6379@16379 master - 0 1603609540000 2 connected 5462-10922

[ root@localhost ~]#redis-cli -h 10.0.0.38 -a 123456 --no-auth-warning cluster replicate 208ee5ef0b688df38824552b2154b64869175b9e
OK #连接到10.0.0.38,为其指定master节点,master节点ID可以通过cluster nodes查看
[root@localhost ~]#redis-cli -h 10.0.0.48 -a 123456 --no-auth-warning cluster replicate b75c9952210283a64f1ce10c5e1ad85d18dc1b36
OK #连接到10.0.0.48,为其指定master节点,master节点ID可以通过cluster nodes查看
[ root@localhost ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster replicate 50083258803ac6bc106182853e6d0bc1ebdee45d
OK #连接到10.0.0.58,为其指定master节点,master节点ID可以通过cluster nodes查看

[ root@localhost ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster nodes
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:6379@16379 master - 0 1603610406000 1 connected 0-5461
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:6379@16379 slave b75c9952210283a64f1ce10c5e1ad85d18dc1b36 0 1603610406176 4 connected
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:6379@16379 master - 0 1603610404000 2 connected 5462-10922
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:6379@16379 master - 0 1603610405162 4 connected 10923-16383
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:6379@16379 slave 208ee5ef0b688df38824552b2154b64869175b9e 0 1603610407196 1 connected
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:6379@16379 myself,slave 50083258803ac6bc106182853e6d0bc1ebdee45d 0 1603610403000 5 connected
[root@localhost ~]#redis-cli -h 10.0.0.58 -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:2
cluster_stats_messages_ping_sent:3333
cluster_stats_messages_pong_sent:3184
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:6518
cluster_stats_messages_ping_received:3179
cluster_stats_messages_pong_received:3334
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:6518
[ root@localhost ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster slots #查看主从关系以及槽位信息
1) 1) (integer) 0
   2) (integer) 5461
   3) 1) "10.0.0.8"
      2) (integer) 6379
      3) "208ee5ef0b688df38824552b2154b64869175b9e"
   4) 1) "10.0.0.38"
      2) (integer) 6379
      3) "e1e8b3a666bb3f15b3b73c689782d9a1236beab3"
2) 1) (integer) 5462
   2) (integer) 10922
   3) 1) "10.0.0.18"
      2) (integer) 6379
      3) "50083258803ac6bc106182853e6d0bc1ebdee45d"
   4) 1) "10.0.0.58"
      2) (integer) 6379
      3) "66173bbc45b3f8e9212286827bb5fc594aab0cd7"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.28"
      2) (integer) 6379
      3) "b75c9952210283a64f1ce10c5e1ad85d18dc1b36"
   4) 1) "10.0.0.48"
      2) (integer) 6379
      3) "4ad896ab9a5e573061decaea13ca72f1acc16d22"

[root@localhost ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning set key1 v1 #当写入或读取的数据不保存至本机的槽位中时,会提示需要到另一台节点执行
(error) MOVED 9189 10.0.0.18:6379
[ root@localhost ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning -c set key1 v1 #加-c选项会自动重定向到目标节点执行
OK
[ root@localhost ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning -c get key1
"v1"
[ root@localhost ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning  get key1
(error) MOVED 9189 10.0.0.18:6379
[ root@localhost ~]#redis-cli -a 123456 --no-auth-warning cluster keyslot 123 #计算key对应的槽位值
(integer) 5970

[ root@localhost ~]#dnf -y instal1 python3 #安装python环境
[ root@localhost ~]#pip3 insta71 redis-py-cluster #安装模块
[ root@localhost ~]#vi redis_cluster_test.py #redis cluster批量写入数据py脚本

#!/usr/bin/env python3
from rediscluster import RedisCluster
startup_nodes = [
    {"host":"10.0.0.8", "port":6379},
    {"host":"10.0.0.18", "port":6379},
    {"host":"10.0.0.28", "port":6379},
    {"host":"10.0.0.38", "port":6379},
    {"host":"10.0.0.48", "port":6379},
    {"host":"10.0.0.58", "port":6379}
]
redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456',decode_responses=True)
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))) 

Deploy redis cluster based on redis 5.0

Use six centos8 servers

[ root@localhost ~]#yum -y install redis #在所有6个节点上安装redis服务
[ root@localhost ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 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' /etc/redis.conf #修改所有节点的配置文件,按顺序排序分别是:修改redis监听端口;修改主节点密码;修改自身登录密码;开启cluster模式;配置cluster的配置文件路径,记录主从关系和槽位信息;关闭集群请求槽位全覆盖,该选项开启的话,如果集群中有一个主库宕机且无备库就会使整个集群不对外提供服务
[ root@localhost ~]#systemctl enable --now redis #启动redis并设置为开机启动
[ root@localhost ~]#redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58:6379 --cluster-replicas 1 #一条命令实现原生命令的meet过程、分配槽位、设置主从关系,--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.38:6379 to 10.0.0.8:6379 #自动配置主从关系
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
M: b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:6379 #显示节点的主从关系和槽位信息
   slots:[0-5460] (5461 slots) master
M: 43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
M: 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
S: 82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:6379
   replicates b530ab922788d06da1cb85012d384ffb4fb7824f
S: 9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:6379
   replicates 43e25d5358edef69bac0dee5c964d20dda20ca4c
S: 4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:6379
   replicates 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1
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.8:6379)
M: b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:6379
   slots: (0 slots) slave
   replicates 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1
S: 9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:6379
   slots: (0 slots) slave
   replicates 43e25d5358edef69bac0dee5c964d20dda20ca4c
S: 82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:6379
   slots: (0 slots) slave
   replicates b530ab922788d06da1cb85012d384ffb4fb7824f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. #所有槽位分配完成
#redis 5.0的redis cluster配置完成
[ root@localhost ~]#redis-cli -a 123456 cluster nodes #检查一下集群节点信息
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:6379@16379 master - 0 1603613374183 2 connected 5461-10922
b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:6379@16379 myself,master - 0 1603613373000 1 connected 0-5460
5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:6379@16379 master - 0 1603613372000 3 connected 10923-16383
4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:6379@16379 slave 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 0 1603613374000 6 connected
9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:6379@16379 slave 43e25d5358edef69bac0dee5c964d20dda20ca4c 0 1603613375205 5 connected
82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:6379@16379 slave b530ab922788d06da1cb85012d384ffb4fb7824f 0 1603613375000 4 connected

Deploy redis cluster based on rendis 4.0

Use six centos7 servers

[ root@localhost ~]#vi install_redis_for_centos.sh  #自动编译安装redis4.0.14脚本

#!/bin/bash
#
#********************************************************************
#Author:        wangxiaochun
#URL:           http://www.magedu.com
#Description:      The test script
#Copyright (C):     2020 All rights reserved
#********************************************************************
. /etc/init.d/functions 
VERSION=redis-4.0.14
PASSWORD=123456
INSTALL_DIR=/apps/redis

install() {
yum  -y install gcc jemalloc-devel || { action "安装软件包失败,请检查网络配置" false ; exit; }

wget http://download.redis.io/releases/${VERSION}.tar.gz || { action "Redis 源码下载失败" false ; exit; }

tar xf ${VERSION}.tar.gz
cd ${VERSION}

ln -s ${INSTALL_DIR}/bin/redis-*  /usr/bin/
mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
cp redis.conf  ${INSTALL_DIR}/etc/

if id redis &> /dev/null ;then 
    action "Redis 用户已存在" false  
else
    useradd -r -s /sbin/nologin redis
    action "Redis 用户创建成功"
fi

chown -R redis.redis ${INSTALL_DIR}

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 && action "Redis 服务启动成功,Redis信息如下:" || { action "Redis 启动失败" false ;exit; } 

redis-cli -a $PASSWORD INFO Server 2> /dev/null

}

install

[ root@localhost ~]#bash install_redis_for_centos.sh #在所有节点执行脚本来安装redis
[ root@localhost ~]#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 #修改所有reids的配置文件
[ root@localhost ~]#systemctl restart redis #重启redis
[1 root@localhost ~]#cp redis-4.0.14/src/redis-trib.rb /usr/bin/ #拷贝redis-trib.rb工具到/usr/bin下,方便使用
[ root@localhost ~]#yum -y install ruby #安装ruby环境,但是由于cneots7yum安装ruby版本太低,需要执行其他操作
[ root@localhost ~]#yum -y install gcc openssl-devel zlib-devel #安装编译ruby依赖包
[ root@localhost ~]#wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz #下载ruby2.5.5的源码包
--2020-10-25 18:53:24--  https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
Resolving cache.ruby-lang.org (cache.ruby-lang.org)... 151.101.77.178, 2a04:4e42:12::434
Connecting to cache.ruby-lang.org (cache.ruby-lang.org)|151.101.77.178|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 15996436 (15M) [application/x-tar]
Saving to: ‘ruby-2.5.5.tar.gz’

100%[=================================================================>] 15,996,436  82.9KB/s   in 2m 57s 

2020-10-25 18:56:27 (88.2 KB/s) - ‘ruby-2.5.5.tar.gz’ saved [15996436/15996436]

[ root@localhost ~]#tar xf ruby-2.5.5.tar.gz  #解压源码包
[ root@localhost ruby-2.5.5]#cd ruby-2.5.5 
[ root@localhost ruby-2.5.5]#./configure #执行configure脚本生成makefile文件
[ root@localhost ruby-2.5.5]#make && make install #编译安装ruby
[ root@localhost ruby-2.5.5]#ruby -v
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux] #查看ruby版本,然后重新登录终端
[ root@localhost ~]#gem install -l redis-4.1.3.gem #安装redis模块,此为安装离线包,需提前将文件下载好,下载链接:https://rubygems.org/downloads/redis-4.1.3.gem ,注意下载此包需要科学上网
Successfully installed redis-4.1.3
Parsing documentation for redis-4.1.3
Installing ri documentation for redis-4.1.3
Done installing documentation for redis after 0 seconds
1 gem installed
[ root@localhost ~]#vi /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.3/lib/redis/client.rb 

# frozen_string_literal: true

require_relative "errors"
require "socket"
require "cgi"

class Redis
  class Client

    DEFAULTS = {
      :url => lambda { ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => 123456,   #修改脚本中连接redis的密码                                                                               
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :reconnect_delay => 0,
      :reconnect_delay_max => 0.5,
      :inherit_socket => false
    }

    attr_reader :options

[ root@localhost ~]#redis-trib.rb create --replicas 1 10.0.0.7:6379 10.0.0.17:6379 10.0.0.27:6379 10.0.0.37:6379 10.0.0.47:6379 10.0.0.57:6379 #创建redis cluster集群,与redis5.0集群不同的是,从节点会随机分配给主节点,而非按顺序一一对应
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.0.7:6379
10.0.0.17:6379
10.0.0.27:6379
Adding replica 10.0.0.47:6379 to 10.0.0.7:6379
Adding replica 10.0.0.57:6379 to 10.0.0.17:6379
Adding replica 10.0.0.37:6379 to 10.0.0.27:6379
M: bf18eec99802798b5215ed2f8ddc12eb1248b3cb 10.0.0.7:6379
   slots:0-5460 (5461 slots) master
M: a0c915ad50c0ad66a0a4c7b6393d9e127576ad64 10.0.0.17:6379
   slots:5461-10922 (5462 slots) master
M: 97b1c4b57dbf5469bab57577f0bc022bd59917e6 10.0.0.27:6379
   slots:10923-16383 (5461 slots) master
S: b18f0f29e766120656abd946b957ef0bd712d638 10.0.0.37:6379
   replicates 97b1c4b57dbf5469bab57577f0bc022bd59917e6
S: fc17f6fea452277328d913324b463a0c91029cd3 10.0.0.47:6379
   replicates bf18eec99802798b5215ed2f8ddc12eb1248b3cb
S: aca9ab569ef599a17449bd2b9088eee4533f515e 10.0.0.57:6379
   replicates a0c915ad50c0ad66a0a4c7b6393d9e127576ad64
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 10.0.0.7:6379)
M: bf18eec99802798b5215ed2f8ddc12eb1248b3cb 10.0.0.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 97b1c4b57dbf5469bab57577f0bc022bd59917e6 10.0.0.27:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: aca9ab569ef599a17449bd2b9088eee4533f515e 10.0.0.57:6379
   slots: (0 slots) slave
   replicates a0c915ad50c0ad66a0a4c7b6393d9e127576ad64
S: fc17f6fea452277328d913324b463a0c91029cd3 10.0.0.47:6379
   slots: (0 slots) slave
   replicates bf18eec99802798b5215ed2f8ddc12eb1248b3cb
M: a0c915ad50c0ad66a0a4c7b6393d9e127576ad64 10.0.0.17:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: b18f0f29e766120656abd946b957ef0bd712d638 10.0.0.37:6379
   slots: (0 slots) slave
   replicates 97b1c4b57dbf5469bab57577f0bc022bd59917e6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[root@localhost ~]#redis-cli -a 123456 cluster slots #查看集群主从关系以及槽位信息
Warning: Using a password with '-a' option on the command line interface may not be safe.
1) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.27"
      2) (integer) 6379
      3) "97b1c4b57dbf5469bab57577f0bc022bd59917e6"
   4) 1) "10.0.0.37"
      2) (integer) 6379
      3) "b18f0f29e766120656abd946b957ef0bd712d638"
2) 1) (integer) 0
   2) (integer) 5460
   3) 1) "10.0.0.7"
      2) (integer) 6379
      3) "bf18eec99802798b5215ed2f8ddc12eb1248b3cb"
   4) 1) "10.0.0.47"
      2) (integer) 6379
      3) "fc17f6fea452277328d913324b463a0c91029cd3"
3) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "10.0.0.17"
      2) (integer) 6379
      3) "a0c915ad50c0ad66a0a4c7b6393d9e127576ad64"
   4) 1) "10.0.0.57"
      2) (integer) 6379
      3) "aca9ab569ef599a17449bd2b9088eee4533f515e"

Guess you like

Origin blog.51cto.com/13887323/2543998