Multiple servers use docker to set up one master, one slave and three sentinel redis (complete)

Blue bulrush 2020-11-09 22:37:57
multiple servers use docker set

Should have continued the previous blog Docker To configure redis Sentinel mode —— Multiple servers · On Write the next one , But suddenly realized that the necessary environment should be packaged as a basic mirror image , On this basis, establish and redis About the mirror image , This allows you to quickly pack , And can highly reuse code .

So this article is actually equivalent to a complete multi server use Docker To deploy Redis The process of sentinel mode .


  1. Server conditions
  2. install Docker
  3. Making the basic image
  4. To configure redis Master slave node
  5. The sentinel node

Server conditions

Because my server is new ( Daily rent ) Of , So we need to recover .

Three servers ( Because at least three sentinels are needed to ensure security )

  • The server 1:
  • The server 2:
  • The server 3:
  • Port number 7000、17000 Released on security team ( Alibaba cloud )
  • Environmental Science :centos8.0

install Docker

Execute the following instructions line by line :

# 1. Update compilation environment
yum -y install gcc
# 2. ditto
yum -y install gcc-c++
# 3. install docker
# 3.1 Uninstall old version
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
# 3.2 Install the required installation package
yum install -y yum-utils
# 3.3 Set up the warehouse for the image , It is recommended to use domestic mirror image , Faster
yum-config-manager \
--add-repo \
# 3.4 to update yum Package index , The function is to store the package locally
yum makecache
# 3.5 install docker dependent docker-ce Community Edition
yum install -y
# 3.6 install
yum install docker-ce docker-ce-cli
# 3.7 start-up docker
systemctl start docker
# 3.8 Set to start in the background
systemctl enable docker
# 3.9 Use docker version Check to see if success
docker version
# 3.10 test ( Can not do )
docker run hello-world
# 3.11 Uninstall delete ( For reference only , Don't do it this time )
yum remove docker-ce docker-ce-cli
rm -rf /var/lib/docker
# 4. Configuration accelerator
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
"registry-mirrors": [""]
sudo systemctl daemon-reload
sudo systemctl restart docker

Making the basic image

docker Warehouse centos The mirror image is very simple , There's a lack of functionality , It's not convenient for us to install and debug , So we put it with redis It doesn't matter in itself, but helps us package the parts we use into a common base image .

The main installation is vim Editor and procps( View the processes running in the container ) So create a directory for basecentos, Create and write in it Dockerfile as follows :

# establish Dockerfile
vi Dockerfile
# All of the following codes are Dockerfile The content of
FROM redis
MAINTAINER blue<[email protected]>
# Install some environment
RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install procps --assume-yes

preservation Dockerf After that, you can create a name called base-centos Mirror image , In the future, you can generate new containers directly from this image , You don't have to download all the time . Steps are as follows :

docker build -t base-centos .
# Look at the existing image :
[[email protected] baseimage]# docker images
base-centos latest 646e329bb8db 2 minutes ago 157MB
# Use docker login Log in to your docker account number

Take it push To our dockerhub On , On the other two servers pull Come down , Now all three servers have this image , I named him lanblue/mygoodcentos.

To configure redis Master slave node

The server we use is the server 1 And the server 2.

TIPS: about xshell You can use the right-click to select the send key to input to all sessions , So you don't need to configure it twice . Or use scp Statement to share the configured file to other servers :

# scp local_file [email protected]_ip:remote_folder
scp redis.conf [email protected]:/blue/redis02
  1. First create a folder , Be careful not to put it in home Under the path , Otherwise, it is easy for the container to fail because of the permission problem . The directory I created is /blue/redis01, The instructions are simple :
cd /
mkdir blue && cd blue
mkdir redis01 && cd redis01
  1. establish Dockerfile, The contents are as follows :
FROM lanblue/mygoodcentos
MAINTAINER blue<[email protected]>
COPY redis.conf /usr/local/etc/redis/redis.conf
VOLUME ["/data1"]
WORKDIR /etc/redis
# Open ports 7000
# Use the configuration file to start
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf"]
  1. Download one from the official website redis.conf Put it in redis01 Under the table of contents ,redis.conf It is redis Some configuration of , This is the same as the previous stand-alone version of redis The configuration of one master and one slave Sentry is basically the same , Make the following changes :
# Comment out bind
# bind
port 7000
# If this is yes Will affect the use of configuration files to start
daemonize no
pidfile /var/run/
# You need to set the following two passwords , The password should be consistent
requirepass testmaster123
masterauth testmaster123
# Change protection mode , If it is yes Will cause the external server to be inaccessible
protected-mode no
# For servers 2, It needs to be added that , It means a server 1 From the server
# slaveof Lord . machine .I.P port
slaveof 7000
  1. Create images and run containers :
# create mirror
docker build -t myredis .
# Start the container
docker run -d -p 7000:7000 --name redis-test myredis
# Enter the inside of the container
docker exec -it redis-test /bin/bash
# View the processes in the container
ps -ef
  1. test :

[email protected]:/etc/redis# redis-cli -p 7000> auth testmaster123
OK> info replication

The sentinel node

stay /blue Create folder sentinel01. It's stored in it Dockerfile and sentinel.conf file .

  1. Dockerfile The contents of the document are as follows :
FROM lanblue/mygoodcentos
MAINTAINER blue<[email protected]>
COPY sentinel.conf /usr/local/etc/redis/sentinel.conf
# VOLUME ["/data2"]
# Open ports 17000
EXPOSE 17000
# Use the configuration file to start
CMD [ "redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]
  1. sentinel.conf The documents are as follows , Note that you can't set daemonize yes, Will cause our specified configuration to fail :
port 17000
sentinel monitor mymaster 7000 2
sentinel auth-pass mymaster testmaster123
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
# Set to no Will be discovered by other hosts
protected-mode no
  1. create mirror , Start the container , Enter the inside of the container :
# create mirror
docker build -t mysentinel .
# Start the container
docker run -d -p 17000:17000 --name sentinel-test mysentinel
# Enter the inside of the container
docker exec -it sentinel-test /bin/bash
# visit 17000 port
redis-cli -p 17000
# Check out the sentry
# Here is the information returned by the port
# Sentinel
# The sentinel node will automatically discover other sentinels , If some of the sentry threads have failed , You can update it with the following statement> SENTINEL RESET *
(integer) 1
  1. Master slave switch test
    Use docker stop Statement can close a running container . After closing the container where the master node is located, you can observe that the slave node becomes master. The switch is basically the same as the previous stand-alone version .
本文为[Blue bulrush]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云