美文网首页程序员
CentOS7.9下安装docker以及redis集群全过程

CentOS7.9下安装docker以及redis集群全过程

作者: 桃子是水果 | 来源:发表于2022-03-09 11:38 被阅读0次

    环境准备

    1. 虚拟机系统地址: http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso

    2. 安装时选择最小安装

    3. 使用virtual box安装完成后的虚拟机版本

      [root@localhost ~]# cat /etc/centos-release
      CentOS Linux release 7.9.2009 (Core)
      
    4. 设置虚拟机端口转发

      宿主机:虚拟机

      1. 18022:22
      2. 7001:7001
      3. 7002:7002
      4. 7003:7003
      5. 7004:7004
      6. 7005:7005
      7. 7006:7006

      redis集群间会通过对应的ip进行连接,如果端口转发设置了17001:7001,那么 外部连接17001能够连到单机,但是外部客户端获取的集群信息将会是虚拟机中真实的端口(如7002),此时外部的客户端将会通过集群连接7002,但是7002在宿主机中没有做端口映射,导致连接失败

    5. 虚拟机用户

      root用户: root/root

      redis用户: redis/1qaz2wsx

      redis已开启密码验证:1qaz2wsx


    本例中常用命令行

    docker ps -a #查看所有的容器
    docker ps #查看运行中的容器
    docker stop container_id #停止指定id的容器,多个以逗号分隔
    docker start container_id #开启容器,多个以逗号分隔
    docker restart container_id #重启容器,多个以逗号分隔
    docker rm container_id #删除容器,多个以逗号分隔
    docker exec -it container_id /bin/bash #进入容器,并继续bash命令行交互
    #批量停止
    docker stop `docker ps | awk 'NR!=1{print $1}'`
    #批量启动(只启动redis)
    docker restart `docker ps -a | grep redis-  | awk '{print $1}'`
    
    docker exec -it redis-master1 /bin/bash #进入容器
    #进入redis-cli控制台
    redis-cli -h 127.0.0.1 -p 7001 -a 1qaz2wsx 
    # 以下命令在redis控制台中执行
    cluster nodes #集群节点信息
    cluster info #集群状态
    

    docker安装

    1. 执行命令查询可安装的docker版本

      [root@localhost ~]# yum list docker-ce --showduplicates | sort -r
      

      此处选择了18.06.3-ce

      [root@localhost ~]# yum install docker-ce-18.06.3-ce
      

      安装后加入开机启动服务

      [root@localhost ~]# systemctl start docker
      [root@localhost ~]# systemctl enable docker
      

      查看docker信息

      
      [root@localhost ~]# docker version
      Client:
       Version:           18.06.3-ce
       API version:       1.38
       Go version:        go1.10.3
       Git commit:        d7080c1
       Built:             Wed Feb 20 02:26:51 2019
       OS/Arch:           linux/amd64
       Experimental:      false
      
      Server:
       Engine:
        Version:          18.06.3-ce
        API version:      1.38 (minimum version 1.12)
        Go version:       go1.10.3
        Git commit:       d7080c1
        Built:            Wed Feb 20 02:28:17 2019
        OS/Arch:          linux/amd64
        Experimental:     false
      

    docker-compose安装

    构建redis集群使用了docker-compose命令。

    1. 下载docker-compose并安装

      [root@localhost ~]# cd /usr/local/bin/
      [root@localhost bin]# wget https://github.com/docker/compose/releases/download/v2.3.1/docker-compose-linux-x86_64
      

      注意此处文件的目录在/usr/local/bin/

      这里建议直接在外部下载好后ftp上传到虚拟机中

      v2.3.1可以根据需要自己到github上更换版本

      地址: https://github.com/docker/compose/releases/

      重命名下载下来的文件包并赋予权限,完成后查看版本信息

      [root@localhost ~]# cd /usr/local/bin/
      [root@localhost bin]# rename docker-compose-linux-x86_64 docker-compose docker-compose-linux-x86_64
      [root@localhost bin]# chmod +x /usr/local/bin/docker-compose
      [root@localhost bin]# docker-compose --version
      Docker Compose version v2.3.1
      

    docker中拉取redis镜像

    1. https://hub.docker.com/_/redis?tab=tags上找到自己需要的redis镜像版本

      这里选择了redis:6.2.6版本

    2. 拉取镜像并查看

      [root@localhost ~]# docker pull redis:6.2.6
      [root@localhost ~]# docker images
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      redis               6.2.6               0e403e3816e8        5 days ago          113MB
      
    3. 创建docker容器和虚拟机间的映射目录(目录位置可随意)

      本例中由于新建了redis用户,所以/home目录下已经存在/redis目录了

      1. /home/redis/redis-config目录下新建6个节点配置目录(redis-config需要自己新建)
      [redis@localhost redis-config]$ mkdir 700{1..6}
      [redis@localhost redis-config]$ ls
      7001  7002  7003  7004  7005  7006
      
    4. 为每个节点准备配置文件(每个节点目录各copy一份)

      port 7001 
      protected-mode no # 关闭保护模式,容许无密码连接
      bind 0.0.0.0 # 绑定0.0.0.0,或者注释此项,容许任意ip连接
      daemonize no # 关闭守护进程
      appendonly yes # 开启AOF持久化
      requirepass 1qaz2wsx # 本机密码-与主节点密码一致,否则无法自动切换主从
      masterauth 1qaz2wsx # 主节点密码
      cluster-enabled yes # 开启 cluster集群
      cluster-config-file 7001.conf #实际文件为node-7001.conf,程序自动会生成
      cluster-node-timeout 10000 # 集群超时(ms) 
      cluster-announce-port 7001 # 集群节点映射端口号
      cluster-announce-bus-port 17001 # 集群节点总线端口号
      

      配置中portcluster-config-filecluster-announce-portcluster-announce-bus-port四项配置需要在copy过程中根据端口号修改

      更多设置参考redis官网的配置

      最终目录结构如下(此步骤只要关注7001-7006几个文件夹即可)

      [root@localhost redis-config]# ls
      7001  7003  7005  cluster.tmpl        redis.conf
      7002  7004  7006  docker-compose.yml
      
    5. 设置防火墙,开启7001-7006的端口,开启后重启防火墙

      [root@localhost redis-config]# firewall-cmd --zone=public --add-port=7001-7006/tcp --permanent
      [root@localhost redis-config]# firewall-cmd --reload
      [root@localhost redis-config]# firewall-cmd --list-ports
      7001-7006/tcp
      
    6. /home/redis/redis-config下创建docker-compose.yml 内容如下:

      注意,该yaml文件中指定了容器以host模式启动,会使用宿主虚拟机的host地址和端口,这样外部设置了端口转发后可以直接访问到容器

      network_mode: host

      version: "3.7"
      services:
        redis-master1:
           image: redis:6.2.6 # 基础镜像
           container_name: redis-master1 # 容器服务名
           restart: always # docker重启时容器也重启,由于docker设置开机自启,实际设置后并没有成功启动,解决方案是添加了开机脚本自动执行重启
           environment: # 环境变量
             - PORT=7001 # 跟 config/nodes-7001.conf 里的配置一样的端口
             - TZ=Asia/Shanghai # 时区设置为上海
           network_mode: host # 使用docker宿主IP
           ports: # 映射端口,对外提供服务
             - "7001:7001" # redis 的服务端口
             - "17001:17001" # redis 集群监控端口
           stdin_open: true # 标准输入打开
           tty: true
           privileged: true # 拥有容器内命令执行的权限
           volumes: # 数据卷,目录挂载 host:container的文件映射关系-容器启动后读取的配置的host中的映射路径文件,然后被加载进/data的映射目录中
             - /home/redis/redis-config/7001/nodes-7001.conf:/etc/redis/redis.conf
             - /home/redis/redis-config/7001/data:/data
           command: ["redis-server", "/etc/redis/redis.conf"] # 覆盖容器启动后默认执行的命令
        redis-master2:
           image: redis:6.2.6
           container_name: redis-master2
           restart: always
           environment:
             - PORT=7002
             - TZ=Asia/Shanghai
           network_mode: host
           ports:
             - "7002:7002"
             - "17002:17002"
           stdin_open: true 
           tty: true
           privileged: true 
           volumes: 
             - /home/redis/redis-config/7002/nodes-7002.conf:/etc/redis/redis.conf
             - /home/redis/redis-config/7002/data:/data
           command: ["redis-server", "/etc/redis/redis.conf"]
        redis-master3:
           image: redis:6.2.6
           container_name: redis-master3
           restart: always
           environment:
             - PORT=7003
             - TZ=Asia/Shanghai
           network_mode: host
           ports:
             - "7003:7003"
             - "17003:17003"
           stdin_open: true 
           tty: true
           privileged: true 
           volumes: 
             - /home/redis/redis-config/7003/nodes-7003.conf:/etc/redis/redis.conf
             - /home/redis/redis-config/7003/data:/data
           command: ["redis-server", "/etc/redis/redis.conf"]
        redis-slave1:
           image: redis:6.2.6
           container_name: redis-slave1
           restart: always
           environment:
             - PORT=7004
           network_mode: host
           ports:
             - "7004:7004"
             - "17004:17004"
           stdin_open: true
           tty: true
           privileged: true
           volumes: 
             - /home/redis/redis-config/7004/nodes-7004.conf:/etc/redis/redis.conf
             - /home/redis/redis-config/7004/data:/data
           command: ["redis-server", "/etc/redis/redis.conf"]
        redis-slave2:
           image: redis:6.2.6
           container_name: redis-salve2
           restart: always
           environment:
              - PORT=7005
           network_mode: host
           ports:
             - "7005:7005"
             - "17005:17005"
           stdin_open: true
           tty: true
           privileged: true
           volumes: 
             - /home/redis/redis-config/7005/nodes-7005.conf:/etc/redis/redis.conf
             - /home/redis/redis-config/7005/data:/data
           command: ["redis-server", "/etc/redis/redis.conf"]
        redis-slave3:
           image: redis:6.2.6
           container_name: redis-slave3
           restart: always
           environment:
              - PORT=7006
           network_mode: host
           ports:
             - "7006:7006"
             - "17006:17006"
           stdin_open: true
           tty: true
           privileged: true
           volumes: 
             - /home/redis/redis-config/7006/nodes-7006.conf:/etc/redis/redis.conf
             - /home/redis/redis-config/7006/data:/data
           command: ["redis-server", "/etc/redis/redis.conf"]
      

      注意volumes配置很重要,该配置是指定虚拟机和docker容器间的文件映射关系

      /home/redis/redis-config/7006/nodes-7006.conf:/etc/redis/redis.conf

      指的是把/home/redis/redis-config/7006/nodes-7006.conf映射给/etc/redis/redis.conf,容器中的redis读取的是默认路径下的配置文件。

      同理,后面的/home/redis/redis-config/7006/data目录是容器执行时存储的持久化文件的目录,该目录在虚拟机宿主中需要提前新建

    7. /home/redis/redis-config下启动测试(容器状态出错可以使用docker logs container_name查看log)

      docker-compose.yml同目录下执行docker-compose up -d将会开始启动容器

      [redis@localhost redis-config]$ sudo docker-compose up -d
      [+] Running 6/6
       ⠿ Container redis-slave3   Started                                                        0.3s
       ⠿ Container redis-master1  Running                                                        0.0s
       ⠿ Container redis-master2  Started                                                        0.4s
       ⠿ Container redis-master3  Started                                                        0.4s
       ⠿ Container redis-slave1   Started                                                        0.3s
       ⠿ Container redis-salve2   Started                                                        0.3s
      [redis@localhost redis-config]$ sudo docker-compose ps
      NAME                COMMAND                  SERVICE             STATUS              PORTS
      redis-master1       "docker-entrypoint.s…"   redis-master1       running
      redis-master2       "docker-entrypoint.s…"   redis-master2       running
      redis-master3       "docker-entrypoint.s…"   redis-master3       running
      redis-salve2        "docker-entrypoint.s…"   redis-slave2        running
      redis-slave1        "docker-entrypoint.s…"   redis-slave1        running
      redis-slave3        "docker-entrypoint.s…"   redis-slave3        running
      # 查看运行log
      [redis@localhost redis-config]$sudo docker logs refis-master1
      1:C 08 Mar 2022 17:40:43.824 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
      1:C 08 Mar 2022 17:40:43.824 # Configuration loaded
      1:M 08 Mar 2022 17:40:43.824 * monotonic clock: POSIX clock_gettime
      1:M 08 Mar 2022 17:40:43.825 * No cluster configuration found, I'm 493be565483b3731218b53121f323d27a15e3344
                      _._
                 _.-``__ ''-._
            _.-``    `.  `_.  ''-._           Redis 6.2.6 (00000000/0) 64 bit
        .-`` .-```.  ```\/    _.,_ ''-._
       (    '      ,       .-`  | `,    )     Running in cluster mode
       |`-._`-...-` __...-.``-._|'` _.-'|     Port: 7001
       |    `-._   `._    /     _.-'    |     PID: 1
        `-._    `-._  `-./  _.-'    _.-'
       |`-._`-._    `-.__.-'    _.-'_.-'|
       |    `-._`-._        _.-'_.-'    |           https://redis.io
        `-._    `-._`-.__.-'_.-'    _.-'
       |`-._`-._    `-.__.-'    _.-'_.-'|
       |    `-._`-._        _.-'_.-'    |
        `-._    `-._`-.__.-'_.-'    _.-'
            `-._    `-.__.-'    _.-'
                `-._        _.-'
                    `-.__.-'
      
      1:M 08 Mar 2022 17:40:43.841 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
      1:M 08 Mar 2022 17:40:43.841 # Server initialized
      1:M 08 Mar 2022 17:40:43.841 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
      1:M 08 Mar 2022 17:40:43.841 * Ready to accept connections
      [redis@localhost redis-config]$
      
    8. 开启集群,任意进入一个容器后查看信息

      docker exec -it redis-master1 /bin/bash命令是进入容器中

      redis-master1可以替换为你自己的容器名(docker ps可以查看运行中的容器信息)

      [root@localhost redis-config]# docker exec -it redis-master1 /bin/bash
      
      #下面是redis容器中的操作了(exit命令退出) 容器中的redis被设定了7001的端口
      #所以指定 7001进入
      root@localhost:/data# redis-cli -h 127.0.0.1 -p 7001
      127.0.0.1:7001> auth 1qaz2wsx
      OK
      127.0.0.1:7001> info replication
      # Replication
      role:master
      connected_slaves:0
      master_failover_state:no-failover
      master_replid:9ea784e6649cbf622b095598cdfb5a0b0cf228c5
      master_replid2:0000000000000000000000000000000000000000
      master_repl_offset:0
      second_repl_offset:-1
      repl_backlog_active:0
      repl_backlog_size:1048576
      repl_backlog_first_byte_offset:0
      repl_backlog_histlen:0
      
    9. 此时所有的容器都是master,集群还未创建,先查看容器ip

      [root@localhost redis-config]# docker ps
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
      aab2ec44ae45        redis:6.2.6         "docker-entrypoint.s…"   About an hour ago   Up 22 minutes                           redis-master2
      b65fb2dbf322        redis:6.2.6         "docker-entrypoint.s…"   About an hour ago   Up 22 minutes                           redis-slave3
      9bd201bd00d3        redis:6.2.6         "docker-entrypoint.s…"   About an hour ago   Up 22 minutes                           redis-slave1
      130d9710f73c        redis:6.2.6         "docker-entrypoint.s…"   About an hour ago   Up 22 minutes                           redis-master3
      707733341de8        redis:6.2.6         "docker-entrypoint.s…"   About an hour ago   Up 22 minutes                           redis-salve2
      d0327b5172f6        redis:6.2.6         "docker-entrypoint.s…"   About an hour ago   Up 22 minutes                           redis-master1
      

      根据容器id查询IP

      [root@localhost redis-config]# docker inspect aab2ec44ae45 b65fb2dbf322 9bd201bd00d3 130d9710f73c 707733341de8 d0327b5172f6 | grep IPA
        "SecondaryIPAddresses": null,
                  "IPAddress": "",
                          "IPAMConfig": null,
                          "IPAddress": "",
                  "SecondaryIPAddresses": null,
      

      由于使用host方式连接,所以此处ip可能为空,容器内地址和宿主虚拟机共享,使用了同一网段,可认为是127.0.0.1,同时容器内的redis可以相互访问

    10. 创建集群(--cluster-replicas 1代表一个主节点有一个从节点)

      注意操作是在容器中直接执行而不是进入redis,注意添加-a 密码认证

      在容器中使用redis-cli命令

      root@localhost:/data#  redis-cli --cluster create -a 1qaz2wsx 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --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 127.0.0.1:7005 to 127.0.0.1:7001
      Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
      Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
      >>> Trying to optimize slaves allocation for anti-affinity
      [WARNING] Some slaves are in the same host as their master
      M: 493be565483b3731218b53121f323d27a15e3344 127.0.0.1:7001
         slots:[0-5460] (5461 slots) master
      M: cea65f3d0079aecf8e6e361ae61cc1330f28f241 127.0.0.1:7002
         slots:[5461-10922] (5462 slots) master
      M: b6703deba75f2a6cca476a236262f95545e1e55a 127.0.0.1:7003
         slots:[10923-16383] (5461 slots) master
      S: 527aec25c7221ab803571854ce7b4fdb67803b21 127.0.0.1:7004
         replicates cea65f3d0079aecf8e6e361ae61cc1330f28f241
      S: e5f66a3c8aa3dcad341f989d76eab924049d747c 127.0.0.1:7005
         replicates b6703deba75f2a6cca476a236262f95545e1e55a
      S: 91a44195b75ada961be2687426b805342a459631 127.0.0.1:7006
         replicates 493be565483b3731218b53121f323d27a15e3344
      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 127.0.0.1:7001)
      M: 493be565483b3731218b53121f323d27a15e3344 127.0.0.1:7001
         slots:[0-5460] (5461 slots) master
         1 additional replica(s)
      S: e5f66a3c8aa3dcad341f989d76eab924049d747c 127.0.0.1:7005
         slots: (0 slots) slave
         replicates b6703deba75f2a6cca476a236262f95545e1e55a
      M: b6703deba75f2a6cca476a236262f95545e1e55a 127.0.0.1:7003
         slots:[10923-16383] (5461 slots) master
         1 additional replica(s)
      S: 91a44195b75ada961be2687426b805342a459631 127.0.0.1:7006
         slots: (0 slots) slave
         replicates 493be565483b3731218b53121f323d27a15e3344
      M: cea65f3d0079aecf8e6e361ae61cc1330f28f241 127.0.0.1:7002
         slots:[5461-10922] (5462 slots) master
         1 additional replica(s)
      S: 527aec25c7221ab803571854ce7b4fdb67803b21 127.0.0.1:7004
         slots: (0 slots) slave
         replicates cea65f3d0079aecf8e6e361ae61cc1330f28f241
      [OK] All nodes agree about slots configuration.
      >>> Check for open slots...
      >>> Check slots coverage...
      [OK] All 16384 slots covered.
      

      数据会自动分片(Slots),集群一定是被分完了16383个槽才能正常运行

    11. 测试集群

      redis-cli -c -p 7006 --pass 1qaz2wsx

      使用-c连接集群

      root@localhost:/data# redis-cli -c -p 7006 --pass 1qaz2wsx
      Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
      127.0.0.1:7006> set tmq lyh
      -> Redirected to slot [5896] located at 127.0.0.1:7002
      OK
      127.0.0.1:7002>
      

      可以看到从7006从库设值会自动跳转到合适的主库(根据hash分片(Slots)定位到对应的主库)

      关闭一个主节点后,从节点自动升级为主节点

      [root@localhost redis-config]# docker ps
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
      2f65f6726eac        redis:6.2.6         "docker-entrypoint.s…"   19 minutes ago      Up 19 minutes                           redis-master3
      0320ff7bb8be        redis:6.2.6         "docker-entrypoint.s…"   19 minutes ago      Up 19 minutes                           redis-master1
      eb37d5d69286        redis:6.2.6         "docker-entrypoint.s…"   19 minutes ago      Up 19 minutes                           redis-slave3
      23c01b207843        redis:6.2.6         "docker-entrypoint.s…"   19 minutes ago      Up 19 minutes                           redis-salve2
      d38ce71d6642        redis:6.2.6         "docker-entrypoint.s…"   19 minutes ago      Up 19 minutes                           redis-slave1
      88be84a76769        redis:6.2.6         "docker-entrypoint.s…"   19 minutes ago      Up 19 minutes                           redis-master2
      [root@localhost redis-config]# docker stop 88be84a76769    #停止7002主节点
      88be84a76769
      [root@localhost redis-config]# docker exec -it redis-master1 /bin/bash
      root@localhost:/data# redis-cli -h 127.0.0.1 -p 7001 -a 1qaz2wsx
      127.0.0.1:7001> cluster nodes  #7002节点状态为fail
      39baee913bf54b477b76c391cdff029a00df4d82 127.0.0.1:7006@17006 slave c9f242c66084f411150091c991e948c7adeab813 0 1646749528647 1 connected
      c9f242c66084f411150091c991e948c7adeab813 127.0.0.1:7001@17001 myself,master - 0 1646749526000 1 connected 0-5460
      ab48156e010439f300d182a2c97405f0284d2812 127.0.0.1:7005@17005 slave d30478df0a2e801eb479bad12507829af8bb066c 0 1646749527000 3 connected
      b261873db577925008b8aa860effae78c54ddb83 127.0.0.1:7002@17002 master,fail - 1646749487394 1646749484000 2 disconnected
      588437eeb993168ac80da825b66a1d128fe9f422 127.0.0.1:7004@17004 master - 0 1646749528000 7 connected 5461-10922
      d30478df0a2e801eb479bad12507829af8bb066c 127.0.0.1:7003@17003 master - 0 1646749528000 3 connected 10923-16383
      127.0.0.1:7001>
      root@localhost:/data# redis-cli -c -p 7005 -a 1qaz2wsx
      Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
      127.0.0.1:7005> set tws 111   #测试集群数据
      -> Redirected to slot [7154] located at 127.0.0.1:7004
      OK
      127.0.0.1:7004>
      root@localhost:/data# redis-cli -c -p 7001 -a 1qaz2wsx
      Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
      127.0.0.1:7001> get tws
      -> Redirected to slot [7154] located at 127.0.0.1:7004
      "111"
      127.0.0.1:7004>
      root@localhost:/data# exit
      exit
      [root@localhost redis-config]# docker start 88be84a76769  #启动7002后 7002变为从节点
      88be84a76769
      [root@localhost redis-config]# docker exec -it redis-master1 /bin/bash
      root@localhost:/data# redis-cli -c -p 7001 -a 1qaz2wsx
      Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
      127.0.0.1:7001> cluster nodes
      39baee913bf54b477b76c391cdff029a00df4d82 127.0.0.1:7006@17006 slave c9f242c66084f411150091c991e948c7adeab813 0 1646749619151 1 connected
      c9f242c66084f411150091c991e948c7adeab813 127.0.0.1:7001@17001 myself,master - 0 1646749618000 1 connected 0-5460
      ab48156e010439f300d182a2c97405f0284d2812 127.0.0.1:7005@17005 slave d30478df0a2e801eb479bad12507829af8bb066c 0 1646749618000 3 connected
      b261873db577925008b8aa860effae78c54ddb83 127.0.0.1:7002@17002 slave 588437eeb993168ac80da825b66a1d128fe9f422 0 1646749620156 7 connected
      588437eeb993168ac80da825b66a1d128fe9f422 127.0.0.1:7004@17004 master - 0 1646749619000 7 connected 5461-10922
      d30478df0a2e801eb479bad12507829af8bb066c 127.0.0.1:7003@17003 master - 0 1646749618147 3 connected 10923-16383
      127.0.0.1:7001>
      

      关闭一个master后,一个从节点会提升为主节点,开启关闭的主节点后,原先的主节点变成了从节点。


    外部连接

    外部客户端直接连接上文设置的转发端口即可。

    redis集群在本例中配置了密码:1qaz2wsx


    相关问题

    删除集群

    如果想删除集群,需要先停止所有的容器,然后根据ID删除

    [root@localhost redis-config]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    69913c91e2dd        redis:6.2.6         "docker-entrypoint.s…"   18 minutes ago      Up 18 minutes                           redis-master2
    a83068b0bb11        redis:6.2.6         "docker-entrypoint.s…"   18 minutes ago      Up 18 minutes                           redis-slave3
    10cf7f6d8030        redis:6.2.6         "docker-entrypoint.s…"   18 minutes ago      Up 18 minutes                           redis-salve2
    567b7bf3fd22        redis:6.2.6         "docker-entrypoint.s…"   18 minutes ago      Up 18 minutes                           redis-slave1
    3f35eb8ca98d        redis:6.2.6         "docker-entrypoint.s…"   18 minutes ago      Up 18 minutes                           redis-master3
    c62e2336f18f        redis:6.2.6         "docker-entrypoint.s…"   18 minutes ago      Up 18 minutes                           redis-master1
    
    [root@localhost redis-config]# docker stop 69913c91e2dd a83068b0bb11 10cf7f6d8030 567b7bf3fd22 3f35eb8ca98d c62e2336f18f
    69913c91e2dd
    a83068b0bb11
    10cf7f6d8030
    567b7bf3fd22
    3f35eb8ca98d
    c62e2336f18f
    [root@localhost redis-config]# docker rm 69913c91e2dd a83068b0bb11 10cf7f6d8030 567b7bf3fd22 3f35eb8ca98d c62e2336f18f
    69913c91e2dd
    a83068b0bb11
    10cf7f6d8030
    567b7bf3fd22
    3f35eb8ca98d
    c62e2336f18f
    

    再删除所有节点的data文件夹下的数据配置

    位于docker-compose.yml中设置的映射地址(/home/redis/redis-config/7004/data:/data)

    重建集群

    进入/home/redis/redis-config(docker-compose.yml同级)目录后执行启动命令

    [root@localhost redis-config]# docker-compose up -d
    

    集群状态错误

    如果实际测试过程中发现endpoint serving hash slot is not reachable错误,需要查看集群节点的状态,如果状态为fail或者集群节点不是对应的三主三从,那么集群就没有建好。

    进入容器后查看集群信息

    127.0.0.1:7005> cluster info
    cluster_state:fail
    cluster_slots_assigned:0
    cluster_slots_ok:0
    cluster_slots_pfail:0
    cluster_slots_fail:0
    cluster_known_nodes:1
    cluster_size:0
    cluster_current_epoch:0
    cluster_my_epoch:0
    cluster_stats_messages_sent:0
    cluster_stats_messages_received:0
    127.0.0.1:7005> cluster node
    (error) ERR Unknown subcommand or wrong number of arguments for 'node'. Try CLUSTER HELP.
    127.0.0.1:7005> cluster nodes
    ab48156e010439f300d182a2c97405f0284d2812 :7005@17005 myself,master - 0 0 0 connected
    

    如果是公网,可以使用下列代码添加集群

    127.0.0.1:7001> cluster meet 127.0.0.1 7002
    OK
    127.0.0.1:7001> cluster meet 127.0.0.1 7003
    OK
    

    执行上述代码后,本节点会以master加入,可能会破坏集群,这一点需要注意。

    如果不小心添加了不存在的ip和端口,那么执行下面命令进行移除

    cluster forget noaddr报错的id
    

    例如:

    127.0.0.1:7006> cluster nodes
    ab48156e010439f300d182a2c97405f0284d2812 :0@0 slave,noaddr d30478df0a2e801eb479bad12507829af8bb066c 1646751982380 1646751982380 3 disconnected
    d30478df0a2e801eb479bad12507829af8bb066c 127.0.0.1:7003@17003 master - 0 1646752420971 3 connected 10923-16383
    b261873db577925008b8aa860effae78c54ddb83 127.0.0.1:7002@17002 slave 588437eeb993168ac80da825b66a1d128fe9f422 0 1646752419000 7 connected
    c9f242c66084f411150091c991e948c7adeab813 127.0.0.1:7001@17001 master - 0 1646752423001 1 connected 0-5460
    588437eeb993168ac80da825b66a1d128fe9f422 127.0.0.1:7004@17004 master - 0 1646752421987 7 connected 5461-10922
    39baee913bf54b477b76c391cdff029a00df4d82 127.0.0.1:7006@17006 myself,slave c9f242c66084f411150091c991e948c7adeab813 0 1646752421000 1 connected
    127.0.0.1:7006>
    

    上面的节点中ab48156e010439f300d182a2c97405f0284d2812这个节点是个错误节点,进入redis控制台删除(进入任意正常的集群即可),然后将未进入集群的redis节点加入集群

    [root@localhost ~]# docker exec -it redis-master1 /bin/bash
    
    root@localhost:/data# redis-cli -h 127.0.0.1 -p 7001 -a 1qaz2wsx
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    127.0.0.1:7001> cluster forget ab48156e010439f300d182a2c97405f0284d2812
    OK
    

    注意-需要进入所有的节点将其遗忘,否则其他节点还是会保留这个noaddr的节点信息

    #以从节点方式添加节点
    redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-slave
    

    开机跟随docker启动

    docker update container_name/id --restart=always

    可以在代码中配置重启参数

    docker-compose.yml中其实已经设置了restart: always属性,并且虚拟机重启后容器确实会跟随重启,但是实际发现重启不完全,集群挂了!

    注意,如果虚拟机重启后集群cluster down了,可能是redis容器没有正常重启,手动重启即可

    所以需要在开机后手动重启所有的容器,让集群正常。

    重启代码

    docker restart `docker ps -a | grep redis-  | awk '{print $1}'`
    

    建议配置成sh脚本文件,开机自运行

    相关文章

      网友评论

        本文标题:CentOS7.9下安装docker以及redis集群全过程

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