安裝 REDIS 叢集

gxlineji發表於2016-09-08

REDIS是KEY-VALUE資料庫,用於做快取是很不錯的選擇。

一、REDIS 叢集安裝步驟

安裝相關軟體
yum install -y ruby  ruby-rdoc;
rpm -ivh rubygems-1.3.7-2.3.noarch.rpm
下載
wget
gem install redis-3.2.1.gem --local

wget
tar zxvf redis-3.0.4.tar.gz 

建立目錄並安裝redis
mkdir -p /usr/local/software/
mv  redis-3.0.4 /usr/local/software/
cd /usr/local/software/redis-3.0.4
make
make install

建立叢集需要的目錄
mkdir -p /usr/local/software/redis-cluster
cd /usr/local/software/redis-cluster
mkdir 7001
mkdir 7002
mkdir 7003
cp -R /usr/local/software/redis-3.0.4/*  /usr/local/software/redis-cluster/7001/
cp -R /usr/local/software/redis-3.0.4/*  /usr/local/software/redis-cluster/7002/
cp -R /usr/local/software/redis-3.0.4/*  /usr/local/software/redis-cluster/7003/

修改配置檔案redis.conf
cp /usr/local/software/redis-3.0.4/redis.conf  /usr/local/software/redis-cluster/
cd /usr/local/software/redis-cluster/
vim redis.conf
##修改配置檔案中的下面選項
bind 192.168.1.93
port 7001
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
logfile "/usr/local/software/redis-cluster/7001/redis-7001.log"
appendonly yes
##修改完redis.conf配置檔案中的這些配置項之後把這個配置檔案分別複製到7001/7002/7003目錄下面
cd /usr/local/software/redis-cluster
mv ./7001/redis.conf  ./7001/redis.conf.bak
mv ./7002/redis.conf  ./7002/redis.conf.bak
mv ./7003/redis.conf  ./7003/redis.conf.bak
cp redis.conf ./7001/
cp redis.conf ./7002/
cp redis.conf ./7003/
##注意:複製完成之後要修改7001/7002/7003目錄下面redis.conf檔案中的port引數,分別改為對應的資料夾的名稱
 
分別啟動這3個redis例項
cd /usr/local/software/redis-cluster/7001
redis-server ./redis.conf
cd /usr/local/software/redis-cluster/7002
redis-server ./redis.conf
cd /usr/local/software/redis-cluster/7003
redis-server ./redis.conf

關閉
/usr/local/software/redis-3.0.4/src/redis-cli -h 127.0.0.1 -p 7001 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 127.0.0.1 -p 7002 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 127.0.0.1 -p 7003 shutdown
開放防火牆:
-A RH-Firewall-1-INPUT -p tcp --dport 7001 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp --dport 7002 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp --dport 7003 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp --dport 17001 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp --dport 17002 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp --dport 17003 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp --dport 6379 -j ACCEPT 

##啟動之後使用命令檢視redis的啟動情況
ps -ef|grep redis
如下圖顯示則說明啟動成功
 
6:執行redis的建立叢集命令建立叢集,讓三個例項互相通訊:
cd /usr/local/software/redis-3.0.4/src
./redis-trib.rb  create --replicas 1 192.168.1.70:7001 192.168.1.70:7002 192.168.1.70:7003 192.168.1.93:7001 192.168.1.93:7002 192.168.1.93:7003

二、重建叢集

#關閉叢集
#killall -9 redis-server

/usr/local/software/redis-3.0.4/src/redis-cli -h 127.0.0.1 -p 7001 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 127.0.0.1 -p 7002 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 127.0.0.1 -p 7003 shutdown

/usr/local/software/redis-3.0.4/src/redis-cli -h 192.168.1.70 -p 7001 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 192.168.1.70 -p 7002 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 192.168.1.70 -p 7003 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 192.168.1.93 -p 7001 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 192.168.1.93 -p 7002 shutdown
/usr/local/software/redis-3.0.4/src/redis-cli -h 192.168.1.93 -p 7003 shutdown

#刪除舊叢集相關檔案
cd /usr/local/software/redis-cluster/7001
rm -rf  nodes*.conf appendonly.aof redis-700*.log
cd /usr/local/software/redis-cluster/7002
rm -rf  nodes*.conf appendonly.aof redis-700*.log
cd /usr/local/software/redis-cluster/7003
rm -rf  nodes*.conf appendonly.aof redis-700*.log

# 啟動叢集
cd /usr/local/software/redis-cluster/7001
redis-server redis.conf
cd /usr/local/software/redis-cluster/7002
redis-server redis.conf
cd /usr/local/software/redis-cluster/7003
redis-server redis.conf

建立叢集
cd /usr/local/software/redis-3.0.4/src
./redis-trib.rb  create --replicas 1 192.168.1.93:7001 192.168.1.93:7002 192.168.1.93:7003 192.168.1.70:7001 192.168.1.70:7002 192.168.1.70:7003

測試
/usr/local/software/redis-3.0.4/src/redis-cli -c -p 7001 -h  192.168.1.93
cluster nodes
set hello world
exit
/usr/local/software/redis-3.0.4/src/redis-cli -c -p 7002 -h  192.168.1.93
get hello
exit
/usr/local/software/redis-3.0.4/src/redis-cli -c -p 7003 -h  192.168.1.93
get hello
exit


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10995764/viewspace-2124691/,如需轉載,請註明出處,否則將追究法律責任。

相關文章