Redis單機版搭建上一篇已經基本介紹了,下面討論Redis集羣搭建方案和示例。redis
一、關於Redis經常使用的集羣方案(三種):數據庫
a、一主多從,如一個Master、兩個Slavecentos
b、薪火相傳,即集羣中的從節點(Slave)同時也是主節點(Master),相似於鏈式傳遞同樣緩存
c、反客爲主,主節點down掉後從節點升級爲主節點,經過人工干預 或者 經過Sentinel 哨兵模式來實現(下篇介紹)app
二、模擬測試(以一主多從爲例)測試
模擬主機信息(在同一臺主機經過不一樣端口模擬):ui
角色 | IP | 端口 |
Master | 127.0.0.1 | 6379 |
Salve | 127.0.0.1 | 6380 |
Salve | 127.0.0.1 | 6381 |
進入Redis目錄,複製redis配置文件,給Slave使用:spa
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 #開啓守護進程debug
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相關的配置項:
一、daemonize 若是須要在後臺運行,把該項改成yes
二、pidfile 配置多個pid的地址 默認在/var/run/redis.pid
三、bind 綁定ip,設置後只接受來自該ip的請求
四、port 監聽端口,默認是6379
五、loglevel 分爲4個等級:debug verbose notice warning
六、logfile 用於配置log文件地址
七、databases 設置數據庫個數,默認使用的數據庫爲0
八、save 設置redis進行數據庫鏡像的頻率。
九、rdbcompression 在進行鏡像備份時,是否進行壓縮
十、dbfilename 鏡像備份文件的文件名
十一、Dir 數據庫鏡像備份的文件放置路徑
十二、Slaveof 設置數據庫爲其餘數據庫的從數據庫
1三、Masterauth 主數據庫鏈接須要的密碼驗證
1四、Requriepass 設置 登錄時須要使用密碼
1五、Maxclients 限制同時使用的客戶數量
1六、Maxmemory 設置redis可以使用的最大內存
1七、Appendonly 開啓append only模式
1八、Appendfsync 設置對appendonly.aof文件同步的頻率(對數據進行備份的第二種方式)
1九、vm-enabled 是否開啓虛擬內存支持 (vm開頭的參數都是配置虛擬內存的)
20、vm-swap-file 設置虛擬內存的交換文件路徑
2一、vm-max-memory 設置redis使用的最大物理內存大小
2二、vm-page-size 設置虛擬內存的頁大小
2三、vm-pages 設置交換文件的總的page數量
2四、vm-max-threads 設置VM IO同時使用的線程數量
2五、Glueoutputbuf 把小的輸出緩存存放在一塊兒
2六、hash-max-zipmap-entries 設置hash的臨界值
2七、Activerehashing 從新hash