Press "Enter" to skip to content

Linux Redis集群搭建(三主三从)

主机环境:

[root@test ~]# uname -a
Linux test 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@test ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@test ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.139.129 netmask 255.255.255.0 broadcast 192.168.139.255
ether 00:0c:29:48:68:b2 txqueuelen 1000 (Ethernet)
RX packets 156636 bytes 234708838 (223.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 45902 bytes 2843208 (2.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
redis版本:redis-4.0.10

具体步骤:

[root@test home]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz
[root@test home]# tar -zxvf redis-4.0.10.tar.gz
[root@test home]# cd redis-4.0.10/
[root@test redis-4.0.10]# make && make install

编译的过程中有报错:

In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
 #include <jemalloc/jemalloc.h>
                               ^
compilation terminated.

解决办法:

[root@test redis-4.0.10]# make MALLOC=libc

启动Redis多个实例:

在Redis安装目录下创建目录cluster,并编写7000.conf~7005.conf 6个配置文件,这6个配置文件用来启动6个实例,后面将使用这6个实例组成集群.
以7000.conf为例,配置文件需要填写如下几项:

port 7000 //端口7000,7001,7002,7003,7004,7005依次修改
bind 192.168.139.129 //默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
pidfile ./redis_7000.pid //pidfile文件对应7000,7001,7002,7003,7004,7005
cluster-enabled yes //开启集群,把注释#去掉
cluster-config-file nodes_7000.conf //集群的配置,配置文件首次启动自动生成7000,7001,7002,7003,7004,7005
cluster-node-timeout 15000 //请求超时,默认15秒,可自行设置
appendonly yes //aof日志开启,有需要就开启,它会每次写操作都记录一条日志

分别启动6个实例

./bin/redis-server cluster/conf/7000.conf
./bin/redis-server cluster/conf/7001.conf
./bin/redis-server cluster/conf/7002.conf
./bin/redis-server cluster/conf/7003.conf
./bin/redis-server cluster/conf/7004.conf
./bin/redis-server cluster/conf/7005.conf

启动成功后:看一下进程

# ps -ef | grep redis | grep cluster

至此机器上已经创建了6个实例,端口号为7000~7005

新建集群(3主3从):

Redis 3.0以上的集群方式是通过Redis安装目录下的bin/redis-trib.rb脚本搭建,集群需要Ruby运行的环境,安装ruby:

[root@test redis-4.0.10]# yum install -y ruby*

ruby安装完成以后使用gem这个命令来安装redis接口:

[root@test redis-4.0.10]# gem install redis //使用gem这个命令来安装redis接口

结果有报错:

[root@test redis-4.0.10]# gem install redis
Fetching: redis-4.0.1.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version &gt;= 2.2.2.

看上去是本机的ruby版本太低,需要2.2.2及以上.
查看本机ruby版本:

[root@test redis-4.0.10]# rpm -qa|grep ruby
ruby-2.0.0.648-33.el7_4.x86_64 //果然是版本不够,需要升级

升级ruby:

[root@test redis-4.0.10]# curl -L get.rvm.io | bash -s stable //安装rvm

发现有如下报错:

按照提示做:

[root@test redis-4.0.10]# gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
[root@test redis-4.0.10]# curl -L get.rvm.io | bash -s stable //重新安装rvm

如果还是失败,再执行如下语句:

[root@test redis-4.0.10]#curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
[root@test redis-4.0.10]# curl -L get.rvm.io | bash -s stable //再重新安装rvm
[root@test redis-4.0.10]# source /usr/local/rvm/scripts/rvm //加载环境变量
[root@test redis-4.0.10]# rvm list known //查看Ruby可用版本
<h1>MRI Rubies</h1>
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
[root@test redis-4.0.10]# rvm install 2.4.1 //安装2.4.1这个版本
[root@test redis-4.0.10]# ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux] //说明升级成功
[root@test redis-4.0.10]# gem install redis //安装gem redis接口
[root@test redis-4.0.10]# yum install -y rubygems

执行上面的脚本新建集群:

[root@test redis-4.0.10]# ruby ./bin/redis-trib.rb create --replicas 1 192.168.139.129:7000 192.168.139.129:7001 192.168.139.129:7002 192.168.139.129:7003 192.168.139.129:7004 192.168.139.129:7005
--replicas 1表示为集群的master节点创建1个副本.那么6个实例里,有三个master,有三个是slave.后面跟上6个实例就好了,形式就是ip:port
可以看到建立了3个master和对应的replica:
Using 3 masters:
192.168.139.129:7000
192.168.139.1297001
192.168.139.129:7002
Adding replica 192.168.139.129:7003 to 192.168.139.129:7000
Adding replica 192.168.139.129:7004 to 192.168.139.129:7001
Adding replica 192.168.139.129:7005 to 192.168.139.129:7002

验证集群状态:

./bin/redis-cli -h 192.168.139.129 -p 7000 -c //-c标识以集群方式登录
192.168.139.129:7000>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:8
cluster_my_epoch:8
cluster_stats_messages_sent:215
cluster_stats_messages_received:215
192.168.139.129:7000>cluster nodes
942c9f97dc68198c39f425d13df0d8e3c40c5a58 192.168.139.129:7004 slave 5ac973bceab0d486c497345fe884ff54d1bb225a 0 1507806791940 5 connected
5ac973bceab0d486c497345fe884ff54d1bb225a 192.168.139.129:7001 master - 0 1507806788937 2 connected 5461-10922
a92a81532b63652bbd862be6f19a9bd8832e5e05 192.168.139.129:7005 slave cc46a4a1c0ec3f621b6b5405c6c10b7cffe73932 0 1507806790939 6 connected
cc46a4a1c0ec3f621b6b5405c6c10b7cffe73932 192.168.139.129:7002 master - 0 1507806789937 3 connected 10923-16383
6346ae8c7af7949658619fcf4021cc7aca454819 192.168.139.129:7000 myself,slave 92f62ec93a0550d962f81213ca7e9b3c9c996afd 0 0 1 connected
92f62ec93a0550d962f81213ca7e9b3c9c996afd 192.168.139.129:7003 master - 0 1507806792941 8 connected 0-5460

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注