#redis单机命令行主从配置步骤(一般是采用配置文件修改设置主从)
#1.下载redis.conf文件,并复制几份修改对应配置
wget https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf
#2.配置项修改
#主机配置项修改
bind 127.0.0.1 -> bind 0.0.0.0
protected-mode no
dbfilename dump.rdb
logfile "6379.log"
#从机配置项修改
bind 127.0.0.1 -> bind 0.0.0.0
dbfilename dump_slave1.rdb
logfile "redis_slave1.log"
pidfile /var/run/redis_slave1.pid
#3.docker以配置文件形式启动redis
#启动master
docker run -p 6379:6379 --name myredis79 -v /config/redis.conf:/etc/redis/redis.conf -v /local/docker/data:/data -d redis redis-server /etc/redis/redis.conf
#启动slave1
docker run -p 6666:6379 --name myredis-slave1 -v /config/redis-slave1.conf:/etc/redis/redis.conf -v /local/docker/data:/data -d redis redis-server /etc/redis/redis.conf
#启动slave2
docker run -p 6667:6379 --name myredis-slave2 -v /config/redis-slave2.conf:/etc/redis/redis.conf -v /local/docker/data:/data -d redis redis-server /etc/redis/redis.conf
#启动slave3,如果需要通过master的容器名设置主从需要先用--link连通slave/master的网络或者使用自定义网络启动容器
docker run -p 6668:6379 --name myredis-slave3 -v /config/redis-slave3.conf:/etc/redis/redis.conf -v /local/docker/data:/data -d --link myredis79 redis redis-server /etc/redis/redis.conf
#进入从机
gkl@ghost:~$ docker exec -it myredis-slave1 /bin/bash
#连接redis客户端
root@6943a4e4d5c8:/data# redis-cli
#slaveof master 内网ip port 或者通过容器名绑定slaveof myredis79 6379
127.0.0.1:6379> slaveof 172.17.0.3 6379
OK
#从机信息查看
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.17.0.3
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:5530
slave_repl_offset:5530
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:dd549906cf602b2b00fc7ce0a20656a464d05068
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5530
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5517
repl_backlog_histlen:14
#主机信息
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:3
slave0:ip=172.17.0.8,port=6379,state=online,offset=43322,lag=0
slave1:ip=172.17.0.4,port=6379,state=online,offset=43322,lag=0
slave2:ip=172.17.0.5,port=6379,state=online,offset=43322,lag=0
master_failover_state:no-failover
master_replid:7ad03562a7a4e69f3e830a343659f21319ef5823
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:43322
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:31721
repl_backlog_histlen:11602
|