redis4.0.2版本集群搭建

redhat7下redis源码编译安装,需要先安装pcre,openssl等依赖。

yum install openssl-devel pcre-devel

然后下载redis-4.0.2.tar.gz,解压,进入redis-4.0.2解压完的目录,以root用户运行make && make install

make install会将src下面可执行程序如redis-server,redis-cli,redis-check-aof,redis-check-rdb,redis-benchmark安装到/usr/local/bin目录下。

redis4.0.2cluster搭建相对于单节点搭建稍微复杂一点,就是要为每个节点单独配置一个配置文件,而且启动的时候必须进入配置文件所在的目录,不能通过redis-server 指定配置文件绝对路径或者相对路径来启动实例,另外在配置文件中需要开启cluster-enabled配置,默认是不开启的。这里假设在一个服务器上开启6个节点构建集群。6个节点对应的端口分别为6379,6380,6381,6382,6383,6384。以其中一个6387/redis.conf配置文件为例:

bind 192.168.42.128
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000

其中cluster-enabled,cluster-config-file是必须配置的,另外为了区别本机上的其他节点,port,pidfile做配置,其余配置基本和以前版本没有区别。

依次启动每一个节点:

[root@server 6379]# /usr/local/bin/redis-server redis.conf 
5930:C 28 Nov 03:25:09.979 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5930:C 28 Nov 03:25:09.979 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5930, just started
5930:C 28 Nov 03:25:09.979 # Configuration loaded
[root@server 6379]# cd ../6380/
[root@server 6380]# /usr/local/bin/redis-server redis.conf
5935:C 28 Nov 03:25:25.177 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5935:C 28 Nov 03:25:25.177 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5935, just started
5935:C 28 Nov 03:25:25.177 # Configuration loaded
[root@server 6380]# cd ../6381/
[root@server 6381]# /usr/local/bin/redis-server redis.conf
5940:C 28 Nov 03:25:35.654 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5940:C 28 Nov 03:25:35.655 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5940, just started
5940:C 28 Nov 03:25:35.655 # Configuration loaded
[root@server 6381]# cd ../6382/
[root@server 6382]# /usr/local/bin/redis-server redis.conf
5945:C 28 Nov 03:25:44.166 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5945:C 28 Nov 03:25:44.166 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5945, just started
5945:C 28 Nov 03:25:44.166 # Configuration loaded
[root@server 6382]# cd ../6383/
[root@server 6383]# /usr/local/bin/redis-server redis.conf
5950:C 28 Nov 03:25:52.197 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5950:C 28 Nov 03:25:52.197 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5950, just started
5950:C 28 Nov 03:25:52.197 # Configuration loaded
[root@server 6383]# cd ../6484/
-bash: cd: ../6484/: No such file or directory
[root@server 6383]# cd ../6384/
[root@server 6384]# /usr/local/bin/redis-server redis.conf
5957:C 28 Nov 03:26:06.756 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5957:C 28 Nov 03:26:06.756 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5957, just started
5957:C 28 Nov 03:26:06.756 # Configuration loaded

查看启动的节点

	

安装rubygems

[root@server redis-4.0.2]# yum install rubygems
[root@server redis-4.0.2]# gem install redis -v 3.3.3
Fetching: redis-3.3.3.gem (100%)
Successfully installed redis-3.3.3
Parsing documentation for redis-3.3.3
Installing ri documentation for redis-3.3.3
1 gem installed

说明:如果你有多个虚拟机,rubygems只需要在一台机器上安装,这里指定gem install redis -v 3.3.3版本,因为最新的版本存在bug,最终执行redis-trib.rb命令构建集群在哪台机器上都可以。

构建集群

[root@server redis-4.0.2]# src/redis-trib.rb create --replicas 1 192.168.42.128:6379 192.168.42.128:6380 192.168.42.128:6381 192.168.42.128:6382 192.168.42.128:6383 192.168.42.128:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.42.128:6379
192.168.42.128:6380
192.168.42.128:6381
Adding replica 192.168.42.128:6382 to 192.168.42.128:6379
Adding replica 192.168.42.128:6383 to 192.168.42.128:6380
Adding replica 192.168.42.128:6384 to 192.168.42.128:6381
M: 8a3124f8fd8f9925b0202c4072fcf9fe34df4a61 192.168.42.128:6379
   slots:0-5460 (5461 slots) master
M: bfda1c760994f1805de78dfd692a19a3807431f5 192.168.42.128:6380
   slots:5461-10922 (5462 slots) master
M: 2a72906ca42ed83065f8ec8a60483ff5e6715bcd 192.168.42.128:6381
   slots:10923-16383 (5461 slots) master
S: dae96e813dc06ae7ad9b771cc4ec7887111ec32a 192.168.42.128:6382
   replicates 8a3124f8fd8f9925b0202c4072fcf9fe34df4a61
S: debbb67f323b41ee4dde48d904003340cde6507b 192.168.42.128:6383
   replicates bfda1c760994f1805de78dfd692a19a3807431f5
S: 10493890436c3a364ccbb640e137810ca4261fe1 192.168.42.128:6384
   replicates 2a72906ca42ed83065f8ec8a60483ff5e6715bcd
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.42.128:6379)
M: 8a3124f8fd8f9925b0202c4072fcf9fe34df4a61 192.168.42.128:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 10493890436c3a364ccbb640e137810ca4261fe1 192.168.42.128:6384
   slots: (0 slots) slave
   replicates 2a72906ca42ed83065f8ec8a60483ff5e6715bcd
S: dae96e813dc06ae7ad9b771cc4ec7887111ec32a 192.168.42.128:6382
   slots: (0 slots) slave
   replicates 8a3124f8fd8f9925b0202c4072fcf9fe34df4a61
S: debbb67f323b41ee4dde48d904003340cde6507b 192.168.42.128:6383
   slots: (0 slots) slave
   replicates bfda1c760994f1805de78dfd692a19a3807431f5
M: 2a72906ca42ed83065f8ec8a60483ff5e6715bcd 192.168.42.128:6381
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: bfda1c760994f1805de78dfd692a19a3807431f5 192.168.42.128:6380
   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.
[root@server redis-4.0.2]#

检验集群

	

三个主节点,三个从节点

你可能感兴趣的:(linux,database)