新聞中心
環(huán)境
- VMware版本號:12.0.0
- centos版本:CentOS 7.3.1611
- 三臺虛擬機(IP):192.168.252.101,192.168.102..102,192.168.252.103
注意事項
安裝 GCC 編譯工具 不然會有編譯不過的問題

創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網(wǎng)站建設、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的隨州網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
$ yum install -y gcc g++ gcc-c++ make升級所有的包,防止出現(xiàn)版本過久不兼容問題
$ yum -y update關(guān)閉防火墻 節(jié)點之前需要開放指定端口,為了方便,生產(chǎn)不要禁用
centos 6.x
$ service iptables stop # 關(guān)閉命令:centos 7.x
$ systemctl stop firewalld.service # 停止firewall集群搭建
安裝 Redis
下載,解壓,編譯安裝
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創(chuàng)建節(jié)點
1.首先在 192.168.252.101機器上 /opt/redis-4.0.1目錄下創(chuàng)建 redis-cluster 目錄
$ mkdir /opt/redis-4.0.1/redis-cluster2.在 redis-cluster 目錄下,創(chuàng)建名為7000、7001、7002的目錄
$ cd /opt/redis-4.0.1/redis-cluster
$ mkdir 7000 7001 7002
3.分別修改這三個配置文件,把如下redis.conf 配置內(nèi)容粘貼進去
$ vi 7000/redis.conf
$ vi 7001/redis.conf
$ vi 7002/redis.confredis.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,需要改為其他節(jié)點機器可訪問的ip,否則創(chuàng)建集群時無法訪問對應的端口,無法創(chuàng)建集群
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)重復以上三步,只是把目錄改為7003、7004、7005、7006、7007、7008對應的配置文件也按照這個規(guī)則修改即可
啟動集群
#第一臺機器上執(zhí)行 3個節(jié)點
$ 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
#第二臺機器上執(zhí)行 3個節(jié)點
$ 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
#第三臺機器上執(zhí)行 3個節(jié)點
$ 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 各個節(jié)點啟動情況
$ ps -ef | grep redis //redis是否啟動成功
$ netstat -tnlp | grep redis //監(jiān)聽redis端口安裝 Ruby
$ yum -y install ruby ruby-devel rubygems rpm-build $ gem install redis創(chuàng)建集群
注意:在任意一臺上運行 不要在每臺機器上都運行,一臺就夠了
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: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出現(xiàn)以下內(nèi)容
[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.關(guān)閉集群
這樣也可以,推薦
$ pkill redis循環(huán)節(jié)點逐個關(guān)閉
$ 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集群驗證
連接集群測試
參數(shù) -C 可連接到集群,因為 redis.conf 將 bind 改為了ip地址,所以 -h 參數(shù)不可以省略,-p 參數(shù)為端口號
- 我們在192.168.252.101機器redis 7000 的節(jié)點set 一個key
$ /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>發(fā)現(xiàn)redis set name 之后重定向到192.168.252.102機器 redis 7003 這個節(jié)點
- 我們在192.168.252.103機器redis 7008 的節(jié)點get一個key
[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>發(fā)現(xiàn)redis get name 重定向到192.168.252.102機器 redis 7003 這個節(jié)點
如果您看到這樣的現(xiàn)象,說明集群已經(jīng)是可用的了
檢查集群狀態(tài)
$ /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.
列出集群節(jié)點
列出集群當前已知的所有節(jié)點(node),以及這些節(jié)點的相關(guān)信息
$ /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 :列出集群當前已知的所有節(jié)點( node),以及這些節(jié)點的相關(guān)信息。節(jié)點
cluster meet :將 ip 和 port 所指定的節(jié)點添加到集群當中,讓它成為集群的一份子。
cluster forget :從集群中移除 node_id 指定的節(jié)點。
cluster replicate :將當前節(jié)點設置為 node_id 指定的節(jié)點的從節(jié)點。
cluster saveconfig :將節(jié)點的配置文件保存到硬盤里面。
槽(slot)
cluster addslots [slot ...] :將一個或多個槽( slot)指派( assign)給當前節(jié)點。
cluster delslots [slot ...] :移除一個或多個槽對當前節(jié)點的指派。
cluster flushslots :移除指派給當前節(jié)點的所有槽,讓當前節(jié)點變成一個沒有指派任何槽的節(jié)點。
cluster setslot node :將槽 slot 指派給 node_id 指定的節(jié)點,如果槽已經(jīng)指派給另一個節(jié)點,那么先讓另一個節(jié)點刪除該槽>,然后再進行指派。
cluster setslot migrating :將本節(jié)點的槽 slot 遷移到 node_id 指定的節(jié)點中。
cluster setslot importing :從 node_id 指定的節(jié)點中導入槽 slot 到本節(jié)點。
cluster setslot stable :取消對槽 slot 的導入( import)或者遷移( migrate)。 鍵
cluster keyslot :計算鍵 key 應該被放置在哪個槽上。
cluster countkeysinslot :返回槽 slot 目前包含的鍵值對數(shù)量。
cluster getkeysinslot :返回 count 個 slot 槽中的鍵 。 分享標題:CentS 7.3 搭建 Redis-4.0.1 Cluster 集群服務
網(wǎng)頁地址:http://fisionsoft.com.cn/article/dhocjpc.html


咨詢
建站咨詢
