三、安装配置
1.环境
使用2台centos服务器,每台机器上部署3个实例,集群为三个主节点与三个从节点:
192.168.36.54:6380
192.168.36.54:6381
192.168.36.54:6382
192.168.36.189:6380
192.168.36.189:6381
192.168.36.189:6382
2.安装依赖
由于通过redis-trib.rb工具构建Redis Cluster,需要rudy环境,执行如下命令安装:
yum -y install zlib ruby rubygems
安装ruby 的redis库:
gem install redis
3.安装redis 3.0
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
tar -zxvf redis-3.0.0.tar.gz
mkdir redis
cd redis-3.0.0
make PREFIX=/home/slim/redis
make PREFIX=/home/slim/redis install
将集群工具复制到/home/slim/redis/bin下
cp /home/slim/redis-3.0.0/src/redis-trib.rb ./bin/
创建数据配置目录
mkdir -p /home/slim/redis/{conf,data,logs}
4.配置
在2台机器上配置如下:
cd /home/slim/redis
cp /home/slim/redis-3.0.0/redis.conf ./conf/redis-6380.conf
cp /home/slim/redis-3.0.0/redis.conf ./conf/redis-6381.conf
cp /home/slim/redis-3.0.0/redis.conf ./conf/redis-6382.conf
修改配置:
#基本配置 daemonize yes pidfile /home/slim/redis/data/redis-6380.pid port 6380 bind 192.168.36.189 unixsocket /home/slim/redis/data/redis-6380.sock unixsocketperm 700 timeout 300 loglevel verbose logfile /home/slim/redis/logs/redis-6380.log databases 16 dbfilename dump-6380.rdb dir /home/slim/redis/data/ #aof持久化 appendonly yes appendfilename appendonly-6380.aof appendfsync everysec no-appendfsync-on-rewrite yes auto-aof-rewrite-percentage 80-100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 #集群配置 cluster-enabled yes cluster-config-file /home/slim/redis/data/nodes-6380.conf cluster-node-timeout 5000
每个实例配置类似,修改一下都应端口、IP地址、文件名称即可。
Redis集群由多个运行在集群模式(cluster mode)下的Redis实例组成,实例的集群模式需要通过配置来开启,开启集群模式的实例将可以使用集群特有的功能和命令。要让集群正常运作至少需要三个主节点, 不过在刚开始试用集群功能时, 强烈建议使用六个节点: 其中三个为主节点, 而其余三个则是各个主节点的从节点。
cluster-enabled:开实例的集群模式
cluster-conf-file:设定了保存节点配置文件的路径,默认值为nodes.conf。节点配置文件无须人为修改,它由Redis集群在启动时创建,并在有需要时自动进行更新。
cluster-node-timeout:集群节点互连超时的阀值
5.启动服务
1)启动2台机器实例
./bin/redis-server ./conf/redis-6380.conf ;tail -f logs/redis-6380.log
./bin/redis-server ./conf/redis-6381.conf ;tail -f logs/redis-6381.log
./bin/redis-server ./conf/redis-6382.conf ;tail -f logs/redis-6382.log
启动日志中打印:
“8591:M 11 Apr 22:20:46.134 * No cluster configuration found, I'm 83fc65283bbbb71b4c089337df05594d67f4cab6”
每个节点都使用 ID 而不是 IP 或者端口号来记录其他节点, 因为 IP 地址和端口号都可能会改变, 而这个独一无二的标识符(identifier)则会在节点的整个生命周期中一直保持不变。
查看启动进程:
ps -ef | grep redis slim 8525 1 0 22:16 ? 00:00:01 ./bin/redis-server 192.168.36.54:6380 [cluster] slim 8544 1 0 22:17 ? 00:00:00 ./bin/redis-server 192.168.36.54:6381 [cluster] slim 8591 1 0 22:20 ? 00:00:00 ./bin/redis-server 192.168.36.54:6382 [cluster]
2)集群配置
在每台机器上启动的redis服务都是相互独立,下面我们就使用redis-trib.rb工具构建Redis Cluster。
./bin/redis-trib.rb create --replicas 1 192.168.36.54:6380 192.168.36.54:6381 192.168.36.54:6382 192.168.36.189:6380 192.168.36.189:6381 192.168.36.189:6382
命令的意义如下:
给定 redis-trib.rb 程序的命令是 create , 这表示我们希望创建一个新的集群。
选项 --replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
之后跟着的其他参数则是实例的地址列表, 我们希望程序使用这些地址所指示的实例来创建新集群。
简单来说, 以上命令的意思就是让 redis-trib 程序创建一个包含三个主节点和三个从节点的集群。
启动日志:
>>> Creating cluster Connecting to node 192.168.36.54:6380: OK Connecting to node 192.168.36.54:6381: OK Connecting to node 192.168.36.54:6382: OK Connecting to node 192.168.36.189:6380: OK Connecting to node 192.168.36.189:6381: OK Connecting to node 192.168.36.189:6382: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.36.54:6380 192.168.36.189:6380 192.168.36.54:6381 Adding replica 192.168.36.189:6381 to 192.168.36.54:6380 Adding replica 192.168.36.54:6382 to 192.168.36.189:6380 Adding replica 192.168.36.189:6382 to 192.168.36.54:6381 M: f6285c8a7506b224840d7b26b2b5d1671320c21f 192.168.36.54:6380 slots:0-5460 (5461 slots) master M: 26ce71d626175f88e0416e3f45b2bfb29304c7b3 192.168.36.54:6381 slots:10923-16383 (5461 slots) master S: 83fc65283bbbb71b4c089337df05594d67f4cab6 192.168.36.54:6382 replicates b1a15a3cd14ea65671a7134850e17b8919a17da5 M: b1a15a3cd14ea65671a7134850e17b8919a17da5 192.168.36.189:6380 slots:5461-10922 (5462 slots) master S: 1080e423a55a2c24dae649dac03ffa09ed26d3e8 192.168.36.189:6381 replicates f6285c8a7506b224840d7b26b2b5d1671320c21f S: de4302f43ff89843675446396552fd19f741246a 192.168.36.189:6382 replicates 26ce71d626175f88e0416e3f45b2bfb29304c7b3 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.36.54:6380) M: f6285c8a7506b224840d7b26b2b5d1671320c21f 192.168.36.54:6380 slots:0-5460 (5461 slots) master M: 26ce71d626175f88e0416e3f45b2bfb29304c7b3 192.168.36.54:6381 slots:10923-16383 (5461 slots) master M: 83fc65283bbbb71b4c089337df05594d67f4cab6 192.168.36.54:6382 slots: (0 slots) master replicates b1a15a3cd14ea65671a7134850e17b8919a17da5 M: b1a15a3cd14ea65671a7134850e17b8919a17da5 192.168.36.189:6380 slots:5461-10922 (5462 slots) master M: 1080e423a55a2c24dae649dac03ffa09ed26d3e8 192.168.36.189:6381 slots: (0 slots) master replicates f6285c8a7506b224840d7b26b2b5d1671320c21f M: de4302f43ff89843675446396552fd19f741246a 192.168.36.189:6382 slots: (0 slots) master replicates 26ce71d626175f88e0416e3f45b2bfb29304c7b3 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.从启动日志可以看出有6个集群实例,主节点为:
注:redis-trib.rb使用参数
Usage: redis-trib <command> <options> <arguments ...> fix host:port call host:port command arg arg .. arg check host:port import host:port --from <arg> set-timeout host:port milliseconds add-node new_host:new_port existing_host:existing_port --master-id <arg> --slave reshard host:port --to <arg> --from <arg> --slots <arg> --yes create host1:port1 ... hostN:portN --replicas <arg> help (show this help) del-node host:port node_id For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.参数说明:
./bin/redis-cli -c -h 192.168.36.189 -p 6380 cluster nodes 1080e423a55a2c24dae649dac03ffa09ed26d3e8 192.168.36.189:6381 slave f6285c8a7506b224840d7b26b2b5d1671320c21f 0 1428817516066 5 connected b1a15a3cd14ea65671a7134850e17b8919a17da5 192.168.36.189:6380 myself,master - 0 0 4 connected 5461-10922 83fc65283bbbb71b4c089337df05594d67f4cab6 192.168.36.54:6382 slave b1a15a3cd14ea65671a7134850e17b8919a17da5 0 1428817515047 4 connected 26ce71d626175f88e0416e3f45b2bfb29304c7b3 192.168.36.54:6381 master - 0 1428817514946 2 connected 10923-16383 f6285c8a7506b224840d7b26b2b5d1671320c21f 192.168.36.54:6380 master - 0 1428817514844 1 connected 0-5460 de4302f43ff89843675446396552fd19f741246a 192.168.36.189:6382 slave 26ce71d626175f88e0416e3f45b2bfb29304c7b3 0 1428817516578 6 connected
参考文章:
1.Redis 官方集群教程
2.Redis 集群原理与使用
3.Redis 3.0.0集群试练
4.Redis3.0.0 集群示例