数据库
首页 > 数据库> > Redis集群

Redis集群

作者:互联网

  网上做Redis集群集群的博客,大多是在一台或者两台虚拟机上起了6个redis,本篇博文,在3台虚拟机上做此实验.

1.环境准备

# 安装ruby、rubygems,是因为创建集群的脚本是用ruby写的
# 单实例的redis,每秒读写最大QPS是11万,一般是控制在5万以内,最佳是3万,超过7万就会出问题
yum -y install ruby rubygems
# 可以替换成国内镜像
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
gem install redis
Fetching: redis-4.1.0.gem (100%)
ERROR:  Error installing redis:
	redis requires Ruby version >= 2.2.2.
# 报错:说是ruby的版本过低,至少得2.2.2,通过yum安装的ruby版本是2.0.0,所以进行以下操作

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm list known    # 列出ruby版本列表
rvm install 2.4.1 # 这里安装ruby2.4.1,大约需要十分钟

下载rvm截图

升级完ruby之后安装redis

2.配置并启动服务

mkdir /var/log/redis
# 在10.0.0.16上创建{7002,7003},10.0.0.17上创建{7004,7005}
mkdir -p mkdir /usr/local/redis_cluster/{7000,7001}
cd /usr/local/redis_cluster
cp /usr/local/redis/redis.conf 7000/
# 修改配置文件,修改如下内容,总共6个配置文件,分别修改IP和端口

bind 10.0.0.15
protected-mode no
port 7000
daemonize yes
pidfile "/var/run/redis_7000.pid"
logfile "/var/log/redis/redis_7000.log"
dir "./"
cluster-enabled  yes
cluster-config-file  nodes_7000.conf
cluster-node-timeout  15000
appendonly yes

# 启动服务
10.0.0.15
redis-server /usr/local/redis_cluster/7000/redis.conf
redis-server /usr/local/redis_cluster/7001/redis.conf
10.0.0.16
redis-server /usr/local/redis_cluster/7002/redis.conf
redis-server /usr/local/redis_cluster/7003/redis.conf
10.0.0.17
redis-server /usr/local/redis_cluster/7004/redis.conf
redis-server /usr/local/redis_cluster/7005/redis.conf

创建集群

/usr/local/redis/src/redis-trib.rb create --replicas 1 10.0.0.15:7000 \
10.0.0.15:7001 10.0.0.16:7002 10.0.0.16:7003 10.0.0.17:7004 10.0.0.17:7005

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.0.15:7000
10.0.0.16:7002
10.0.0.17:7004
Adding replica 10.0.0.16:7003 to 10.0.0.15:7000
Adding replica 10.0.0.15:7001 to 10.0.0.16:7002
Adding replica 10.0.0.17:7005 to 10.0.0.17:7004
M: 5c7abc77cc35032a1ecf5b7f25301336dce8a4dc 10.0.0.15:7000
   slots:0-5460 (5461 slots) master
S: 8683a7e9bb9fcb435a77b54dcfdcb31eb1efcc9f 10.0.0.15:7001
   replicates 9fa8b7143f87b7326a35d5a1108b554afaf6e37a
M: 9fa8b7143f87b7326a35d5a1108b554afaf6e37a 10.0.0.16:7002
   slots:5461-10922 (5462 slots) master
S: c32f1532f8cdf76876d46b84b4fcb284d4bda1d7 10.0.0.16:7003
   replicates 5c7abc77cc35032a1ecf5b7f25301336dce8a4dc
M: b6a45a8dfc746997e481c3cb8bd6ef544db693f0 10.0.0.17:7004
   slots:10923-16383 (5461 slots) master
S: 4c266ca5b75544eb231aaf4c7ff659c3a16fe46e 10.0.0.17:7005
   replicates b6a45a8dfc746997e481c3cb8bd6ef544db693f0
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.0.0.15:7000)
M: 5c7abc77cc35032a1ecf5b7f25301336dce8a4dc 10.0.0.15:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 9fa8b7143f87b7326a35d5a1108b554afaf6e37a 10.0.0.16:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 4c266ca5b75544eb231aaf4c7ff659c3a16fe46e 10.0.0.17:7005
   slots: (0 slots) slave
   replicates b6a45a8dfc746997e481c3cb8bd6ef544db693f0
M: b6a45a8dfc746997e481c3cb8bd6ef544db693f0 10.0.0.17:7004
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 8683a7e9bb9fcb435a77b54dcfdcb31eb1efcc9f 10.0.0.15:7001
   slots: (0 slots) slave
   replicates 9fa8b7143f87b7326a35d5a1108b554afaf6e37a
S: c32f1532f8cdf76876d46b84b4fcb284d4bda1d7 10.0.0.16:7003
   slots: (0 slots) slave
   replicates 5c7abc77cc35032a1ecf5b7f25301336dce8a4dc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

# 登录集群中某一个节点
redis-cli -h 10.0.0.15 -c -p 7000
# 查看集群节点
redis-cli -p 7000 -h 10.0.0.25 cluster nodes
# 当数据分布不均匀时,可以重新分片
/usr/local/redis/src/redis-trib.rb reshard 10.0.0.15:7000

  需要注意的是:必须要3个或以上的主节点,否则在创建集群时会失败;并且当存活的主节点数小于总节点数的一半时,整个集群就无法提供服务了.

 

Redis集群搭建与简单使用:https://www.cnblogs.com/wuxl360/p/5920330.html

redis三主三从搭建:http://blog.51cto.com/13520772/2316992

Redis API-PHP连接redis、Python连接redis、redis cluster的连接与操作、sentinel集群连接并操作:http://blog.51cto.com/13520772/2109405

 

标签:10.0,Redis,redis,0.15,cluster,集群,7000,slots
来源: https://www.cnblogs.com/fawaikuangtu123/p/10388188.html