Redis筆記-集群搭建


Redis單機版搭建上一篇已經基本介紹了,下面討論Redis集群搭建方案和示例。

1、關於Redis常用的集群方案(三種):

  a、一主多從,如一個Master、兩個Slave

  b、薪火相傳,即集群中的從節點(Slave)同時也是主節點(Master),類似於鏈式傳遞一樣

  c、反客為主,主節點down掉后從節點升級為主節點,通過人工干預 或者 通過Sentinel 哨兵模式來實現(下篇介紹)

2、模擬測試(以一主多從為例)

模擬主機信息(在同一台主機通過不同端口模擬):

角色 IP 端口
Master 127.0.0.1 6379
Salve 127.0.0.1 6380
Salve 127.0.0.1 6381

 

 

 

 

 

進入Redis目錄,復制redis配置文件,給Slave使用:

1 [root@VM_0_14_centos redis]# ls -lrt
2 total 12700
3 -rwxr-xr-x 1 root root 8100759 Mar 20 16:12 redis-server
4 -rwxr-xr-x 1 root root 4805624 Mar 20 16:13 redis-cli
5 -rw-r--r-- 1 root root   62156 Mar 20 17:17 redis.conf
6 [root@VM_0_14_centos redis]# cp redis.conf ./redis.6380.conf
7 [root@VM_0_14_centos redis]# cp redis.conf ./redis.6381.conf
8 [root@VM_0_14_centos redis]# 

編輯Master配置文件redis.conf文件,主要配置以下參數:

daemonize  yes                                #開啟守護進程

pidfile /var/run/redis_6379.pid         #開啟守護進程后會將進程ID寫入該文件

logfile "/var/log/redis.6379.log"        #配置日志文件

masterauth funnyboy0128               #master驗證密碼

requirepass funnyboypass              #Redis登錄密碼

編輯Slave配置文件redis.6380.conf 和redis.6381.conf ,修改pidfile和logfile的值,並在最后追加 slaveof 配置項,修改端口分貝為6380和6380

port  6380  和   port 6381

slaveof 127.0.0.1 6379                   #Master的I配合端口

 

啟動Master節點:

1 [root@VM_0_14_centos redis]# 
2 [root@VM_0_14_centos redis]# 
3 [root@VM_0_14_centos redis]# 
4 [root@VM_0_14_centos redis]# ./redis-server ./redis.conf 

 

啟動Slave節點:

1 [root@VM_0_14_centos redis]# 
2 [root@VM_0_14_centos redis]# 
3 [root@VM_0_14_centos redis]# ./redis-server ./redis.6380.conf 
4 [root@VM_0_14_centos redis]# ./redis-server ./redis.6381.conf 
5 [root@VM_0_14_centos redis]# 

 

客戶端連接測試:

 1 [root@VM_0_14_centos redis]# 
 2 [root@VM_0_14_centos redis]# ./redis-cli -h 127.0.0.1 -p 6379 -a funnyboypass
 3 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 4 127.0.0.1:6379> keys *
 5 (empty list or set)
 6 127.0.0.1:6379> set name hello redis
 7 (error) ERR syntax error
 8 127.0.0.1:6379> set name "hello redis"
 9 OK
10 127.0.0.1:6379> 
11 127.0.0.1:6379> 
12 127.0.0.1:6379> get name
13 "hello redis"
14 127.0.0.1:6379> 

連接Master,set信息到Redis。然后連接Slave查看數據,

1 [root@VM_0_14_centos redis]# 
2 [root@VM_0_14_centos redis]# ./redis-cli -h 127.0.0.1 -p 6380 -a funnyboypass
3 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4 127.0.0.1:6380> 
5 127.0.0.1:6380> 
6 127.0.0.1:6380> 
7 127.0.0.1:6380> get name
8 "hello redis"
9 127.0.0.1:6380> 

6380 Slave節點數據OK。

1 [root@VM_0_14_centos redis]# ./redis-cli -h 127.0.0.1 -p 6381 -a funnyboypass
2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
3 127.0.0.1:6381> get name
4 "hello redis"
5 127.0.0.1:6381> 
6 127.0.0.1:6381> 

 

測試Slave是否支持寫入:

1 127.0.0.1:6381> 
2 127.0.0.1:6381> set sname hello
3 (error) READONLY You can't write against a read only replica.
4 127.0.0.1:6381> 

結果顯示Slave值只支持讀操作。

補充關於redis.conf相關的配置項:

 

1、daemonize 如果需要在后台運行,把該項改為yes

 

2、pidfile 配置多個pid的地址 默認在/var/run/redis.pid

 

3、bind 綁定ip,設置后只接受來自該ip的請求

 

4、port 監聽端口,默認是6379

 

5、loglevel 分為4個等級:debug verbose notice warning

 

6、logfile 用於配置log文件地址

 

7、databases 設置數據庫個數,默認使用的數據庫為0

 

8、save 設置redis進行數據庫鏡像的頻率。

 

9、rdbcompression 在進行鏡像備份時,是否進行壓縮

 

10、dbfilename 鏡像備份文件的文件名

 

11、Dir 數據庫鏡像備份的文件放置路徑

 

12、Slaveof 設置數據庫為其他數據庫的從數據庫

 

13、Masterauth 主數據庫連接需要的密碼驗證

 

14、Requriepass 設置 登陸時需要使用密碼

 

15、Maxclients 限制同時使用的客戶數量

 

16、Maxmemory 設置redis能夠使用的最大內存

 

17、Appendonly 開啟append only模式

 

18、Appendfsync 設置對appendonly.aof文件同步的頻率(對數據進行備份的第二種方式)

 

19、vm-enabled 是否開啟虛擬內存支持 (vm開頭的參數都是配置虛擬內存的)

 

20、vm-swap-file 設置虛擬內存的交換文件路徑

 

21、vm-max-memory 設置redis使用的最大物理內存大小

 

22、vm-page-size 設置虛擬內存的頁大小

 

23、vm-pages 設置交換文件的總的page數量

 

24、vm-max-threads 設置VM IO同時使用的線程數量

 

25、Glueoutputbuf 把小的輸出緩存存放在一起

 

26、hash-max-zipmap-entries 設置hash的臨界值

 

27、Activerehashing 重新hash

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM