当前位置:   article > 正文

【Docker笔记03】【MySQL 与 Redis的主从安装】

【Docker笔记03】【MySQL 与 Redis的主从安装】

一、前言

本系列是根据 B 站 尚硅谷 Docker 视频 学习记录笔记。因为没有视频课件,部分内容摘自 https://www.yuque.com/tmfl/cloud/dketq0

本系列仅为自身学习笔记记录使用,记录存在偏差,推荐阅读原视频内容或本文参考笔记。


二、Mysql 安装

  1. 创建主服务容器实例(端口为 3307)

    [root@192 /]# docker run -d -p 3307:3306 --privileged=true -v /mydocker/mysql-master/log:/var/log/mysql -v /mydocker/mysql-master/data:/var/lib/mysql -v /mydocker/mysql-master/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root --name mysql-master  mysql:5.7
    074c2c85947c79a139c6d8a938480de504f65c9f214171b125c34e19e99f14e6
    [root@192 /]# docker ps
    CONTAINER ID   IMAGE         COMMAND                   CREATED         STATUS         PORTS                                                  NAMES
    074c2c85947c   mysql:5.7     "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
    
    • 1
    • 2
    • 3
    • 4
    • 5
  2. 在 /mydocker/mysql-master/conf 目录下新建 my.cnf 并新增如下内容

    [root@192 conf]# vim my.cnf
    [root@192 conf]# cat my.cnf 
    [mysqld]
    ## 设置server_id, 同一个局域网中需要唯一
    server_id=101
    ## 指定不需要同步的数据库名称
    binlog-ignore-db=mysql
    ## 开启二进制日志功能
    log-bin=mall-mysql-bin
    ## 设置二进制日志使用内存大小(事务)
    binlog_cache_size=1M
    ## 设置使用的二进制日志格式(mixed,statement,row)
    binlog_format=mixed
    ## 二进制日志过期清理时间。默认值为0,表示不自动清理
    expire_logs_days=7
    ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断
    ## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
    slave_skip_errors=1062
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  3. 重启 mysql-master 实例

    [root@192 conf]# docker restart mysql-master 
    mysql-master
    [root@192 conf]# docker ps
    CONTAINER ID   IMAGE       COMMAND                   CREATED         STATUS         PORTS                                                  NAMES
    074c2c85947c   mysql:5.7   "docker-entrypoint.s…"   8 minutes ago   Up 2 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
    
    • 1
    • 2
    • 3
    • 4
    • 5
  4. 连接 mysql-master 创建主从同步用户

    # 进入
    [root@192 conf]#  docker exec -it mysql-master  /bin/bash
    # 登录 mysql
    root@074c2c85947c:/# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.36-log MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    # 创建 slave 用户,密码 123456	
    mysql> create user 'slave'@'%' identified by '123456';
    Query OK, 0 rows affected (0.01 sec)
    # 对 slave 用户进行授权
    mysql> grant replication slave, replication client on *.* to 'slave'@'%';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  5. 创建从服务器(端口为 3308)

    [root@192 conf]# docker run -d -p 3308:3306 --privileged=true -v /mydocker/mysql-slave/log:/var/log/mysql -v /mydocker/mysql-slave/data:/var/lib/mysql -v /mydocker/mysql-slave/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root --name mysql-slave  mysql:5.7
    0401aa0491f475635e66a1535479b68ab60ec558229df4adbebe66dc95d1e387
    [root@192 conf]# docker ps
    CONTAINER ID   IMAGE       COMMAND                   CREATED          STATUS         PORTS                                                  NAMES
    0401aa0491f4   mysql:5.7   "docker-entrypoint.s…"   3 seconds ago    Up 2 seconds   33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql-slave
    074c2c85947c   mysql:5.7   "docker-entrypoint.s…"   14 minutes ago   Up 6 minutes   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  6. 在 /mydocker/mysql-slave/conf 目录下新建 my.cnf 并新增如下内容

    [root@192 conf]# vim my.cnf
    [root@192 conf]# cat my.cnf 
    [mysqld]
    ## 设置server_id, 同一个局域网内需要唯一
    server_id=102
    ## 指定不需要同步的数据库名称
    binlog-ignore-db=mysql
    ## 开启二进制日志功能,以备slave作为其它数据库实例的Master时使用
    log-bin=mall-mysql-slave1-bin
    ## 设置二进制日志使用内存大小(事务)
    binlog_cache_size=1M
    ## 设置使用的二进制日志格式(mixed,statement,row)
    binlog_format=mixed
    ## 二进制日志过期清理时间。默认值为0,表示不自动清理
    expire_logs_days=7
    ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断
    ## 如:1062错误是指一些主键重复,1032是因为主从数据库数据不一致
    slave_skip_errors=1062
    ## relay_log配置中继日志
    relay_log=mall-mysql-relay-bin
    ## log_slave_updates表示slave将复制事件写进自己的二进制日志
    log_slave_updates=1
    ## slave设置只读(具有super权限的用户除外)
    read_only=1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  7. 重启 slave 容器

    [root@192 mysql-slave]# docker restart mysql-slave 
    mysql-slave
    [root@192 mysql-slave]# docker ps
    CONTAINER ID   IMAGE       COMMAND                   CREATED          STATUS          PORTS                                                  NAMES
    0401aa0491f4   mysql:5.7   "docker-entrypoint.s…"   3 minutes ago    Up 29 seconds   33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql-slave
    074c2c85947c   mysql:5.7   "docker-entrypoint.s…"   17 minutes ago   Up 9 minutes    33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  8. 在主服务器中查看主从同步状态 (show master status; 命令)

    [root@192 mysql-slave]# docker exec -it mysql-master /bin/bash
    root@074c2c85947c:/# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.36-log MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> show master status;
    +-----------------------+----------+--------------+------------------+-------------------+
    | File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +-----------------------+----------+--------------+------------------+-------------------+
    | mall-mysql-bin.000001 |      769 |              | mysql            |                   |
    +-----------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  9. 进入 slave 服务器,配置主从复制关系

    # 格式:
    # change master to master_host='宿主机ip',master_user='主数据库配置的主从复制用户名',master_password='主数据库配置的主从复制用户密码',master_port=宿主机主数据库端口,master_log_file='主数据库主从同步状态的文件名File',master_log_pos=主数据库主从同步状态的Position,master_connect_retry=连接失败重试时间间隔(秒);
    mysql> change master to master_host='192.168.64.132',master_user='slave',master_password='123456',master_port=3307,master_log_file='mall-mysql-bin.000001',master_log_pos=769,master_connect_retry=30;
    Query OK, 0 rows affected, 2 warnings (0.01 sec)
    mysql> 
    # 查看主从同步关系 (或使用 show slave status \G; 命令查看key value 格式)
    mysql> show slave status;
    +----------------+----------------+-------------+-------------+---------------+-----------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
    | Slave_IO_State | Master_Host    | Master_User | Master_Port | Connect_Retry | Master_Log_File       | Read_Master_Log_Pos | Relay_Log_File              | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
    +----------------+----------------+-------------+-------------+---------------+-----------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
    |                | 192.168.64.132 | slave       |        3307 |            30 | mall-mysql-bin.000001 |                 769 | mall-mysql-relay-bin.000001 |             4 | mall-mysql-bin.000001 | No               | No                |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 769 |             154 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                  NULL | No                            |             0 |               |              0 |                |                             |                0 |             | /var/lib/mysql/master.info |         0 |                NULL |                         |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
    +----------------+----------------+-------------+-------------+---------------+-----------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
    1 row in set (0.00 sec)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    其中看到参数 Slave_IO_Running 和 Slave_SQL_Running 两个复制线程都还没有开始

  10. 在从数据库中开启主从同步

    # 开启主从同步
    mysql> start slave;
    Query OK, 0 rows affected (0.00 sec)
    # 查看 Slave_IO_Running 和 Slave_SQL_Running 已经开启主从同步  
    mysql> show slave status;
    +----------------------------------+----------------+-------------+-------------+---------------+-----------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
    | Slave_IO_State                   | Master_Host    | Master_User | Master_Port | Connect_Retry | Master_Log_File       | Read_Master_Log_Pos | Relay_Log_File              | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
    +----------------------------------+----------------+-------------+-------------+---------------+-----------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
    | Waiting for master to send event | 192.168.64.132 | slave       |        3307 |            30 | mall-mysql-bin.000001 |                 769 | mall-mysql-relay-bin.000002 |           325 | mall-mysql-bin.000001 | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 769 |             537 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |              101 | a382dfa8-e507-11ee-9375-0242ac110004 | /var/lib/mysql/master.info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
    +----------------------------------+----------------+-------------+-------------+---------------+-----------------------+---------------------+-----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
    1 row in set (0.01 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  11. 验证 主从同步,可以通过命令或客户端验证,这里不再展示。

三、Redis 安装

1. Redis 三主三从

  1. 搭建六台 Redis 节点

    # 启动6台Redis节点
    # --net host 使用宿主机的IP和端口,默认
    # --cluster-enabled yes 开启redis集群
    # --appendonly yes 开启redis持久化
    # --port 6381 配置redis端口号
    [root@192 ~]# docker run -d --name redis-node-1 --net host --privileged=true -v /mydocker/redis-cluster/share/redis-node-1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
    [root@192 ~]# docker run -d --name redis-node-2 --net host --privileged=true -v /mydocker/redis-cluster/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
    ca49edab5c26631b0e35a40ed460a1a8a00f9c3ff74f58c9267dc52b62abfeab
    [root@192 ~]# docker run -d --name redis-node-3 --net host --privileged=true -v /mydocker/redis-cluster/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
    72932ccc332432d0dd3a7e5b129c0d80d3be53bfb031894ac392460bf1c4a9d2
    [root@192 ~]# docker run -d --name redis-node-4 --net host --privileged=true -v /mydocker/redis-cluster/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
    461cb9285bda92e8f0e87a3f087b17e421068b12d857e8699d676acaa168d25e
    [root@192 ~]# docker run -d --name redis-node-5 --net host --privileged=true -v /mydocker/redis-cluster/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
    c13f90d780d4c21e9b7f7c9af8d2fafd62f1f22baa7ec1e8d6cffdaaf707c9c2
    [root@192 ~]# docker run -d --name redis-node-6 --net host --privileged=true -v /mydocker/redis-cluster/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386
    6b81a9d791a78cb37358383580041016441523c452f45f081367658e61591e06
    [root@192 ~]# docker ps
    CONTAINER ID   IMAGE         COMMAND                   CREATED             STATUS             PORTS                                                  NAMES
    6b81a9d791a7   redis:6.0.8   "docker-entrypoint.s…"   6 seconds ago       Up 6 seconds                                                              redis-node-6
    c13f90d780d4   redis:6.0.8   "docker-entrypoint.s…"   12 seconds ago      Up 12 seconds                                                             redis-node-5
    461cb9285bda   redis:6.0.8   "docker-entrypoint.s…"   19 seconds ago      Up 19 seconds                                                             redis-node-4
    72932ccc3324   redis:6.0.8   "docker-entrypoint.s…"   24 seconds ago      Up 24 seconds                                                             redis-node-3
    ca49edab5c26   redis:6.0.8   "docker-entrypoint.s…"   33 seconds ago      Up 33 seconds                                                             redis-node-2
    ab397ce2f7f0   redis:6.0.8   "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes                                                              redis-node-1
    0401aa0491f4   mysql:5.7     "docker-entrypoint.s…"   53 minutes ago      Up 51 minutes      33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp   mysql-slave
    074c2c85947c   mysql:5.7     "docker-entrypoint.s…"   About an hour ago   Up About an hour   33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp   mysql-master
    [root@192 ~]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  2. 任选一台 Redis 主机,构建主从关系

    # 进入一台Redis集群服务
    [root@192 etc]# docker exec -it redis-node-1 /bin/bash
    # 执行主从构建命令.可以从日志中看到打印的Redis 槽的指派信息
    root@192:/data# redis-cli --cluster create 192.168.64.132:6381 192.168.64.132:6382 192.168.64.132:6383 192.168.64.132:6384 192.168.64.132:6385 192.168.64.132:6386 --cluster-replicas 1
    >>> Performing hash slots allocation on 6 nodes...
    Master[0] -> Slots 0 - 5460
    Master[1] -> Slots 5461 - 10922
    Master[2] -> Slots 10923 - 16383
    Adding replica 192.168.64.132:6385 to 192.168.64.132:6381
    Adding replica 192.168.64.132:6386 to 192.168.64.132:6382
    Adding replica 192.168.64.132:6384 to 192.168.64.132:6383
    >>> Trying to optimize slaves allocation for anti-affinity
    [WARNING] Some slaves are in the same host as their master
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[0-5460] (5461 slots) master
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[5461-10922] (5462 slots) master
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[10923-16383] (5461 slots) master
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    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.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
  3. 查看集群状态

    # 进入一个 Redis 节点,通过 cluster info 命令查看集群状态
    root@192:/data# redis-cli -p 6381
    127.0.0.1:6381> 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:656
    cluster_stats_messages_pong_sent:699
    cluster_stats_messages_sent:1355
    cluster_stats_messages_ping_received:694
    cluster_stats_messages_pong_received:656
    cluster_stats_messages_meet_received:5
    cluster_stats_messages_received:1355
    
    # 查看集群节点信息
    127.0.0.1:6381> cluster nodes
    c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385@16385 slave 4e378a4800f20ff6fb36c833310a19041d70c905 0 1710763297834 1 connected
    9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382@16382 master - 0 1710763296827 2 connected 5461-10922
    0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384@16384 slave 6455206ef6ce922ced57adf10f05d58d7d46125d 0 1710763296000 3 connected
    6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383@16383 master - 0 1710763297000 3 connected 10923-16383
    89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386@16386 slave 9efa8469bdd5450dd38453d5b7f8b28e619ca147 0 1710763296000 2 connected
    # 当前节点编号是 4e378a4800f20ff6fb36c833310a19041d70c905, c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 是 当前节点的从节点,即 6381 的从节点是 6385
    4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381@16381 myself,master - 0 1710763297000 1 connected 0-5460
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  4. 集群模式连接 Redis (redis-cli -p 端口 -c

    # 集群模式连接 Redis, 加 -c 代表  cluster 
    root@192:/data# redis-cli -p 6381 -c
    # set key 值会计算出所属的Redis 槽,然后路由过去
    127.0.0.1:6381> set k1 v1
    -> Redirected to slot [12706] located at 192.168.64.132:6383
    OK
    192.168.64.132:6383> set k2 v2
    -> Redirected to slot [449] located at 192.168.64.132:6381
    OK
    192.168.64.132:6381> set k3 k3
    OK
    192.168.64.132:6381> get k1
    -> Redirected to slot [12706] located at 192.168.64.132:6383
    "v1"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  5. 查看集群信息 (cluster check)

    # 通过 --cluster check 可以检测集群情况
    root@192:/data# redis-cli --cluster check 192.168.64.132:6381
    192.168.64.132:6381 (4e378a48...) -> 2 keys | 5461 slots | 1 slaves.
    192.168.64.132:6382 (9efa8469...) -> 0 keys | 5462 slots | 1 slaves.
    192.168.64.132:6383 (6455206e...) -> 1 keys | 5461 slots | 1 slaves.
    [OK] 3 keys in 3 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

补充:

  1. 默认情况下Redis 集群主节点下线后,从节点会变成主节点。
  2. Redis 集群三主三从中如果同一主从都掉线,Redis 集群处于下线状态,即使可以连接某个节点也是无法操作任何数据。
    # 停止到6381 和 其从节点 6385 后,集群处于掉线状态,
    [root@192 etc]# docker exec -it redis-node-2 /bin/bash
    # 连接 6382 节点但是无法操作数据
    root@192:/data# redis-cli -p 6382 -c
    127.0.0.1:6382> get k1
    (error) CLUSTERDOWN The cluster is down
    127.0.0.1:6382> set k5 v5
    (error) CLUSTERDOWN The cluster is down
    # 查看集群状态处于 fail状态
    127.0.0.1:6382> cluster info
    cluster_state:fail
    cluster_slots_assigned:16384
    cluster_slots_ok:10923
    cluster_slots_pfail:0
    cluster_slots_fail:5461
    cluster_known_nodes:6
    cluster_size:3
    cluster_current_epoch:6
    cluster_my_epoch:2
    cluster_stats_messages_ping_sent:2561
    cluster_stats_messages_pong_sent:2564
    cluster_stats_messages_meet_sent:1
    cluster_stats_messages_fail_sent:3
    cluster_stats_messages_sent:5129
    cluster_stats_messages_ping_received:2564
    cluster_stats_messages_pong_received:2560
    cluster_stats_messages_fail_received:2
    cluster_stats_messages_received:5126
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

2. Redis 扩容和缩容

2.1 扩容

扩容为四主四从:

  1. 创建新的Redis节点 6387 和 6388

    [root@192 etc]# docker run -d --name redis-node-7 --net host --privileged=true -v /mydocker/redis-cluster/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
    8735fe90539868853389975bf135a94833853c66552cd0a175f626fcf81a7e86
    [root@192 etc]# docker run -d --name redis-node-8 --net host --privileged=true -v /mydocker/redis-cluster/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
    4b88401a4f27e257197c46913c2f49c18e0e49f6bfd31d515f6e0f0628394b8c
    
    • 1
    • 2
    • 3
    • 4
  2. 将 6387 节点作为主节点加入到集群中

    # 进入 6387 节点
    [root@192 etc]# docker exec -it redis-node-7 /bin/bash
    # redis-cli --cluster add-node 本节点地址 要加入的集群中的其中一个节点地址
    root@192:/data# redis-cli --cluster add-node 192.168.64.132:6387 192.168.64.132:6381
    >>> Adding node 192.168.64.132:6387 to cluster 192.168.64.132:6381
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    >>> Send CLUSTER MEET to node 192.168.64.132:6387 to make it join the cluster.
    [OK] New node added correctly.
    
    # 节点检测可以看到 6387 已经加入集群并作为一个主节点
    # 但是需要注意这里并没有给6387节点分配槽信息
    root@192:/data# redis-cli --cluster check 192.168.64.132:6381
    192.168.64.132:6381 (4e378a48...) -> 2 keys | 5461 slots | 1 slaves.
    192.168.64.132:6387 (827e12ca...) -> 0 keys | 0 slots | 0 slaves.
    192.168.64.132:6382 (9efa8469...) -> 0 keys | 5462 slots | 1 slaves.
    192.168.64.132:6383 (6455206e...) -> 1 keys | 5461 slots | 1 slaves.
    [OK] 3 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: 827e12cac658b16653c3d5028ba414b0b4390414 192.168.64.132:6387
       slots: (0 slots) master
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
  3. 对集群进行槽位重分配

    # redis经过槽位检查后,会提示需要分配的槽位数量: 
    # 例如,我们现在是4台master,我们想要给node7分配4096个槽位,这样每个节点都是4096个槽位。
    # 输入4096后,会让输入要接收这些哈希槽的节点ID,填入node7的节点ID即可。(就是节点信息中很长的一串十六进制串)。
    # 然后会提示,询问要从哪些节点中拨出一部分槽位凑足4096个分给Node7。一般选择 all,即将之前的3个主节点的槽位都均一些给Node7,这样可以使得每个节点的槽位数相等均衡。
    # 输入all之后,redis会列出一个计划,内容是自动从前面的3台master中拨出一部分槽位分给Node7的槽位,需要确认一下分配的计划。
    # 输入yes确认后,redis便会自动重新洗牌,给Node7分配槽位。 
    root@192:/data# redis-cli --cluster reshard 192.168.64.132:6381
    # 询问想要重新分配几个槽位
    How many slots do you want to move (from 1 to 16384)? 4096
    # 询问哪个节点接受这些槽信息
    What is the receiving node ID? 827e12cac658b16653c3d5028ba414b0b4390414 
    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.
    # 选择all
    Source node #1: all
    ...
    
    # 重新分配后可以通过如下命令查看分配结果
    root@192:/data# redis-cli --cluster check 192.168.64.132:6381
    192.168.64.132:6381 (4e378a48...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.64.132:6387 (827e12ca...) -> 1 keys | 4096 slots | 0 slaves.
    192.168.64.132:6382 (9efa8469...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.64.132:6383 (6455206e...) -> 1 keys | 4096 slots | 1 slaves.
    [OK] 3 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
    M: 827e12cac658b16653c3d5028ba414b0b4390414 192.168.64.132:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    这里需要注意的是:重新分配后槽位并非是完全重新分配的,因为对于之前的节点槽位来说,完全重新分配代价太大,涉及大量数据迁移,因此这里会将之前节点的部分槽位分配给新的节点。如下:

    节点1:[1365-5460](供4096个槽位),,,分配前为[0-5460](共5461个槽位)
    节点2:[6827-10922](共4096个槽位),,,分配前为[5461-10922](共5461个槽位)
    节点3:[12288-16383](共4096个槽位),,,分配前为[10923-16383](共5462个槽位)
    
    节点7:[0-1364],[5461-6826],[10923-12287](共4096个槽位),从每个节点中匀出来了一部分给了节点7
    
    • 1
    • 2
    • 3
    • 4
    • 5
  4. 为主节点 6387 分配从节点 6388

    # redis-cli --cluster add-node 192.168.xxx.xxx:6388 192.168.xxx.xxx:6381 --cluster-slave --cluster-master-id node7节点的十六进制编号字符串
    root@192:/data# redis-cli --cluster add-node 192.168.64.132:6388 192.168.64.132:6381 --cluster-slave --cluster-master-id 827e12cac658b16653c3d5028ba414b0b4390414
    >>> Adding node 192.168.64.132:6388 to cluster 192.168.64.132:6381
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
    M: 827e12cac658b16653c3d5028ba414b0b4390414 192.168.64.132:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    >>> Send CLUSTER MEET to node 192.168.64.132:6388 to make it join the cluster.
    Waiting for the cluster to join
    
    >>> Configure node as replica of 192.168.64.132:6387.
    [OK] New node added correctly.
    # 查看集群状态
    root@192:/data# redis-cli --cluster check 192.168.64.132:6381
    192.168.64.132:6381 (4e378a48...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.64.132:6387 (827e12ca...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.64.132:6382 (9efa8469...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.64.132:6383 (6455206e...) -> 1 keys | 4096 slots | 1 slaves.
    [OK] 3 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
    S: 62c616c6fac267a8c8d0dcf122b4e28ec0a4728c 192.168.64.132:6388
       slots: (0 slots) slave
       replicates 827e12cac658b16653c3d5028ba414b0b4390414
    M: 827e12cac658b16653c3d5028ba414b0b4390414 192.168.64.132:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
       1 additional replica(s)
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

2.2 主从缩容

将四主四从缩容为三主三从

  1. 删除 6388 节点

    # 获取节点状态
    root@192:/data# redis-cli --cluster check 192.168.64.132:6381
    192.168.64.132:6381 (4e378a48...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.64.132:6387 (827e12ca...) -> 1 keys | 4096 slots | 1 slaves.
    192.168.64.132:6382 (9efa8469...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.64.132:6383 (6455206e...) -> 1 keys | 4096 slots | 1 slaves.
    [OK] 3 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
    S: 62c616c6fac267a8c8d0dcf122b4e28ec0a4728c 192.168.64.132:6388
       slots: (0 slots) slave
       replicates 827e12cac658b16653c3d5028ba414b0b4390414
    M: 827e12cac658b16653c3d5028ba414b0b4390414 192.168.64.132:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
       1 additional replica(s)
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    # 移除 6388 节点
    root@192:/data# redis-cli --cluster del-node 192.168.64.132:6388 62c616c6fac267a8c8d0dcf122b4e28ec0a4728c
    >>> Removing node 62c616c6fac267a8c8d0dcf122b4e28ec0a4728c from cluster 192.168.64.132:6388
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> Sending CLUSTER RESET SOFT to the deleted node.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  2. 对 6387 节点进行槽位重分配

    # 对整个集群进行重新分片
    redis-cli --cluster reshard 192.168.64.132:6381
    ...
    # Redis经过槽位检查后,会提示需要分配的槽位数量: 
    # 如果我们想直接把node7的4096个哈希槽全部分给某个节点,可以直接输入4096。
    # 输入4096后,会让输入要接收这些哈希槽的节点ID。假如我们想把这4096个槽都分给Node1,直接输入node1节点的编号即可。
    # 然后会提示,询问要从哪些节点中拨出一部分槽位凑足4096个分给Node1。这里我们输入node7的节点编号,回车后输入done。 
    
    # 需要重新分配多少个节点
    How many slots do you want to move (from 1 to 16384)? 4096
    # 哪个节点接收这些槽位,这里选择节点1
    What is the receiving node ID? 4e378a4800f20ff6fb36c833310a19041d70c905
    # 选择这些槽位从那些节点划出用来分配给节点1
    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.
    # 选择节点7来划出槽位供分配
    Source node #1: 827e12cac658b16653c3d5028ba414b0b4390414
    Source node #2: done
    
    # 查看集群状态
    root@192:/data# redis-cli --cluster check 192.168.64.132:6381
    192.168.64.132:6381 (4e378a48...) -> 2 keys | 8192 slots | 1 slaves.
    # 6387 节点已经没有槽位了
    192.168.64.132:6387 (827e12ca...) -> 0 keys | 0 slots | 0 slaves.
    192.168.64.132:6382 (9efa8469...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.64.132:6383 (6455206e...) -> 1 keys | 4096 slots | 1 slaves.
    [OK] 3 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[0-6826],[10923-12287] (8192 slots) master
       1 additional replica(s)
    M: 827e12cac658b16653c3d5028ba414b0b4390414 192.168.64.132:6387
       slots: (0 slots) master
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    # 删除 6387 节点
    root@192:/data# redis-cli --cluster del-node 192.168.64.132:6387 827e12cac658b16653c3d5028ba414b0b4390414
    >>> Removing node 827e12cac658b16653c3d5028ba414b0b4390414 from cluster 192.168.64.132:6387
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> Sending CLUSTER RESET SOFT to the deleted node.
    
    # 检查集群状态
    root@192:/data# redis-cli --cluster check 192.168.64.132:6381
    192.168.64.132:6381 (4e378a48...) -> 2 keys | 8192 slots | 1 slaves.
    192.168.64.132:6382 (9efa8469...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.64.132:6383 (6455206e...) -> 1 keys | 4096 slots | 1 slaves.
    [OK] 3 keys in 3 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.64.132:6381)
    M: 4e378a4800f20ff6fb36c833310a19041d70c905 192.168.64.132:6381
       slots:[0-6826],[10923-12287] (8192 slots) master
       1 additional replica(s)
    S: c970d5d73d9b1aa7a2f1d1d06779e3b8856673ae 192.168.64.132:6385
       slots: (0 slots) slave
       replicates 4e378a4800f20ff6fb36c833310a19041d70c905
    M: 9efa8469bdd5450dd38453d5b7f8b28e619ca147 192.168.64.132:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    M: 6455206ef6ce922ced57adf10f05d58d7d46125d 192.168.64.132:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    S: 89ac15e265ee54982059e546d4c5db3ba460790e 192.168.64.132:6386
       slots: (0 slots) slave
       replicates 9efa8469bdd5450dd38453d5b7f8b28e619ca147
    S: 0f172b39151a1917ecab8d57a52ca7d3403d3616 192.168.64.132:6384
       slots: (0 slots) slave
       replicates 6455206ef6ce922ced57adf10f05d58d7d46125d
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91

四、参考内容

B 站 尚硅谷 Docker 视频
https://www.yuque.com/tmfl/cloud/dketq0

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/360968
推荐阅读
相关标签
  

闽ICP备14008679号