1. 程式人生 > >Redis Cluster 安裝配置

Redis Cluster 安裝配置

伺服器

CentOS
Centos 伺服器初始環境配置最好先配置好,伺服器時間最好配置為一致
我用的是6個伺服器,一個伺服器一個埠,便於配置檔案的修改

首先下載redis3 到本地(需要3以後的版本,我下載的最新的版本:3.0.4)

wget http://download.redis.io/releases/redis-3.0.4.tar.gz

分別上傳到每個伺服器

scp -r redis-3.0.4.tar.gz redis@redis1:~/

分別在每個伺服器上安裝 gcc tcl ruby rubygems gem_redis

sudo yum -y install gcc
sudo
yum -y install tcl sudo yum -y install ruby rubygems sudo gem install redis --version 3.0.4

(gem_redis如果裝不上,可多試幾次,若是ubuntu,換源,換成taobao,建議伺服器用centOS)

解壓安裝redis

tar -zxvf redis-3.0.4.tar.gz
cd redis-3.0.4/
sudo make test
sudo make install

修改配置檔案redis.conf 以埠9000為例

sudo vim redis.conf

port 9000
pidfile /var
/run/redis-9000.pid dbfilename dump-9000.rdb appendfilename "appendonly-9000.aof" cluster-config-file nodes-9000.conf cluster-enabled yes cluster-node-timeout 5000 appendonly yes

然後分別在每個伺服器上啟動redis

src/redis-server redis.conf

檢視每個埠是否成功開啟

ps -aux | grep redis

結果如下:

redis 26674 1 0 Sep15 ? 00:01:21 src/redis-server
*:9000 [cluster]

如果後面有[cluster] 證明開啟成功

然後建立叢集

src/redis-trib.rb create --replicas 1 192.168.32.195:9000 192.168.32.196:9000
192.168.32.197:9000 192.168.32.198:9000 192.168.32.199:9000 192.168.6.214:9000

(master 至少有三個 1 代表 一個master 對應一個slave)

如果下面這樣就代表成功(埠 9000 )

>>> Creating cluster
Connecting to node 192.168.32.195:9000: OK
Connecting to node 192.168.32.196:9000: OK
Connecting to node 192.168.32.197:9000: OK
Connecting to node 192.168.32.198:9000: OK
Connecting to node 192.168.32.199:9000: OK
Connecting to node 192.168.6.214:9000: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.32.199:9000
192.168.32.198:9000
192.168.32.197:9000
Adding replica 192.168.32.196:9000 to 192.168.32.199:9000
Adding replica 192.168.32.195:9000 to 192.168.32.198:9000
Adding replica 192.168.6.214:9000 to 192.168.32.197:9000
S: f4c625f49b6c1b073a379c41073b9ff9786f45f6 192.168.32.195:9000
replicates 20ecac926dba10a056f4d36a72bed35bbb7fcd57
S: 77e68bf6eb86d8bf6cb993b32d726e2cd4d05a54 192.168.32.196:9000
replicates 3276d09525fff0195dd366c8d05f80773d7d9af3
M: c6f4504b2ceb8bcffb682483168b8016e6f5973e 192.168.32.197:9000
slots:10923-16383 (5461 slots) master
M: 20ecac926dba10a056f4d36a72bed35bbb7fcd57 192.168.32.198:9000
slots:5461-10922 (5462 slots) master
M: 3276d09525fff0195dd366c8d05f80773d7d9af3 192.168.32.199:9000
slots:0-5460 (5461 slots) master
S: 627c0ae61d75570dc15a03cbdb03b1a8abd56f58 192.168.6.214:9000
replicates c6f4504b2ceb8bcffb682483168b8016e6f5973e
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.32.195:9000)
M: f4c625f49b6c1b073a379c41073b9ff9786f45f6 192.168.32.195:9000
slots: (0 slots) master
replicates 20ecac926dba10a056f4d36a72bed35bbb7fcd57
M: 77e68bf6eb86d8bf6cb993b32d726e2cd4d05a54 192.168.32.196:9000
slots: (0 slots) master
replicates 3276d09525fff0195dd366c8d05f80773d7d9af3
M: c6f4504b2ceb8bcffb682483168b8016e6f5973e 192.168.32.197:9000
slots:10923-16383 (5461 slots) master
M: 20ecac926dba10a056f4d36a72bed35bbb7fcd57 192.168.32.198:9000
slots:5461-10922 (5462 slots) master
M: 3276d09525fff0195dd366c8d05f80773d7d9af3 192.168.32.199:9000
slots:0-5460 (5461 slots) master
M: 627c0ae61d75570dc15a03cbdb03b1a8abd56f58 192.168.6.214:9000
slots: (0 slots) master
replicates c6f4504b2ceb8bcffb682483168b8016e6f5973e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

以叢集的方式登陸(9000為例)

redis-cli -c -p 9000

檢視 主從關係

$ 127.0.0.1:9000> cluster nodes
baa1779a73a8d329dfc96104ac3116a24b16a681 192.168.32.199:9000 myself,master - 0 0
5 connected 0-5460
140a8c76dfd44a6e43045d43a4d853fa330dfd42 192.168.32.198:9000 master - 0
1442403482170 12 connected 5461-10922
c99974ae9a92ed18166d580e4d7bb438a26ad3a2 192.168.32.195:9000 slave
140a8c76dfd44a6e43045d43a4d853fa330dfd42 0 1442403481669 12 connected
e07467f74f81f31c024bb5626c37e550396eebe5 192.168.6.214:9000 master - 0
1442403481168 9 connected 10923-16383
04f3bcd569fb325e77a109a530fd376eb84512ce 192.168.32.196:9000 slave
baa1779a73a8d329dfc96104ac3116a24b16a681 0 1442403480667 5 connected
fd383105b93797d7502a7d6bcaa77308cccf2e71 192.168.32.197:9000 slave
e07467f74f81f31c024bb5626c37e550396eebe5 0 1442403482671 9 connected
$127.0.0.1:9000>