File docker compose
version: '3'
services:
redis-master:
image: redis
container_name: redis-master
ports:
- "6279:6379"
redis-slave-1:
image: redis
container_name: redis-slave-1
ports:
- "6179:6379"
redis-slave-2:
image: redis
container_name: redis-slave-2
ports:
- "6079:6379"
Khởi tạo 3 containers master và 2 slave tương ứng với các port: 6279, 6179 và 6079. Cả 3 containers đều được expose port ra bên ngoài bằng cổng 6379
-> Chạy lệnh docker compose -f docker-compose.yml up để pull image và start containers
-> Thực hiện exec vào container redis-slave-1 và redis-slave-2 chạy lệnh slaveof redis-master 6379 và sau đó info replication kiểm tra trạng thái của slave xem đã connect tới redis-master chưa
# redis-cli
127.0.0.1:6379> slaveof redis-master 6379
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:redis-master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_read_repl_offset:14
slave_repl_offset:14
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:76199e2ac4b98caee734d87f52db6844b60ed264
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
127.0.0.1:6379>
Last login: Mon Feb 5 23:02:51 on ttys008
➜ ~ docker exec -it 36b5113c9fff /bin/sh
# redis-cli
127.0.0.1:6379> slaveof redis-master 6379
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:redis-master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:28
slave_repl_offset:28
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:76199e2ac4b98caee734d87f52db6844b60ed264
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29
repl_backlog_histlen:0
127.0.0.1:6379>
Trạng thái của master đã nhận cấu hình của 2 slave
➜ ~ docker exec -it 4c14c8f23a39 /bin/sh
# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.21.0.2,port=6379,state=online,offset=308,lag=1
slave1:ip=172.21.0.3,port=6379,state=online,offset=308,lag=1
master_failover_state:no-failover
master_replid:76199e2ac4b98caee734d87f52db6844b60ed264
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:308
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:308
127.0.0.1:6379>
-> Chạy lệnh set 1 key test giá trị 123 ở master và kiểm tra key này ở 2 slave. Kết quả đã đồng bộ thành công