Redis4.0.10集群搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis4.0.10集群搭建相关的知识,希望对你有一定的参考价值。
192.168.254.128
192.168.254.129
192.168.254.127
注意事项
安裝 GCC 编译工具 不然会有编译不过的问题
yum install -y gcc g++ gcc-c++ make
升级所有的包,防止出现版本过久不兼容问题
yum -y update
环境准备
安装ruby;
关闭防火墙 节点之前需要开放指定端口,为了方便,生产不要禁用
centos 6.x
service iptables stop # 关闭命令:
centos 7.x
systemctl stop firewalld.service # 停止firewall
集群搭建
首先在每台服务器上分别搭建redis及配置
安装 Redis
下载,解压,编译安装
cd /opt
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
tar xzf redis-4.0.10.tar.gz
cd redis-4.0.10
make
如果因为上次编译失败,有残留的文件
make distclean
创建节点
分别在每台服务器上 创建集群目录
cd /usr/local/
mkdir redis-cluster
在每台 redis-cluster 目录下
cd redis-cluster/
分别在每台服务器上创建
192.168.254.128
mkdir 6000 6001 6002
192.168.254.129
mkdir 6003 6004 6005
192.168.254.127
mkdir 6006 6007 6008
分别修改每台服务器上的三个配置文件,把如下redis.conf 配置内容粘贴进去
192.168.254.128
vi 6000/redis.conf
vi 6001/redis.conf
vi 6002/redis.conf
192.168.254.129
vi 6003/redis.conf
vi 6004/redis.conf
vi 6005/redis.conf
192.168.254.127
vi 6006/redis.conf
vi 6007/redis.conf
vi 6008/redis.conf
分别在每台服务器上配置相应的配置
redis.conf 配置说明
#端口 6000 6001 6002
port 6000
#默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群
bind 192.168.254.128
#redis后台运行
daemonize yes
#pidfile文件对应6000,6001,6002
pidfile /var/run/redis_6000.pid
#开启集群,把注释#去掉
cluster-enabled yes
#集群的配置,配置文件首次启动自动生成 6000,6001,6002
cluster-config-file nodes_6000.conf
#请求超时,默认15秒,可自行设置
cluster-node-timeout 10100
#aof日志开启,有需要就开启,它会每次写操作都记录一条日志
appendonly yes
接着在另外两台机器上(192.168.254.129;192.168.254.127)重复以上三步,只是把目录改为6003、6004、6005;6006、6007、008对应的配置文件也按照这个规则修改即可
#第一台机器上执行 3个节点
for((i=0;i<=2;i++)); do /opt/redis-4.0.10/src/redis-server /usr/local/redis-cluster/600$i/redis.conf; done
第二台机器上执行 3个节点
for((i=3;i<=5;i++)); do /opt/redis-4.0.10/src/redis-server /usr/local/redis-cluster/600$i/redis.conf; done
第三台机器上执行 3个节点
for((i=6;i<=8;i++)); do /opt/redis-4.0.10/src/redis-server /usr/local/redis-cluster/600$i/redis.conf; done
检查服务
检查各 Redis 各个节点启动情况
ps -ef | grep redis //redis是否启动成功
netstat -tnlp | grep redis //监听redis端口
安装ruby
在刚安装好的redis服务器上安装ruby
因为创建集群时需要用Ruby 运行redis-trib.rb
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis 这个自动安装不了
只能把它下载下来,手工安装
wget http://rubygems.global.ssl.fastly.net/gems/redis-4.0.1.gem
gem install -l ./redis-4.0.1.gem
/opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 192.168.254.128:6000 192.168.254.128:6001 192.168.254.128:6002 192.168.254.129:6003 192.168.254.129:6004 192.168.254.129:6005 192.168.254.127:6006 192.168.254.127:6007 192.168.254.127:6008
>> Creating cluster
>> Performing hash slots allocation on 9 nodes...
Using 4 masters:
192.168.254.128:6000
192.168.254.129:6003
192.168.254.127:6006
192.168.254.128:6001
Adding replica 192.168.254.127:6007 to 192.168.254.128:6000
Adding replica 192.168.254.128:6002 to 192.168.254.129:6003
Adding replica 192.168.254.129:6005 to 192.168.254.127:6006
Adding replica 192.168.254.127:6008 to 192.168.254.128:6001
Adding replica 192.168.254.129:6004 to 192.168.254.128:6000
M: a4652ca08fd832def811f585ac14a3367364b6bc 192.168.254.128:6000
slots:0-4095 (4096 slots) master
M: 6275ac1e805e1d54281b66a7d49d64d2b0e802da 192.168.254.128:6001
slots:12288-16383 (4096 slots) master
S: 84a752afcb6fda24df7a2fa220d38170daac5976 192.168.254.128:6002
replicates d9228859beb0180ab6ff198a1819088d401f936a
M: d9228859beb0180ab6ff198a1819088d401f936a 192.168.254.129:6003
slots:4096-8191 (4096 slots) master
S: 4fc65bf3c4747d767fa56b357891d4062b310f52 192.168.254.129:6004
replicates a4652ca08fd832def811f585ac14a3367364b6bc
S: ea51fa38217674f51a968b341732d77eabe7561d 192.168.254.129:6005
replicates b14eddc61b984089360713932cd16bd114f3aff4
M: b14eddc61b984089360713932cd16bd114f3aff4 192.168.254.127:6006
slots:8192-12287 (4096 slots) master
S: 84645ac8b50a4f199b413a290835f363c2c17cd8 192.168.254.127:6007
replicates a4652ca08fd832def811f585ac14a3367364b6bc
S: 172be3915c71f16302fa3ce8219af024f950f0e5 192.168.254.127:6008
replicates 6275ac1e805e1d54281b66a7d49d64d2b0e802da
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.254.128:6000)
M: a4652ca08fd832def811f585ac14a3367364b6bc 192.168.254.128:6000
slots:0-4095 (4096 slots) master
2 additional replica(s)
S: ea51fa38217674f51a968b341732d77eabe7561d 192.168.254.129:6005
slots: (0 slots) slave
replicates b14eddc61b984089360713932cd16bd114f3aff4
S: 172be3915c71f16302fa3ce8219af024f950f0e5 192.168.254.127:6008
slots: (0 slots) slave
replicates 6275ac1e805e1d54281b66a7d49d64d2b0e802da
S: 84645ac8b50a4f199b413a290835f363c2c17cd8 192.168.254.127:6007
slots: (0 slots) slave
replicates a4652ca08fd832def811f585ac14a3367364b6bc
S: 4fc65bf3c4747d767fa56b357891d4062b310f52 192.168.254.129:6004
slots: (0 slots) slave
replicates a4652ca08fd832def811f585ac14a3367364b6bc
M: d9228859beb0180ab6ff198a1819088d401f936a 192.168.254.129:6003
slots:4096-8191 (4096 slots) master
1 additional replica(s)
M: 6275ac1e805e1d54281b66a7d49d64d2b0e802da 192.168.254.128:6001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: 84a752afcb6fda24df7a2fa220d38170daac5976 192.168.254.128:6002
slots: (0 slots) slave
replicates d9228859beb0180ab6ff198a1819088d401f936a
M: b14eddc61b984089360713932cd16bd114f3aff4 192.168.254.127:6006
slots:8192-12287 (4096 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.
[[email protected] ~]# ps -ef|grep redis
root 17107 1 0 15:49 ? 00:00:24 /opt/redis-4.0.10/src/redis-server 192.168.254.128:6000 [cluster]
root 17112 1 0 15:49 ? 00:00:24 /opt/redis-4.0.10/src/redis-server 192.168.254.128:6001 [cluster]
root 17114 1 0 15:49 ? 00:00:24 /opt/redis-4.0.10/src/redis-server 192.168.254.128:6002 [cluster]
root 36435 16949 0 20:21 pts/0 00:00:00 grep --color=auto redis
关闭集群
这样也可以,推荐
$ pkill redis
循环节点逐个关闭
for((i=0;i<=2;i++)); do /opt/redis-4.0.10/src/redis-cli -c -h 192.168.254.128 -p 600$i shutdown; done
for((i=3;i<=5;i++)); do /opt/redis-4.0.10/src/redis-cli -c -h 192.168.254.129 -p 600$i shutdown; done
for((i=6;i<=8;i++)); do /opt/redis-4.0.10/src/redis-cli -c -h 192.168.254.127 -p 600$i shutdown; done
集群验证
连接集群测试
参数 -C 可连接到集群,因为 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略,-p 参数为端口号
查看集群状态
cluster nodes
以上是关于Redis4.0.10集群搭建的主要内容,如果未能解决你的问题,请参考以下文章