ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

详解.Redis Cluster 工作原理和集群创建和使用

2020-10-26 07:00:57  阅读:169  来源: 互联网

标签:10.0 Cluster redis Redis cluster 6379 详解 slots root


利用原生命令手动部署redis cluster

Redis Cluster 工作原理

在哨兵sentinel机制中,可以解决redis高可用问题,即当master故障后可以自动将slave提升为master,从而可以保证redis服务的正常使用,但是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受限于单机的内存大小、并发数量、网卡速率等因素。

早期Redis 分布式集群部署方案:

  1. 客户端分区:由客户端程序决定key写分配和写入的redis node,但是需要客户端自己处理写入分配、高可用管理和故障转移等
  2. 代理方案:基于三方软件实现redis proxy,客户端先连接之代理层,由代理层实现key的写入分配,对客户端来说是有比较简单,但是对于集群管节点增减相对比较麻烦,而且代理本身也是单点和性能瓶颈。

redis 3.0版本之后推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存当前节点数据和整个集群状态,每个节点都和其他所有节点连接

Redis Cluster特点如下:

  1. 所有Redis节点使用(PING机制)互联
  2. 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
  3. 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
  4. redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redisnode上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担16384/N个槽位
  5. Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点上,从而有效解决单机瓶颈。
    环境介绍:6台服务器,分别为master/slave,适用于生产环境,(实验)
    详解.Redis Cluster 工作原理和集群创建和使用
    #集群节点
    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

使用六台centos8服务器

[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))) 

基于redis 5.0部署redis cluster

使用六台centos8服务器

[ 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

基于rendis 4.0部署redis cluster

使用六台centos7服务器

[ 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"

标签:10.0,Cluster,redis,Redis,cluster,6379,详解,slots,root
来源: https://blog.51cto.com/13887323/2543998

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有