docker-compose部署redis一主两从

docker-compose 部署redis哨兵模式

1.docker和docker-compose部署
#1.安装docker
$ yum install docker -y

#2.安装docker-compose
curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
2.编写docker-compose.yml文件实现redis一主二从
version: ‘3.7‘
services:
  master:
    image: redis:5.0.5
    container_name: redis-master
    restart: always
    command: redis-server --port 6379 --requirepass test@dbuser2018  --appendonly yes
    ports:
      - 6379:6379
    volumes:
      - ./data:/data

  slave1:
    image: redis:5.0.5
    container_name: redis-slave-1
    restart: always
    command: redis-server --slaveof 10.0.0.81 6379 --port 6380  --requirepass test@dbuser2018 --masterauth test@dbuser2018  --appendonly yes
    ports:
      - 6380:6380
    volumes:
      - ./data:/data


  slave2:
    image: redis:5.0.5
    container_name: redis-slave-2
    restart: always
    command: redis-server --slaveof 10.0.0.81 6379 --port 6381  --requirepass test@dbuser2018 --masterauth test@dbuser2018  --appendonly yes
    ports:
      - 6381:6381
    volumes:
      - ./data:/data
#启动redis,使用如下命令 -d参数是挂载后台
$ docker-compose up -d
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
c2bba756de9c        redis:5.0.5         "docker-entrypoint..."   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:26381->26379/tcp   redis-sentinel-3
b97258f1c2fc        redis:5.0.5         "docker-entrypoint..."   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:26380->26379/tcp   redis-sentinel-2
2747e5c2e37f        redis:5.0.5         "docker-entrypoint..."   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:26379->26379/tcp   redis-sentinel-1
23284d995f32        redis:5.0.5         "docker-entrypoint..."   16 minutes ago      Up 6 seconds        0.0.0.0:6379->6379/tcp               redis-master
1a95774fabca        redis:5.0.5         "docker-entrypoint..."   16 minutes ago      Up 16 minutes       6379/tcp, 0.0.0.0:6381->6381/tcp     redis-slave-2
37bf97b29272        redis:5.0.5         "docker-entrypoint..."   16 minutes ago      Up 16 minutes       6379/tcp, 0.0.0.0:6380->6380/tcp     redis-slave-1

#进入容器(主redis)
$ docker eec -it 23284 bash
root@23284d995f32:/data# redis-cli
127.0.0.1:6379> auth test@dbuser2018  #账号密码 在docker-compose里面设置
OK
127.0.0.1:6379> keys *
1) "name2"
2) "name1"
127.0.0.1:6379> set name3 lili
OK
127.0.0.1:6379> get name3
"lili"
127.0.0.1:6379>
#证明完毕,退出容器. ctrl+c退出redis,输入如下命令退出容器
$ exit
#同样的方式进入redis从服务器,查看是否能够获取到name3,且不具备写功能
$ docker exec -it 1a9 bash
root@1a95774fabca:/data# redis-cli -p 6381
127.0.0.1:6381> AUTH test@dbuser2018
OK
127.0.0.1:6381> keys *
1) "name2"
2) "name1"
3) "name3"
127.0.0.1:6381> set name4 11
(error) READONLY You can‘t write against a read only replica.
127.0.0.1:6381> 
#上述实验表明,redis一主二从部署完毕,且能运行
#验证数据是否持久化,使用下面的命令
$ docker-compose down 
$ docker-compose up -d
#关闭重启后,进入容器内部看看上述的数据是否存在,如果存在就表明数据持久化成功
3.部署redis-sentinel
  • 编写docker-compose.yml
  version: ‘3.7‘
  services:
    sentinel1:
      image: redis:5.0.5
      container_name: redis-sentinel-1
      command: redis-sentinel /usr/local/etc/redis/sentinel.conf
      restart: always
      ports:
        - 26379:26379
      volumes:
        - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
   
    sentinel2:
      image: redis:5.0.5
      container_name: redis-sentinel-2
      command: redis-sentinel /usr/local/etc/redis/sentinel.conf
      restart: always
      ports:
        - 26380:26379
      volumes:
        - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
   
    sentinel3:
      image: redis:5.0.5
      container_name: redis-sentinel-3
      command: redis-sentinel /usr/local/etc/redis/sentinel.conf
      restart: always
      ports:
        - 26381:26379
      volumes:
        - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
  • 编写sentinel.conf
 port 26379
 dir /tmp
 # 自定义集群名,其中 10.0.0.81 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
 sentinel monitor mymaster 10.0.0.81 6379 2
 sentinel down-after-milliseconds mymaster 30000
 sentinel parallel-syncs mymaster 1
 sentinel auth-pass mymaster test@dbuser2018
 sentinel failover-timeout mymaster 180000
 sentinel deny-scripts-reconfig yes
  • 复制配置文件
# 复制3份redis-sentinel配置文件
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf
  • 启动redis-sentinel
  docker-compose up -d
  • 查看redis-sentinel是否启动成功
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                NAMES
c2bba756de9c        redis:5.0.5         "docker-entrypoint..."   56 minutes ago      Up 56 minutes       6379/tcp, 0.0.0.0:26381->26379/tcp   redis-sentinel-3
b97258f1c2fc        redis:5.0.5         "docker-entrypoint..."   56 minutes ago      Up 56 minutes       6379/tcp, 0.0.0.0:26380->26379/tcp   redis-sentinel-2
2747e5c2e37f        redis:5.0.5         "docker-entrypoint..."   56 minutes ago      Up 56 minutes       6379/tcp, 0.0.0.0:26379->26379/tcp   redis-sentinel-1
23284d995f32        redis:5.0.5         "docker-entrypoint..."   About an hour ago   Up 45 minutes       0.0.0.0:6379->6379/tcp               redis-master
1a95774fabca        redis:5.0.5         "docker-entrypoint..."   About an hour ago   Up About an hour    6379/tcp, 0.0.0.0:6381->6381/tcp     redis-slave-2
37bf97b29272        redis:5.0.5         "docker-entrypoint..."   About an hour ago   Up About an hour    6379/tcp, 0.0.0.0:6380->6380/tcp     redis-slave-1
  • 进入redis-sentinel容器中,查看redis连接状态
$ docker exec -it 容器Id bash<br>redis-cli -p 26379
$ docker exec -it 2747 bash 
root@2747e5c2e37f:/data# redis-cli -p 26379
#执行如下命令,查看redis主信息
127.0.0.1:26379> sentinel master mymaster
......
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
......
127.0.0.1:26379> 
#显示上面信息则表明成功

#执行如下命令,查看从redis信息是否正常
127.0.0.1:26379> sentinel slaves mymaster
......
31) "master-link-status"
  32) "ok"
  33) "master-host"
  34) "172.18.0.1"
  35) "master-port"
  36) "6380"
  37) "slave-priority"
......
#显示如上信息代表正常  
4.测试主redis挂了之后,哨兵能否正常选举redis
  • 使用如下命令,停掉主redis. [停掉容器也可以使用容器对应的NAMES来停掉]
$docker stop redis-master
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                                NAMES
c2bba756de9c        redis:5.0.5         "docker-entrypoint..."   4 minutes ago       Up 4 minutes                6379/tcp, 0.0.0.0:26381->26379/tcp   redis-sentinel-3
b97258f1c2fc        redis:5.0.5         "docker-entrypoint..."   4 minutes ago       Up 4 minutes                6379/tcp, 0.0.0.0:26380->26379/tcp   redis-sentinel-2
2747e5c2e37f        redis:5.0.5         "docker-entrypoint..."   4 minutes ago       Up 4 minutes                6379/tcp, 0.0.0.0:26379->26379/tcp   redis-sentinel-1
23284d995f32        redis:5.0.5         "docker-entrypoint..."   9 minutes ago       Exited (0) 12 seconds ago                                        redis-master
1a95774fabca        redis:5.0.5         "docker-entrypoint..."   9 minutes ago       Up 9 minutes                6379/tcp, 0.0.0.0:6381->6381/tcp     redis-slave-2
37bf97b29272        redis:5.0.5         "docker-entrypoint..."   9 minutes ago       Up 9 minutes                6379/tcp, 0.0.0.0:6380->6380/tcp     redis-slave-1                                                
  • 查看redis-sentinel日志,看其将那个redis选举为主,如下,其将端口位6380的redis选举为主
 $ docker logs -f  b972
1:X 24 Aug 2021 18:17:25.125 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:X 24 Aug 2021 18:17:25.125 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
1:X 24 Aug 2021 18:17:25.125 # Configuration loaded
1:X 24 Aug 2021 18:17:25.137 * Running mode=sentinel, port=26379.
1:X 24 Aug 2021 18:17:25.137 # 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:X 24 Aug 2021 18:17:25.138 # Sentinel ID is 4f22376aa13da2d4b73dcf795bf00b99ed5bee20
1:X 24 Aug 2021 18:17:25.138 # +monitor master mymaster 10.0.0.81 6379 quorum 2
1:X 24 Aug 2021 18:17:25.139 * +slave slave 172.18.0.1:6381 172.18.0.1 6381 @ mymaster 10.0.0.81 6379
1:X 24 Aug 2021 18:17:25.140 * +slave slave 172.18.0.1:6380 172.18.0.1 6380 @ mymaster 10.0.0.81 6379
1:X 24 Aug 2021 18:17:27.017 * +sentinel sentinel b6c14aabeed294db25a2f9e93ddabcc12b39ad71 172.19.0.2 26379 @ mymaster 10.0.0.81 6379
1:X 24 Aug 2021 18:17:27.153 * +sentinel sentinel d6d28bdd75cde73af427c6bff7515f88038b3c2c 172.19.0.4 26379 @ mymaster 10.0.0.81 6379
1:X 24 Aug 2021 18:21:45.965 # +sdown master mymaster 10.0.0.81 6379
1:X 24 Aug 2021 18:21:46.065 # +new-epoch 1
1:X 24 Aug 2021 18:21:46.065 # +vote-for-leader d6d28bdd75cde73af427c6bff7515f88038b3c2c 1
1:X 24 Aug 2021 18:21:46.342 # +config-update-from sentinel d6d28bdd75cde73af427c6bff7515f88038b3c2c 172.19.0.4 26379 @ mymaster 10.0.0.81 6379
1:X 24 Aug 2021 18:21:46.342 # +switch-master mymaster 10.0.0.81 6379 172.18.0.1 6380
1:X 24 Aug 2021 18:21:46.342 * +slave slave 172.18.0.1:6381 172.18.0.1 6381 @ mymaster 172.18.0.1 6380
1:X 24 Aug 2021 18:21:46.342 * +slave slave 10.0.0.81:6379 10.0.0.81 6379 @ mymaster 172.18.0.1 6380
1:X 24 Aug 2021 18:22:16.344 # +sdown slave 10.0.0.81:6379 10.0.0.81 6379 @ mymaster 172.18.0.1 6380
  • 测试端口号为6380的redis是否具备写功能,如下则表明成功
 $ docker exec -it redis-slave-1 bash
root@37bf97b29272:/data# redis-cli -p 6380
127.0.0.1:6380> auth test@dbuser2018
OK
127.0.0.1:6380> keys *
1) "name2"
2) "name1"
3) "name3"
127.0.0.1:6380> set name4 yuan
OK
127.0.0.1:6380> 

docker-compose部署redis一主两从

上一篇:AU篇——处理人声混BGM_(尽可能)去BGM提取人声


下一篇:Qt 桌面服务 QDesktopServices