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哨兵集群部署完成

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,386评论 6 479
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,939评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,851评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,953评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,971评论 5 369
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,784评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,126评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,765评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,148评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,744评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,858评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,479评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,080评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,053评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,278评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,245评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,590评论 2 343