redis 集羣 搭建(非哨兵)

環境

#environment
OS: CentOS 7.2 64

#ip
192.168.21.137~139

redis 節點

目錄和文件準備

#三臺各自執行
mkdir /redis
wget -P /redis http://download.redis.io/releases/redis-4.0.1.tar.gz

#安裝
cd redis-4.0.1
make && make install

節點

#經過配置文件給每臺服務器配置三個redis節點
mkdir -p /redis_cluster/6001 /redis_cluster/6002 /redis_cluster/6003
cp /redis/redis-4.0.1/redis.conf  /redis_cluster/6001/
cp /redis/redis-4.0.1/redis.conf  /redis_cluster/6002/
cp /redis/redis-4.0.1/redis.conf  /redis_cluster/6003/

#配置
#redis後臺運行
daemonize    yes   
                      
#pidfile文件 6001~6003
pidfile  /var/run/redis_6000.pid   
      
#6001~6003
port  7000          
                     
#開啓集羣
cluster-enabled  yes   
                  
#配置文件首次啓動自動生成
cluster-config-file  nodes_6001.conf 
    
#請求超時
cluster-node-timeout  5000    
           
#aof日誌
appendonly  no  

#綁定地址,須要別的機器能ping通的地址
bind 192.168.21.137

啓動

redis-server  /redis_cluster/6001/redis.conf
redis-server  /redis_cluster/6002/redis.conf
redis-server  /redis_cluster/6003/redis.conf

檢查是否啓動成功及信息

[root@localhost /]# ps -ef | grep redis
root      16700      1  0 23:08 ?        00:00:00 redis-server 127.0.0.1:6001
root      16705      1  0 23:08 ?        00:00:00 redis-server 127.0.0.1:6002
root      16710      1  0 23:08 ?        00:00:00 redis-server 127.0.0.1:6003
root      16715  12065  0 23:08 pts/1    00:00:00 grep --color=auto redis
[root@localhost /]# netstat -tnlp | grep redis
tcp        0      0 127.0.0.1:6001          0.0.0.0:*               LISTEN      16700/redis-server  
tcp        0      0 127.0.0.1:6002          0.0.0.0:*               LISTEN      16705/redis-server  
tcp        0      0 127.0.0.1:6003          0.0.0.0:*               LISTEN      16710/redis-server  
[root@localhost /]#

至此,redis 節點準備完成。node

搭建集羣

redis 官方提供了一個 redis-trib.rb (src目錄下) 工具用於搭建集羣。很明顯是 ruby 寫的,因此須要 ruby 環境。redis

安裝 ruby

yum -y install ruby ruby-devel rubygems rpm-build

用 gem 安裝 redis 接口

[root@localhost /]# gem install redis
Fetching: redis-3.3.3.gem (100%)
Successfully installed redis-3.3.3
Parsing documentation for redis-3.3.3
Installing ri documentation for redis-3.3.3
1 gem installed

確保開放端口,可參考個人另外一個微博 Linux 開放端口
運行 redis-trib.rb 查看 集羣建立幫助segmentfault

[root@localhost /]# /redis/redis-4.0.1/src/redis-trib.rb 
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port
  info            host:port
  fix             host:port
                  --timeout <arg>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  del-node        host:port node_id
  set-timeout     host:port milliseconds
  call            host:port command arg arg .. arg
  import          host:port
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

建立集羣

#其中 --replicas  2 意思爲爲每一個 master 分配 2 各 slave
[root@localhost ~]# /redis/redis-4.0.1/src/redis-trib.rb  create  --replicas  2  192.168.21.137:6001 192.168.21.137:6002 192.168.21.137:6003 192.168.21.138:6001 192.168.21.138:6002 192.168.21.138:6003 192.168.21.139:6001 192.168.21.139:6002 192.168.21.139:6003
>>> Creating cluster
>>> Performing hash slots allocation on 9 nodes...
Using 3 masters:
192.168.21.137:6001
192.168.21.138:6001
192.168.21.139:6001
Adding replica 192.168.21.138:6002 to 192.168.21.137:6001
Adding replica 192.168.21.139:6002 to 192.168.21.137:6001
Adding replica 192.168.21.137:6002 to 192.168.21.138:6001
Adding replica 192.168.21.137:6003 to 192.168.21.138:6001
Adding replica 192.168.21.138:6003 to 192.168.21.139:6001
Adding replica 192.168.21.139:6003 to 192.168.21.139:6001
M: 40217a88d10bb85bdd0d082df24f036435b82513 192.168.21.137:6001
   slots:0-5460 (5461 slots) master
S: bc847ed99feac417a066baaa48fa0748ac409c67 192.168.21.137:6002
   replicates 4a391d4987ee5f0cf342108890cf8236d56ea412
S: 840ab2d448387c801b67a958543090ce1d8ee1ba 192.168.21.137:6003
   replicates 4a391d4987ee5f0cf342108890cf8236d56ea412
M: 4a391d4987ee5f0cf342108890cf8236d56ea412 192.168.21.138:6001
   slots:5461-10922 (5462 slots) master
S: b977d15fbeaa3e88b830bf5b6caa22a27a2dce8f 192.168.21.138:6002
   replicates 40217a88d10bb85bdd0d082df24f036435b82513
S: bd0fdf29c06fb27b2aa9eca478ec20ab9daf81da 192.168.21.138:6003
   replicates c32b18ec7a947f01bde73a3c93abc31d60b060ee
M: c32b18ec7a947f01bde73a3c93abc31d60b060ee 192.168.21.139:6001
   slots:10923-16383 (5461 slots) master
S: e323d818a7be4149053669552230f4a98332734c 192.168.21.139:6002
   replicates 40217a88d10bb85bdd0d082df24f036435b82513
S: 3bcd9cf11589f418fe5cbc1895ac5a23b0b6714e 192.168.21.139:6003
   replicates c32b18ec7a947f01bde73a3c93abc31d60b060ee
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 192.168.21.137:6001)
M: 40217a88d10bb85bdd0d082df24f036435b82513 192.168.21.137:6001
   slots:0-5460 (5461 slots) master
   2 additional replica(s)
S: bc847ed99feac417a066baaa48fa0748ac409c67 192.168.21.137:6002
   slots: (0 slots) slave
   replicates 4a391d4987ee5f0cf342108890cf8236d56ea412
S: 3bcd9cf11589f418fe5cbc1895ac5a23b0b6714e 192.168.21.139:6003
   slots: (0 slots) slave
   replicates c32b18ec7a947f01bde73a3c93abc31d60b060ee
S: bd0fdf29c06fb27b2aa9eca478ec20ab9daf81da 192.168.21.138:6003
   slots: (0 slots) slave
   replicates c32b18ec7a947f01bde73a3c93abc31d60b060ee
S: 840ab2d448387c801b67a958543090ce1d8ee1ba 192.168.21.137:6003
   slots: (0 slots) slave
   replicates 4a391d4987ee5f0cf342108890cf8236d56ea412
M: c32b18ec7a947f01bde73a3c93abc31d60b060ee 192.168.21.139:6001
   slots:10923-16383 (5461 slots) master
   2 additional replica(s)
S: b977d15fbeaa3e88b830bf5b6caa22a27a2dce8f 192.168.21.138:6002
   slots: (0 slots) slave
   replicates 40217a88d10bb85bdd0d082df24f036435b82513
M: 4a391d4987ee5f0cf342108890cf8236d56ea412 192.168.21.138:6001
   slots:5461-10922 (5462 slots) master
   2 additional replica(s)
S: e323d818a7be4149053669552230f4a98332734c 192.168.21.139:6002
   slots: (0 slots) slave
   replicates 40217a88d10bb85bdd0d082df24f036435b82513
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

測試集羣

#在 192.168.21.138:6001 機器上設置一個鍵值對 testKey--testValue ,由於綁定了 IP ,因此 h 參數不能省略
[root@localhost ~]# redis-cli -h 192.168.21.138 -c -p 6001
192.168.21.138:6001> set testKey testValue
-> Redirected to slot [5203] located at 192.168.21.137:6001
OK
192.168.21.137:6001> get testKey
"testValue"
192.168.21.137:6001> 

.
.
.

#在 192.168.21.137:6003 上獲取 testKey 的值,發現自動定向到 192.168.21.137:6001,說明成功
[root@localhost ~]# redis-cli -h 192.168.21.137 -c -p 6003
192.168.21.137:6003> get testKey
-> Redirected to slot [5203] located at 192.168.21.137:6001
"testValue"
192.168.21.137:6001> 

.
.
.

#在 192.168.21.139:6002 上獲取 testKey 的值,發現自動定向到 192.168.21.137:6001,說明成功
[root@localhost ~]# redis-cli -h 192.168.21.139 -c -p 6002
192.168.21.139:6002> get testKey
-> Redirected to slot [5203] located at 192.168.21.137:6001
"testValue"
192.168.21.137:6001>

至此,Redis 集羣搭建完成ruby

集羣操做

檢查狀態

[root@localhost ~]# /redis/redis-4.0.1/src/redis-trib.rb check 192.168.21.139:6002
>>> Performing Cluster Check (using node 192.168.21.139:6002)
S: e323d818a7be4149053669552230f4a98332734c 192.168.21.139:6002
   slots: (0 slots) slave
   replicates 40217a88d10bb85bdd0d082df24f036435b82513
S: 840ab2d448387c801b67a958543090ce1d8ee1ba 192.168.21.137:6003
   slots: (0 slots) slave
   replicates 4a391d4987ee5f0cf342108890cf8236d56ea412
S: bd0fdf29c06fb27b2aa9eca478ec20ab9daf81da 192.168.21.138:6003
   slots: (0 slots) slave
   replicates c32b18ec7a947f01bde73a3c93abc31d60b060ee
M: c32b18ec7a947f01bde73a3c93abc31d60b060ee 192.168.21.139:6001
   slots:10923-16383 (5461 slots) master
   2 additional replica(s)
S: b977d15fbeaa3e88b830bf5b6caa22a27a2dce8f 192.168.21.138:6002
   slots: (0 slots) slave
   replicates 40217a88d10bb85bdd0d082df24f036435b82513
S: bc847ed99feac417a066baaa48fa0748ac409c67 192.168.21.137:6002
   slots: (0 slots) slave
   replicates 4a391d4987ee5f0cf342108890cf8236d56ea412
M: 4a391d4987ee5f0cf342108890cf8236d56ea412 192.168.21.138:6001
   slots:5461-10922 (5462 slots) master
   2 additional replica(s)
M: 40217a88d10bb85bdd0d082df24f036435b82513 192.168.21.137:6001
   slots:0-5460 (5461 slots) master
   2 additional replica(s)
S: 3bcd9cf11589f418fe5cbc1895ac5a23b0b6714e 192.168.21.139:6003
   slots: (0 slots) slave
   replicates c32b18ec7a947f01bde73a3c93abc31d60b060ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看全部節點信息

[root@localhost ~]# redis-cli -h 192.168.21.138 -c -p 6001
192.168.21.138:6001> cluster nodes
e323d818a7be4149053669552230f4a98332734c 192.168.21.139:6002@16002 slave 40217a88d10bb85bdd0d082df24f036435b82513 0 1503628318000 8 connected
bc847ed99feac417a066baaa48fa0748ac409c67 192.168.21.137:6002@16002 slave 4a391d4987ee5f0cf342108890cf8236d56ea412 0 1503628318620 4 connected
c32b18ec7a947f01bde73a3c93abc31d60b060ee 192.168.21.139:6001@16001 master - 0 1503628317000 7 connected 10923-16383
bd0fdf29c06fb27b2aa9eca478ec20ab9daf81da 192.168.21.138:6003@16003 slave c32b18ec7a947f01bde73a3c93abc31d60b060ee 0 1503628318000 7 connected
40217a88d10bb85bdd0d082df24f036435b82513 192.168.21.137:6001@16001 master - 0 1503628318117 1 connected 0-5460
4a391d4987ee5f0cf342108890cf8236d56ea412 192.168.21.138:6001@16001 myself,master - 0 1503628317000 4 connected 5461-10922
840ab2d448387c801b67a958543090ce1d8ee1ba 192.168.21.137:6003@16003 slave 4a391d4987ee5f0cf342108890cf8236d56ea412 0 1503628318519 4 connected
3bcd9cf11589f418fe5cbc1895ac5a23b0b6714e 192.168.21.139:6003@16003 slave c32b18ec7a947f01bde73a3c93abc31d60b060ee 0 1503628317111 9 connected
b977d15fbeaa3e88b830bf5b6caa22a27a2dce8f 192.168.21.138:6002@16002 slave 40217a88d10bb85bdd0d082df24f036435b82513 0 1503628317111 5 connected
192.168.21.138:6001> 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:3
cluster_current_epoch:9
cluster_my_epoch:4
cluster_stats_messages_ping_sent:4391
cluster_stats_messages_pong_sent:4314
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:8707
cluster_stats_messages_ping_received:4308
cluster_stats_messages_pong_received:4393
cluster_stats_messages_meet_received:6
cluster_stats_messages_received:8707
192.168.21.138:6001>

節點命令

#將 ip 和 port 所在的節點添加到集羣當中
cluster meet <ip> <port> 

#從集羣中移除 node_id 指定的節點。
cluster forget <node_id> 

#將當前結點設置爲 node_id 的 slave(從節點)
cluster replicate <node_id>

#保存節點配置文件
cluster saveconfig

槽命令

#將一個或多個槽( slot)指派( assign)給當前節點。
cluster addslots <slot> [slot ...] 

#移除一個或多個槽對當前節點的指派。
cluster delslots <slot> [slot ...] 

#移除指派給當前節點的全部槽,讓當前節點變成一個沒有指派任何槽的節點。
cluster flushslots 

#將槽 slot 指派給 node_id 指定的節點,若是槽已經指派給另外一個節點,那麼先讓另外一個節點刪除該槽>,而後再進行指派。
cluster setslot <slot> node <node_id> 

#將本節點的槽 slot 遷移到 node_id 指定的節點中。
cluster setslot <slot> migrating <node_id> 

#從 node_id 指定的節點中導入槽 slot 到本節點。
cluster setslot <slot> importing <node_id> 

#取消對槽 slot 的導入( import)或者遷移( migrate)。
cluster setslot <slot> stable

#計算鍵 key 應該被放置在哪一個槽上。
cluster keyslot <key> 

#返回槽 slot 目前包含的鍵值對數量。
cluster countkeysinslot <slot> 

#返回 count 個 slot 槽中的鍵 。
cluster getkeysinslot <slot> <count>

腳本

啓動節點

#!/bin/bash
for i in 1 2 3
do
        /redis/redis-4.0.1/src/redis-server /redis_cluster/600$i/redis.conf;
done

啓動集羣

#!/bin/bash
/redis/redis-4.0.1/src/redis-trib.rb  create  --replicas  2  192.168.21.137:6001 192.168.21.137:6002 192.168.21.137:6003 192.168.21.138:6001 192.168.21.138:6002 192.168.21.138:6003 192.168.21.139:6001 192.168.21.139:6002 192.168.21.139:6003
關閉
#!/bin/bash
for i in 137 138 139
do
        for j in 1 2 3
        do
                /redis/redis-4.0.1/src/redis-cli -c -h 192.168.21.$i -p 600$j shutdown;
        done
done
相關文章
相關標籤/搜索