首页 > 程序开发 > 软件开发 > 其他 >

Redis集群

2017-01-09

Redis集群:要让 Redis3 0 集群正常工作至少需要 3 个 Master 节点,要想实现高可用,每个 Master 节点要配备少 1 个 Slave 节点。

Redis集群:要让 Redis3.0 集群正常工作至少需要 3 个 Master 节点,要想实现高可用,每个 Master 节点要配备少 1 个 Slave 节点。

Server:10.10.36.126

初始Master:7111, 7112, 7113

初始Slave:7114, 7115, 7116

每个redis对应的集群端口为服务端口+10000,eg:redis-01服务端口为7111,其集群端口为17111

将7111~7116, 17111~17116端口在防火墙中打开

1、redis安装

tar -zxvf redis-3.2.6.tar.gz

mv redis-3.2.6 redis

cd ./redis/redis-3.2.6/

make PREFIX=/home/wangzi/soft/redis/install install

可以在/home/wangzi/soft/redis/install/bin 目录下看到

redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server

cd ..

mkdir redis-7111

cp ./redis-3.2.6/redis.conf ./redis-7111/redis.conf

vi./redis-7111/redis.conf

pidfile /var/run/redis_7111.pid

port 7111

daemonize yes

bind 10.10.36.126

cluster-enabled yes

cluster-config-file /home/wangzi/soft/redis/redis-7111/nodes-7111.conf #集群配置文件(启动自动生成),不用人为干涉

cluster-node-timeout 15000

cluster-migration-barrier 1

cluster-require-full-coverage yes

appendonly yes #启用 aof 持久化方式

dir /home/wangzi/soft/redis/redis-7111 #节点数据持久化存放目录

其余五个节点的安装与此类似。

2、启动redis节点实例

./install/bin/redis-server ./redis-7111/redis.conf

其余五个节点启动方式与此类似,加载相应的配置文件即可。

查看进程

ps -ef | grep redis

wangzi 46971 1 0 15:36 ? 00:00:00 ./install/bin/redis-server 10.10.36.126:7111 [cluster]
wangzi 46975 1 0 15:36 ? 00:00:00 ./install/bin/redis-server 10.10.36.126:7112 [cluster]
wangzi 46979 1 0 15:36 ? 00:00:00 ./install/bin/redis-server 10.10.36.126:7113 [cluster]
wangzi 46983 1 0 15:36 ? 00:00:00 ./install/bin/redis-server 10.10.36.126:7114 [cluster]
wangzi 46987 1 0 15:36 ? 00:00:00 ./install/bin/redis-server 10.10.36.126:7115 [cluster]
wangzi 46991 1 0 15:36 ? 00:00:00 ./install/bin/redis-server 10.10.36.126:7116 [cluster]
wangzi 46995 3039 0 15:36 pts/0 00:00:00 grep --color=auto redis

注意:启动完毕后,6 个 Redis 实例尚未构成集群。

3、创建集群

(1)安装ruby

tar -zxvfruby-2.4.0.tar.gz

cdruby-2.4.0

./configure
make
make install

(2)安装rubygems

tar -zxvfrubygems-2.6.8.tgz

cdrubygems-2.6.8

ruby setup.rb

(3)安装gem-redis

gem install -l ./redis-3.3.2.gem

(4)创建redis集群

cp ./redis/redis-3.2.6/src/redis-trib.rb ./redis/intsall/bin/redis-trib

cdredis/intsall/bin/redis-trib

./redis-trib create --replicas 1 10.10.36.126:7111 10.10.36.126:7112 10.10.36.126:7113 10.10.36.126:7114 10.10.36.126:7115 10.10.36.126:7116

输出:

>>> Creating cluster
/usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.2/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.10.36.126:7111
10.10.36.126:7112
10.10.36.126:7113
Adding replica 10.10.36.126:7114 to 10.10.36.126:7111
Adding replica 10.10.36.126:7115 to 10.10.36.126:7112
Adding replica 10.10.36.126:7116 to 10.10.36.126:7113
M: 7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111
slots:0-5460 (5461 slots) master
M: 17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112
slots:5461-10922 (5462 slots) master
M: 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113
slots:10923-16383 (5461 slots) master
S: fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114
replicates 7c18d707e804ef4ad492efeb34712c2b94c37913
S: 4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115
replicates 17b0d77e68f8abbac920ccb198493351159a9ce5
S: ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116
replicates 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb
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 10.10.36.126:7111)
M: 7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114
slots: (0 slots) slave
replicates 7c18d707e804ef4ad492efeb34712c2b94c37913
S: ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116
slots: (0 slots) slave
replicates 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb
S: 4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115
slots: (0 slots) slave
replicates 17b0d77e68f8abbac920ccb198493351159a9ce5
M: 17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112
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.

集群创建成功,连接任一节点查看集群状态:

./redis-cli -h 10.10.36.126 -p 7111 cluster nodes

输出:

1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 10.10.36.126:7113 master- 0 1483861289775 3 connected 10923-16383
fb8ce3074b44876946da5348b2a784d95cf22918 10.10.36.126:7114 slave 7c18d707e804ef4ad492efeb34712c2b94c37913 0 1483861292798 4 connected
ee447f5137df462f306d926e46db973a3a385ee8 10.10.36.126:7116 slave 1ac7c046b4c1fbe8c30d769792d4071c6b546cfb 0 1483861288768 6 connected
7c18d707e804ef4ad492efeb34712c2b94c37913 10.10.36.126:7111 myself,master - 0 0 1 connected 0-5460
4e47df8998c7d508dd6b6fd6b2e98b803239c411 10.10.36.126:7115 slave 17b0d77e68f8abbac920ccb198493351159a9ce5 0 1483861291791 5 connected
17b0d77e68f8abbac920ccb198493351159a9ce5 10.10.36.126:7112 master - 0 1483861285742 2 connected 5461-10922

集群创建过程说明:

(1) 给定 redis-trib 程序的命令是 create , 这表示我们希望创建一个新的集群;

(2) 这里的 --replicas 1 表示每个主节点下有一个从节点;

(3) 之后跟着的其它参数则是实例的地址列表,程序使用这些地址所指示的实例来创建新集群 ;

总的来说,以上命令的意思就是让 redis-trib 程序创建一个包含三个主节点和三个从节点的集群。

接着,redis-trib 会打印出一份预想中的配置给你看,如果你觉得没问题的话(注意核对主从关系是否是

你想要的),就可以输入 yes , redis-trib 就会将这份配置应用到集群当中。

相关文章
最新文章
热点推荐