How to configure redis sentinel mode in Docker (on multiple servers)

How to configure redis sentinel mode in Docker (on multiple servers)

Preface

The redis I learned before has always been running on a single server and was deployed directly on the server. I heard that using Docker will make the configuration process easier (not true). This time, I configured Redis with one master, one slave and three sentinels based on Docker. This article is about configuring data nodes, that is, two nodes, one master and one slave.

condition

Three servers (because at least three sentinels 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 in the security group (Alibaba Cloud)
  • Environment: centos8.0

Install Docker

Execute the following instructions line by line:

# 1. Update the compilation 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 package yum install -y yum-utils
# 3.3 Set up the mirror warehouse. It is recommended to use domestic mirrors, which are 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 version 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 Install 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 check whether it is successful
# 3.10 Test (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 (originally there was one master and two slaves, but I shut down server 2).

TIPS: For xshell, you can right-click and select the send key to input it to all sessions, so there is no need to configure it twice. Or use the scp statement to share the configured file to other servers:

# scp local_file remote_username@remote_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 the container may fail to run due to permission issues. 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, which is used to load some environments we need.

vi Dockerfile

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

# Copy the files in the directory where 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 CMD using the configuration file [ "redis-server", "/usr/local/etc/redis/redis.conf"]

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

# Comment out bind 127.0.0.1 
# bind 127.0.0.1

port 7000

# If yes here, it will affect the use of configuration files to start daemonize no

pidfile /var/run/redis_7000.pid

# You need to set the following two passwords, and the passwords must be consistent requirepass testmaster123
masterauth testmaster123

# Modify the protection mode. If it is yes, external servers will be inaccessible. protected-mode no

# For server 3, you need to add a sentence to indicate that it is the slave server of server 1 # slaveof host.machine.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, so you need to wait patiently:

# You should use docker build -t myredis . It is more recognizable, but it succeeded when I tested it, so I don't want to change it. This sentence creates a new image based on our Dockerfile. You can push it to the warehouse so that you can pull it directly next time.
docker build -t test .

5. Start the container using the following statement:

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

# The following statement views the running container docker ps

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

6. You can use the following statement inside the container to view the running threads:

# View the running threads ps -ef

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

# Enter the 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 and slave nodes are configured successfully.

This is the end of this article about Docker configuration redis sentinel mode (on multiple servers). For more information about Docker configuration redis sentinel mode, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to build a redis cluster using docker
  • Implementation of Redis master-slave cluster based on Docker
  • Building a Redis cluster on Docker
  • How to deploy stand-alone Pulsar and clustered Redis using Docker (development artifact)
  • Use Docker to build a Redis master-slave replication cluster
  • Example of how to quickly build a Redis cluster with Docker
  • Implementation of docker redis5.0 cluster cluster construction
  • Implementation of Redis one master, two slaves and three sentinels based on Docker
  • Teach you how to build Redis cluster mode and sentinel mode with docker in 5 minutes

<<:  Implementation of Vue counter

>>:  A brief discussion on which fields in Mysql are suitable for indexing

Recommend

Tutorial on installing mysql8 on linux centos7

1. RPM version installation Check if there are ot...

Vue implements simple production of counter

This article example shares the simple implementa...

When should a website place ads?

I recently discussed "advertising" with...

What is the base tag and what does it do?

The <base> tag specifies the default addres...

The difference between Vue interpolation expression and v-text directive

Table of contents 1. Use plugin expressions 2. Us...

Implementation steps for installing FTP server in Ubuntu 14.04

Table of contents Install Software Management Ano...

Detailed explanation of HTML page header code example

Knowledge point 1: Set the base URL of the web pa...

A colorful cat under Linux

Friends who have used the Linux system must have ...

Install and build a server environment of PHP+Apache+MySQL on CentOS

Yum (full name Yellow dog Updater, Modified) is a...

Mysql slow query optimization method and optimization principle

1. For comparison of date size, the date format p...

Detailed description of ffmpeg Chinese parameters

FFMPEG 3.4.1 version parameter details Usage: ffm...

MySQL Error 1290 (HY000) Solution

I struggled with a problem for a long time and re...

Detailed explanation of the solution to image deformation under flex layout

Flex layout is a commonly used layout method nowa...

How to start a Vue.js project

Table of contents 1. Node.js and Vue 2. Run the f...

Causes and solutions for front-end exception 502 bad gateway

Table of contents 502 bad gateway error formation...