服務器環境:
192.168.1.105
192.168.1.160
每臺服務器搭建3個節點,組成3個主節點,3個從節點的redis集羣。html
注意:防火牆必定要開放監聽的redis端口,不然會建立失敗。node
1、redis cluster安裝redis
一、下載和編譯安裝ruby
cd /home/xm6f/dev wget http://download.redis.io/releases/redis-3.2.4.tar.gz tar -zxvf redis-3.2.4.tar.gz cd redis-3.2.4/ make && make install
二、建立redis節點
選擇2臺服務器,分別爲:192.168.1.105,192.168.1.160,每臺服務器有3個節點,組成3個主節點,3個從節點的redis集羣。bash
a、首先在192.168.1.105建立3個節點:服務器
cd /home/xm6f/dev mkdir redis_cluster #建立集羣目錄 cd redis_cluster/ mkdir 7000 7001 7002 #分別表明三個節點,對應端口分別爲7000、7001、7002 #redis.conf拷貝到7000目錄 cp redis-3.2.4/redis.conf redis_cluster/7000/ #redis.conf拷貝到7001目錄 cp redis-3.2.4/redis.conf redis_cluster/7001/ #redis.conf拷貝到7002目錄 cp redis-3.2.4/redis.conf redis_cluster/7002/
b、分別對7000、7001,7002文件夾中的3個redis.conf文件修改對應的配置:app
daemonize yes #redis後臺運行 pidfile /var/run/redis_7000.pid #pidfile文件對應7000, 7001, 7002 port 7000 #端口7000, 7001, 7002 cluster-enabled yes #開啓集羣 把註釋#去掉 cluster-config-file nodes_7000.conf #集羣的配置 配置文件首次啓動自動生成 7000,7001,7002,存在/home/xm6f/dev/redis-3.2.4/src目錄 cluster-node-timeout 5000 #請求超時,設置5秒便可 appendonly yes #aof日誌開啓,有須要就開啓,它會每次寫操做都記錄一條日誌 logfile "/home/xm6f/dev/redis_cluster/7000/logs/redis.log" bind 192.168.1.105 #綁定當前服務器的IP,不然的話在集羣通訊的時候會出現:[ERR] Sorry, can't connect to node 192.168.200.140:7001
dbfilename dump_7000.rdb #存在/home/xm6f/dev/redis-3.2.4/src目錄
appendfilename "appendonly_7000.aof" #存在/home/xm6f/dev/redis-3.2.4/src目錄工具
requirepass 123456 #設置密碼,每一個節點的密碼都必須一致的ui
masterauth 123456spa
在192.168.1.160建立3個節點:對應的端口改成7003,7004,7005.配置對應的改一下就能夠了。
三、兩臺機器啓動各節點(兩臺服務器方式同樣)
cd /home/xm6f/dev/redis-3.2.4/src ./redis-server ../../redis_cluster/7000/redis.conf & ./redis-server ../../redis_cluster/7001/redis.conf & ./redis-server ../../redis_cluster/7002/redis.conf & ./redis-server ../../redis_cluster/7003/redis.conf & ./redis-server ../../redis_cluster/7004/redis.conf & ./redis-server ../../redis_cluster/7005/redis.conf &
四、查看服務
ps -ef | grep redis #查看是否啓動成功 netstat -tnlp | grep redis #能夠看到redis監聽端口
五、殺死全部redis進程
pkill -9 redis
2、建立集羣
前面已經準備好了搭建集羣的redis節點,接下來咱們要把這些節點都串連起來搭建集羣。官方提供了一個工具:redis-trib.rb(/home/xm6f/dev/redis-3.2.4/src/redis-trib.rb) 看後綴就知道這東西不能直接執行,它是用ruby寫的一個程序,因此咱們還得安裝ruby.
yum -y install ruby ruby-devel rubygems rpm-build
再用 gem 這個命令來安裝 redis 接口,gem是ruby的一個工具包。
gem install redis //等一下子就行了
固然,方便操做,兩臺Server都要安裝。
注意:在執行gem install redis時,報ERROR:Error installing redis:redis requires Ruby version >= 2.2.2異常。
點擊此處查看解決方案
上面的步驟完事了,接下來運行一下redis-trib.rb
cd /home/xm6f/dev/redis-3.2.4/src ./redis-trib.rb
看到這,應該明白了吧,就是靠上面這些操做完成redis集羣搭建。
確認全部的節點都啓動,接下來使用參數 create 建立 (在192.168.1.160中來建立)
./redis-trib.rb create --replicas 1 192.168.1.105:7000 192.168.1.105:7001 192.168.1.105:7002 192.168.1.160:7003 192.168.1.160:7004 192.168.1.160:7005
注意:
a、--replicas 1參數表示爲每一個主節點建立一個從節點,其餘參數是實例的地址集合。
b、防火牆必定要開放監聽的端口,不然會建立失敗。
[root@localhost redis-cluster]#./redis-trib.rb create --replicas 1 192.168.1.105:7000 192.168.1.105:7001 192.168.1.105:7002 192.168.1.160:7003 192.168.1.160:7004 192.168.1.160:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 Adding replica 127.0.0.1:7004 to 127.0.0.1:7001 Adding replica 127.0.0.1:7005 to 127.0.0.1:7002 Adding replica 127.0.0.1:7006 to 127.0.0.1:7003 M: dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001 slots:0-5460 (5461 slots) master M: e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002 slots:5461-10922 (5462 slots) master M: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003 slots:10923-16383 (5461 slots) master S: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004 replicates dfd510594da614469a93a0a70767ec9145aefb1a S: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005 replicates e02eac35110bbf44c61ff90175e04d55cca097ff S: 10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006 replicates 4385809e6f4952ecb122dbfedbee29109d6bb234 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join...... >>> Performing Cluster Check (using node 127.0.0.1:7001) M: dfd510594da614469a93a0a70767ec9145aefb1a 127.0.0.1:7001 slots:0-5460 (5461 slots) master M: e02eac35110bbf44c61ff90175e04d55cca097ff 127.0.0.1:7002 slots:5461-10922 (5462 slots) master M: 4385809e6f4952ecb122dbfedbee29109d6bb234 127.0.0.1:7003 slots:10923-16383 (5461 slots) master M: ec02c9ef3acee069e8849f143a492db18d4bb06c 127.0.0.1:7004 slots: (0 slots) master replicates dfd510594da614469a93a0a70767ec9145aefb1a M: 83e5a8bb94fb5aaa892cd2f6216604e03e4a6c75 127.0.0.1:7005 slots: (0 slots) master replicates e02eac35110bbf44c61ff90175e04d55cca097ff M: 10c097c429ca24f8720986c6b66f0688bfb901ee 127.0.0.1:7006 slots: (0 slots) master replicates 4385809e6f4952ecb122dbfedbee29109d6bb234 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
上面顯示建立成功,有3個主節點,3個從節點,每一個節點都是成功鏈接狀態。
查看集羣節點信息:./redis-trib.rb check 192.168.1.105:7002 #任意一個節點便可
[root@localhost src]# ./redis-trib.rb check 192.168.1.105:7002 >>> Performing Cluster Check (using node 192.168.1.105:7002) S: 910e14d9655a1e6e7fc007e799006d3f0d1cebe5 192.168.1.105:7002 slots: (0 slots) slave replicates 94f51658302cb5f1d178f14caaa79f27a9ac3703 M: 94f51658302cb5f1d178f14caaa79f27a9ac3703 192.168.1.160:7003 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 28a51a8e34920e2d48fc1650a9c9753ff73dad5d 192.168.1.105:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 6befec567ca7090eb3731e48fd5275a9853fb394 192.168.1.105:7001 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 54ca4fbc71257fd1be5b58d0f545b95d65f8f6b8 192.168.1.160:7005 slots: (0 slots) slave replicates 6befec567ca7090eb3731e48fd5275a9853fb394 S: cf02aa3d58d48215d9d61121eedd194dc5c50eeb 192.168.1.160:7004 slots: (0 slots) slave replicates 28a51a8e34920e2d48fc1650a9c9753ff73dad5d [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
以上集羣安裝成功了。
./redis-trib.rb create --replicas 1 10.104.111.174:7000 10.104.111.174:7001 10.104.111.174:7002 10.135.172.233:7003 10.135.172.233:7004 10.135.172.233:7005
執行該命令一直出現:Waiting for the cluster to join 一直等待
用 redis-trib.rb check 192.168.18.111:6379 檢查,提示 [ERR] Not all 16384 slots are covered by nodes。
解決辦法:把建立節點位置換下便可。
./redis-trib.rb create --replicas 1 10.135.172.233:7003 10.135.172.233:7004 10.135.172.233:7005 10.104.111.174:7000 10.104.111.174:7001 10.104.111.174:7002
執行經過。