一、主從Redis部署(docker) 首先,我準備了兩台linux,一臺準備當作master,ip是192.168.241.128,另一臺是當作slave,ip是192.168.241.129。 1. 安裝redis docker pull redis 2. 下載對應版本的redis.conf 可 ...
一、主從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了。