===============================================================
准备三台机器做redis
192.168.99.5
192.168.99.28
192.168.99.29
操作系统:
CentOS6.5 64bit
软件包:
redis-3.2.7.tar.gz
redis-3.3.3.gem
rubygems-1.3.7-5.el6.noarch.rpm
================================================================
1.编译安装redis
tar zxf redis-3.2.7.tar.gz
cd redis-3.2.7
[
Centos6.5上需要修改一下Makefile文件
$redis/src下
修改如下属性
FINAL_LIBS = -lm -lrt
]
make
sudo make install
2.安装rubygems
sudo yum install -y rubygems-1.3.7-5.el6.noarch.rpm
3.gem 安装集群用的redis
sudo gem install -l redis-3.3.3.gem
================================================================
在每一台几点上都要执行以下步骤
1.创建集群配置文件夹
sudo mkdir /usr/local/redis-cluster/{7000,7001,7002}
2.7000,7001,7002下,每个文件夹下创建redis.conf,
sudo touch /usr/local/redis-cluster/7000(7001,7002)/redis.conf
并添加如下配置:
#监听的端口,7000,7001,7002
port 7000
#redis本机ip
bind 192.168.99.5
daemonize yes
#按端口号修改pid的序号
pidfile /var/run/redis_7000.pid
#启用集群模式
cluster-enabled yes
#也是按照端口号修改序号
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000
appendonly yes
3.指定配置文件启动3个redis实例
sudo /usr/local/bin/redis-server /usr/local/redis-cluster/7000/redis.conf
sudo /usr/local/bin/redis-server /usr/local/redis-cluster/7001/redis.conf
sudo /usr/local/bin/redis-server /usr/local/redis-cluster/7002/redis.conf
4.查看端口
sudo netstat -tnlp
会有7000,7001,7002 ,17000 ,17001 ,17002 这几个redis进程
创建集群
1.在任一台机器,解压后的redis的/src/目录下执行以下程序,选出三台机器上的三个节点作为redis集群的三个master节点
./redis-trib.rb create 192.168.99.5:7000 192.168.99.28:7000 192.168.99.29:7000
输出:
Using 3 masters:
192.168.99.29:7000
192.168.99.28:7000
192.168.99.5:7000
M: 29608e1a8b3d3e28315d1231b3a79dc3349bde21 192.168.99.5:7000
slots:10923-16383 (5461 slots) master
M: eeb89dca7b91e683e77a443f03187eef4ab26cda 192.168.99.28:7000
slots:5461-10922 (5462 slots) master
M: 0d589ff3efc479fc4acae086045ee1293767bdc6 192.168.99.29:7000
slots:0-5460 (5461 slots) master
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.99.5:7000)
M: 29608e1a8b3d3e28315d1231b3a79dc3349bde21 192.168.99.5:7000
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 0d589ff3efc479fc4acae086045ee1293767bdc6 192.168.99.29:7000
slots:0-5460 (5461 slots) master
0 additional replica(s)
M: eeb89dca7b91e683e77a443f03187eef4ab26cda 192.168.99.28:7000
slots:5461-10922 (5462 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
You have new mail in /var/spool/mail/mobileoa
2.检查集群:选定集群中一个master节点的ip就可以查询整个集群
[mobileoa@db2test1 src]$ ./redis-trib.rb check 192.168.99.5:7000
>```
Performing Cluster Check (using node 192.168.99.5:7000)
M: 29608e1a8b3d3e28315d1231b3a79dc3349bde21 192.168.99.5:7000
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 0d589ff3efc479fc4acae086045ee1293767bdc6 192.168.99.29:7000
slots:0-5460 (5461 slots) master
0 additional replica(s)
M: eeb89dca7b91e683e77a443f03187eef4ab26cda 192.168.99.28:7000
slots:5461-10922 (5462 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
3.给集群每个主节点分散配置从节点,
添加节点 类型 要给那个master添加从节点 从节点ip 要添加到的集群
./redis-trib.rb add-node --slave --master-id 29608e1a8b3d3e28315d1231b3a79dc3349bde21 192.168.99.28:7001 192.168.99.5:7000
./redis-trib.rb add-node --slave --master-id 29608e1a8b3d3e28315d1231b3a79dc3349bde21 192.168.99.29:7001 192.168.99.5:7000
./redis-trib.rb add-node --slave --master-id 0d589ff3efc479fc4acae086045ee1293767bdc6 192.168.99.5:7002 192.168.99.5:7000
./redis-trib.rb add-node --slave --master-id 0d589ff3efc479fc4acae086045ee1293767bdc6 192.168.99.28:7002 192.168.99.5:7000
./redis-trib.rb add-node --slave --master-id eeb89dca7b91e683e77a443f03187eef4ab26cda 192.168.99.5:7001 192.168.99.5:7000
./redis-trib.rb add-node --slave --master-id eeb89dca7b91e683e77a443f03187eef4ab26cda 192.168.99.29:7002 192.168.99.5:7000
4.查看集群状态
./redis-trib.rb check 192.168.99.5:7000
----
测试集群
redis-cli -h 192.168.99.28 -c -p 7002 ,加参数 -C 可连接到集群,因为上面 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略。
set hello world
查看key
在另一个节点连接redis ,并查询
redis-cli -h 192.168.99.5 -c -p 7001
get hello