使用docker-compose搭建redis哨兵,启动哨兵master节点就变成了slave了????

发布于 2022-09-12 04:05:07 字数 4538 浏览 27 评论 0

使用docker-compose搭建redis哨兵集群
是一主二从三哨兵的模式:
docker-compose.yml搭建主从

version: '3.0'
services:
  master:
    container_name: redis-master
    restart: always
    image: redis:5.0.9
    ports:
      - "6379:6379"
    volumes:
      - ./master/conf/redis.conf:/usr/local/etc/redis/redis.conf:rw
      - ./master/data:/data:rw
      - ./master/logs:/var/log/:rw
    command: redis-server --requirepass pwd_123  --masterauth pwd_123

  slave1:
    container_name: redis-slave-1
    restart: always
    image: redis:5.0.9
    ports:
      - "6380:6379"
    volumes:
      - ./slave1/conf/redis.conf:/usr/local/etc/redis/redis.conf:rw
      - ./slave1/data:/data:rw
      - ./slave1/logs:/var/log/:rw
    command:  redis-server --slaveof redis-master 6379 --requirepass pwd_123 --masterauth pwd_123 --appendonly yes

  slave2:
    container_name: redis-slave-2
    restart: always
    image: redis:5.0.9
    ports:
      - "6381:6379"
    volumes:
      - ./slave2/conf/redis.conf:/usr/local/etc/redis/redis.conf:rw
      - ./slave2/data:/data:rw
      - ./slave2/logs:/var/log/:rw
    command:  redis-server --slaveof redis-master 6379 --requirepass pwd_123 --masterauth pwd_123 --appendonly yes

查看master的信息
角色是master角色

 docker exec -it 434e343e9a4e /bin/bash
root@434e343e9a4e:/data# redis-cli
127.0.0.1:6379> auth pwd_123
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.19.0.1,port=6379,state=online,offset=56,lag=1
slave1:ip=172.19.0.1,port=6379,state=online,offset=56,lag=1
master_replid:7e029af3597a6390cbf2fed4b1a114c8c6a92c77
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56

哨兵的docker-compose.yml

version: '3.0'
services:
  sentinel1:
    image: redis:5.0.9
    container_name: redis-sentinel-1
    ports:
      - 26379:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
  sentinel2:
    image: redis:5.0.9
    container_name: redis-sentinel-2
    ports:
      - 26380:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
  sentinel3:
    image: redis:5.0.9
    container_name: redis-sentinel-3
    ports:
      - 26381:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf

三个哨兵的sentinel.conf为:

port 26379
dir /tmp
sentinel monitor mymaster 192.168.72.158 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster pwd_123
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

哨兵启动后,再次进入master容器,角色就变成了slave,而且其它两个节点角色也都是slave。没有角色为mater节点了。

docker exec -it 434e343e9a4e /bin/bash
root@434e343e9a4e:/data# redis-cli
127.0.0.1:6379> auth pwd_123
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.72.158
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:3649
master_link_down_since_seconds:1596963854
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7e029af3597a6390cbf2fed4b1a114c8c6a92c77
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3649
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3649

查看其中一个哨兵发现:

redis-cli -p 26379
127.0.0.1:26379> sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "192.168.72.158"
 5) "port"
 6) "6379"
 7) "runid"
 8) "3f270936377dd5132a482abb3fc4ce238f440240"
 9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "134"
19) "last-ping-reply"
20) "134"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "6669"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "46803"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "3"
33) "num-other-sentinels"
34) "3"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

num-slaves 变成三个从节点了,见鬼!!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文