How Docker configures redis sentinel mode (on multiple servers)

Time:2022-9-23

foreword

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.

condition

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

  • Server 1: 8.131.78.18
  • Server 2: 8.131.69.106
  • Server 3: 8.131.71.196
  • 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 \
   docker-engine
# 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 \
 https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.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 https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.13-3.1.el7.x86_64.rpm
# 3.6 Installation
yum install docker-ce docker-ce-cli containerd.io
# 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 containerd.io
rm -rf /var/lib/docker

# 4. Configure the accelerator
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
 "registry-mirrors": ["https://qdxc3615.mirror.aliyuncs.com"]
}
EOF
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
EXPOSE 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 127.0.0.1 
# bind 127.0.0.1

port 7000

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

pidfile /var/run/redis_7000.pid

# 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 master.host.ip port
slaveof 8.131.78.18 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
role:slave
master_host:8.131.78.18
master_port:7000
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:28
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:438c700922a30ebbc66ee5c89a176c426924b2a7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

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!