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

jQuery plugin to achieve carousel effect

A jQuery plugin every day - jQuery plugin to impl...

How to convert JavaScript array into tree structure

1. Demand The backend provides such data for the ...

How to view and terminate running background programs in Linux

Linux task management - background running and te...

Build Tomcat9 cluster through Nginx and realize session sharing

Use Nginx to build Tomcat9 cluster and Redis to r...

Chrome 4.0 supports GreaseMonkey scripts

GreaseMokey (Chinese people call it Grease Monkey...

jQuery implements form validation function

jQuery form validation example / including userna...

mysql5.7.20 installation and configuration method graphic tutorial (mac)

MySQL 5.7.20 installation and configuration metho...

VMware vSAN Getting Started Summary

1. Background 1. Briefly introduce the shared sto...

WeChat applet custom tabbar component

This article shares the specific code of the WeCh...

Practical record of Vue3 combined with TypeScript project development

Table of contents Overview 1. Compositon API 1. W...

How to build nfs service in ubuntu16.04

Introduction to NFS NFS (Network File System) is ...

Apache Calcite code for dialect conversion

definition Calcite can unify Sql by parsing Sql i...

How to use flat style to design websites

The essence of a flat website structure is simpli...

Web page CSS priority is explained in detail for you

Before talking about CSS priority, we need to und...

Vue simple implementation of turntable lottery

This article shares the specific code of Vue to s...