redis-cluster是redis3.0以后出现的特性,它采用无中心思想,每个节点都可以保存数据,也提供查询,一个节点fail,其他节点照样组成一个cluster,当节点重新up,数据会从其他节点转移,数据又会恢复,这样保证数据一致性。也保证了高可靠性。
redis-cluster的搭建比单节点多了一步,就是最后让单个节点组成集群,这一步会涉及一些别的环境ruby,搭建的过程耗时的地方在这里。
另外配置文件也会稍微有些变化,这里会多出一些属性,比如cluster-enabled,cluster-config-file等,这是组成cluster集群必须的属性。cluster环境搭建过程如下:
一、安装源码编译所需依赖和ruby环境需要的依赖
yum install zlib-devel openssl-devel gcc-c++
wget http://download.redis.io/releases/redis-4.0.2.tar.gz
tar -xzf redis-4.0.2.tar.gz
cd redis-4.0.2
make && make install
最后的make install是将redis-server,redis-cli相关命令安装到/usr/local/bin目录,这样可以直接在命令行下输入redis-server,redis-cli就可以执行redis相关命令,不用进入源码目录。
如果make出现缺少jemalloc文件,可以使用make MALLOC=libc来编译。
到了这里redis单机版就可以使用了,这里对配置文件稍作修改。我们的架构如下图所示。
三、修改配置文件,增加节点。
cd /data/redis-4.0.2
mkdir config
cd config
mkdir redis-6379 redis-6387
cp /data/redis-4.0.2/redis.conf redis-6379/
cp /data/redis-4.0.2/redis.conf redis-6387/
修改配置,对如下配置做更改
bind 192.168.42.131
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
启动redis
进入配置文件的文件夹,然后启动,不能通过指定配置文件路径的方式启动,如:redis-server redis-6379/redis.conf
cd /data/redis-4.0.2/config/redis-6379
redis-server redis.conf
cd ../redis-6387
redis-server redis.conf
依次在每个节点上启动每一个实例。
启动之后,redis进程如下所示:
[root@node01 redis-6379]# ps -ef |grep redis
root 79475 1 0 00:51 ? 00:00:12 redis-server 192.168.42.131:6379 [cluster]
root 79503 1 0 00:52 ? 00:00:12 redis-server 192.168.42.131:6387 [cluster]
root 112995 2557 0 03:13 pts/0 00:00:00 grep --color=auto redis
最终三台机器均开启两个实例,对应的端口分别为6379和6387
四、这一步需要通过gem install redis,而gem是ruby环境中的命令,因此需要安装ruby。
wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.gz
tar -xzf ruby-2.4.2.tar.gz
cd ruby-2.4.2
./configure
make && make install
接着可以使用gem install redis来安装redis依赖。而这一步可能会出现两个错误,分别是缺少zlib库和openssl库,不是我们没有安装,而是需要将他们集成到ruby。集成到ruby的办法就是进入ruby相关目录,执行以下操作。
cd /data/ruby-2.4.2/ext/zlib
ruby extconf.rb
make && make install
openssl类似,在执行完ruby extconf.rb之后,会改变Makefile文件,这里需要将文件中的$(top_srcdir)统一改为../..,如果不修改make会提示找不到相关头文件。
gem install redis出现以下结果,表示安装成功。
[root@node01 ruby-2.4.2]# gem install redis
Fetching: redis-4.0.1.gem (100%)
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 1 seconds
1 gem installed
五、构建群集
./redis-trib.rb create --replicas 1 192.168.42.131:6379 192.168.42.131:6387 192.168.42.132:6379 192.168.42.132:6387 192.168.42.133:6379 192.168.42.133:6387
顺利的话会出现如下,输入yes,cluster构建成功。
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.42.131:6379
192.168.42.132:6379
192.168.42.133:6379
Adding replica 192.168.42.132:6387 to 192.168.42.131:6379
Adding replica 192.168.42.131:6387 to 192.168.42.132:6379
Adding replica 192.168.42.133:6387 to 192.168.42.133:6379
M: ac7e3c93b7092c4e183ac01637c1faa453a217ef 192.168.42.131:6379
slots:0-5460 (5461 slots) master
S: b036e775d1e0d92f9aad6613abdcae5023c8dc86 192.168.42.131:6387
replicates 327d987fde60892fdfb7a9da72364639091e16e2
M: 327d987fde60892fdfb7a9da72364639091e16e2 192.168.42.132:6379
slots:5461-10922 (5462 slots) master
S: 8869369755d8fca9f43e08bf585634dc520b1f6b 192.168.42.132:6387
replicates ac7e3c93b7092c4e183ac01637c1faa453a217ef
M: c6b91f0c53c2de0fc135b8fca1f4060f56f3612c 192.168.42.133:6379
slots:10923-16383 (5461 slots) master
S: 117c1907d2ceac100b6dabb6e7c9f975efbafec8 192.168.42.133:6387
replicates c6b91f0c53c2de0fc135b8fca1f4060f56f3612c
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.131:6379)
M: ac7e3c93b7092c4e183ac01637c1faa453a217ef 192.168.42.131:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 8869369755d8fca9f43e08bf585634dc520b1f6b 192.168.42.132:6387
slots: (0 slots) slave
replicates ac7e3c93b7092c4e183ac01637c1faa453a217ef
S: 117c1907d2ceac100b6dabb6e7c9f975efbafec8 192.168.42.133:6387
slots: (0 slots) slave
replicates c6b91f0c53c2de0fc135b8fca1f4060f56f3612c
M: 327d987fde60892fdfb7a9da72364639091e16e2 192.168.42.132:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: b036e775d1e0d92f9aad6613abdcae5023c8dc86 192.168.42.131:6387
slots: (0 slots) slave
replicates 327d987fde60892fdfb7a9da72364639091e16e2
M: c6b91f0c53c2de0fc135b8fca1f4060f56f3612c 192.168.42.133:6379
slots:10923-16383 (5461 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.