Method steps of building redis master and slave using docker

  • 2021-10-11 20:01:26
  • OfStack

1. Build an Docker environment

1. Create 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 a mirror image


docker build -t docker-test .

3. View the current image


docker iamges
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 and slave

1. Create an redis-master container


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

Parameter interpretation:

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

-t: reallocates 1 pseudo input terminal for the container, usually used in conjunction with-i

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

--name: Name the container created

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

-p: Port mapping in the format: Host (Host) Port: Container Port

--ip: Develop a fixed ip for the container

After that, specify the image used under 1 (this is the image docker-test just created)

2. View the container in operation


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 network


docker network inspect haveyb-network

3. Create redis-slave1, 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 build -t docker-test .
0

Note: Exit container ` exit `

(2) Modify the redis. conf configuration file


vi /etc/redis.conf

(3) Modify the parameter bind 127.0. 0.1 to 0.0. 0.0


bind 0.0.0.0

(4) Set the main redis password


docker build -t docker-test .
3

(5) Start the main redis


docker build -t docker-test .
4

(6) redis-cli


docker build -t docker-test .
5

5. Configure redis-slave1

(1) Enter the redis-slave1 container


docker build -t docker-test .
6

(2) Modify the redis. conf configuration file


vi /etc/redis.conf

(3) Modify 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 password is set for the main redis, this parameter is required to verify the right for the connection from redis)


docker build -t docker-test .
9

(5) Set slaveof (set ip and port of main 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 redis-slave1

7. Execute ` info replication ` in redis-cli to view 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. Writing key in redis-master, redis-slave1, and redis-slave2 is already available


Related articles: