CentOS7.9下安裝docker以及redis集群全過程

環(huán)境準(zhǔn)備

  1. 虛擬機(jī)系統(tǒng)地址: http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-2009.iso

  2. 安裝時(shí)選擇最小安裝

  3. 使用virtual box安裝完成后的虛擬機(jī)版本

    [root@localhost ~]# cat /etc/centos-release
    CentOS Linux release 7.9.2009 (Core)
    
  4. 設(shè)置虛擬機(jī)端口轉(zhuǎn)發(fā)

    宿主機(jī):虛擬機(jī)

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

    redis集群間會(huì)通過對(duì)應(yīng)的ip進(jìn)行連接,如果端口轉(zhuǎn)發(fā)設(shè)置了17001:7001,那么 外部連接17001能夠連到單機(jī),但是外部客戶端獲取的集群信息將會(huì)是虛擬機(jī)中真實(shí)的端口(如7002),此時(shí)外部的客戶端將會(huì)通過集群連接7002,但是7002在宿主機(jī)中沒有做端口映射,導(dǎo)致連接失敗

  5. 虛擬機(jī)用戶

    root用戶: root/root

    redis用戶: redis/1qaz2wsx

    redis已開啟密碼驗(yàn)證:1qaz2wsx


本例中常用命令行

docker ps -a #查看所有的容器
docker ps #查看運(yùn)行中的容器
docker stop container_id #停止指定id的容器,多個(gè)以逗號(hào)分隔
docker start container_id #開啟容器,多個(gè)以逗號(hào)分隔
docker restart container_id #重啟容器,多個(gè)以逗號(hào)分隔
docker rm container_id #刪除容器,多個(gè)以逗號(hào)分隔
docker exec -it container_id /bin/bash #進(jìn)入容器,并繼續(xù)bash命令行交互
#批量停止
docker stop `docker ps | awk 'NR!=1{print $1}'`
#批量啟動(dòng)(只啟動(dòng)redis)
docker restart `docker ps -a | grep redis-  | awk '{print $1}'`

docker exec -it redis-master1 /bin/bash #進(jìn)入容器
#進(jìn)入redis-cli控制臺(tái)
redis-cli -h 127.0.0.1 -p 7001 -a 1qaz2wsx 
# 以下命令在redis控制臺(tái)中執(zhí)行
cluster nodes #集群節(jié)點(diǎn)信息
cluster info #集群狀態(tài)

docker安裝

  1. 執(zhí)行命令查詢可安裝的docker版本

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

    此處選擇了18.06.3-ce

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

    安裝后加入開機(jī)啟動(dòng)服務(wù)

    [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安裝

構(gòu)建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上傳到虛擬機(jī)中

    v2.3.1可以根據(jù)需要自己到github上更換版本

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

    重命名下載下來的文件包并賦予權(quán)限,完成后查看版本信息

    [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. 創(chuàng)建docker容器和虛擬機(jī)間的映射目錄(目錄位置可隨意)

    本例中由于新建了redis用戶,所以/home目錄下已經(jīng)存在/redis目錄了

    1. /home/redis/redis-config目錄下新建6個(gè)節(jié)點(diǎn)配置目錄(redis-config需要自己新建)
    [redis@localhost redis-config]$ mkdir 700{1..6}
    [redis@localhost redis-config]$ ls
    7001  7002  7003  7004  7005  7006
    
  4. 為每個(gè)節(jié)點(diǎn)準(zhǔn)備配置文件(每個(gè)節(jié)點(diǎn)目錄各copy一份)

    port 7001 
    protected-mode no # 關(guān)閉保護(hù)模式,容許無密碼連接
    bind 0.0.0.0 # 綁定0.0.0.0,或者注釋此項(xiàng),容許任意ip連接
    daemonize no # 關(guān)閉守護(hù)進(jìn)程
    appendonly yes # 開啟AOF持久化
    requirepass 1qaz2wsx # 本機(jī)密碼-與主節(jié)點(diǎn)密碼一致,否則無法自動(dòng)切換主從
    masterauth 1qaz2wsx # 主節(jié)點(diǎn)密碼
    cluster-enabled yes # 開啟 cluster集群
    cluster-config-file 7001.conf #實(shí)際文件為node-7001.conf,程序自動(dòng)會(huì)生成
    cluster-node-timeout 10000 # 集群超時(shí)(ms) 
    cluster-announce-port 7001 # 集群節(jié)點(diǎn)映射端口號(hào)
    cluster-announce-bus-port 17001 # 集群節(jié)點(diǎn)總線端口號(hào)
    

    配置中port、cluster-config-filecluster-announce-port、cluster-announce-bus-port四項(xiàng)配置需要在copy過程中根據(jù)端口號(hào)修改

    更多設(shè)置參考redis官網(wǎng)的配置

    最終目錄結(jié)構(gòu)如下(此步驟只要關(guān)注7001-7006幾個(gè)文件夾即可)

    [root@localhost redis-config]# ls
    7001  7003  7005  cluster.tmpl        redis.conf
    7002  7004  7006  docker-compose.yml
    
  5. 設(shè)置防火墻,開啟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下創(chuàng)建docker-compose.yml 內(nèi)容如下:

    注意,該yaml文件中指定了容器以host模式啟動(dòng),會(huì)使用宿主虛擬機(jī)的host地址和端口,這樣外部設(shè)置了端口轉(zhuǎn)發(fā)后可以直接訪問到容器

    network_mode: host

    version: "3.7"
    services:
      redis-master1:
         image: redis:6.2.6 # 基礎(chǔ)鏡像
         container_name: redis-master1 # 容器服務(wù)名
         restart: always # docker重啟時(shí)容器也重啟,由于docker設(shè)置開機(jī)自啟,實(shí)際設(shè)置后并沒有成功啟動(dòng),解決方案是添加了開機(jī)腳本自動(dòng)執(zhí)行重啟
         environment: # 環(huán)境變量
           - PORT=7001 # 跟 config/nodes-7001.conf 里的配置一樣的端口
           - TZ=Asia/Shanghai # 時(shí)區(qū)設(shè)置為上海
         network_mode: host # 使用docker宿主IP
         ports: # 映射端口,對(duì)外提供服務(wù)
           - "7001:7001" # redis 的服務(wù)端口
           - "17001:17001" # redis 集群監(jiān)控端口
         stdin_open: true # 標(biāo)準(zhǔn)輸入打開
         tty: true
         privileged: true # 擁有容器內(nèi)命令執(zhí)行的權(quán)限
         volumes: # 數(shù)據(jù)卷,目錄掛載 host:container的文件映射關(guān)系-容器啟動(dòng)后讀取的配置的host中的映射路徑文件,然后被加載進(jìn)/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"] # 覆蓋容器啟動(dòng)后默認(rèn)執(zhí)行的命令
      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配置很重要,該配置是指定虛擬機(jī)和docker容器間的文件映射關(guān)系

    /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讀取的是默認(rèn)路徑下的配置文件。

    同理,后面的/home/redis/redis-config/7006/data目錄是容器執(zhí)行時(shí)存儲(chǔ)的持久化文件的目錄,該目錄在虛擬機(jī)宿主中需要提前新建

  7. /home/redis/redis-config下啟動(dòng)測試(容器狀態(tài)出錯(cuò)可以使用docker logs container_name查看log)

    docker-compose.yml同目錄下執(zhí)行docker-compose up -d將會(huì)開始啟動(dòng)容器

    [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
    # 查看運(yùn)行l(wèi)og
    [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. 開啟集群,任意進(jìn)入一個(gè)容器后查看信息

    docker exec -it redis-master1 /bin/bash命令是進(jìn)入容器中

    redis-master1可以替換為你自己的容器名(docker ps可以查看運(yùn)行中的容器信息)

    [root@localhost redis-config]# docker exec -it redis-master1 /bin/bash
    
    #下面是redis容器中的操作了(exit命令退出) 容器中的redis被設(shè)定了7001的端口
    #所以指定 7001進(jìn)入
    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. 此時(shí)所有的容器都是master,集群還未創(chuàng)建,先查看容器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
    

    根據(jù)容器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可能為空,容器內(nèi)地址和宿主虛擬機(jī)共享,使用了同一網(wǎng)段,可認(rèn)為是127.0.0.1,同時(shí)容器內(nèi)的redis可以相互訪問

  10. 創(chuàng)建集群(--cluster-replicas 1代表一個(gè)主節(jié)點(diǎn)有一個(gè)從節(jié)點(diǎn))

    注意操作是在容器中直接執(zhí)行而不是進(jìn)入redis,注意添加-a 密碼認(rèn)證

    在容器中使用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.
    

    數(shù)據(jù)會(huì)自動(dòng)分片(Slots),集群一定是被分完了16383個(gè)槽才能正常運(yùn)行

  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從庫設(shè)值會(huì)自動(dòng)跳轉(zhuǎn)到合適的主庫(根據(jù)hash分片(Slots)定位到對(duì)應(yīng)的主庫)

    關(guān)閉一個(gè)主節(jié)點(diǎn)后,從節(jié)點(diǎn)自動(dòng)升級(jí)為主節(jié)點(diǎn)

    [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主節(jié)點(diǎn)
    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節(jié)點(diǎn)狀態(tài)為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   #測試集群數(shù)據(jù)
    -> 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  #啟動(dòng)7002后 7002變?yōu)閺墓?jié)點(diǎn)
    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>
    

    關(guān)閉一個(gè)master后,一個(gè)從節(jié)點(diǎn)會(huì)提升為主節(jié)點(diǎn),開啟關(guān)閉的主節(jié)點(diǎn)后,原先的主節(jié)點(diǎn)變成了從節(jié)點(diǎn)。


外部連接

外部客戶端直接連接上文設(shè)置的轉(zhuǎn)發(fā)端口即可。

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


相關(guān)問題

刪除集群

如果想刪除集群,需要先停止所有的容器,然后根據(jù)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

再刪除所有節(jié)點(diǎn)的data文件夾下的數(shù)據(jù)配置

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

重建集群

進(jìn)入/home/redis/redis-config(docker-compose.yml同級(jí))目錄后執(zhí)行啟動(dòng)命令

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

集群狀態(tài)錯(cuò)誤

如果實(shí)際測試過程中發(fā)現(xiàn)endpoint serving hash slot is not reachable錯(cuò)誤,需要查看集群節(jié)點(diǎn)的狀態(tài),如果狀態(tài)為fail或者集群節(jié)點(diǎn)不是對(duì)應(yīng)的三主三從,那么集群就沒有建好。

進(jìn)入容器后查看集群信息

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

如果是公網(wǎng),可以使用下列代碼添加集群

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

執(zhí)行上述代碼后,本節(jié)點(diǎn)會(huì)以master加入,可能會(huì)破壞集群,這一點(diǎn)需要注意。

如果不小心添加了不存在的ip和端口,那么執(zhí)行下面命令進(jìn)行移除

cluster forget noaddr報(bào)錯(cuò)的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>

上面的節(jié)點(diǎn)中ab48156e010439f300d182a2c97405f0284d2812這個(gè)節(jié)點(diǎn)是個(gè)錯(cuò)誤節(jié)點(diǎn),進(jìn)入redis控制臺(tái)刪除(進(jìn)入任意正常的集群即可),然后將未進(jìn)入集群的redis節(jié)點(diǎn)加入集群

[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

注意-需要進(jìn)入所有的節(jié)點(diǎn)將其遺忘,否則其他節(jié)點(diǎn)還是會(huì)保留這個(gè)noaddr的節(jié)點(diǎn)信息

#以從節(jié)點(diǎn)方式添加節(jié)點(diǎn)
redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-slave

開機(jī)跟隨docker啟動(dòng)

docker update container_name/id --restart=always

可以在代碼中配置重啟參數(shù)

docker-compose.yml中其實(shí)已經(jīng)設(shè)置了restart: always屬性,并且虛擬機(jī)重啟后容器確實(shí)會(huì)跟隨重啟,但是實(shí)際發(fā)現(xiàn)重啟不完全,集群掛了!

注意,如果虛擬機(jī)重啟后集群cluster down了,可能是redis容器沒有正常重啟,手動(dòng)重啟即可

所以需要在開機(jī)后手動(dòng)重啟所有的容器,讓集群正常。

重啟代碼

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

建議配置成sh腳本文件,開機(jī)自運(yùn)行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容