docker 下快速部署Redis哨兵集群

本文参考:

https://www.cnblogs.com/boshen-hzb/p/10471665.html

文章中的redis集群没有设置密码,我们这里做一版带密码的

说明:

单节点,伪集群,1master,2slave,2哨兵的示例

1.拉取镜像:

docker pull registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:3
docker pull registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3

2.创建数据文件夹

mkdir -p /home/redis-sentinel-cluster/data/master
mkdir -p /home/redis-sentinel-cluster/data/slave1
mkdir -p /home/redis-sentinel-cluster/data/slave2
mkdir -p /home/redis-sentinel-cluster/data/sentinel-1
mkdir -p /home/redis-sentinel-cluster/data/sentinel-2

3.编辑配置文件

哨兵的配置文件:(这里跟参考文章中不同的是加入了auth-pass参数,用来连接带密码的redis集群)

其中sentinel auth-pass myredismaster redispassword的后两个参数分别为redis cluser name 和 password,一般项目在配置连接的时候会使用到这两个参数

vim /home/redis-sentinel-cluster/sentinel-1.conf

port 26379

dir "/data"
sentinel monitor myredismaster 192.168.2.104 16379 2
sentinel auth-pass myredismaster redispassword
sentinel down-after-milliseconds myredismaster 5000
sentinel failover-timeout myredismaster 5000
sentinel parallel-syncs myredismaster 1

第二个哨兵实例:

vim /home/redis-sentinel-cluster/sentinel-2.conf

port 26380

dir "/data"
sentinel monitor myredismaster 192.168.2.104 16379 2
sentinel auth-pass myredismaster redispassword
sentinel down-after-milliseconds myredismaster 5000
sentinel failover-timeout myredismaster 5000
sentinel parallel-syncs myredismaster 1

如果哨兵的实例启动报文件权限问题,把这两个配置文件权限设为777可以解决,建议提前先设置好权限再启动

1)monitor 指的是初始化的监控主切点,ip和端口,后面的数字2代表,必须2个sentinel才能判断主节点是否失败

2)down-after-milliseconds 指的是超过5000秒,且没有回复,则判定主节点不可达

3)failover-timeout 指的是故障转移时间

4)parallel-syncs 指的是故障转移到新的主节点时,从节点的复制节点数量

  1. 创建 /home/redis-sentinel-cluster/docker-compose.yml

    (这里跟原文比,添加了--requirepass参数设置密码,slave还添加了--masterauth来与master通信)

    version: '2.1'
    services:
      master:
        image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3
        container_name: master
        restart: always
        network_mode: "host"
        volumes:
          - ./data/master:/data
        command: redis-server --port 16379 --requirepass redispassword
      slave1:
        image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3
        container_name: slave1
        restart: always
        network_mode: "host"
        volumes:
          - ./data/slave1:/data
        command: redis-server --port 16380 --slaveof 192.168.2.104 16379 --requirepass redispassword --masterauth redispassword
      slave2:
        image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3
        container_name: slave2
        restart: always
        network_mode: "host"
        volumes:
          - ./data/slave2:/data
        command: redis-server --port 16381 --slaveof 192.168.2.104 16379 --requirepass redispassword --masterauth redispassword
      sentinel-1:
        image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:3
        container_name: sentinel-1
        restart: always
        network_mode: "host"
        volumes:
          - ./sentinel-1.conf:/etc/redis/sentinel.conf
          - ./data/sentinel-1:/data
      sentinel-2:
        image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:3
        container_name: sentinel-2
        restart: always
        network_mode: "host"
        volumes:
          - ./sentinel-2.conf:/etc/redis/sentinel.conf
          - ./data/sentinel-2:/data
    
  1. 启动redis集群

    cd /home/redis-sentinel-cluster
    docker-compose up -d
    
[root@localhost redis-sentinel-cluster]# docker ps
CONTAINER ID        IMAGE                                                          COMMAND                  CREATED             STATUS              PORTS               NAMES
04a9cc388d19        registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3            "docker-entrypoint.s…"   20 minutes ago      Up 16 minutes                           slave1
958aac29b019        registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3            "docker-entrypoint.s…"   20 minutes ago      Up 16 minutes                           slave2
bb671925d543        registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3            "docker-entrypoint.s…"   36 minutes ago      Up 16 minutes                           master
9f2ae4a38042        registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:3   "sentinel-entrypoint…"   About an hour ago   Up 16 minutes                           sentinel-2
d34ed9e217bd        registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:3   "sentinel-entrypoint…"   About an hour ago   Up 16 minutes                           sentinel-1

可以看到都起来了

查看哨兵的配置文件,会自动的抓取集群信息并修改配置文件:

[root@localhost redis-sentinel-cluster]# cat sentinel-1.conf
port 26379

dir "/data"
sentinel myid d0940e2c477e49e131f933e379623ed43fba8e8c
sentinel monitor myredismaster 192.168.2.104 16379 2
sentinel down-after-milliseconds myredismaster 5000
sentinel failover-timeout myredismaster 5000
sentinel auth-pass myredismaster redispassword

# Generated by CONFIG REWRITE
sentinel config-epoch myredismaster 0
sentinel leader-epoch myredismaster 0
sentinel known-slave myredismaster 192.168.2.104 16381
sentinel known-slave myredismaster 192.168.2.104 16380
sentinel known-sentinel myredismaster 192.168.2.104 26380 1f179ed754c2f7f30a36221bdcef0462d7347bc2
sentinel current-epoch 0
  1. 验证集群

    哨兵实例状态:

    [root@localhost redis-sentinel-cluster]# docker exec -it sentinel-1 redis-cli -h 192.168.2.104 -p 26379
    192.168.2.104:26379> info
    # Server
    redis_version:3.2.12
    redis_git_sha1:00000000
    redis_git_dirty:0
    redis_build_id:b0df607ad3315254
    redis_mode:sentinel
    os:Linux 3.10.0-1062.el7.x86_64 x86_64
    arch_bits:64
    multiplexing_api:epoll
    gcc_version:6.3.0
    process_id:1
    run_id:ef2aaab897c0ffd0e37cabbdd6c3a17d841a8195
    tcp_port:26379
    uptime_in_seconds:1195
    uptime_in_days:0
    hz:10
    lru_clock:13176031
    executable:/data/redis-server
    config_file:/etc/redis/sentinel.conf
    
    # Clients
    connected_clients:2
    client_longest_output_list:0
    client_biggest_input_buf:0
    blocked_clients:0
    
    # CPU
    used_cpu_sys:2.85
    used_cpu_user:1.02
    used_cpu_sys_children:0.00
    used_cpu_user_children:0.00
    
    # Stats
    total_connections_received:7804
    total_commands_processed:9530
    instantaneous_ops_per_sec:2
    total_net_input_bytes:631707
    total_net_output_bytes:283491
    instantaneous_input_kbps:0.17
    instantaneous_output_kbps:0.01
    rejected_connections:0
    sync_full:0
    sync_partial_ok:0
    sync_partial_err:0
    expired_keys:0
    evicted_keys:0
    keyspace_hits:0
    keyspace_misses:0
    pubsub_channels:0
    pubsub_patterns:0
    latest_fork_usec:0
    migrate_cached_sockets:0
    
    # Sentinel
    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    sentinel_simulate_failure_flags:0
    master0:name=myredismaster,status=ok,address=192.168.2.104:16379,slaves=2,sentinels=2
    

master状态

192.168.2.104:26379> sentinel masters
1)  1) "name"
    2) "myredismaster"
    3) "ip"
    4) "192.168.2.104"
    5) "port"
    6) "16379"
    7) "runid"
    8) "43addeccdf587cc939c0277f4455f84f09ac9e88"
    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) "791"
   19) "last-ping-reply"
   20) "791"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "9026"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "1203568"
   29) "config-epoch"
   30) "0"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "1"
   35) "quorum"
   36) "2"
   37) "failover-timeout"
   38) "5000"
   39) "parallel-syncs"
   40) "1"

slave状态

192.168.2.104:26379> sentinel slaves myredismaster
1)  1) "name"
    2) "192.168.2.104:16381"
    3) "ip"
    4) "192.168.2.104"
    5) "port"
    6) "16381"
    7) "runid"
    8) "be510b55c5589c0a75aba298d3a00a246ee463c5"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "131"
   19) "last-ping-reply"
   20) "131"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "8730"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "1203243"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.2.104"
   35) "master-port"
   36) "16379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "190133"
2)  1) "name"
    2) "192.168.2.104:16380"
    3) "ip"
    4) "192.168.2.104"
    5) "port"
    6) "16380"
    7) "runid"
    8) "16fadcd2386524c9ee8047f52855c2bf6230d3cd"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "130"
   19) "last-ping-reply"
   20) "130"
   21) "down-after-milliseconds"
   22) "5000"
   23) "info-refresh"
   24) "8730"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "1203242"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.2.104"
   35) "master-port"
   36) "16379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "190133"

至此,基于docker的Redis哨兵集群部署完成

你可能感兴趣的:(docker 下快速部署Redis哨兵集群)