0 编译安装
$ wget http://download.redis.io/releases/redis-3.0.3.tar.gz
$ tar xzvf redis-3.0.3.tar.gz
$ cd redis-3.0.3
$ make all
$ make install # use sudo user.
也可以去github下载最新代码:https://github.com/antirez/redis.git
1 创建目录
$ mkdir 7000 7001 7002 7003
2 修改如下配置redis.conf
port 7000 #7000修改为对应的7001、7002、7003
daemonize yes
cluster-enabled yes
cluster-config-file nodes-7000.conf #7000修改为对应的7001、7002、7003
luster-node-timeout 15000
将修改好的配置文件放到对用的目录下:./7000, ./7001, ./7002, ./7003 。
3 启动redis服务
$ cd 7000
$ redis-server ./redis.conf
$ cd ../7001
$ redis-server ./redis.conf
$ cd ../7002
$ redis-server ./redis.conf
$ cd ../7003
$ redis-server ./redis.conf
$ # 检查redis启动情况
$ ps -ef | grep redis | grep -v grep
cjf 5834 1 0 16:33 ? 00:00:00 redis-server 127.0.0.1:7000 [cluster]
cjf 5839 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster]
cjf 5843 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster]
cjf 5847 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster]
4 将redis实例增加到集群中
主要是通过用cluster meet <host> <port>
给集群增加节点,如下:
$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
14d4071b97121e703e44bbc834f42d1b31c8ea95 :7000 myself,master - 0 0 0 connected
127.0.0.1:7000> cluster meet 127.0.0.1 7001
OK
127.0.0.1:7000> cluster meet 127.0.0.1 7002
OK
127.0.0.1:7000> cluster meet 127.0.0.1 7003
OK
127.0.0.1:7000> cluster nodes
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850262990 0 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850265007 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850263998 2 connected
127.0.0.1:7000>
如果cluster nodes
出现上面的四行记录就表示meet
成功,标识myself的是客户端正在连接的redis实例。
可以通过cluster forget
清除集群中的某个实例。
5 分配slots
redis-cluster总共有16384个槽位(0-16383),按如下方式修改槽位:
$ cat 7000/nodes-7000.conf #分配slots:0-4095(注意127.0.0.1:7000对应的行尾)
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850266016 3 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850265007 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850263998 2 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7001/nodes-7001.conf #分配slots:4096-8191(注意127.0.0.1:7001对应的行尾)
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850265714 3 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 myself,master - 0 0 2 connected 4096-8191
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850264705 0 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263695 1 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7002/nodes-7002.conf #分配slots:8192-12287(注意127.0.0.1:7002对应的行尾)
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850262787 2 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263796 1 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 myself,master - 0 0 3 connected 8192-12287
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850264806 0 connected
vars currentEpoch 3 lastVoteEpoch 0
$
$ cat 7003/nodes-7003.conf #分配slots:12288-16383(注意127.0.0.1:7003对应的行尾)
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850264808 2 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850265819 3 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438850263798 1 connected
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 myself,master - 0 0 0 connected 12288-16383
vars currentEpoch 3 lastVoteEpoch 0
$
配置修改成功之后,重新启动redis:
$ # 停止redis
$ ps -ef|grep redis
cjf 5834 1 0 16:33 ? 00:00:00 redis-server 127.0.0.1:7000 [cluster]
cjf 5839 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster]
cjf 5843 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster]
cjf 5847 1 0 16:34 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster]
cjf 5921 5252 0 16:46 pts/2 00:00:00 grep redis
$ kill 5834 5839 5843 5847
$ # 重新启动redis
$ cd 7000
$ redis-server ./redis.conf
$ cd ../7001
$ redis-server ./redis.conf
$ cd ../7002
$ redis-server ./redis.conf
$ cd ../7003
$ redis-server ./redis.conf
$ # 检查分配的slots是否生效:
$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438850861230 0 connected 12288-16383
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438850860221 2 connected 4096-8191
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438850859209 3 connected 8192-12287
127.0.0.1:7000>
127.0.0.1:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:4
cluster_size:4
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:416
cluster_stats_messages_received:73
127.0.0.1:7000>
当前情况下,redis-cluster部署完毕。
6 给master节点配一个slave节点
在前面,我们配置好了redis的集群并分配了slots,集群已经可用了,集群中现在有4个master节点,所有写入这个集群中的数据都会分片到这4个redis实例中,但是现在存在这样一个问题:如果某个master节点挂掉的话,那么这个集群就不可用了,所以我们现在为集群中的4个master实例各启一个slave实例做主备。
$ mkdir 8000 8001 8002 8003
$ cp 7000/redis.conf 8000
$ cp 7000/redis.conf 8001
$ cp 7000/redis.conf 8002
$ cp 7000/redis.conf 8003
将redis.conf中下面两个配置修改为对应的端口:
port 800X
cluster-config-file nodes-800X.conf
然后启动这4个实例:
$ cd 8000/
$ redis-server ./redis.conf
$ cd ../8001
$ redis-server ./redis.conf
$ cd ../8002
$ redis-server ./redis.conf
$ cd ../8003
$ redis-server ./redis.conf
$
$ ps -ef|grep redis
cjf 5928 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7000 [cluster]
cjf 5933 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7001 [cluster]
cjf 5937 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7002 [cluster]
cjf 5941 1 0 16:47 ? 00:00:02 redis-server 127.0.0.1:7003 [cluster]
cjf 6136 1 0 17:20 ? 00:00:00 redis-server 127.0.0.1:8000 [cluster]
cjf 6141 1 0 17:20 ? 00:00:00 redis-server 127.0.0.1:8001 [cluster]
cjf 6145 1 0 17:21 ? 00:00:00 redis-server 127.0.0.1:8002 [cluster]
cjf 6150 1 0 17:21 ? 00:00:00 redis-server 127.0.0.1:8003 [cluster]
cjf 6157 5252 0 17:21 pts/2 00:00:00 grep redis
[cjf@host1 8003]$
再将这4个redis实例cluster meet
到集群中:
$ redis-cli -p 7000
127.0.0.1:7000> cluster nodes
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438852991236 0 connected 12288-16383
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438852992245 2 connected 4096-8191
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438852993255 3 connected 8192-12287
127.0.0.1:7000> cluster meet 127.0.0.1 8000
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8001
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8002
OK
127.0.0.1:7000> cluster meet 127.0.0.1 8003
OK
127.0.0.1:7000> cluster nodes
79c41ed9454f686fe7c35e0a6c505732b951b226 127.0.0.1:8000 master - 0 1438853011586 4 connected
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 myself,master - 0 0 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438853014613 7 connected 12288-16383
833bb1d37b22b2055d2281a5f2dad3d869195f0d 127.0.0.1:8002 master - 0 1438853012596 6 connected
0d03ff5e90fa369c5c39c9415db39f711b943494 127.0.0.1:8001 master - 0 1438853013605 5 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438853010578 3 connected 8192-12287
a34eb2aa56f309793d78ba2f69141be268a23659 127.0.0.1:8003 master - 0 1438853012092 0 connected
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438853009564 2 connected 4096-8191
127.0.0.1:7000>
最后用cluster replicate <nodeid>
将8000、8001、8002、8003分别挂到7000、7001、7002、7003上:
$ redis-cli -p 8000
127.0.0.1:8000> cluster replicate 14d4071b97121e703e44bbc834f42d1b31c8ea95
OK
127.0.0.1:8000>
[[email protected] 8003]$ redis-cli -p 8001
127.0.0.1:8001> cluster replicate 2875f2155e3214d7950dd5916f5ecf5edd3d9fee
OK
127.0.0.1:8001>
[[email protected] 8003]$ redis-cli -p 8002
127.0.0.1:8002> cluster replicate 75334aae3ced44cf0e8416691aeef8249e7a0621
OK
127.0.0.1:8002>
[[email protected] 8003]$ redis-cli -p 8003
127.0.0.1:8003> cluster replicate 8b721ff1ac2e383ffcbcf133d502c6957f8df713
OK
127.0.0.1:8003> cluster nodes
14d4071b97121e703e44bbc834f42d1b31c8ea95 127.0.0.1:7000 master - 0 1438853245955 1 connected 0-4095
8b721ff1ac2e383ffcbcf133d502c6957f8df713 127.0.0.1:7003 master - 0 1438853242927 7 connected 12288-16383
2875f2155e3214d7950dd5916f5ecf5edd3d9fee 127.0.0.1:7001 master - 0 1438853242423 2 connected 4096-8191
833bb1d37b22b2055d2281a5f2dad3d869195f0d 127.0.0.1:8002 slave 75334aae3ced44cf0e8416691aeef8249e7a0621 0 1438853245955 6 connected
a34eb2aa56f309793d78ba2f69141be268a23659 127.0.0.1:8003 myself,slave 8b721ff1ac2e383ffcbcf133d502c6957f8df713 0 0 0 connected
0d03ff5e90fa369c5c39c9415db39f711b943494 127.0.0.1:8001 slave 2875f2155e3214d7950dd5916f5ecf5edd3d9fee 0 1438853241925 5 connected
79c41ed9454f686fe7c35e0a6c505732b951b226 127.0.0.1:8000 slave 14d4071b97121e703e44bbc834f42d1b31c8ea95 0 1438853244945 4 connected
75334aae3ced44cf0e8416691aeef8249e7a0621 127.0.0.1:7002 master - 0 1438853243935 3 connected 8192-12287
127.0.0.1:8003>
如上,配置成功。
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-08 09:27:45