0%

Docker-Compose-Redis-Master-Slave

使用docker-compose搭建Redis主从测试环境。

源码地址

编写docker-compose配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
version : '3'

networks:
network-redis:
driver: bridge

services:
master:
image: redis:5.0.5
container_name: redis5.0.6-master
networks:
- network-redis
ports:
- '6379:6379'
command: redis-server --requirepass redis_pwd --masterauth redis_pwd # 启动redis命令
slaves1:
image: redis:5.0.5
container_name: redis5.0.6-slaves1
networks:
- network-redis
ports:
- '6380:6379'
command: redis-server --slaveof redis5.0.6-master 6379 --requirepass redis_pwd --masterauth redis_pwd # 启动redis命令
slaves2:
image: redis:5.0.5
container_name: redis5.0.6-slaves2
networks:
- network-redis
ports:
- '6381:6379'
command: redis-server --slaveof redis5.0.6-master 6379 --requirepass redis_pwd --masterauth redis_pwd # 启动redis命令
  • 指定密码redis_pwd 作为测试密码

启动测试

1
docker-compse -f docker-compose up -d
  • Console
1
2
3
Starting redis5.0.6-master ... done
Starting redis5.0.6-slaves2 ... done
Starting redis5.0.6-slaves1 ... done
  • 检查docker运行
1
docker ps

  • 使用redis-cli检查
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
redis-cli
127.0.0.1:6379> auth redis_pwd
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.21.0.3,port=6379,state=online,offset=126,lag=0
slave1:ip=172.21.0.4,port=6379,state=online,offset=126,lag=0
master_replid:76d204429eefdd1df062c54ce8eae3b8ba9268b6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126
127.0.0.1:6379>
  • 这里可以有两个slave。

  • 可以使用客户端工具往master写入数据,看slave是否存在数据。

END