How Docker configures redis sentinel mode (on multiple servers)



The redis I learned before has always been running on a single server and deployed directly on the server. I heard that using docker will make the configuration process easier(not at all)This time, on the basis of Docker, Redis is configured with one master, one slave and three sentinels. This article is to configure data nodes, that is, one master and one slave.


Three servers (because at least three sentries are required to ensure security) [can be rented on Alibaba Cloud for a few hours]

  • Server 1:
  • Server 2:
  • Server 3:
  • Port numbers 7000 and 17000 have been released on the security group (Alibaba Cloud)
  • Environment: centos8.0

Install Docker

Execute the following instructions line by line:

# 1. Update the build environment
yum -y install gcc

# 2. Same as above
yum -y install gcc-c++

# 3. Install docker
# 3.1 Uninstall the 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 packages
yum install -y yum-utils
# 3.3 Set up the mirror warehouse, it is recommended to use the domestic mirror, which is faster
yum-config-manager \
 --add-repo \
# 3.4 Update the yum package index, the function is to cache the package locally
yum makecache 
# 3.5 Install docker-ce community edition related to docker
yum install -y
# 3.6 Installation
yum install docker-ce docker-ce-cli
# 3.7 Start docker
systemctl start docker
# 3.8 Set to start in the background
systemctl enable docker 
# 3.9 Use docker version to see if it is successful
docker version
# 3.10 Testing (optional)
docker run hello-world
# 3.11 Uninstall and delete (for reference only, not done this time)
yum remove docker-ce docker-ce-cli
rm -rf /var/lib/docker

# 4. Configure the accelerator
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
 "registry-mirrors": [""]
sudo systemctl daemon-reload
sudo systemctl restart docker

Configure redis master and slave nodes

The servers we use are server 1 and server 3 (it was originally one master and two slaves, I turned off server 2).

TIPS: For xshell, you can use the right button to select the send key input to all sessions, so that you don’t need to configure it twice. Or use the 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 the home path, otherwise it is easy to cause the container to fail due to permission problems. The directory I created is /blue/redis02, and the instructions are relatively simple:

cd /
mkdir blue && cd blue
mkdir redis02 && cd redis02

2. Create a Dockerfile, the Dockerfile is to load some of the environments we need.

vi Dockerfile

# Dockerfile content is as follows:
# Inherit from base image redis
FROM redis
MAINTAINER blue<[email protected]>

# Copy the files in the directory where the Dockerfile is located to the container /usr/local/etc/redis directory
COPY redis.conf /usr/local/etc/redis/redis.conf

# install some environments
RUN apt-get update
RUN apt-get install -y vim
RUN apt-get install procps --assume-yes

VOLUME ["/data"]

WORKDIR /etc/redis

# open port 7000

# start with config file
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf"]

3. Download a redis.conf from the official website and make the following modifications. redis.conf is some configuration of redis, which is basically the same as the configuration of single-machine redis, one master, one slave and three sentinels.

# Comment out bind 
# bind

port 7000

# If yes, it will affect the startup using the configuration file
daemonize no

pidfile /var/run/

# Need to set the following two passwords, the passwords must be the same
requirepass testmaster123
masterauth testmaster123

# Modify the protected mode, if yes, the external server will be inaccessible
protected-mode no

# For server 3, you also need to add a sentence, indicating that it is the slave server of server 1 
# slaveof port
slaveof 7000

4. After saving the above configuration, you can use the following statement to create an image. This process takes a long time and requires patience:

# You should use docker build -t myredis . It is more recognizable, but it was successful as a test, and I don't want to modify it. This sentence is to create a new image according to our Dockerfile. You can push him to the warehouse, so that you can pull it directly next time.
docker build -t test .

5. Start the container with the following statement:

# start the container
docker run -d -p 7000:7000 --name redis-test test

# The following statement shows the running container
docker ps

# Enter inside the container
docker exec -it redis-test /bin/bash

6. Inside the container, you can use the following statement to view the running thread:

# View running threads
ps -ef

# Use the redis client to access port 7000
redis-cli -p 7000

# enter password
auth testmaster123

# View information
info replication

# The following is the information returned from the node
# Replication

At this point, the master-slave node configuration is successful.

So far, this article about Docker configuration redis sentinel mode (on multiple servers) is introduced. For more information on Docker configuration redis sentinel mode, please search for previous articles by developpaer or continue to browse the related articles below. I hope you will support more in the future. developpaer!