1 環(huán)境配置:
ubuntu16.04
redis3.0.6
2 機器:
192.168.50.156
192.168.50.154
192.168.50.155
3 下載redis獨立文件位置:
http://download.redis.io/releases/
4 安裝redis:
apt-get install redis-server
關鍵點:redis-trib.rb工具實現(xiàn)集群
5 復制redis-trib.rb到/usr/local/bin目錄下。
首先查找:find / -name redis-trib.rb
復制操作:cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/redis-trib.rb
6 創(chuàng)建redis節(jié)點文件
進入到root目錄: cd /root
創(chuàng)建相應的節(jié)點目錄: mkdir redis_cluster
進入到redis_cluster目錄,cd redis_cluster
創(chuàng)建子目錄:mkdir 7000 7001 7002
查找redis.conf文件:find / -name redis.conf
拷貝配置文件到相應的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 修改配置文件
默認修改主日志里面的:
集中調(diào)整
port 7000
端口 7000,7001,7002
bind 192.168.50.156
監(jiān)聽IP 其他人可以訪問的IP
daemonize yes
redis 后臺運行
pidfile /var/run/redis/redis-server.pid
pidfile /var/run/redis/redis_7000.pid
pidfile 對應pid 文件
cluster-enabled yes
開啟集群
cluster-config-file nodes-7000.conf
集群的配置 配置文件首次啟動自動生成 7000,7001,7002
cluster-node-timeout 15000
請求超時 默認15秒不跟,可自行設置
appendonly yes
aof 日志開啟 有需要就開啟,它會每次寫操作都記錄一條日志
對應的 7001及7002修改相應的數(shù)字
8 調(diào)整其它程序
備份機器上執(zhí)行:192.168.50.154
備份機器上執(zhí)行:192.168.50.155
如上調(diào)整方式米碰,3窝革,4,5见间,6聊闯,7工猜,8 端口進行設置米诉。
9 運行程序
主機上執(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
備份機器上執(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
備份機器上執(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)如下代碼:
主機上執(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]
備份機器上執(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]
備份機器上執(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)建集群【單獨主服務器操作即可】
redis官方提供創(chuàng)建集群工具 redis-trib.rb工具,默認這個工具是在工具箱內(nèi)篷帅,在上面的第五步驟中史侣,我們已經(jīng)復制到/usr/local/bin下拴泌,也就意味者可以全局執(zhí)行這個命令了。
創(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í)行gem install redis即可蚪腐。
11 驗證集群
分別連接3臺服務器:
連接方式如下:
第一臺機器:
redis-cli -h 192.168.50.156 -c -p 7002
說明:加參數(shù) -C 可連接到集群,因為上面 redis.conf 將 bind 改為了ip地址税朴,所以 -h 參數(shù)不可以省略回季。
第二臺機器:
redis-cli -h 192.168.50.154 -c -p 7003
第三臺機器:
redis-cli -h 192.168.50.155 -c -p 7008
分別設置及讀取,說明正林。說明集群正常工作泡一。
驗證效果:
設置數(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>
著作權歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權觅廓,非商業(yè)轉(zhuǎn)載請注明出處鼻忠。互聯(lián)網(wǎng)+時代杈绸,時刻要保持學習帖蔓,攜手千鋒PHP,Dream It Possible。