Deploy redis master-slave configuration in docker

Environmental description:

  • Alibaba Cloud Server
  • Ubuntu 16.04
  • docker

 

1. Pull the Redis image

docker pull redis

 

 

2. Configure the Redis startup configuration file, here I create a dedicated directory to store Redis related data and configuration

mkdir /docker/redis

Download the service startup configuration file in this directory

wget http://download.redis.io/redis-stable/redis.conf

After downloading, enter the file to modify the following 3 parameters (in the non-editing state, use / bind to search the string position)

vim / docker / redis / redis.conf 
# Modify the following three configuration parameters #bind
127.0 . 0.1 #If the bind option is empty, then allow all connections from available network interfaces protected - mode no #Protected mode, if yes , Only allow local clients to connect appendonly yes # After opening, Redis will write each written data to the appendonly.aof file after receiving. Every time Redis will read the data of this file into memory

 

 

3. Create a new Master (redis6379) folder under this directory and copy the above conf file to the folder. The path of Master's conf file is

/docker/redis/redis6379/redis.conf

 

 

4. Use conf to create a Master container

# -restart always (this image also starts automatically when docker starts) 
# --name The container name is redis- 6379 
# -p maps the local port 6379 to the container port 6379 
# -v binds to the Master directory / docker / redis / redis6379 mapped to / docker of Data 
# - D background container, and returns the container ID 
# specified image Redis 
# conf start using the service Redis -server / Data / redis.conf Docker rUN

 --restart Always --name redis- 6379 -p 6379 : 6379 -v / docker / redis / redis6379: / data -d redis redis-server /data/redis.conf

 

 

5. View container startup

root@iZ282zj76jqZ:/docker/redis/redis6379# docker ps |grep redis-6379
792067d225e6        redis               "docker-entrypoint.s…"   2 hours ago         Up 2 hours          0.0.0.0:6379->6379/tcp              redis-6379

View the IP of the Master container to configure the parameters in Salve

# View Master container IP, 
# docker inspect redis-6379 View IPAdress docker inspect
--format = ' {{.NetworkSettings.IPAddress}} ' redis- 6379 # 192.168 . 0.4 in the NetworkSettings configuration

 

 

6. Create a Salve folder (redis6380, redis6381, redis6382), copy the above redis.conf files to three folders, and modify the configuration

vim / docker / redis / redis.conf 
 
# Modify the following four configuration parameters 
#bind 127.0 . 0.1         # If the bind option is empty, all connections from available network interfaces are allowed
 protected - mode no
 #Protected       mode, if yes , Only allow local clients to connect
 appendonly yes          # After opening, Redis will write the data written every time to the appendonly.aof file after receiving, and Redis will first read the data of this file into the memory every time it starts 
# replicaof <master ip> <master port> 
replicaof 192.168 . 0.4  6379  #Redis host (Master) IP port

Start three Salve services

docker run --restart always --name redis-6380 -p 6380:6379 -v /docker/redis/redis6380:/data -d redis redis-server /data/redis.conf
docker run --restart always --name redis-6381 -p 6381:6379 -v /docker/redis/redis6381:/data -d redis redis-server /data/redis.conf
docker run --restart always --name redis-6382 -p 6382:6379 -v /docker/redis/redis6382:/data -d redis redis-server /data/redis.conf

 

 

7. View container services

root@iZ282zj76jqZ:/docker/redis# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
43488a8c668c        redis               "docker-entrypoint.s…"   3 seconds ago       Up 1 second         0.0.0.0:6382->6379/tcp              redis-6382
ca3caaa77f4a        redis               "docker-entrypoint.s…"   2 hours ago         Up About an hour    0.0.0.0:6381->6379/tcp              redis-6381
792067d225e6        redis               "docker-entrypoint.s…"   2 hours ago         Up 2 hours          0.0.0.0:6379->6379/tcp              redis-6379
979318c633be        redis               "docker-entrypoint.s…"   2 hours ago         Up About an hour    0.0.0.0:6380->6379/tcp              redis-6380

Enter the Master container to operate Redis

root@iZ282zj76jqZ:/docker/redis# docker exec -it redis-6379 /bin/bash
root@792067d225e6:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.0.3,port=6379,state=online,offset=7129,lag=1
slave1:ip=192.168.0.6,port=6379,state=online,offset=7129,lag=0
slave2:ip=192.168.0.5,port=6379,state=online,offset=7129,lag=0
master_replid:09e8f31f23d920034b8f5e20749489ffff3fec17
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:7143
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:6892
repl_backlog_histlen:252
127.0.0.1:6379> dbsize
(integer) 3

Enter Salve container operation Redis

root@iZ282zj76jqZ:~# docker exec -it redis-6381 /bin/bash
root@ca3caaa77f4a:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.0.4
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:6891
master_link_down_since_seconds:8
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7d6ef858609048f3210a178772ea98ac20446c9e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6891
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1027
repl_backlog_histlen:5865
127.0.0.1:6379> dbsize
(integer) 3

 

 

8. You can also directly use the docker command to operate Redis

# 
-it reassigns a pseudo input terminal to the container and runs the container in interactive mode 
# Container name redis - 6379 
# redis - cli set Key Value docker exec -it redis- 6379 redis-cli set name tomkluas

 

 

Guess you like

Origin www.cnblogs.com/tomkluas/p/12682489.html