发布于2021-03-14 06:04 阅读(958) 评论(0) 点赞(11) 收藏(4)
执行命令
我的官方群点击此处。
docker pull redis
启动三台机器,6379 容器作为主节点,其余作为从节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ├── conf │ ├── redis_6379.conf │ ├── redis_6380.conf │ ├── redis_6381.conf │ ├── sentinel_26379.conf │ ├── sentinel_26380.conf │ └── sentinel_26381.conf ├── data_6379 │ ├── appendonly.aof │ └── dump.rdb ├── data_6380 │ └── dump.rdb ├── data_6381 │ └── dump.rdb └── scripts ├── run.sh └── sentinel.sh |
下载配置文件,具体配置另行调整即可
1 | https: //raw.githubusercontent.com/antirez/redis/5.0/redis.conf |
1 2 3 4 5 | port 26379 dir "/etc/redis" sentinel monitor mymaster 172.17.0.2 6379 1 sentinel down-after-milliseconds mymaster 60000 sentinel failover-timeout mymaster 180000 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #!/usr/bin/env bash set -e # 脚本当前目录 cPath=$(cd $(dirname "$0" ) || exit; pwd) # 根目录 dirPath=$(dirname "$cPath" ) # 获取端口 port= "$1" if [[ ! "$port" ]]; then port=6379 fi # 创建数据目录 mkdir -p "$dirPath" /data_ "$port" # 删除已启动服务 containerId=$(docker ps -a | grep "redis_$port" | awk -F ' ' '{print $1}' ) if [[ "$containerId" ]]; then echo "正在删除服务:${containerId}" docker rm -f ${containerId} > /dev/ null fi # 启动服务 docker run -itd --privileged= true -p "$port" :6379 --name redis_ "$port" \ -v= "$dirPath" /conf/redis_ "$port" .conf:/etc/redis/redis.conf \ -v= "$dirPath" /data_ "$port" :/data \ redis \ redis-server /etc/redis/redis.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #!/usr/bin/env bash set -e # 脚本当前目录 cPath=$(cd $(dirname "$0" ) || exit; pwd) # 根目录 dirPath=$(dirname "$cPath" ) # 设置端口 port=$1 if [[ ! "$port" ]]; then port=26379 fi # 删除已启动服务 containerId=$(docker ps -a | grep "sentinel_$port" | awk -F ' ' '{print $1}' ) if [[ "$containerId" ]]; then echo "正在删除服务:${containerId}" docker rm -f ${containerId} > /dev/ null fi # 启动服务 docker run -itd --privileged= true -p "$port" :26379 --name "sentinel_$port" \ -v= "${dirPath}/conf/sentinel_${port}.conf" :/etc/redis/sentinel.conf \ -w=/etc/redis/ \ redis \ redis-server /etc/redis/sentinel.conf --sentinel |
启动 sentinel 服务如果如下错误
1 2 | 1:X 10 May 2020 08:10:48.234 # 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 10 May 2020 08:10:48.235 # Sentinel config file /etc/redis/sentinel.conf is not writable: Permission denied. Exiting... |
sentinel.sh
脚本1 | docker run -it --rm --privileged= true -v=/root/docker-config/redis/conf/:/etc/redis/ redis chown -R redis /etc/redis |
主要语句:==chown -R redis /etc/redis==
1 2 3 4 5 6 7 8 | root@DESKTOP-Q13EI52:~/docker-config/redis# docker run -it --rm --privileged= true -v=/root/docker-config/redis/conf/:/etc/redis/ redis ls -l /etc/redis/ total 204 -rw-r--r-- 1 redis root 61793 May 10 07:41 redis_6379.conf -rw-r--r-- 1 redis root 61864 May 10 12:52 redis_6380.conf -rw-r--r-- 1 redis root 61890 May 10 12:52 redis_6381.conf -rw-r--r-- 1 redis root 459 May 10 12:52 sentinel_26379.conf -rw-r--r-- 1 redis root 161 May 10 07:56 sentinel_26380.conf -rw-r--r-- 1 redis root 161 May 10 10:16 sentinel_26381.conf |
1 | sh scripts/run.sh 6379 |
1 2 | sh scripts/run.sh 6380 sh scripts/run.sh 6381 |
1 2 3 4 5 6 7 | docker exec -it redis_6380 bash docker exec -it redis_6381 bash # 进入容器后执行命令 redis-cli # 设置为从服务 slaveof 172.17.0.2 6379 |
效果图
1 | sh scripts/sentinel.sh |
效果图
1 2 3 4 5 6 | root@DESKTOP-Q13EI52:~/docker-config/redis# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e518580ce8eb redis "docker-entrypoint.s…" About a minute ago Up About a minute 6379/tcp, 0.0.0.0:26379->26379/tcp sentinel_26379 e2ef253d3513 redis "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:6381->6379/tcp redis_6381 15fdc81eb530 redis "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:6380->6379/tcp redis_6380 77f4fdc84cb1 redis "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:6379->6379/tcp redis_6379 |
执行命令:docker stop redis_6379
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1:X 10 May 2020 12:52:15.634 # +sdown master mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:15.634 # +odown master mymaster 172.17.0.2 6379 #quorum 1/1 1:X 10 May 2020 12:52:15.634 # + new -epoch 1 1:X 10 May 2020 12:52:15.634 # + try -failover master mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:15.649 # +vote- for -leader 6905a137af7baedbdfce4978d1d9126fdaad4faf 1 1:X 10 May 2020 12:52:15.649 # +elected-leader master mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:15.649 # +failover-state- select -slave master mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:15.725 # +selected-slave slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:15.725 * +failover-state-send-slaveof-noone slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:15.781 * +failover-state-wait-promotion slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:16.798 # +promoted-slave slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:16.798 # +failover-state-reconf-slaves master mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:16.845 * +slave-reconf-sent slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:17.837 * +slave-reconf-inprog slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:17.837 * +slave-reconf-done slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.2 6379 1:X 10 May 2020 12:52:17.937 # +failover-end master mymaster 172.17.0.2 6379 # 生成执行故障转移 1:X 10 May 2020 12:52:17.937 # + switch -master mymaster 172.17.0.2 6379 172.17.0.3 6379 1:X 10 May 2020 12:52:17.938 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.3 6379 1:X 10 May 2020 12:52:17.938 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.3 6379 1:X 10 May 2020 12:53:18.010 # +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.3 6379 |
注意
已经掉线的 172.17.0.2:6379
主节点重新上线后将自动切换为从服务
更多学习内容请访问:
腾讯T3-T4标准精品PHP架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)
作者:春天的故事
链接:http://www.phpheidong.com/blog/article/3487/937c3e83050c57f55221/
来源:php黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!