安裝環(huán)境: centos7 64位 MINI版,當(dāng)前Redis最新版本為3.2,所以本文以3.2為例擔(dān)建Redis集群。
1、Redis服務(wù)器說(shuō)明
用2臺(tái)虛擬機(jī)(192.168.0.201和192.168.0.202),各安裝3個(gè)Redis實(shí)例。分別為3個(gè)master和3個(gè)slave,模擬6臺(tái)機(jī)器擔(dān)建一個(gè)Redis集群。
192.168.0.201:6379
192.168.0.201:6380
192.168.0.201:6381
192.168.0.202:6382
192.168.0.202:6383
192.168.0.202:6384
2、安裝Redis集群節(jié)點(diǎn)實(shí)例?
編譯Redis源碼前先檢查系統(tǒng)是否安裝了gcc,沒(méi)安裝的話執(zhí)行yum install -y gcc
安裝。
1> 安裝Redis
shell> wget http://download.redis.io/releases/redis-3.2.0.tar.gz
shell> tar -zxvf redis-3.2.0.tar.gz
shell> cd redis-3.2.0
shell> make && make install
shell> ln -s /usr/local/bin/redis* /usr/bin/
shell> cp src/redis-trib.rb /usr/bin/
2創(chuàng)建節(jié)點(diǎn)
shell> ./utils/install_server.sh
在兩臺(tái)虛擬機(jī)上依次執(zhí)行install_server.sh腳本分別各安裝3個(gè)redis實(shí)例。在安裝提示時(shí)輸入上面約定的端口(如:6380),改變端口后同時(shí)配置文件、日志文件和數(shù)據(jù)存儲(chǔ)目錄名會(huì)自動(dòng)加上端口號(hào),以和其它實(shí)例區(qū)別。如果對(duì)安裝路徑?jīng)]有特殊要求的話,在安裝時(shí)只需改變端口號(hào),其它都保持默認(rèn)即可。默認(rèn)配置文件如下:
配置文件:/etc/redis/port.conf
日志文件:/etc/log/redis_port.log
數(shù)據(jù)存儲(chǔ)目錄(aof文件、rdb文件、集群節(jié)點(diǎn)配置文件):/var/lib/redis/port
注意:port為你設(shè)置的端口
make時(shí)如果遇到zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory錯(cuò)誤,用make MALLOC=libc && make install重新安裝。
Redis服務(wù)安裝完成之后,服務(wù)會(huì)同時(shí)啟動(dòng),且會(huì)自動(dòng)加入到系統(tǒng)服務(wù)中,并設(shè)為開(kāi)機(jī)啟動(dòng)。
3> 修改Redis實(shí)例的集群配置
修改綁定IP
Redis默認(rèn)綁定的是127.0.0.0地址,需要將其修改為本機(jī)IP或0.0.0.0,集群中的各個(gè)節(jié)點(diǎn)才能互相通信。
# 192.168.0.201節(jié)點(diǎn)
shell> sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis/6379.conf
shell> sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis/6380.conf
shell> sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis/6381.conf
# 192.168.0.202節(jié)點(diǎn)
shell> sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis/16379.conf
shell> sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis/16380.conf
shell> sed -i 's/127.0.0.1/0.0.0.0/g' /etc/redis/16381.conf
修改端口號(hào)
安裝完redis服務(wù)后,配置文件中集群的相關(guān)端口配置默認(rèn)為6379,需要將其它幾個(gè)非6379的端口修改過(guò)來(lái)。
# 192.168.0.201節(jié)點(diǎn)
shell> sed -i 's/6379/6380/g' /etc/redis/6380.conf
shell> sed -i 's/6379/6381/g' /etc/redis/6381.conf
# 192.168.0.202節(jié)點(diǎn)
shell> sed -i 's/6379/16379/g' /etc/redis/16379.conf
shell> sed -i 's/6379/16380/g' /etc/redis/16380.conf
shell> sed -i 's/6379/16381/g' /etc/redis/16381.conf
檢查修改結(jié)果:
shell> cat /etc/redis/6380.conf | awk '{if($0 !~ /^$/ && $0 !~ /#/) {print $0' | grep 6380
port 6380
pidfile /var/run/redis_6380.pid
logfile /var/log/redis_6380.log
dir /var/lib/redis/6380
cluster-config-file nodes-6380.conf
有5處修改端口的地方,修改成功!
修改集群配置
cluster-enabled yes ?
cluster-config-file nodes-6379.conf ?
cluster-node-timeout 5000 ?
appendonly yes
cluster-enabled:開(kāi)啟集群模式
cluster-config-file:保存節(jié)點(diǎn)的配置信息,如集群中所有節(jié)點(diǎn)的IP、端口、狀態(tài)、節(jié)點(diǎn)類型(master/slave)、節(jié)點(diǎn)ID、slots等
cluster-node-timeout:節(jié)點(diǎn)心跳超時(shí)時(shí)長(zhǎng)
appendonly:開(kāi)啟aof文件存儲(chǔ)
依次將每個(gè)實(shí)例配置文件中的以上注釋打開(kāi),并修改成對(duì)應(yīng)的值。
重新啟動(dòng)所有redis服務(wù):
shell> service redis_portN restart
但它們現(xiàn)在都還是獨(dú)立的實(shí)例,還沒(méi)有分配到一個(gè)集群當(dāng)中。沒(méi)有master和slave關(guān)系。
查看服務(wù)詳情及配置文件信息:
此時(shí)通過(guò)PS命令查看redis進(jìn)程,和普通進(jìn)程不同的是在進(jìn)程名后邊加了一個(gè)[cluster]標(biāo)識(shí)。192.168.0.201節(jié)點(diǎn)如下圖所示:
?
?
?