美文网首页linux运维
Linux Redis集群安装手册

Linux Redis集群安装手册

作者: 无字天书 | 来源:发表于2019-10-11 11:40 被阅读0次

    提示:搭建redis集群最少6个redis服务器

    第一节工具原料

    网址:
    rubby
    redis
    edis-3.3.5.gem
    版本:
    ruby-2.6.5
    redis-4.0.14
    redis-3.3.5.gem
    系统环境:

    主机名 操作系统 ip地址 安装软件 redis端口 备注
    nsql140 CentOS 7.4 192.168.122.140 ruby-2.6.5 redis-4.0.14 redis-3.3.5.gem 6379 6380 redis集群管理节点
    nsql141 CentOS 7.4 192.168.122.141 redis-4.0.14 6379 6380
    nsql142 CentOS 7.4 192.168.122.142 redis-4.0.14 6379 6380

    实验前提(所有主机都要操作)

    [root@nosql140 ~]# systemctl stop firewalld
    [root@nosql140 ~]# systemctl disable firewalld
    [root@nosql140 ~]# yum -y install iptables-services
    [root@nosql140 ~]# service iptables -F
    [root@nosql140 ~]# service iptables save
    [root@nosql140 ~]# setenforce 0
    [root@nosql140 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    

    redis安装与初始化

    1.安装

    [root@nosql140 iso]# yum -y install gcc-c++ make
    [root@nosql140 iso]# tar -xvf redis-4.0.14.tar.gz 
    [root@nosql140 iso]# cd redis-4.0.14
    [root@nosql140 redis-4.0.14]# make 
    [root@nosql140 redis-4.0.14]# mmake install
    
    

    2.初始化
    一台主机安装2个redis

    #执行脚本命令,一路默认
    [root@nosql140 redis-4.0.14]# ./utils/install_server.sh
    ....
    Starting Redis server...
    Installation successful!
    [root@nosql140 ~]# vim /etc/redis/6379.conf
    ....
    bind 192.168.122.140
    cluster-enabled yes
    cluster-config-file nodes-6379.conf
    cluster-node-timeout 15000
    ....
    [root@nosql140 ~]# /etc/init.d/redis_6379 restart
    [root@nosql140 ~]# ss -tunlp | grep 6379
    tcp    LISTEN     0      128    192.168.122.140:6379                  *:*                   users:(("redis-server",pid=819,fd=6))
    tcp    LISTEN     0      128    192.168.122.140:16379                 *:*                   users:(("redis-server",pid=819,fd=8))
    #写入6380就一路默认
    [root@nosql140 redis-4.0.14]# ./utils/install_server.sh 
    Welcome to the redis service installer
    This script will help you easily set up a running redis server
    
    Please select the redis port for this instance: [6379] 6380
    Please select the redis config file name [/etc/redis/6380.conf]
    ....
    [root@nosql140 ~]# vim /etc/redis/6380.conf
    ....
    bind 192.168.122.140
    cluster-enabled yes
    cluster-config-file nodes-6380.conf
    cluster-node-timeout 15000
    ....
    [root@nosql140 ~]# /etc/init.d/redis_6380 restart
    [root@nosql140 ~]# ss -tunlp | grep 6380
    tcp    LISTEN     0      128    192.168.122.140:6380                  *:*                   users:(("redis-server",pid=822,fd=6))
    tcp    LISTEN     0      128    192.168.122.140:16380                 *:*                   users:(("redis-server",pid=822,fd=8))
    
    

    3.管理命令

    #启动 | 停止 | 查看状态 | 重启
    /etc/init.d/redis_6379 start | stop | status | restart
    

    第三节安装rubby

    [root@nosql140 iso]# yum -y install openssl-devel
    [root@nosql140 iso]# mkdir -p /usr/local/ruby2.6
    [root@nosql140 iso]# tar -xvf ruby-2.6.5.tar.gz 
    [root@nosql140 iso]# cd ruby-2.6.5
    [root@nosql140 ruby-2.6.5]# ./configure -prefix=/usr/local/ruby2.6
    [root@nosql140 ruby-2.6.5]# make && make install
    [root@nosql140 ~]# vim /etc/profile
    ....
    export RUBBY_HOME=/usr/local/ruby2.6/
    export PATH=$RUBBY_HOME/bin:$PATH
    [root@nosql140 ~]# source /etc/profile
    [root@nosql140 ~]# ruby -v
    ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
    [root@nosql140 ~]# gem -v
    3.0.3
    
    

    第四节使用gem安装redis的接口

    [root@nosql140 iso]# gem install ./redis-3.3.5.gem
    Successfully installed redis-3.3.5
    Parsing documentation for redis-3.3.5
    Installing ri documentation for redis-3.3.5
    Done installing documentation for redis after 0 seconds
    1 gem installed
    
    

    第五节redis-trib.rb创建集群

    [root@nosql140 iso]# cd  redis-4.0.14
    [root@nosql140 redis-4.0.14]# cp src/redis-trib.rb /usr/local/bin/
    [root@nosql140 redis-4.0.14]# redis-trib.rb create --replicas 1 192.168.122.140:6379 192.168.122.141:6379 192.168.122.142:6379 192.168.122.140:6380 192.168.122.141:6380 192.168.122.142:6380
    >>> Creating cluster
    >>> Performing hash slots allocation on 6 nodes...
    Using 3 masters:
    192.168.122.140:6379
    192.168.122.141:6379
    192.168.122.142:6379
    Adding replica 192.168.122.141:6380 to 192.168.122.140:6379
    Adding replica 192.168.122.142:6380 to 192.168.122.141:6379
    Adding replica 192.168.122.140:6380 to 192.168.122.142:6379
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:0-5460 (5461 slots) master
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:5461-10922 (5462 slots) master
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:10923-16383 (5461 slots) master
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    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 192.168.122.140:6379)
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       slots: (0 slots) slave
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       slots: (0 slots) slave
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    简单解释一下这个命令:调用 ruby 命令来进行创建集群:

    --replicas 1 表示主从复制比例为 1:1,即一个主节点对应一个从节点;然后,默认给我们分配好了每个主节点和对应从节点服务,以及 solt 的大小,因为在 Redis 集群中有且仅有 16383 个 solt ,默认情况会给我们平均分配,当然你可以指定,后续的增减节点也可以重新分配。

    第六节集群验证

    随便登陆一台验证,这里以140的6379为例

    #加参数 -c 可连接到集群,因为 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略,-p 参数为端口号 
    [root@nosql140 ~]# redis-cli -c -h 192.168.122.140 -p 6379
    #查看集群状态
    192.168.122.140:6379> 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:6
    cluster_my_epoch:1
    cluster_stats_messages_ping_sent:66
    cluster_stats_messages_pong_sent:67
    cluster_stats_messages_sent:133
    cluster_stats_messages_ping_received:62
    cluster_stats_messages_pong_received:66
    cluster_stats_messages_meet_received:5
    cluster_stats_messages_received:133
    #查看集群节点信息
    192.168.122.140:6379> cluster nodes
    829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380@16380 slave 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 0 1570704238536 4 connected
    d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380@16380 slave 457ad182d1f12a65cf0c0903ed129542d74d1034 0 1570704239000 5 connected
    2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379@16379 master - 0 1570704237000 3 connected 10923-16383
    457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379@16379 myself,master - 0 1570704237000 1 connected 0-5460
    1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379@16379 master - 0 1570704237534 2 connected 5461-10922
    ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380@16380 slave 1503ad1b97e6891b612146c48a1ad16dc626331e 0 1570704239538 6 connected
    
    

    第七节管理集群

    语法格式:

    redis-trib.rb  选项  参数
    

    选项:

    • add-node 添加master主机
    • check 检测集群
    • reshard 重新分片
    • add-node --slave 添加slave主机
    • del-node 删除主机

    添加新节点

    1.添加master节点
    安装新redis节点(在140主机上)

    [root@nosql140 ~]# cd /iso/redis-4.0.14/
    [root@nosql140 redis-4.0.14]# ./utils/install_server.sh 
    Welcome to the redis service installer
    This script will help you easily set up a running redis server
    
    Please select the redis port for this instance: [6379] 6378
    ...
    [root@nosql140 redis-4.0.14]# vim /etc/redis/6378.conf
    bind 192.168.122.140
    cluster-enabled yes
    cluster-config-file nodes-6378.conf
    cluster-node-timeout 15000
    [root@nosql140 redis-4.0.14]# /etc/init.d/redis_6378 restart
    [root@nosql140 redis-4.0.14]# ss -tunpl | grep 6378
    tcp    LISTEN     0      128    192.168.122.140:16378                 *:*                   users:(("redis-server",pid=1193,fd=8))
    tcp    LISTEN     0      128    192.168.122.140:6378                  *:*                   users:(("redis-server",pid=1193,fd=6))
    

    2.添加节点到集群中

    [root@nosql140 ~]# redis-trib.rb add-node 192.168.122.140:6378 192.168.122.140:6379
    >>> Adding node 192.168.122.140:6378 to cluster 192.168.122.140:6379
    ....
    >>> Send CLUSTER MEET to node 192.168.122.140:6378 to make it join the cluster.
    [OK] New node added correctly.
    

    3.查看状态

    [root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6378
    >>> Performing Cluster Check (using node 192.168.122.140:6378)
    M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
       slots: (0 slots) master    #6378发现没有hash槽
       0 additional replica(s)
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       slots: (0 slots) slave
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       slots: (0 slots) slave
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    4手动对集群进行分片迁移
    提示:总共16384,总共有4个master,即每个master=16384/4=4096

    [root@nosql140 ~]# redis-trib.rb reshard 192.168.122.140:6378
    >>> Performing Cluster Check (using node 192.168.122.140:6378)
    M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
       slots: (0 slots) master
       0 additional replica(s)
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:5461-10922 (5462 slots) master
       1 additional replica(s)
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       slots: (0 slots) slave
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:0-5460 (5461 slots) master
       1 additional replica(s)
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:10923-16383 (5461 slots) master
       1 additional replica(s)
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       slots: (0 slots) slave
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    How many slots do you want to move (from 1 to 16384)? 4096
    What is the receiving node ID? cd989d61fad9ff2f0596b051f6a566fd2d541e3c
    Please enter all the source node IDs.
      Type 'all' to use all the nodes as source nodes for the hash slots.
      Type 'done' once you entered all the source nodes IDs.
    Source node #1:all
    ....
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    #查看状态,全均分hash槽
    [root@nosql140 iso]# redis-trib.rb check 192.168.122.140:6378
    >>> Performing Cluster Check (using node 192.168.122.140:6378)
    M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
       slots:0-1364,5461-6826,10923-12287 (4096 slots) master
       0 additional replica(s)
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:6827-10922 (4096 slots) master
       1 additional replica(s)
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       slots: (0 slots) slave
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:1365-5460 (4096 slots) master
       1 additional replica(s)
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:12288-16383 (4096 slots) master
       1 additional replica(s)
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       slots: (0 slots) slave
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    到此添加redis的master节点成功。

    添加slave节点
    1.安装新redis6378(在141主机上)

    [root@nosql141 ~]# /iso/redis-4.0.14/utils/install_server.sh 
    Welcome to the redis service installer
    This script will help you easily set up a running redis server
    
    Please select the redis port for this instance: [6379] 6378
    ....
    Installation successful!
    
    bind 192.168.122.141
    cluster-enabled yes
    cluster-config-file nodes-6378.conf
    cluster-node-timeout 15000
    [root@nosql141 ~]# /etc/init.d/redis_6378 restart
    [root@nosql141 ~]# ss -tunlp | grep 6378
    tcp    LISTEN     0      128    192.168.122.141:16378                 *:*                   users:(("redis-server",pid=1238,fd=8))
    tcp    LISTEN     0      128    192.168.122.141:6378                  *:*                   users:(("redis-server",pid=1238,fd=6))
    

    2.添加从节点到集群中(随机添加)

    [root@nosql140 iso]# redis-trib.rb add-node --slave 192.168.122.141:6378 192.168.122.140:6379
    ....
    >>> Configure node as replica of 192.168.122.140:6378.
    [OK] New node added correctly
    [root@nosql140 iso]# redis-cli -c -h 192.168.122.140 -p 6379
    192.168.122.140:6379> cluster nodes
    ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380@16380 slave 1503ad1b97e6891b612146c48a1ad16dc626331e 0 1570760432983 8 connected
    829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380@16380 slave 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 0 1570760435000 4 connected
    716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 192.168.122.141:6378@16378 slave cd989d61fad9ff2f0596b051f6a566fd2d541e3c 0 1570760437000 9 connected
    457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379@16379 myself,master - 0 1570760436000 1 connected 1365-5460
    1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379@16379 master - 0 1570760436992 8 connected 6827-10922
    2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379@16379 master - 0 1570760437000 3 connected 12288-16383
    cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378@16378 master - 0 1570760437995 9 connected 0-1364 5461-6826 10923-12287
    d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380@16380 slave 457ad182d1f12a65cf0c0903ed129542d74d1034 0 1570760438998 5 connected
    
    

    删除节点

    1.删除master节点
    提示:删除master节点,先删除主机占用的hash槽
    简单计算下
    4096/3=1365
    4096=1366+1365+1365
    140:6379hash槽=4096+1366=5462
    141:6379hash槽=4096+1365=5461
    141:6379hash槽=4096+1365=5461
    16384=5462+5461+5461
    删除主机占用的hash槽

    #确定master节点
    [root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6379
    >>> Performing Cluster Check (using node 192.168.122.140:6379)
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:1365-5460 (4096 slots) master
       1 additional replica(s)
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       slots: (0 slots) slave
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       slots: (0 slots) slave
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    S: 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 192.168.122.141:6378
       slots: (0 slots) slave
       replicates cd989d61fad9ff2f0596b051f6a566fd2d541e3c
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:6827-10922 (4096 slots) master
       1 additional replica(s)
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:12288-16383 (4096 slots) master
       1 additional replica(s)
    M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
       slots:0-1364,5461-6826,10923-12287 (4096 slots) master
       1 additional replica(s)
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    [root@nosql140 ~]# redis-trib.rb reshard 192.168.122.140:6379
    How many slots do you want to move (from 1 to 16384)? 1366
    What is the receiving node ID? 457ad182d1f12a65cf0c0903ed129542d74d1034
    Please enter all the source node IDs.
      Type 'all' to use all the nodes as source nodes for the hash slots.
      Type 'done' once you entered all the source nodes IDs.
    Source node #1:cd989d61fad9ff2f0596b051f6a566fd2d541e3c
    Source node #2:done
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    [root@nosql140 ~]# redis-trib.rb reshard 192.168.122.141:6379
    ....
    How many slots do you want to move (from 1 to 16384)? 1365
    What is the receiving node ID? 1503ad1b97e6891b612146c48a1ad16dc626331e
    Please enter all the source node IDs.
      Type 'all' to use all the nodes as source nodes for the hash slots.
      Type 'done' once you entered all the source nodes IDs.
    Source node #1:cd989d61fad9ff2f0596b051f6a566fd2d541e3c
    Source node #2:done
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    [root@nosql140 ~]# redis-trib.rb reshard 192.168.122.142:6379
    ....
    How many slots do you want to move (from 1 to 16384)? 1365
    What is the receiving node ID? 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    Please enter all the source node IDs.
      Type 'all' to use all the nodes as source nodes for the hash slots.
      Type 'done' once you entered all the source nodes IDs.
    Source node #1:cd989d61fad9ff2f0596b051f6a566fd2d541e3c
    Source node #2:done
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    

    2.查看状态

    [root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6379
    >>> Performing Cluster Check (using node 192.168.122.140:6379)
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:2730-6826,10923-12287 (5462 slots) master
       2 additional replica(s)
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       slots: (0 slots) slave
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       slots: (0 slots) slave
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    S: 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 192.168.122.141:6378
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:0-1364,6827-10922 (5461 slots) master
       1 additional replica(s)
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:1365-2729,12288-16383 (5461 slots) master
       1 additional replica(s)
    M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
       slots: (0 slots) master    #无占用hash槽
       0 additional replica(s)
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    [OK] All nodes agree about slots configuration.
    

    3.删除节点(删除之后redis服务自动关闭)

    [root@nosql140 ~]# redis-trib.rb del-node 192.168.122.140:6378 cd989d61fad9ff2f0596b051f6a566fd2d541e3c
    >>> Removing node cd989d61fad9ff2f0596b051f6a566fd2d541e3c from cluster 192.168.122.140:6378
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> SHUTDOWN the node.
    [root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6379
    >>> Performing Cluster Check (using node 192.168.122.140:6379)
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:2730-6826,10923-12287 (5462 slots) master
       2 additional replica(s)
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       slots: (0 slots) slave
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       slots: (0 slots) slave
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    S: 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 192.168.122.141:6378
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:0-1364,6827-10922 (5461 slots) master
       1 additional replica(s)
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:1365-2729,12288-16383 (5461 slots) master
       1 additional replica(s)
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    查看状态3台master,到此删除master节点就成功了。

    删除slave节点
    提示:移除从节点,从节点主机没有槽位范围,直接移除即可

    [root@nosql140 ~]# redis-trib.rb del-node 192.168.122.141:6378 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78
    >>> Removing node 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 from cluster 192.168.122.141:6378
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> SHUTDOWN the node.
    [root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6379
    >>> Performing Cluster Check (using node 192.168.122.140:6379)
    M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
       slots:2730-6826,10923-12287 (5462 slots) master
       1 additional replica(s)
    S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
       slots: (0 slots) slave
       replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
    S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
       slots: (0 slots) slave
       replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
    M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
       slots:0-1364,6827-10922 (5461 slots) master
       1 additional replica(s)
    M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
       slots:1365-2729,12288-16383 (5461 slots) master
       1 additional replica(s)
    S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
       slots: (0 slots) slave
       replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    查看状态,3台slave无发现140:6378redis,删除slave节点成功。

    常见错误

    错误1

    现象:

    [root@nosql140 iso]# redis-trib.rb reshard 192.168.122.141:6379
    >>> Check for open slots...
    [WARNING] Node 192.168.122.141:6379 has slots in migrating state (5798).
    [WARNING] Node 192.168.122.140:6378 has slots in importing state (5798).
    [WARNING] The following slots are open: 5798
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    *** Please fix your cluster problems before resharding
    

    原因:

        错误背景描述
        redis版本:4.0.14
        ruby gem reids 版本:4.0.0
    
        ruby gem安装的redis库,版本不能使用最新的4.0,否则redis-trib.rb reshard 192.168.122.140:6378 重新分片时会报错误
    
    

    解决办法:
    a. 卸载最新redis库,gem uninstall redis
    b. 安装3.x版本,gem install redis -v 3.3.5 测试3.2.1到3.3.5都可以,4.x以上的分片报错
    c. 登入两个提示错误的节点,执行清除命令即可

    [root@nosql140 iso]# redis-cli -h 192.168.122.141 -p 6379
    192.168.122.141:6379> cluster setslot 5798 stable
    OK
    [root@nosql140 iso]# redis-cli -h 192.168.122.140 -p 6378
    192.168.122.140:6378> cluster setslot 5798 stable
    OK
    

    错误2:安装错误

    现象:
    zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
    解决办法:办法解决

    参考文章

    结束语:
    更多精彩内容持续更新中,关注微信公众号,有你更精彩。

    微信公众号.jpg

    相关文章

      网友评论

        本文标题:Linux Redis集群安装手册

        本文链接:https://www.haomeiwen.com/subject/yzmbpctx.html