安裝環境: centos7 64位 MINI版,當前Redis最新版本為3.2,所以本文以3.2為例擔建Redis集群。
1、Redis服務器說明
用2臺虛擬機(192.168.0.201和192.168.0.202),各安裝3個Redis實例。分別為3個master和3個slave,模擬6臺機器擔建一個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集群節點實例?
編譯Redis源碼前先檢查系統是否安裝了gcc,沒安裝的話執行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創建節點
shell> ./utils/install_server.sh
在兩臺虛擬機上依次執行install_server.sh腳本分別各安裝3個redis實例。在安裝提示時輸入上面約定的端口(如:6380),改變端口后同時配置文件、日志文件和數據存儲目錄名會自動加上端口號,以和其它實例區別。如果對安裝路徑沒有特殊要求的話,在安裝時只需改變端口號,其它都保持默認即可。默認配置文件如下:
配置文件:/etc/redis/port.conf
日志文件:/etc/log/redis_port.log
數據存儲目錄(aof文件、rdb文件、集群節點配置文件):/var/lib/redis/port
注意:port為你設置的端口
make時如果遇到zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory錯誤,用make MALLOC=libc && make install重新安裝。
Redis服務安裝完成之后,服務會同時啟動,且會自動加入到系統服務中,并設為開機啟動。
3> 修改Redis實例的集群配置
修改綁定IP
Redis默認綁定的是127.0.0.0地址,需要將其修改為本機IP或0.0.0.0,集群中的各個節點才能互相通信。
# 192.168.0.201節點
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節點
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
修改端口號
安裝完redis服務后,配置文件中集群的相關端口配置默認為6379,需要將其它幾個非6379的端口修改過來。
# 192.168.0.201節點
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節點
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
檢查修改結果:
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:開啟集群模式
cluster-config-file:保存節點的配置信息,如集群中所有節點的IP、端口、狀態、節點類型(master/slave)、節點ID、slots等
cluster-node-timeout:節點心跳超時時長
appendonly:開啟aof文件存儲
依次將每個實例配置文件中的以上注釋打開,并修改成對應的值。
重新啟動所有redis服務:
shell> service redis_portN restart
但它們現在都還是獨立的實例,還沒有分配到一個集群當中。沒有master和slave關系。
查看服務詳情及配置文件信息:
此時通過PS命令查看redis進程,和普通進程不同的是在進程名后邊加了一個[cluster]標識。192.168.0.201節點如下圖所示:
?
?
?