redis集群

1.下载和安装

安装redis

1.1 下载和解包

cd /usr/local/

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

tar -zxvf /redis-3.2.1.tar.gz

1.2 编译安装 

 cd redis-3.2.1
 make install PREFIX=/usr/local/redis

1. 3 创建redis节点

mkdir redis_cluster
cd redis_cluster
mkdir -p 8001/data 8002/data 8003/data 8004/data 8005/data 8006/data

2 创建集群

2.1 复制新的 Redis  实例

mv /usr/local/redis/* /usr/local/redis_cluster/8001

到redis安装目录把redis.conf复制/usr/local/redis_cluster/8001/redis/bin

cp redis.conf /usr/local/redis_cluster/8001/redis/bin

 修改配置文件

vim redis.conf

port 8001(每个节点的端口号)
daemonize yes
bind 192.168.156.60(绑定当前机器 IP)
dir /usr/local/redis_cluster/8001/data/(数据文件存放位置)
pidfile /var/run/redis_8001.pid(pid 8001和port要对应)
cluster-enabled yes(启动集群模式)
cluster-config-file nodes-8001.conf(8001和port要对应)
cluster-node-timeout 15000
appendonly yes

 继续复制实例

[root@itcast redis_cluster]# \cp -rf /usr/local/redis_cluster/8001/* /usr/local/redis_cluster/8002
[root@itcast redis_cluster]# \cp -rf /usr/local/redis_cluster/8001/* /usr/local/redis_cluster/8003
[root@itcast redis_cluster]# \cp -rf /usr/local/redis_cluster/8001/* /usr/local/redis_cluster/8004
[root@itcast redis_cluster]# \cp -rf /usr/local/redis_cluster/8001/* /usr/local/redis_cluster/8005
[root@itcast redis_cluster]# \cp -rf /usr/local/redis_cluster/8001/* /usr/local/redis_cluster/8006

修改配置文件

vim redis.conf
shift+G 到最底下按shift+:
%s/8001/8002g  回车
替换了四行
port 8002
dir /usr/local/redis-cluster/8002/data/
cluster-config-file nodes-8002.conf
pidfile /var/run/redis_8002.pid

3 测试

六个节点已经全部启动成功

/usr/local/redis_cluster/8001/redis/bin/redis-server  /usr/local/redis_cluster/8001/redis/bin/redis.conf

................

/usr/local/redis_cluster/8006/redis/bin/redis-server  /usr/local/redis_cluster/8006/redis/bin/redis.conf

[root@itcast bin]# ps -el | grep redis
5 S     0  14417      1  0  80   0 - 34363 ep_pol ?        00:00:00 redis-server
5 S     0  14437      1  0  80   0 - 34363 ep_pol ?        00:00:00 redis-server
5 S     0  14460      1  0  80   0 - 34363 ep_pol ?        00:00:00 redis-server
5 S     0  14465      1  0  80   0 - 34363 ep_pol ?        00:00:00 redis-server
5 S     0  14477      1  0  80   0 - 34363 ep_pol ?        00:00:00 redis-server
5 S     0  14487      1  0  80   0 - 34363 ep_pol ?        00:00:00 redis-server

设置开机自启动

为了避免以后集群所有设备都手工启动redis,可以为这6台设备设置开机自启动

[root@redis1 bin]# vim /etc/rc.d/rc.local

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start
/usr/local/nginx/sbin/nginx
/usr/local/redis_cluster/8001/redis/bin/redis-server  /usr/local/redis_cluster/8001/redis/bin/redis.conf
/usr/local/redis_cluster/8002/redis/bin/redis-server  /usr/local/redis_cluster/8002/redis/bin/redis.conf
/usr/local/redis_cluster/8003/redis/bin/redis-server  /usr/local/redis_cluster/8003/redis/bin/redis.conf
/usr/local/redis_cluster/8004/redis/bin/redis-server  /usr/local/redis_cluster/8004/redis/bin/redis.conf
/usr/local/redis_cluster/8005/redis/bin/redis-server  /usr/local/redis_cluster/8005/redis/bin/redis.conf
/usr/local/redis_cluster/8006/redis/bin/redis-server  /usr/local/redis_cluster/8006/redis/bin/redis.conf
~

启动了 Redis 集群服务,但是他们暂时还并不在一个集群中 

[root@itcast redis_cluster]#  /usr/local/redis_cluster/8005/redis/bin/redis-cli -h 192.168.156.60 -p 8002
192.168.156.60:8002> keys *
(empty list or set)
192.168.156.60:8002> set name mafy
(error) CLUSTERDOWN The cluster is down
192.168.156.60:8002> exit

4 安装命令

4.1集群的搭建需要用到ruby环境,因此我们需要安装ruby环境

yum install ruby

  4.2  安装完ruby之后,我们安装rubygem(也就是ruby第三方包管理工具)

yum install rubygems

 4.3 安装完ruby和rubygems之后,我们需要安装ruby脚本运行所需要的一个包redis-3.0.0.gem

redis-3.0.0.gem

gem install redis-3.0.0.gem

5 安装集群

上面做了那么多准备其实是为一个脚本文件(redis-trib.rb)服务的,它在redis的安装目录redis-3.2.1

[root@itcast src]# pwd
/usr/local/redis-3.2.1/src
[root@itcast src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 4月   1 2015 redis-trib.rb
[root@itcast src]# cp redis-trib.rb /usr/local/redis_cluster/8001/redis/bin
[root@itcast src]# cp redis-trib.rb /usr/local/redis_cluster/8002/redis/bin
[root@itcast src]# cp redis-trib.rb /usr/local/redis_cluster/8003/redis/bin
[root@itcast src]# cp redis-trib.rb /usr/local/redis_cluster/8004/redis/bin
[root@itcast src]# cp redis-trib.rb /usr/local/redis_cluster/8005/redis/bin
[root@itcast src]# cp redis-trib.rb /usr/local/redis_cluster/8006/redis/bin

正式搭建集群,命令中--replicas是指定每个节点备份的节点的数量,我们现在是每个节点备份一个,因此输入1

[root@itcast src]# /usr/local/redis_cluster/8001/redis/bin/redis-trib.rb create --replicas 1 192.168.156.60:8001 192.168.156.60:8002 192.168.156.60:8003 192.168.156.60:8004 192.168.156.60:8005 192.168.156.60:8006
>>> Creating cluster
Connecting to node 192.168.156.60:8001: OK
Connecting to node 192.168.156.60:8002: OK
Connecting to node 192.168.156.60:8003: OK
Connecting to node 192.168.156.60:8004: OK
Connecting to node 192.168.156.60:8005: OK
Connecting to node 192.168.156.60:8006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.156.60:8001
192.168.156.60:8002
192.168.156.60:8003
Adding replica 192.168.156.60:8004 to 192.168.156.60:8001
Adding replica 192.168.156.60:8005 to 192.168.156.60:8002
Adding replica 192.168.156.60:8006 to 192.168.156.60:8003
M: 02a227114ec6e3f35837f41b82f770eb8dbd0f58 192.168.156.60:8001
   slots:0-5460 (5461 slots) master
M: 2b8c75d594a0d84052d23200a131dd0545078225 192.168.156.60:8002
   slots:5461-10922 (5462 slots) master
M: d65717148b5d478803622288999eb846ea313506 192.168.156.60:8003
   slots:10923-16383 (5461 slots) master
S: 9d9cc258f7337323c2365aeec48c3bb668db50c4 192.168.156.60:8004
   replicates 02a227114ec6e3f35837f41b82f770eb8dbd0f58
S: f7f11fb32d279cc5dd73fdc8ef184453ed04583c 192.168.156.60:8005
   replicates 2b8c75d594a0d84052d23200a131dd0545078225
S: 11268161e8278938559bb864968a7463d9561329 192.168.156.60:8006
   replicates d65717148b5d478803622288999eb846ea313506
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.156.60:8001)
M: 02a227114ec6e3f35837f41b82f770eb8dbd0f58 192.168.156.60:8001
   slots:0-5460 (5461 slots) master
M: 2b8c75d594a0d84052d23200a131dd0545078225 192.168.156.60:8002
   slots:5461-10922 (5462 slots) master
M: d65717148b5d478803622288999eb846ea313506 192.168.156.60:8003
   slots:10923-16383 (5461 slots) master
M: 9d9cc258f7337323c2365aeec48c3bb668db50c4 192.168.156.60:8004
   slots: (0 slots) master
   replicates 02a227114ec6e3f35837f41b82f770eb8dbd0f58
M: f7f11fb32d279cc5dd73fdc8ef184453ed04583c 192.168.156.60:8005
   slots: (0 slots) master
   replicates 2b8c75d594a0d84052d23200a131dd0545078225
M: 11268161e8278938559bb864968a7463d9561329 192.168.156.60:8006
   slots: (0 slots) master
   replicates d65717148b5d478803622288999eb846ea313506
[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/root

验证一下

[root@itcast src]#  ./redis-cli -h 192.168.156.60 -p 8001 -c
192.168.156.60:8001> set key 111
-> Redirected to slot [12539] located at 192.168.156.60:8003
OK
192.168.156.60:8001> set key1 vvvva
OK
192.168.156.60:8003> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_sent:581
cluster_stats_messages_received:581
192.168.156.60:8003> cluster nodes
2b8c75d594a0d84052d23200a131dd0545078225 192.168.156.60:8002 master - 0 1534337443124 2 connected 5461-10922
02a227114ec6e3f35837f41b82f770eb8dbd0f58 192.168.156.60:8001 master - 0 1534337445139 1 connected 0-5460
11268161e8278938559bb864968a7463d9561329 192.168.156.60:8006 slave d65717148b5d478803622288999eb846ea313506 0 1534337442116 6 connected
9d9cc258f7337323c2365aeec48c3bb668db50c4 192.168.156.60:8004 slave 02a227114ec6e3f35837f41b82f770eb8dbd0f58 0 1534337447152 1 connected
f7f11fb32d279cc5dd73fdc8ef184453ed04583c 192.168.156.60:8005 slave 2b8c75d594a0d84052d23200a131dd0545078225 0 1534337446145 5 connected
d65717148b5d478803622288999eb846ea313506 192.168.156.60:8003 myself,master - 0 0 3 connected 10923-16383
192.168.156.60:8003> exit

搭建完成。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值