分别使用宿主机的6381 6382 6382 端口,
[root@iz2zei9wv79ob7vwy7y1jcz redis]# docker run -p 6381:6379 --name redis001 -v $PWD/data:/data -d redis redis-server --appendonly yes
a5e55a288549d67adddb613bc6f8dd4c25c3f4bd443962401f93c9f3d29fc74a
[root@iz2zei9wv79ob7vwy7y1jcz redis]# docker run -p 6382:6379 --name redis002 -d redis redis-server
e1cea94b016b2f5371a38c50964c232a4200e4d2fd645308db8f518d8cd3400d
[root@iz2zei9wv79ob7vwy7y1jcz redis]# docker run -p 6383:6379 --name redis003 -d redis redis-server
9efbc0272aae1a7152af995841de6b56d4ae17c3c16f3e2052064c9e7566554f
-p 用来设置容器端口到主机端口的映射。
–name 用来设置容器别名用来以后方便操作
-v 用来将宿主机指定目录挂在到容器中 冒号前为主机目录,冒号后为容器中目录
-d redis 为下载的redis镜像
redis-server 用来再创建容器的时候就启动redis的server端口
–appendonly yes 用来开启redis的持久化。
docker exec -it redis001 redis-cli 用来登陆redis客户端
info可以用来查看redis容器的各项参数
[root@iz2zei9wv79ob7vwy7y1jcz redis]# docker exec -it redis001 redis-cli
127.0.0.1:6379> info
# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:90b5046eb8b5d254
redis_mode:standalone
os:Linux 3.10.0-957.27.2.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.3.0
[root@iz2zei9wv79ob7vwy7y1jcz ~]# docker inspect redis001
"172.17.0.6" ==>redis001
"172.17.0.7" ==>redis002
"172.17.0.8" ==>redis003
进入容器内部,查看当前redis是主master还是从slave
[root@iz2zei9wv79ob7vwy7y1jcz ~]# docker exec -it redis003 redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:16b463b4a214d67d98fbcd60a2016417c83fbdec
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
可以得到redis容器都是master
指定redis001 为master ,redis002和redis003为从机
进入从机redis客户端,使用slaveof 命令设置master ip 和 port
需要注意的是,这里port是容器中redis对应的端口,而不是我们宿主机对应的port端口,因为我们使用的是容器的内网ip
[root@iz2ei9wv79ob7vwy7y1jcz ~]# docker exec -it redis002 redis-cli
127.0.0.1:6379> slaveof 172.17.0.6 6379
OK
使用info replication 命令查看主从复制配置,可以看到master配置已经变成redis001的端口和ip
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.17.0.6
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:0
master_link_down_since_seconds:1565155373
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4cedd9fa077ab77c4b43e824c1c191acdfe0bdb8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
同理设置redis003
进入master ==>redis001,可以看到已经有两个slave连接上了
[root@iz2zei9wv79ob7vwy7y1jcz ~]# docker exec -it redis001 redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.7,port=6379,state=online,offset=252,lag=1
slave1:ip=172.17.0.8,port=6379,state=online,offset=252,lag=1
master_replid:242e2099f1209d5cfe81fdea8342d0cddc7a93de
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:266