主从Redis及哨兵部署(docker)
一、主从Redis部署(docker)
首先,我准备了两台linux,一台准备当作master,ip是192.168.241.128,另一台是当作slave,ip是192.168.241.129。
1. 安装redis
docker pull redis
2. 下载对应版本的redis.conf
可以从github上下载。新建配置环境目录和持久化数据目录。
mkdir -p /home/redis/data
mkdir -p /home/redis/conf
mkdir -p /home/redis/log
修改配置redis.conf:
logfile "/log/redis.log"
dir /data
将redis.conf放到mkdir -p /home/redis/conf下。
3. 创建主redis容器
docker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 --name redis -v /home/redis/data:/data -v /home/redis/conf/redis.conf:/etc/redis/redis.conf -v /home/redis/log/redis.log:/log/redis.log -d redis redis-server /etc/redis/redis.conf --appendonly yes
创建容器后,调用redis-server启动用挂载的路径的redis.conf启动。并且开启AOF持久化。
docker ps -a
查看,发现已成功启动。
如果报这个错:Can't open the log file: Is a directory,可能得手动建一个空文件 /home/redis/log/redis.log
4. 创建第从redis容器
和上面的1,2,3步骤一样,在第二台linux服务器上创建第二个redis。稍微不同的是,在第2个步骤配置redis.conf时,要做修改。
redis.conf加上配置:
192.168.241.128 是我上面配置的第一台linux的地址,意思是第二台是第一台的slave库
slaveof 192.168.241.128 6379
依样部署好第二台redis就可以了。
5. 验证
在redis客户端中执行命令info replication
128(主):
> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.241.129,port=6379,state=online,offset=0,lag=0
master_failover_state:no-failover
master_replid:8fade8b7a32d08413701d8816661604e773c6cd3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
129(从):
> 192.168.241.129@6379 connected!
> info replication
# Replication
role:slave
master_host:192.168.241.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
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:8fade8b7a32d08413701d8816661604e773c6cd3
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
很明显,一个role 是master,一个是slave。
验证一下数据同步:
一开始129尝试get "h" ,发现数据不存在。想要写入,因为是从节点,所以提示无法写入。
然后我们往128加入"h"->"hello", 切换到129发现这条数据已经同步过来了,接着128删除这个键,129也是跟着删除了数据。
二、Redis 哨兵部署(docker)
准备好三台服务器,已经部署了redis,其中一台主,两台从。(可以按上面步骤再部署一台从节点)
1. 配置文件
准备好sentinel.conf配置文件。可以从上面的那个github分支下载对应版本的config文件。
拷到路径 /home/redis-sentinel/conf/sentinel.conf
mkdir -p /home/redis-sentinel/data
mkdir -p /home/redis-sentinel/conf
2. 创建容器
docker run --restart=always -p 26379:26379 --name redis-sentinel -v /home/redis-sentinel/data:/data -v /home/redis-sentinel/conf/sentinel.conf:/etc/redis/sentinel.conf -d redis redis-sentinel /etc/redis/sentinel.conf
3. 验证
当三台都装好之后,依次在redis客户端打印主从信息: info replication
可以看出此时主节点是128,两个从节点是129和131
> 192.168.241.128@6379 connected!
> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.241.129,port=6379,state=online,offset=136208,lag=0
slave1:ip=192.168.241.131,port=6379,state=online,offset=136208,lag=0
master_failover_state:no-failover
master_replid:8fade8b7a32d08413701d8816661604e773c6cd3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:136348
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:136348
> 192.168.241.129@6379 connected!
> info replication
# Replication
role:slave
master_host:192.168.241.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:138042
slave_repl_offset:138042
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:8fade8b7a32d08413701d8816661604e773c6cd3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:138042
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:85875
repl_backlog_histlen:52168
> 192.168.241.131@6379 connected!
> info replication
# Replication
role:slave
master_host:192.168.241.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:138742
slave_repl_offset:138742
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:8fade8b7a32d08413701d8816661604e773c6cd3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:138742
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:86738
repl_backlog_histlen:52005
此时,将128主节点的redis关掉。等三十秒(这个时间是sentinel.conf默认配置的),重新打印剩下两台从节点的信息:
> 192.168.241.129@6379 connected!
> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.241.131,port=6379,state=online,offset=253237,lag=1
master_failover_state:no-failover
master_replid:94187cf053b9394bd53e13bd0334ab19f376a6d2
master_replid2:47fa536c9702cd72db072304c3904265e52b52de
master_repl_offset:253517
second_repl_offset:178390
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:175950
repl_backlog_histlen:77568
> 192.168.241.131@6379 connected!
> info replication
# Replication
role:slave
master_host:192.168.241.129
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:254371
slave_repl_offset:254371
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:94187cf053b9394bd53e13bd0334ab19f376a6d2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:254371
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:180513
repl_backlog_histlen:73859
可以发现,129从原本的slave角色变为master角色了。而131从节点的master_host也改为128了。