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

How to forget the password of Jenkins in Linux

1.Jenkins installation steps: https://www.jb51.ne...

Detailed explanation of how to use Node.js to implement hot reload page

Preface Not long ago, I combined browser-sync+gul...

VMware ESXI server virtualization cluster

Table of contents summary Environment and tool pr...

Vue+element implements drop-down menu with local search function example

need: The backend returns an array object, which ...

HTML page jump and parameter transfer issues

HTML page jump: window.open(url, "", &q...

How to embed flash video format (flv, swf) files in html files

Flash file formats: .FLV and .SWF There are two ex...

JS realizes picture digital clock

This article example shares the specific code of ...

MySQL stored procedure in, out and inout parameter examples and summary

Stored Procedures 1. Create a stored procedure an...

20 JavaScript tips to help you improve development efficiency

Table of contents 1. Declare and initialize array...

Detailed explanation of the wonderful uses of SUID, SGID and SBIT in Linux

Preface Linux's file permission management is...

Solution to the failure of entering the container due to full docker space

Since the problem occurred rather suddenly and th...

A brief analysis of Linux resolv.conf

1. Introduction resolv.conf is the configuration ...

Analysis of the process of building a LAN server based on http.server

I don’t know if you have ever encountered such a ...

503 service unavailable error solution explanation

1. When you open the web page, 503 service unavai...

A super detailed Vue-Router step-by-step tutorial

Table of contents 1. router-view 2. router-link 3...