Redis 4 叢集搭建
一、概述
Redis3.0版本之後支援Cluster,目前redis cluster支援的特性有:
節點自動發現,slave->master 選舉,叢集容錯,Hot resharding:線上分片
進群管理:cluster xxx,基於配置(nodes-port.conf)的叢集管理,ASK 轉向/MOVED 轉向機制.
二、redis cluster安裝
1、下載和解包
cd /usr/local/
wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar -xvzf redis-4.0.1.tar.gz
2、 編譯安裝
cd redis-4.0.1
make && make install
3、建立redis節點
選擇2臺伺服器,分別為:192.168.59.131,192.168..59.130.每分伺服器有3個節點。
先在192.168.59.131建立3個節點:
cd /usr/local/ //建立叢集目錄 mkdir redis_cluster //分別代表三個節點 其對應埠 7000 7001 7002 mkdir 7000 7001 7002 //以建立7000節點為例,拷貝到7000目錄 cp /usr/local/redis-4.0.1/redis.conf ./redis_cluster/7000/ //拷貝到7001目錄 cp /usr/local/redis-4.0.1/redis.conf ./redis_cluster/7001/ //拷貝到7002目錄 cp /usr/local/redis-4.0.1/redis.conf ./redis_cluster/7002/
分別對7001,7002、7003資料夾中的3個檔案修改對應的配置
daemonize yes //redis後臺執行
pidfile /var/run/redis_7000.pid //pidfile檔案對應7000,7002,7003
bind 192.168.59.131 //繫結ip地址
port 7000 //埠7000,7002,7003
cluster-enabled yes //開啟叢集 把註釋#去掉
cluster-config-file nodes_7000.conf //叢集的配置 配置檔案首次啟動自動生成 7000,7001,7002
cluster-node-timeout 5000 //請求超時 設定5秒夠了
appendonly yes //aof日誌開啟 有需要就開啟,它會每次寫操作都記錄一條日誌
在192.168.59.130建立3個節點:對應的埠改為7003,7004,7005.配置對應的改一下就可以了
4、兩臺機啟動各節點
第一臺機器上執行
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
5、檢視服務
ps -ef | grep redis #檢視是否啟動成功
netstat -tnlp | grep redis #可以看到redis監聽埠
三、建立叢集
前面已經準備好了搭建叢集的redis節點,接下來我們要把這些節點都串連起來搭建叢集。
官方提供了一個工具:redis-trib.rb(/usr/local/redis-4.0.1/src/redis-trib.rb) 看字尾就知道它是用ruby寫的一個程式,所以我們還得安裝ruby.
yum -y install ruby ruby-devel rubygems rpm-build
再用 gem 這個命令來安裝 redis介面 gem是ruby的一個工具包.
gem install redis
接下來執行一下redis-trib.rb
[root@localhost local]# /usr/local/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.
確認所有的節點都啟動,接下來使用引數create 建立 (在192.168.59.131中來建立)
/usr/local/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.59.131:7000 192.168.59.131:7001 192.168.59.131:7003 192.168.59.130:7003 192.168.59.130:7004 192.168.59.130:7005
--replicas 1 表示 自動為每一個master節點分配一個slave節點 上面有6個節點,程式會按照一定規則生成 3個master(主)3個slave(從)
防火牆一定要開放監聽的埠,否則會建立失敗。
執行中,提示Can I set the above configuration? (type 'yes' to accept): yes //輸入yes
[root@localhost local]# /usr/local/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.59.131:7000 192.168.59.131:7001 192.168.59.131:7003 192.168.59.130:7003 192.168.59.130:7004 192.168.59.130:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.59.131:7000
192.168.59.130:7003
192.168.59.131:7001
Adding replica 192.168.59.130:7004 to 192.168.59.131:7000
Adding replica 192.168.59.131:7002 to 192.168.59.130:7003
Adding replica 192.168.59.130:7005 to 192.168.59.131:7001
M: 048937fd4ac542c135e7527f681be12348e8c8d0 192.168.59.131:7000
slots:0-5460 (5461 slots) master
M: 2039bc32bf55d21d74296cdc91a316022a7afc4b 192.168.59.131:7001
slots:10923-16383 (5461 slots) master
S: 76514c28d0a40f9751ccaf117d1f8f1d0878475a 192.168.59.131:7002
replicates a58a304964ff10d10f4323341f7084c9f60f5124
M: a58a304964ff10d10f4323341f7084c9f60f5124 192.168.59.130:7003
slots:5461-10922 (5462 slots) master
S: 7512d76b416d09619d3b639a94945a676d7b8826 192.168.59.130:7004
replicates 048937fd4ac542c135e7527f681be12348e8c8d0
S: 7824b9721cb21de60c8d132d668fd3ac5c4eae49 192.168.59.130:7005
replicates 2039bc32bf55d21d74296cdc91a316022a7afc4b
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.59.131:7000)
M: 048937fd4ac542c135e7527f681be12348e8c8d0 192.168.59.131:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 76514c28d0a40f9751ccaf117d1f8f1d0878475a 192.168.59.131:7002
slots: (0 slots) slave
replicates a58a304964ff10d10f4323341f7084c9f60f5124
M: a58a304964ff10d10f4323341f7084c9f60f5124 192.168.59.130:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 7512d76b416d09619d3b639a94945a676d7b8826 192.168.59.130:7004
slots: (0 slots) slave
replicates 048937fd4ac542c135e7527f681be12348e8c8d0
M: 2039bc32bf55d21d74296cdc91a316022a7afc4b 192.168.59