ubuntu/centos 搭建 搭建 redis 集群

1 環(huán)境配置:

 ubuntu16.04 
 redis3.0.6

2 機(jī)器:

 192.168.50.156
 192.168.50.154
 192.168.50.155

3 下載redis獨(dú)立文件位置:

http://download.redis.io/releases/

4 安裝redis:

 apt-get install redis-server

關(guān)鍵點(diǎn):redis-trib.rb工具實(shí)現(xiàn)集群

5 復(fù)制redis-trib.rb到/usr/local/bin目錄下。

首先查找:find / -name redis-trib.rb
復(fù)制操作:cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/redis-trib.rb

6 創(chuàng)建redis節(jié)點(diǎn)文件

進(jìn)入到root目錄: cd /root
創(chuàng)建相應(yīng)的節(jié)點(diǎn)目錄: mkdir redis_cluster
進(jìn)入到redis_cluster目錄,cd redis_cluster
創(chuàng)建子目錄:mkdir 7000 7001 7002
查找redis.conf文件:find / -name redis.conf
拷貝配置文件到相應(yīng)的7001,7002,7000文件夾下面:
cp /etc/redis/redis.conf /root/redis_cluster/7000/redis.conf
cp /etc/redis/redis.conf /root/redis_cluster/7001/redis.conf
cp /etc/redis/redis.conf /root/redis_cluster/7002/redis.conf

7 修改配置文件

默認(rèn)修改主日志里面的:

集中調(diào)整

port 7000

端口 7000,7001,7002

bind 192.168.50.156

監(jiān)聽I(yíng)P 其他人可以訪問的IP

daemonize yes

redis 后臺(tái)運(yùn)行

pidfile /var/run/redis/redis-server.pid

pidfile /var/run/redis/redis_7000.pid

pidfile 對(duì)應(yīng)pid 文件

cluster-enabled yes

開啟集群

cluster-config-file nodes-7000.conf

集群的配置 配置文件首次啟動(dòng)自動(dòng)生成 7000,7001,7002

cluster-node-timeout 15000

請(qǐng)求超時(shí) 默認(rèn)15秒,可自行設(shè)置

appendonly yes

aof 日志開啟 有需要就開啟,它會(huì)每次寫操作都記錄一條日志

對(duì)應(yīng)的 7001及7002修改相應(yīng)的數(shù)字

8 調(diào)整其它程序

備份機(jī)器上執(zhí)行:192.168.50.154
備份機(jī)器上執(zhí)行:192.168.50.155
如上調(diào)整方式,3,4,5,6,7,8 端口進(jìn)行設(shè)置。

9 運(yùn)行程序

主機(jī)上執(zhí)行:192.168.50.156
redis-server /root/redis_cluster/7000/redis.conf
redis-server /root/redis_cluster/7001/redis.conf
redis-server /root/redis_cluster/7002/redis.conf
備份機(jī)器上執(zhí)行:192.168.50.154
redis-server /root/redis_cluster/7003/redis.conf
redis-server /root/redis_cluster/7004/redis.conf
redis-server /root/redis_cluster/7005/redis.conf
備份機(jī)器上執(zhí)行:192.168.50.155
redis-server /root/redis_cluster/7006/redis.conf
redis-server /root/redis_cluster/7007/redis.conf
redis-server /root/redis_cluster/7008/redis.conf

通過ps查看可以發(fā)現(xiàn)如下代碼:
主機(jī)上執(zhí)行:192.168.50.156:執(zhí)行ps xH
1661 ? Ssl 0:00 redis-server 192.168.50.156:7000 [cluster]
1661 ? Ssl 0:00 redis-server 192.168.50.156:7000 [cluster]
1661 ? Ssl 0:00 redis-server 192.168.50.156:7000 [cluster]
1665 ? Ssl 0:00 redis-server 192.168.50.156:7001 [cluster]
1665 ? Ssl 0:00 redis-server 192.168.50.156:7001 [cluster]
1665 ? Ssl 0:00 redis-server 192.168.50.156:7001 [cluster]
1669 ? Ssl 0:00 redis-server 192.168.50.156:7002 [cluster]
1669 ? Ssl 0:00 redis-server 192.168.50.156:7002 [cluster]
1669 ? Ssl 0:00 redis-server 192.168.50.156:7002 [cluster]

備份機(jī)器上執(zhí)行:192.168.50.154:執(zhí)行ps xH
1544 ? Ssl 0:00 redis-server 192.168.50.154:7003 [cluster]
1544 ? Ssl 0:00 redis-server 192.168.50.154:7003 [cluster]
1544 ? Ssl 0:00 redis-server 192.168.50.154:7003 [cluster]
1548 ? Ssl 0:00 redis-server 192.168.50.154:7004 [cluster]
1548 ? Ssl 0:00 redis-server 192.168.50.154:7004 [cluster]
1548 ? Ssl 0:00 redis-server 192.168.50.154:7004 [cluster]
1552 ? Ssl 0:00 redis-server 192.168.50.154:7005 [cluster]
1552 ? Ssl 0:00 redis-server 192.168.50.154:7005 [cluster]
1552 ? Ssl 0:00 redis-server 192.168.50.154:7005 [cluster]

備份機(jī)器上執(zhí)行:192.168.50.155:執(zhí)行ps xH
1530 ? Ssl 0:00 redis-server 192.168.50.155:7006 [cluster]
1530 ? Ssl 0:00 redis-server 192.168.50.155:7006 [cluster]
1530 ? Ssl 0:00 redis-server 192.168.50.155:7006 [cluster]
1534 ? Ssl 0:00 redis-server 192.168.50.155:7007 [cluster]
1534 ? Ssl 0:00 redis-server 192.168.50.155:7007 [cluster]
1534 ? Ssl 0:00 redis-server 192.168.50.155:7007 [cluster]
1538 ? Ssl 0:00 redis-server 192.168.50.155:7008 [cluster]
1538 ? Ssl 0:00 redis-server 192.168.50.155:7008 [cluster]
1538 ? Ssl 0:00 redis-server 192.168.50.155:7008 [cluster]

10 創(chuàng)建集群【單獨(dú)主服務(wù)器操作即可】

redis官方提供創(chuàng)建集群工具 redis-trib.rb工具,默認(rèn)這個(gè)工具是在工具箱內(nèi),在上面的第五步驟中,我們已經(jīng)復(fù)制到/usr/local/bin下,也就意味者可以全局執(zhí)行這個(gè)命令了。
創(chuàng)建集群命令:
redis-trib.rb create --replicas 1 192.168.50.156:7000 192.168.50.156:7001 192.168.50.156:7002 192.168.50.154:7003 192.168.50.154:7004 192.168.50.154:7005 192.168.50.155:7006 192.168.50.155:7007 192.168.50.155:7008
發(fā)現(xiàn)環(huán)境需要ruby:
centos下安裝方案:
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis

ubuntu16.04 下安裝方案:
apt-get install ruby ruby-dev rubygems rpm-build -y
apt-get install ruby ruby-dev rubygems -y

如果:gem install redis 不好用,可以切換到淘寶源:
gem sources -a https://ruby.taobao.org/
然后再次執(zhí)行g(shù)em install redis即可。

11 驗(yàn)證集群

分別連接3臺(tái)服務(wù)器:
連接方式如下:
第一臺(tái)機(jī)器:
redis-cli -h 192.168.50.156 -c -p 7002
說明:加參數(shù) -C 可連接到集群,因?yàn)樯厦?redis.conf 將 bind 改為了ip地址,所以 -h 參數(shù)不可以省略。
第二臺(tái)機(jī)器:
redis-cli -h 192.168.50.154 -c -p 7003
第三臺(tái)機(jī)器:
redis-cli -h 192.168.50.155 -c -p 7008
分別設(shè)置及讀取,說明。說明集群正常工作。

驗(yàn)證效果:

設(shè)置數(shù)據(jù):
root@ubuntu:~# redis-cli -h 192.168.50.156 -c -p 7002
192.168.50.156:7002> set hello world
-> Redirected to slot [866] located at 192.168.50.156:7000
OK
192.168.50.156:7000>
獲取數(shù)據(jù):
root@ubuntu:~# redis-cli -h 192.168.50.154 -c -p 7004
192.168.50.154:7004> get hello
-> Redirected to slot [866] located at 192.168.50.156:7000
"world"
192.168.50.156:7000>
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。互聯(lián)網(wǎng)+時(shí)代,時(shí)刻要保持學(xué)習(xí),攜手千鋒PHP,Dream It Possible。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容