本文轉載自:http://www.ymq.ionode
安裝 GCC 編譯工具 否則會有編譯不過的問題c++
$ yum install -y gcc g++ gcc-c++ make
升級全部的包,防止出現版本太久不兼容問題redis
$ yum -y update
關閉防火牆 節點以前須要開放指定端口,爲了方便,生產不要禁用centos
centos 6.xruby
$ service iptables stop # 關閉命令:
centos 7.x微信
$ systemctl stop firewalld.service # 中止firewall
下載,解壓,編譯安裝app
cd /opt $ wget http://download.redis.io/releases/redis-4.0.1.tar.gz $ tar xzf redis-4.0.1.tar.gz $ cd redis-4.0.1 $ make
若是由於上次編譯失敗,有殘留的文件工具
$ make distclean
1.首先在 192.168.252.101機器上 /opt/redis-4.0.1目錄下建立redis-cluster目錄測試
$ mkdir /opt/redis-4.0.1/redis-cluster
2.在redis-cluster目錄下,建立名爲7000、700一、7002的目錄ui
$ cd /opt/redis-4.0.1/redis-cluster $ mkdir 7000 7001 7002
3.分別修改這三個配置文件,把以下redis.conf配置內容粘貼進去
$ vi 7000/redis.conf $ vi 7001/redis.conf $ vi 7002/redis.conf
redis.conf 配置
port 7000 bind 192.168.252.101 daemonize yes pidfile /var/run/redis_7000.pid cluster-enabled yes cluster-config-file nodes_7000.conf cluster-node-timeout 10100 appendonly yes
redis.conf 配置說明
#端口7000,7001,7002 port 7000 #默認ip爲127.0.0.1,須要改成其餘節點機器可訪問的ip,不然建立集羣時沒法訪問對應的端口,沒法建立集羣 bind 192.168.252.101 #redis後臺運行 daemonize yes #pidfile文件對應7000,7001,7002 pidfile /var/run/redis_7000.pid #開啓集羣,把註釋#去掉 cluster-enabled yes #集羣的配置,配置文件首次啓動自動生成 7000,7001,7002 cluster-config-file nodes_7000.conf #請求超時,默認15秒,可自行設置 cluster-node-timeout 10100 #aof日誌開啓,有須要就開啓,它會每次寫操做都記錄一條日誌 appendonly yes
接着在另外兩臺機器上(192.168.252.102,192.168.252.103)上重複以上三步,只是把目錄改成700三、700四、700五、700六、700七、7008對應的配置文件也按照這個規則修改便可
#第一臺機器上執行 3個節點 $ for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done #第二臺機器上執行 3個節點 $ for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done #第三臺機器上執行 3個節點 $ for((i=6;i<=8;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done
檢查各 Redis 各個節點啓動狀況
$ ps -ef | grep redis //redis是否啓動成功 $ netstat -tnlp | grep redis //監聽redis端口
$ yum -y install ruby ruby-devel rubygems rpm-build $ gem install redis
注意:在任意一臺上運行 不要在每臺機器上都運行,一臺就夠了
Redis 官方提供了redis-trib.rb這個工具,就在解壓目錄的 src 目錄中
$ /opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.252.101:7000 192.168.252.101:7001192.168.252.101:7002 192.168.252.102:7003 192.168.252.102:7004 192.168.252.102:7005192.168.252.103:7006 192.168.252.103:7007 192.168.252.103:7008
出現如下內容
[root@localhost redis-cluster]# /opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.252.101:7000 192.168.252.101:7001 192.168.252.101:7002 192.168.252.102:7003 192.168.252.102:7004 192.168.252.102:7005 192.168.252.103:7006 192.168.252.103:7007 192.168.252.103:7008 >>> Creating cluster >>> Performing hash slots allocation on 9 nodes... Using 4 masters: 192.168.252.101:7000 192.168.252.102:7003 192.168.252.103:7006 192.168.252.101:7001 Adding replica 192.168.252.102:7004 to 192.168.252.101:7000 Adding replica 192.168.252.103:7007 to 192.168.252.102:7003 Adding replica 192.168.252.101:7002 to 192.168.252.103:7006 Adding replica 192.168.252.102:7005 to 192.168.252.101:7001 Adding replica 192.168.252.103:7008 to 192.168.252.101:7000 M: 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 192.168.252.101:7000 slots:0-4095 (4096 slots) master M: 44c81c15b01d992cb9ede4ad35477ec853d70723 192.168.252.101:7001 slots:12288-16383 (4096 slots) master S: 38f03c27af39723e1828eb62d1775c4b6e2c3638 192.168.252.101:7002 replicates f1abb62a8c9b448ea14db421bdfe3f1d8075189c M: 987965baf505a9aa43e50e46c76189c51a8f17ec 192.168.252.102:7003 slots:4096-8191 (4096 slots) master S: 6555292fed9c5d52fcf5b983c441aff6f96923d5 192.168.252.102:7004 replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf S: 2b5ba254a0405d4efde4c459867b15176f79244a 192.168.252.102:7005 replicates 44c81c15b01d992cb9ede4ad35477ec853d70723 M: f1abb62a8c9b448ea14db421bdfe3f1d8075189c 192.168.252.103:7006 slots:8192-12287 (4096 slots) master S: eb4067373d36d8a8df07951f92794e67a6aac022 192.168.252.103:7007 replicates 987965baf505a9aa43e50e46c76189c51a8f17ec S: 2919e041dd3d1daf176d6800dcd262f4e727f366 192.168.252.103:7008 replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf Can I set the above configuration? (type 'yes' to accept): yes
輸入 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 192.168.252.101:7000) M: 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 192.168.252.101:7000 slots:0-4095 (4096 slots) master 2 additional replica(s) S: 6555292fed9c5d52fcf5b983c441aff6f96923d5 192.168.252.102:7004 slots: (0 slots) slave replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf M: 44c81c15b01d992cb9ede4ad35477ec853d70723 192.168.252.101:7001 slots:12288-16383 (4096 slots) master 1 additional replica(s) S: 2919e041dd3d1daf176d6800dcd262f4e727f366 192.168.252.103:7008 slots: (0 slots) slave replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf M: f1abb62a8c9b448ea14db421bdfe3f1d8075189c 192.168.252.103:7006 slots:8192-12287 (4096 slots) master 1 additional replica(s) S: eb4067373d36d8a8df07951f92794e67a6aac022 192.168.252.103:7007 slots: (0 slots) slave replicates 987965baf505a9aa43e50e46c76189c51a8f17ec S: 38f03c27af39723e1828eb62d1775c4b6e2c3638 192.168.252.101:7002 slots: (0 slots) slave replicates f1abb62a8c9b448ea14db421bdfe3f1d8075189c S: 2b5ba254a0405d4efde4c459867b15176f79244a 192.168.252.102:7005 slots: (0 slots) slave replicates 44c81c15b01d992cb9ede4ad35477ec853d70723 M: 987965baf505a9aa43e50e46c76189c51a8f17ec 192.168.252.102:7003 slots:4096-8191 (4096 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
這樣也能夠,推薦
$ pkill redis
循環節點逐個關閉
$ for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 192.168.252.101 -p 700$i shutdown; done $ for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 192.168.252.102 -p 700$i shutdown; done $ for((i=6;i<=8;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 192.168.252.103 -p 700$i shutdown; done
參數 -C 可鏈接到集羣,由於 redis.conf 將 bind 改成了ip地址,因此 -h 參數不能夠省略,-p 參數爲端口號
$ /opt/redis-4.0.1/src/redis-cli -h 192.168.252.101 -c -p 7000 192.168.252.101:7000> set name www.ymq.io -> Redirected to slot [5798] located at 192.168.252.102:7003 OK 192.168.252.102:7003> get name "www.ymq.io" 192.168.252.102:7003>
發現redis set name 以後重定向到192.168.252.102機器 redis 7003 這個節點
[root@localhost redis-cluster]# /opt/redis-4.0.1/src/redis-cli -h 192.168.252.103 -c -p 7008 192.168.252.103:7008> get name -> Redirected to slot [5798] located at 192.168.252.102:7003 "www.ymq.io" 192.168.252.102:7003>
發現redis get name 重定向到192.168.252.102機器 redis 7003 這個節點。若是您看到這樣的現象,說明集羣已是可用的了
$ /opt/redis-4.0.1/src/redis-trib.rb check 192.168.252.101:7000
>>> Performing Cluster Check (using node 192.168.252.101:7000) M: 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 192.168.252.101:7000 slots:0-4095 (4096 slots) master 2 additional replica(s) S: 6555292fed9c5d52fcf5b983c441aff6f96923d5 192.168.252.102:7004 slots: (0 slots) slave replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf M: 44c81c15b01d992cb9ede4ad35477ec853d70723 192.168.252.101:7001 slots:12288-16383 (4096 slots) master 1 additional replica(s) S: 2919e041dd3d1daf176d6800dcd262f4e727f366 192.168.252.103:7008 slots: (0 slots) slave replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf M: f1abb62a8c9b448ea14db421bdfe3f1d8075189c 192.168.252.103:7006 slots:8192-12287 (4096 slots) master 1 additional replica(s) S: eb4067373d36d8a8df07951f92794e67a6aac022 192.168.252.103:7007 slots: (0 slots) slave replicates 987965baf505a9aa43e50e46c76189c51a8f17ec S: 38f03c27af39723e1828eb62d1775c4b6e2c3638 192.168.252.101:7002 slots: (0 slots) slave replicates f1abb62a8c9b448ea14db421bdfe3f1d8075189c S: 2b5ba254a0405d4efde4c459867b15176f79244a 192.168.252.102:7005 slots: (0 slots) slave replicates 44c81c15b01d992cb9ede4ad35477ec853d70723 M: 987965baf505a9aa43e50e46c76189c51a8f17ec 192.168.252.102:7003 slots:4096-8191 (4096 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
列出集羣當前已知的全部節點(node),以及這些節點的相關信息
$ /opt/redis-4.0.1/src/redis-cli -h 192.168.252.101 -c -p 7000 192.168.252.101:7000> cluster nodes
6555292fed9c5d52fcf5b983c441aff6f96923d5 192.168.252.102:7004@17004 slave 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 0 1502815268317 5 connected 44c81c15b01d992cb9ede4ad35477ec853d70723 192.168.252.101:7001@17001 master - 0 1502815268000 2 connected 12288-16383 2919e041dd3d1daf176d6800dcd262f4e727f366 192.168.252.103:7008@17008 slave 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 0 1502815269000 9 connected 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 192.168.252.101:7000@17000 myself,master - 0 1502815269000 1 connected 0-4095 f1abb62a8c9b448ea14db421bdfe3f1d8075189c 192.168.252.103:7006@17006 master - 0 1502815269000 7 connected 8192-12287 eb4067373d36d8a8df07951f92794e67a6aac022 192.168.252.103:7007@17007 slave 987965baf505a9aa43e50e46c76189c51a8f17ec 0 1502815267000 8 connected 38f03c27af39723e1828eb62d1775c4b6e2c3638 192.168.252.101:7002@17002 slave f1abb62a8c9b448ea14db421bdfe3f1d8075189c 0 1502815269327 7 connected 2b5ba254a0405d4efde4c459867b15176f79244a 192.168.252.102:7005@17005 slave 44c81c15b01d992cb9ede4ad35477ec853d70723 0 1502815270336 6 connected 987965baf505a9aa43e50e46c76189c51a8f17ec 192.168.252.102:7003@17003 master - 0 1502815271345 4 connected 4096-8191 192.168.252.101:7000>
$ 192.168.252.101:7000> cluster info
cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:9 cluster_size:4 cluster_current_epoch:9 cluster_my_epoch:1 cluster_stats_messages_ping_sent:485 cluster_stats_messages_pong_sent:485 cluster_stats_messages_sent:970 cluster_stats_messages_ping_received:477 cluster_stats_messages_pong_received:485 cluster_stats_messages_meet_received:8 cluster_stats_messages_received:970 192.168.252.101:7000>
語法格式
redis-cli -c -p port
cluster info :打印集羣的信息 cluster nodes :列出集羣當前已知的全部節點( node),以及這些節點的相關信息。
cluster meet <ip> <port> :將 ip 和 port 所指定的節點添加到集羣當中,讓它成爲集羣的一份子。 cluster forget <node_id> :從集羣中移除 node_id 指定的節點。 cluster replicate <node_id> :將當前節點設置爲 node_id 指定的節點的從節點。 cluster saveconfig :將節點的配置文件保存到硬盤裏面。
cluster addslots <slot> [slot ...] :將一個或多個槽( slot)指派( assign)給當前節點。 cluster delslots <slot> [slot ...] :移除一個或多個槽對當前節點的指派。 cluster flushslots :移除指派給當前節點的全部槽,讓當前節點變成一個沒有指派任何槽的節點。 cluster setslot <slot> node <node_id> :將槽 slot 指派給 node_id 指定的節點,若是槽已經指派給另外一個節點,那麼先讓另外一個節點刪除該槽>,而後再進行指派。 cluster setslot <slot> migrating <node_id> :將本節點的槽 slot 遷移到 node_id 指定的節點中。 cluster setslot <slot> importing <node_id> :從 node_id 指定的節點中導入槽 slot 到本節點。 cluster setslot <slot> stable :取消對槽 slot 的導入( import)或者遷移( migrate)。
cluster keyslot <key> :計算鍵 key 應該被放置在哪一個槽上。 cluster countkeysinslot <slot> :返回槽 slot 目前包含的鍵值對數量。 cluster getkeysinslot <slot> <count> :返回 count 個 slot 槽中的鍵 。
爲了方便你們交流,本人開通了微信公衆號和QQ羣,QQ羣:291519319,喜歡技術的一塊兒來交流吧