How to use docker to build redis master-slave

How to use docker to build redis master-slave

1. Build a Docker environment

1. Create a Dockerfile

FROM centos:latest
RUN groupadd -r redis && useradd -r -g redis redis
RUN yum -y update && yum -y install epel-release && yum -y install redis && yum -y install net-tools
EXPOSE 6379

2. Build an image

docker build -t docker-test .

3. View the current image

docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-test latest ccebd30e466a 12 minutes ago 396MB
centos latest 470671670cac 7 weeks ago 237MB

4. View the default network type of docker

docker network ls
NETWORK ID NAME DRIVER SCOPE
a43e79987e98 bridge bridge local
6b73067403dc host host local
b8ad4981e57e none null local

5. Create a custom network type

docker network create --subnet=172.10.0.0/16 haveyb-network

2. Build Redis master-slave

1. Create a redis-master container

docker run -itd --name redis-master --net haveyb-network -p 6380:6379 --ip 172.10.0.2 docker-test

Parameter explanation:

-i: Run the container in interactive mode, usually used with -t

-t: reallocate a pseudo input terminal for the container, usually used with -i

-d: Run the container in the background and return the container ID;

--name: Name the created container

--net: Specify the network mode (here specify the custom network mode just created)

-p: port mapping, the format is: host port: container port

--ip: Set a fixed ip for the container

Then specify the image to be used (the image docker-test just created is used here)

2. View the running container

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
dc9344bbd25f docker-test "/bin/bash" 2 minutes ago
 
STATUS PORTS NAMES
Up 2 minutes 0.0.0.0:6380->6379/tcp redis-master

Note: View the IP address of a container under a certain network

docker network inspect haveyb-network

3. Create redis-slave1 and redis-slave2 containers

docker run -itd --name redis-slave1 --net haveyb-network -p 6381:6379 --ip 172.10.0.3 docker-test
docker run -itd --name redis-slave2 --net haveyb-network -p 6382:6379 --ip 172.10.0.4 docker-test

4. Configure the redis-master container

(1) Enter the redis-master container

docker exec -it redis-master bash

Note: Exit the container `exit`

(2) Modify the redis.conf configuration file

vi /etc/redis.conf

(3) Change the parameter bind 127.0.0.1 to 0.0.0.0

bind 0.0.0.0

(4) Set the master redis password

requirepass YourPasswordSettings

(5) Start the main redis

redis-server /etc/redis.conf &

(6) redis-cli

redis-cli
auth yourPasswordSettings

5. Configure redis-slave1

(1) Enter the redis-slave1 container

docker exec -it redis-slave1 bash

(2) Modify the redis.conf configuration file

vi /etc/redis.conf

(3) Change the parameter bind 127.0.0.1 to 0.0.0.0

bind 0.0.0.0

(4) Set masterauth and add the following code (after the master redis sets the password, this parameter is required to verify the connection from the redis)

masterauth yourPasswordSettings

(5) Set slaveof (set the IP and port of the master redis)

slaveof 172.10.0.2 6379

(5) Start from redis

redis-server /etc/redis.conf &

(6) redis client

redis-cli

6. Configure redis-slave2

Same configuration as redis-slave1

7. Execute `info replication` in redis-cli to view the master-slave information

redis-master

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.10.0.3,port=6379,state=online,offset=3105,lag=1
slave1:ip=172.10.0.4,port=6379,state=online,offset=3105,lag=1
master_replid:a3a43b1957bc5b9f18cb3004301990085e49b0d1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3105
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3105
127.0.0.1:6379>

redis-slave1

127.0.0.1:6379> info replication 
# Replication
role:slave
master_host:172.10.0.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago: 1
master_sync_in_progress:0
slave_repl_offset:3203
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a3a43b1957bc5b9f18cb3004301990085e49b0d1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3203
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3203
127.0.0.1:6379>

8. Write the key in redis-master, redis-slave1 and redis-slave2 can already obtain it

This is the end of this article about the steps to use docker to build redis master-slave. For more relevant content about using docker to build redis master-slave, 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:
  • Implementation of Redis master-slave cluster based on Docker
  • Detailed explanation of Redis master-slave replication practice using Docker
  • Use Docker to build a Redis master-slave replication cluster
  • CentOS 6 uses Docker to deploy redis master-slave database operation example
  • Detailed explanation of the master-slave configuration tutorial of redis under Docker
  • Example practice of building Redis master-slave + sentinel based on Docker

<<:  A brief discussion on size units in CSS

>>:  Class in front-end JavaScript

Recommend

HTML cellpadding and cellspacing attributes explained in pictures

Cell -- the content of the table Cell margin (tabl...

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...

Detailed explanation of TIMESTAMPDIFF case in MySQL

1. Syntax TIMESTAMPDIFF(unit,begin,end); Returns ...

What are the differences between var let const in JavaScript

Table of contents 1. Repeated declaration 1.1 var...

Docker learning method steps to build ActiveMQ message service

Preface ActiveMQ is the most popular and powerful...

JavaScript to implement limited time flash sale function

This article shares the specific code of JavaScri...

Javascript operation mechanism Event Loop

Table of contents 1. Four concepts 1. JavaScript ...

CentOS IP connection network implementation process diagram

1. Log in to the system and enter the directory: ...

Install centos7 virtual machine on win10

1. Download VMware Workstation 64 version https:/...

Application of HTML and CSS in Flash

Application of HTML and CSS in Flash: I accidental...

Differences in the hr separator between browsers

When making a web page, you sometimes use a dividi...

Discussion on Web Imitation and Plagiarism

A few months after entering the industry in 2005, ...

Pitfall notes of vuex and pinia in vue3

Table of contents introduce Installation and Usag...

Solve the problem of mysql's int primary key self-increment

Introduction When we use the MySQL database, we a...