redis多機集羣部署文檔(centos6.2)node
(要讓集羣正常工做至少須要3個主節點,在這裏咱們要建立6個redis節點,其中三個爲主節點,三個爲從節點,對應的redis節點的ip和端口對應關係以下)git
10.168.32.116:6379github
10.168.32.117:6379redis
10.168.32.118:6379centos
10.168.32.119:6379ruby
10.168.32.120:6379服務器
10.168.32.121:6379app
在安裝集羣以前,須要在服務器上安裝ruby環境,spa
yum install -y ruby rubygems3d
gem install redis
下載redis版本,
#git clone https://github.com/antirez/redis.git
#mv redis /usr/local/redis
#cd /usr/local/redis
#make && make install
修改配置集羣須要的基本配置,其他部分,根據實際須要配置
#vi /usr/local/redis/redis.conf
bind 10.168.32.116
port 6379
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
appendonly yes
修改完這幾行,把redis.conf分發到其他的服務器,全部服務器的配置能夠徹底一致,bind是主機ip(若是是在同一臺服務器進行實驗,須要修改port,每一個redis使用獨立的端口)
配置同步以後,啓動全部服務器的redis
redis-server /usr/local/redis/redis.conf
執行redis的建立集羣命令建立集羣:
#./redis-trib.rb create --replicas 1 10.168.32.116:6379 10.168.32.117:6379 10.168.32.118:6379 10.168.32.119:6379 10.168.32.120:6379 10.168.32.121:6379
>>> Creating cluster
Connecting to node 10.168.32.116:6379: OK
Connecting to node 10.168.32.117:6379: OK
Connecting to node 10.168.32.118:6379: OK
Connecting to node 10.168.32.119:6379: OK
Connecting to node 10.168.32.120:6379: OK
Connecting to node 10.168.32.121:6379: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.168.32.121:6379
10.168.32.120:6379
10.168.32.119:6379
Adding replica 10.168.32.118:6379 to 10.168.32.121:6379
Adding replica 10.168.32.117:6379 to 10.168.32.120:6379
Adding replica 10.168.32.116:6379 to 10.168.32.119:6379
S: f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379
replicates a5a22ba976c05b1d495812241a8dd10a5d4a5b42
S: 314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379
replicates 9880ecc98cde59e0d6341ba7c54bbcae9b27c877
S: 07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379
replicates b7f33a1b733fe4a51447a788e334fab625a984bc
M: a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379
slots:10923-16383 (5461 slots) master
M: 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379
slots:5461-10922 (5462 slots) master
M: b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379
slots:0-5460 (5461 slots) master
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 10.168.32.116:6379)
M: f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379
slots: (0 slots) master
replicates a5a22ba976c05b1d495812241a8dd10a5d4a5b42
M: 314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379
slots: (0 slots) master
replicates 9880ecc98cde59e0d6341ba7c54bbcae9b27c877
M: 07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379
slots: (0 slots) master
replicates b7f33a1b733fe4a51447a788e334fab625a984bc
M: a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379
slots:10923-16383 (5461 slots) master
M: 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379
slots:5461-10922 (5462 slots) master
M: b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379
slots:0-5460 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
至此redis集羣即搭建成功!
執行命令redis-cli -c -p 6379 cluster nodes,查看集羣節點
[root@puppetmaster1 src]# redis-cli -c -h 10.168.32.116 -p 6379 cluster nodes
314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379 slave 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 0 1425114266105 5 connected
a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379 master - 0 1425114267205 4 connected 10923-16383
07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379 slave b7f33a1b733fe4a51447a788e334fab625a984bc 0 1425114265605 6 connected
b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379 master - 0 1425114266205 6 connected 0-5460
f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379 myself,slave a5a22ba976c05b1d495812241a8dd10a5d4a5b42 0 0 1 connected
9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379 master - 0 1425114266505 5 connected 5461-10922
7:使用redis-cli命令進入集羣環境
redis-cli -c -h 10.168.32.117 -p 6379 |
Redis集羣操做節點
1:新增一個節點,IP爲10.168.32.122,端口爲6379
首先把須要添加的節點啓動
cd /usr/local/redis/ #vi /usr/local/redis/redis.conf bind 10.168.32.122 port 6379 啓動redis redis-server /usr/local/redis/redis.conf
將新節點添加到集羣 |
cd /usr/local/redis/src
[root@puppetmaster1 src]# ./redis-trib.rb add-node 10.168.32.122:6379 10.168.32.116:6379
>>> Adding node 10.168.32.122:6379 to cluster 10.168.32.116:6379
Connecting to node 10.168.32.116:6379: OK
Connecting to node 10.168.32.117:6379: OK
Connecting to node 10.168.32.119:6379: OK
Connecting to node 10.168.32.118:6379: OK
Connecting to node 10.168.32.121:6379: OK
Connecting to node 10.168.32.120:6379: OK
>>> Performing Cluster Check (using node 10.168.32.116:6379)
S: f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379
slots: (0 slots) slave
replicates a5a22ba976c05b1d495812241a8dd10a5d4a5b42
S: 314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379
slots: (0 slots) slave
replicates 9880ecc98cde59e0d6341ba7c54bbcae9b27c877
M: a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379
slots: (0 slots) slave
replicates b7f33a1b733fe4a51447a788e334fab625a984bc
M: b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379
slots:5461-10922 (5462 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.
Connecting to node 10.168.32.122:6379: OK
>>> Send CLUSTER MEET to node 10.168.32.122:6379 to make it join the cluster.
[OK] New node added correctly.
查看集羣節點
[root@puppetmaster1 src]# redis-cli -c -h 10.168.32.116 -p 6379 cluster nodes
314f957e1021b0c262e635ad4feeb340dfe5955c 10.168.32.117:6379 slave 9880ecc98cde59e0d6341ba7c54bbcae9b27c877 0 1425114360107 5 connected
38c2f060533c24698334a16b26f678ae6bd7ec41 10.168.32.122:6379 master - 0 1425114361107 0 connected
a5a22ba976c05b1d495812241a8dd10a5d4a5b42 10.168.32.119:6379 master - 0 1425114361507 4 connected 10923-16383
07634642c88fdf1b886d3db87dedc4b9857fce7c 10.168.32.118:6379 slave b7f33a1b733fe4a51447a788e334fab625a984bc 0 1425114362507 6 connected
b7f33a1b733fe4a51447a788e334fab625a984bc 10.168.32.121:6379 master - 0 1425114360507 6 connected 0-5460
f3314ecda2ef4ea0ec140e06de3e4bf656a5e71b 10.168.32.116:6379 myself,slave a5a22ba976c05b1d495812241a8dd10a5d4a5b42 0 0 1 connected
9880ecc98cde59e0d6341ba7c54bbcae9b27c877 10.168.32.120:6379 master - 0 1425114362107 5 connected 5461-10922
刪除redis集羣節點
redis-trib.rb del-node host:port node_id
./redis-trib.rb del-node 10.168.32.122:6379 38c2f060533c24698334a16b26f678ae6bd7ec41