数据库
首页 > 数据库> > redis cluster搭建

redis cluster搭建

作者:互联网

环境:

搭建环境为centos7,有外网

IP                           实例端口

192.168.8.28      7000、7001、7002

192.168.8.29      7003、7004、7005

 

一、搭建过程

1、源码安装新版redis(redis3.0版本后开始支持cluster 模式)

# yum install -y gcc g++ make gcc-c++ kernel-devel automake autoconf libtool make wget tcl vim ruby rubygems unzip git

# cd /usr/local/src/

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

# tar xf redis-4.0.10.tar.gz

# cd redis-4.0.10

# make && make test && make PREFIX=/usr/local/redis.4_0_10 install

和已安装的redis分开

# mkdir /usr/local/redis.4_0_10/conf

# cp *.conf /usr/local/redis.4_0_10/conf/

# cp src/redis-trib.rb /usr/local/redis.4_0_10/bin/

# ln -s /usr/local/redis.4_0_10/bin/* /usr/local/bin/

# ll /usr/local/bin/

 

2、reidis集群配置

# mkdir -p /data/redis/redis-cluster

# cd /data/redis/redis-cluster/

# mkdir 7000 7001 7002

# for i in 1 2 3; do echo -e "port 700$i\n\

bind `hostname -I`\n\

daemonize yes\n\

pidfile /var/run/redis_700$i.pid\n\

cluster-enabled yes\n\

cluster-config-file nodes_700$i.conf\n\

cluster-node-timeout 10100" > 700$i/redis.conf; done

# vim 7000/redis.conf

# vim 7001/redis.conf

# vim 7002/redis.conf

 

3、启动所有的redis

# redis-server /data/redis/redis-cluster/7001/redis.conf

# redis-server /data/redis/redis-cluster/7002/redis.conf

# redis-server /data/redis/redis-cluster/7000/redis.conf

# netstat -antup|grep redis

 

4、安装ruby-redis集群插件

# gem install redis

安装提示ruby版本必须大于2.2

# gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

导入ssl秘钥,安装rvm

# curl -sSL https://get.rvm.io | bash -s stable

# source /usr/local/rvm/scripts/rvm

# rvm list known

列举包版本信息

# rvm install 2.4

安装2.4

# gem install redis

 

5、创建redis集群

# redis-trib.rb create --replicas 1 192.168.8.28:7000 192.168.8.28:7001 192.168.8.28:7002 192.168.8.29:7003 192.168.8.29:7004 192.168.8.29:7005

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.8.28:7000

192.168.8.29:7003

192.168.8.28:7001

Adding replica 192.168.8.29:7005 to 192.168.8.28:7000

Adding replica 192.168.8.28:7002 to 192.168.8.29:7003

Adding replica 192.168.8.29:7004 to 192.168.8.28:7001

M: ae59c157ae584061c3aa95d5cf19215c2e9d0a73 192.168.8.28:7000

slots:0-5460 (5461 slots) master

M: 2a90c2f1cd733564bffeceff52180c6bc75e8eb9 192.168.8.28:7001

slots:10923-16383 (5461 slots) master

S: 1ec034666aa3ff04fdbd4dd9d1cf18e78aaa1fd7 192.168.8.28:7002

replicates 21d545433fb2df095abc8071a68a869800e2e86a

M: 21d545433fb2df095abc8071a68a869800e2e86a 192.168.8.29:7003

slots:5461-10922 (5462 slots) master

S: b94f1d869a2ccea8fe80b42bbd1f0f3252c0e64a 192.168.8.29:7004

replicates 2a90c2f1cd733564bffeceff52180c6bc75e8eb9

S: 7a4a6f54f4529f001ae3fa4b15a2a392c9c377c2 192.168.8.29:7005

replicates ae59c157ae584061c3aa95d5cf19215c2e9d0a73

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.8.28:7000)

M: ae59c157ae584061c3aa95d5cf19215c2e9d0a73 192.168.8.28:7000

slots:0-5460 (5461 slots) master

1 additional replica(s)

M: 2a90c2f1cd733564bffeceff52180c6bc75e8eb9 192.168.8.28:7001

slots:10923-16383 (5461 slots) master

1 additional replica(s)

S: b94f1d869a2ccea8fe80b42bbd1f0f3252c0e64a 192.168.8.29:7004

slots: (0 slots) slave

replicates 2a90c2f1cd733564bffeceff52180c6bc75e8eb9

M: 21d545433fb2df095abc8071a68a869800e2e86a 192.168.8.29:7003

slots:5461-10922 (5462 slots) master

1 additional replica(s)

S: 7a4a6f54f4529f001ae3fa4b15a2a392c9c377c2 192.168.8.29:7005

slots: (0 slots) slave

replicates ae59c157ae584061c3aa95d5cf19215c2e9d0a73

S: 1ec034666aa3ff04fdbd4dd9d1cf18e78aaa1fd7 192.168.8.28:7002

slots: (0 slots) slave

replicates 21d545433fb2df095abc8071a68a869800e2e86a

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

6、使用redis-cli测试并获取集群信息
# redis-cli -h 192.168.8.28 -c -p 7000
192.168.8.28:7000> set name wuhan
-> Redirected to slot [5798] located at 192.168.8.29:7003
OK
192.168.8.29:7003> get name
"wuhan"
# redis-cli -h 192.168.8.29 -c -p 7005
192.168.8.29:7005> get name
-> Redirected to slot [5798] located at 192.168.8.29:7003
"wuhan"
192.168.8.29:7003> CLUSTER NODES
列出集群节点
21d545433fb2df095abc8071a68a869800e2e86a 192.168.8.29:7003@17003 myself,master - 0 1550554527000 4 connected 5461-10922
7a4a6f54f4529f001ae3fa4b15a2a392c9c377c2 192.168.8.29:7005@17005 slave ae59c157ae584061c3aa95d5cf19215c2e9d0a73 0 1550554529807 6 connected
ae59c157ae584061c3aa95d5cf19215c2e9d0a73 192.168.8.28:7000@17000 master - 0 1550554529000 1 connected 0-5460
1ec034666aa3ff04fdbd4dd9d1cf18e78aaa1fd7 192.168.8.28:7002@17002 slave 21d545433fb2df095abc8071a68a869800e2e86a 0 1550554529000 4 connected
2a90c2f1cd733564bffeceff52180c6bc75e8eb9 192.168.8.28:7001@17001 master - 0 1550554529083 2 connected 10923-16383
b94f1d869a2ccea8fe80b42bbd1f0f3252c0e64a 192.168.8.29:7004@17004 slave 2a90c2f1cd733564bffeceff52180c6bc75e8eb9 0 1550554527000 5 connected
192.168.8.29:7003> 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:4
cluster_stats_messages_ping_sent:7279
cluster_stats_messages_pong_sent:7086
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:14368
cluster_stats_messages_ping_received:7084
cluster_stats_messages_pong_received:7282
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:14368

 

参考网址:https://www.cnblogs.com/bigdevilking/p/9758143.html

标签:redis,192.168,cluster,slots,8.28,8.29,搭建
来源: https://www.cnblogs.com/elephanyu/p/10721889.html