美文网首页
docker搭建redis哨兵多宿主机

docker搭建redis哨兵多宿主机

作者: 真的有神 | 来源:发表于2019-05-25 22:39 被阅读0次

    参考了该博客:https://www.jianshu.com/p/ce1d78cd368a

    1 使用docker下载redis镜像,默认下载最redis最新版本,目前版本号为5.0.0,如果需要其他版本请登录https://hub.docker.com/进行搜索

    [root@localhost ~]#  docker pull redis

    2 下载完毕后分别创建/home/docker/redis/data、redis-6379-data、redis-6380-data、redis-6381-data、sentinel-26379-data、sentinel-26380-data、sentinel-26381-data这十个文件夹

    [root@localhost ~]# cd /home[root@localhost home]# mkdir docker[root@localhost home]# cd docker[root@localhost docker]# mkdir redis[root@localhost docker]# cd redis[root@localhost redis]# mkdir redis-6379-data[root@localhost redis]# mkdir redis-6380-data[root@localhost redis]# mkdir redis-6381-data[root@localhost redis]# mkdir sentinel-26379-data[root@localhost redis]# mkdir sentinel-26380-data[root@localhost redis]# mkdir sentinel-26381-data

    3 再创建redis-6379.conf、redis-6380.conf、redis-6381.conf这三个自定义redis配置文件,用来配置redis,其中redis-6379.conf是master服务器配置文件,redis-6380.conf、redis-6381.conf 是slave服务器配置文件

    [root@localhost redis]# touch redis-6379.conf[root@localhost redis]# touch redis-6380.conf[root@localhost redis]# touch redis-6381.conf

    4 vi编辑自定义redis配置文件,复制下列代码

    4.1 redis-6379.conf文件复制:

    port 6379logfile"redis-6379.log"dir /dataappendonly yesappendfilename appendonly.aofmasterauth 123456requirepass 123456

    4.2 redis-6380.conf文件复制:

    port 6380logfile"redis-6380.log"dir /dataappendonly yesappendfilename appendonly.aofslaveof 192.168.43.188 6379masterauth 123456requirepass 123456

    4.2 redis-6381.conf文件复制:

    port 6381logfile"redis-6381.log"dir /dataappendonly yesappendfilename appendonly.aofslaveof 192.168.43.188 6379masterauth 123456requirepass 123456

    5 按Esc后,输入wq保存退出

    6 启动三个redis容器

    [root@localhost redis]# docker run -p 6379:6379 --net=host --restart=always --name redis-6379 -v /home/docker/redis/redis-6379.conf:/etc/redis/redis-6379.conf -v /home/docker/redis/redis-6379-data:/data -d redis redis-server /etc/redis/redis-6379.conf[root@localhost redis]# docker run -p 6380:6380 --net=host --restart=always --name redis-6380 -v /home/docker/redis/redis-6380.conf:/etc/redis/redis-6380.conf -v /home/docker/redis/redis-6380-data:/data -d redis redis-server /etc/redis/redis-6380.conf[root@localhost redis]# docker run -p 6381:6381 --net=host --restart=always --name redis-6381 -v /home/docker/redis/redis-6381.conf:/etc/redis/redis-6381.conf -v /home/docker/redis/redis-6381-data:/data -d redis redis-server /etc/redis/redis-6381.conf

    6.1 以下为启动命令说明和注意事项,属于延伸阅读,有时间和兴趣可以看一下,不属于配置过程,执行配置请直接跳到步骤 7

    6.1.1 命令参数:-v /home/docker/redis/redis-6379-data:/data 让我们回顾一下,刚才在自定义redis配置文件中还指定了dir/data,dir表示数据存储目录,现在回到该命令中来,该命令是将redis容器中,redis配置文件中指定的数据存储目录/data下文件的内容共享到宿主机/home/docker/redis/redis-6379-data目录下

    6.1.1.1 为何必须挂载 /data 目录?有状态容器都有数据持久化需求,在容器的生命周期内,数据持久化是持续的,包括容器在被停止后,但当容器被删除后,数据也随之被删除了,因此Docker 采用 volume (卷)的形式来向容器提供持久化存储,如果不设置该命令,数据库中的数据会默认保存在redis容器中的/data目录下,这样当执行 docker rm 容器id/容器name 命令,会丢失数据库中的数据

    7  启动redis容器以后查看redis容器状态

    [root@localhost redis]# docker ps -aCONTAINER ID        IMAGE                  COMMAND                CREATED              STATUS19fb5e8cd1b0redis"docker-entrypoint..."4seconds ago      Up2seconds

    7.1 如果STATUS状态是Up表示成功启动容器,如果STATUS状态是Exited (1) 或者 Restarting (1) 表示未能正常启动,这时候我们需要查看redis容器日志修改配置文件内容重新运行容器

    [root@localhost redis]# docker logs redis-6379|less

    7.2 查找出问题后停止redis容器

    [root@localhost redis]# docker stop redis-6379

    7.3 删除redis容器

    [root@localhost redis]# docker rm redis-6379

    7.3 重新执行步骤 6

    8 启动redis容器后,分别观察三个redis容器内部情况

    [root@localhost redis]# docker exec -it redis-6379 /bin/bashroot@zhangqian527halbin:/data# redis-cli127.0.0.1:6379>auth123456ok127.0.0.1:6379>info replication

    8.1 主机显示

    # Replicationrole:masterconnected_slaves:2slave0:ip=172.17.0.1,port=6379,state=online,offset=707,lag=0slave1:ip=172.17.0.1,port=6379,state=online,offset=707,lag=0master_replid:db3135b2f4cba8e6c1eb4be290b9dbfc2ec3b6d0master_replid2:0000000000000000000000000000000000000000master_repl_offset:707second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:707

    8.2 两台从机显示

    role:slavemaster_host:192.168.43.188master_port:6379master_link_status:upmaster_last_io_seconds_ago:8master_sync_in_progress:0slave_repl_offset:833slave_priority:100slave_read_only:1connected_slaves:0master_replid:db3135b2f4cba8e6c1eb4be290b9dbfc2ec3b6d0master_replid2:0000000000000000000000000000000000000000master_repl_offset:833second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:833

    8.3 看到主机role:master、connected_slaves:2,两台从机role:slave、master_host有地址,可以确认redis主从模式已经建立成功

    10 配置哨兵模式,主机上创建sentinel-26379.conf、sentinel-26380。c哨兵配置文件

    [root@localhost redis]# touch sentinel-26379.conf[root@localhost redis]# touch sentinel-26380.conf[root@localhost redis]# touch sentinel-26381.conf

    11 vi编辑自定义redis配置文件,复制下列代码

    复制后一定要对比原文,因为编辑器带格式,复制时容易落下部分内容,复制分后一定要对比原文!!!

    4.1 sentinel-26379.conf文件复制:

    port26379dir"/data"logfile"sentinel-26379.log"sentinel monitor mymaster192.168.43.18863792sentinel down-after-milliseconds mymaster10000sentinel failover-timeout mymaster60000sentinel auth-passmymaster123456

    4.2 sentinel-26380.conf文件复制:

    port26380dir"/data"logfile"sentinel-26380.log"sentinel monitor mymaster192.168.43.18863792sentinel down-after-milliseconds mymaster10000sentinel failover-timeout mymaster60000sentinel auth-passmymaster123456

    4.3 sentinel-26381.conf文件复制:

    port26381dir"/data"logfile"sentinel-26381.log"sentinel monitor mymaster192.168.43.18863792sentinel down-after-milliseconds mymaster10000sentinel failover-timeout mymaster60000sentinel auth-passmymaster123456

    12 启动哨兵模式:

    [root@localredis]# docker run -p 26379:26379 --restart=always --name sentinel-26379 -v/home/docker/redis/sentinel-26379.conf:/etc/redis/sentinel.conf -v /home/docker/redis/sentinel-26379-data:/data -d redis redis-sentinel /etc/redis/sentinel.conf[root@localredis]# docker run -p 26380:26380 --restart=always --name sentinel-26380 -v/home/docker/redis/sentinel-26380.conf:/etc/redis/sentinel.conf -v /home/docker/redis/sentinel-26380-data:/data -d redis redis-sentinel /etc/redis/sentinel.conf[root@localredis]# docker run -p 26381:26381 --restart=always --name sentinel-26381 -v/home/docker/redis/sentinel-26381.conf:/etc/redis/sentinel.conf -v /home/docker/redis/sentinel-26381-data:/data -d redis redis-sentinel /etc/redis/sentinel.conf

    13  启动哨兵模式以后查看哨兵容器状态

    [root@localhost redis]# docker ps -aCONTAINER ID        IMAGE                  COMMAND                CREATED              STATUS19fb5e8cd1b0redis"docker-entrypoint..."4seconds ago      Up2seconds

    13.1  如果STATUS状态是Up表示成功启动哨兵容器,如果不是请重新执行步骤 7.1

    14 至此,哨兵模式已经建立起来,查看哨兵信息

    [root@localhost redis]# docker exec -it sentinel-26379 /bin/bashroot@zhangqian527halbin:/data# redis-cli -p 26379127.0.0.1:26379>info sentinel# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.43.188:6379,slaves=2,sentinels=3

    15 可以看到,现在端口号为:6379的redis服务器是master服务器,这时我们关闭端口号为6379的redis-6379容器测试哨兵配置是否生效

    [root@localhost redis]# docker stop redis-6379

    16 然后再查看哨兵信息,哨兵模式下,当master服务器宕机之后,哨兵自动会在从slave redis服务器里面投票选举一个master服务器来,这个master服务器也可以进行读写操作

    [root@localhost redis]# docker exec -it sentinel-26379 /bin/bashroot@zhangqian527halbin:/data# redis-cli -p 26379127.0.0.1:26379>info sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.43.188:6380,slaves=2,sentinels=3

    Redis-6379.conf 配置如下

    port 6379

    logfile

    "redis-6379.log"

    dir /data

    appendonly yes

    appendfilename appendonly.aof

    slaveof172.16.249.204 6379    //从机需要配置主不需要

    masterauth

    123456

    requirepass

    123456

    sentinel-26379.conf配置文件如下

    port 26379

    dir

    "/data"

    logfile

    "sentinel-26379.log"

    sentinel myid

    3a3adf574432af64ae09f71844f54634cec86d8f

    sentinel deny-scripts-reconfig

    yes

    sentinel monitormymaster 172.16.249.205 6379 2 

    sentinel

    down-after-milliseconds mymaster 10000

    # Generated by

    CONFIG REWRITE

    sentinel

    failover-timeout mymaster 60000

    sentinel

    auth-pass mymaster 123456

    sentinel

    config-epoch mymaster 2

    sentinel

    leader-epoch mymaster 2

    sentinel

    known-replica mymaster 172.16.249.206 6379

    sentinel

    known-replica mymaster 172.16.249.204 6379

    sentinel

    known-sentinel mymaster 172.17.0.2 26379

    e612e155c1f154de582c2c024ef91921d85e5d6f

    sentinel

    known-sentinel mymaster 172.17.0.2 26379

    3a3adf574432af64ae09f71844f54634cec86d8f

    sentinel

    known-sentinel mymaster 172.17.0.2 26379

    eb1919d33ac52203e6d79787d6569327551931dc

    sentinel

    current-epoch 2

    把上边的redis.conf 、sentinel.conf 直接复制到相同的宿主机上就行了

    docker run -p 6379:6379 --net=host--restart=always --name redis-6379 -v/home/docker/redis/redis-6379.conf:/etc/redis/redis-6379.conf -v/home/docker/redis/redis-6379-data:/data -d redis redis-server/etc/redis/redis-6379.conf

    [if !supportLineBreakNewLine]

    [endif]

    docker run -p 26379:26379 --restart=always--name sentinel-26379-v/home/docker/redis/sentinel-26379.conf:/etc/redis/sentinel.conf -v/home/docker/redis/sentinel-26379-data:/data -d redis redis-sentinel/etc/redis/sentinel.conf

    相关文章

      网友评论

          本文标题:docker搭建redis哨兵多宿主机

          本文链接:https://www.haomeiwen.com/subject/ahrttctx.html