[Redis]-設定Redis的master-slave複寫

Redis建置學習心得

原則上為了讓伺服器設定達到最大化使用率, 下列是最終計劃的Redis複寫配置與監控、Failover等的架構圖.

 

 

接下來看看複寫的設定. 依照安裝Linux時的規劃, 會有下列三台機器

編號

Server Name

IP Address

1

Redis01.ColinDomain

192.168.127.101

2

Redis02.ColinDomain

192.168.127.102

3

Redis03.ColinDomain

192.168.127.103

 

再加入一些複寫相關的資訊

編號

Server Name

IP Address

Redis M/S

1

Redis01.ColinDomain

192.168.127.101

6379-master

2

Redis02.ColinDomain

192.168.127.102

6379-slave

3

Redis03.ColinDomain

192.168.127.103

6379-slave p*

p*表示failover priority 1

 

若是從最終架構圖抽出來的話會是這樣的.

 

 

因此要先有三台伺服器, 分別安裝完成CentOS 7 (或是其他版本), 然後都完成Redis的安裝, 並且設定的PORT都是該伺服器的6379.

 

 

依照預設, Redis一裝好後, 都會是master的角色, 並然沒有任何slave來連結. 先來看看各台機器目前的複寫狀況.

 

[root@Redis01 ~]# redis-cli -h 192.168.127.101 -p 6379 info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

 

[root@Redis02 ~]# redis-cli -h 192.168.127.102 -p 6379 info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

 

[root@Redis03 ~]# redis-cli -h 192.168.127.103 -p 6379 info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

 

 

設定Redismaster-slave複寫動作很簡單, 就是把要設定為slave, 指定他的slaveof 為哪一個master就可以了, 所以在上述的範例中, 192.168.127.102192.168.127.103, 6379 portRedis configure檔案修改一下, 以整個範例的話, 會在 /etc/redis/6379.conf, 先進行192.168.127.102的操作

 

# vi /etc/redis/6379.conf

 

增加一行如下的語法

# slaveof <masterip> <masterport>

slaveof 192.168.127.101 6379

 

設定完後, 要將Redis instance重啟. (此以192.168.127.102為語法範例)

# redis-cli -h 192.168.127.102 -p 6379 save

# redis-cli -h 192.168.127.102 -p 6379 shutdown

# /etc/init.d/redis_6379 start

 

這時在master (192.168.127.101) slave (192.168.127.102) 的複寫資訊就會不一樣

 

[root@Redis01 ~]# redis-cli -h 192.168.127.101 -p 6379 info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.127.102,port=6379,state=online,offset=113,lag=0

master_repl_offset:113

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:112

 

[root@Redis02 ~]# redis-cli -h 192.168.127.102 -p 6379 info replication

# Replication

role:slave

master_host:192.168.127.101

master_port:6379

master_link_status:up

master_last_io_seconds_ago:9

master_sync_in_progress:0

slave_repl_offset:99

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

 

 

接著再將192.168.127.103依照步驟2, 3操作後, 即可完成 1 master -> slave的配置

 

[root@Redis01 ~]# redis-cli -h 192.168.127.101 -p 6379 info replication

# Replication

role:master

connected_slaves:2

slave0:ip=192.168.127.102,port=6379,state=online,offset=729,lag=0

slave1:ip=192.168.127.103,port=6379,state=online,offset=729,lag=0

master_repl_offset:729

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:728

 

[root@Redis03 ~]# redis-cli -h 192.168.127.103 -p 6379 info replication

# Replication

role:slave

master_host:192.168.127.101

master_port:6379

master_link_status:up

master_last_io_seconds_ago:5

master_sync_in_progress:0

slave_repl_offset:715

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0