首先确定自己需要建立多少个Redis主节点,因为Redis集群需要进行选举判决,所以主节点个数必须是奇数个,也就是说集群里面最少的主节点个数就是3个。因为每个主节点至少有一个备份节点,所以面我将在一台Linux服务器(在多台linux服务器之间搭建集群的流程和一台上搭建一样)里面搭建一个主节点为3个总节点数为6个的Redis集群,其中谁为主节点谁为副节点由集群自己判断。
redis的各个版本的下载地址:redis下载
redis的中文版的集群资料可以访问:http://www.redis.cn/topics/cluster-tutorial.html
安装配置redis
上传redis的安装包到服务器,编译安装 PREFIX后面写的是自己的安装路径
[root@inode-2 server]# tar -zxvf redis-4.0.9.tar.gz
[root@inode-2 server]# cd redis-4.0.9/
[root@inode-2 redis-4.0.9]# make
[root@inode-2 redis-4.0.9]# make install PREFIX=/export/server/redis-cluster
如果运行make出现:gcc: Command not found 此类错误则安装gcc即可,然后删除redis-4.0.9,重新编译安装
[root@inode-2 redis-4.0.9]# yum -y install gcc automake autoconf libtool make
[root@inode-2 redis-4.0.9]# yum install gcc gcc-c++
正确编译安装后安装目录下会有一个bin文件夹,我们将这个bin文件夹复制5份就得到了redis集群的6个节点。我将这个6个bin文件夹分别命名为redis01到redis06(重命名bin:mv bin/ redis01;复制:cp -r redis01/ redis02)。然后在redis01里面新建一个redis.conf配置文件,写入相关配置,然后将配置文件复制到其余5个文件夹。
[root@inode-2 redis01]# vi redis.conf
然后将如下内容写入redis.conf文件,其中port是这个redis的端口号,将redis.conf文件复制给其余5个文件夹时端口号要修改,我是配置的7001~7006
daemonize yes
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes
protected-mode no
要创建redis集群要用到redis-trib,安装redis-trib需要ruby环境,复制redis解压文件src下的redis-trib.rb文件到redis-cluster目录
[root@inode-2 redis-cluster]# cp /export/server/redis-4.0.9/src/redis-trib.rb ./
安装ruby环境
我们需要ruby版本要大于等于2.2.2所以我先安装RVM,通过RVM来安装高版本ruby
[root@inode-2 redis-cluster]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
[root@inode-2 redis-cluster]# curl -sSL https://get.rvm.io | bash -s stable
[root@inode-2 redis-cluster]# source /etc/profile.d/rvm.sh
[root@inode-2 redis-cluster]# rvm reload
[root@inode-2 redis-cluster]# rvm requirements run
[root@inode-2 redis-cluster]# rvm install 2.4.4
[root@inode-2 redis-cluster]# ruby -v
之后再安装rubygem redis依赖
[root@inode-2 redis-cluster]# wget http://rubygems.org/downloads/redis-4.0.1.gem
[root@inode-2 redis-cluster]# gem install ./redis-4.0.1.gem
安装到这里redis集群所需要的环境就安装完全了,我们可以编写一个startall.sh的脚本来启动所有的redis节点
#!/bin/bash
cd ./redis01
./redis-server ./redis.conf
cd ../redis02
./redis-server ./redis.conf
cd ../redis03
./redis-server ./redis.conf
cd ../redis04
./redis-server ./redis.conf
cd ../redis05
./redis-server ./redis.conf
cd ../redis06
./redis-server ./redis.conf
给startall.sh赋权限并启动所有redis
[root@inode-2 redis-cluster]# chmod 755 startall.sh
[root@inode-2 redis-cluster]# ./startall.sh
查看redis运行进程是否启动成功,看到端口号7001~7006,6个节点都启动成功
[root@inode-2 redis-cluster]# ps aux | grep redis
root 1374 0.0 0.7 145308 7580 ? Ssl 08:46 0:00 ./redis-server *:7001 [cluster]
root 1379 0.0 0.7 145308 7584 ? Ssl 08:46 0:00 ./redis-server *:7002 [cluster]
root 1384 0.0 0.7 145308 7584 ? Ssl 08:46 0:00 ./redis-server *:7003 [cluster]
root 1389 0.0 0.7 145308 7588 ? Ssl 08:46 0:00 ./redis-server *:7004 [cluster]
root 1394 0.0 0.7 145308 7584 ? Ssl 08:46 0:00 ./redis-server *:7005 [cluster]
root 1457 0.0 0.7 145308 7580 ? Ssl 08:51 0:00 ./redis-server *:7006 [cluster]
root 1462 0.0 0.0 112704 972 pts/0 R+ 08:51 0:00 grep --color=auto redis
我们可以通过进程名称用pkill来杀掉redis进程来让redis停止
[root@inode-2 redis-cluster]# pkill -9 redis
[root@inode-2 redis-cluster]# ps aux | grep redis
root 1465 0.0 0.0 112704 968 pts/0 R+ 08:54 0:00 grep --color=auto redis
创建redis集群
使用redis-trib.rb创建集群,在这之前保证6个节点都已经开启,不然会报一下错误,下面的192.168.231.13是我虚拟机的ip,你需要换成你自己的服务器ip,或者你也可以写127.0.0.1,不过我不建议写127.0.0.1,因为它会让其它服务器或者你windows上的redis访问工具连接不上你的redis集群
[root@inode-2 redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.231.13:7001 192.168.231.13:7002 192.168.231.13:7003 192.168.231.13:7004 192.168.231.13:7005 192.168.231.13:7006
>>> Creating cluster
[ERR] Sorry, can't connect to node 192.168.231.13:7001
中途会出现Can I set the above configuration? (type 'yes' to accept):提示你是否可以设置,输入yes即可
[root@inode-2 redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.231.13:7001 192.168.231.13:7002 192.168.231.13:7003 192.168.231.13:7004 192.168.231.13:7005 192.168.231.13:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.231.13:7001
192.168.231.13:7002
192.168.231.13:7003
Adding replica 192.168.231.13:7005 to 192.168.231.13:7001
Adding replica 192.168.231.13:7006 to 192.168.231.13:7002
Adding replica 192.168.231.13:7004 to 192.168.231.13:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: e4678a9a8410bf5d2c653e6848c1c7ba68a3e21e 192.168.231.13:7001
slots:0-5460 (5461 slots) master
M: 486f781e0f1ba8f23f963f7e99b0995e2fcd9fda 192.168.231.13:7002
slots:5461-10922 (5462 slots) master
M: 69f56075e9542d9fa000eaa72f67c5a023f65eff 192.168.231.13:7003
slots:10923-16383 (5461 slots) master
S: 32a1a109212a5d6ac0e8b518c6df184445714a72 192.168.231.13:7004
replicates 69f56075e9542d9fa000eaa72f67c5a023f65eff
S: 28fb9022061f2fa40c2a0fc7fddf909d2f9a5609 192.168.231.13:7005
replicates e4678a9a8410bf5d2c653e6848c1c7ba68a3e21e
S: 63b909534ee5fae34200cf63e70b3bf1f162ce8c 192.168.231.13:7006
replicates 486f781e0f1ba8f23f963f7e99b0995e2fcd9fda
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.231.13:7001)
M: e4678a9a8410bf5d2c653e6848c1c7ba68a3e21e 192.168.231.13:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 69f56075e9542d9fa000eaa72f67c5a023f65eff 192.168.231.13:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 28fb9022061f2fa40c2a0fc7fddf909d2f9a5609 192.168.231.13:7005
slots: (0 slots) slave
replicates e4678a9a8410bf5d2c653e6848c1c7ba68a3e21e
M: 486f781e0f1ba8f23f963f7e99b0995e2fcd9fda 192.168.231.13:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 63b909534ee5fae34200cf63e70b3bf1f162ce8c 192.168.231.13:7006
slots: (0 slots) slave
replicates 486f781e0f1ba8f23f963f7e99b0995e2fcd9fda
S: 32a1a109212a5d6ac0e8b518c6df184445714a72 192.168.231.13:7004
slots: (0 slots) slave
replicates 69f56075e9542d9fa000eaa72f67c5a023f65eff
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Waiting for the cluster to join.. (这里有可能出现一直等待的结果)
>>> Performing Cluster Check (using node 192.168.231.13:7001)
M: e4678a9a8410bf5d2c653e6848c1c7ba68a3e21e 192.168.231.13:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 69f56075e9542d9fa000eaa72f67c5a023f65eff 192.168.231.13:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 28fb9022061f2fa40c2a0fc7fddf909d2f9a5609 192.168.231.13:7005
slots: (0 slots) slave
replicates e4678a9a8410bf5d2c653e6848c1c7ba68a3e21e
M: 486f781e0f1ba8f23f963f7e99b0995e2fcd9fda 192.168.231.13:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 63b909534ee5fae34200cf63e70b3bf1f162ce8c 192.168.231.13:7006
slots: (0 slots) slave
replicates 486f781e0f1ba8f23f963f7e99b0995e2fcd9fda
S: 32a1a109212a5d6ac0e8b518c6df184445714a72 192.168.231.13:7004
slots: (0 slots) slave
replicates 69f56075e9542d9fa000eaa72f67c5a023f65eff
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
期间如果出现Waiting for the cluster to join . . . . . . . . . 一直等待没有响应的结果,是因为redis总线端口没有开放。redis集群不仅需要开通redis客户端连接的端口,而且需要开通集群总线端口,集群总线端口为redis客户端连接的端口 + 10000,如redis端口为7001,则集群总线端口为17001,如果是防火墙要开放相应总线端口,如果是阿里云则需要在安全组规则里面配置开放相应的端口。
开放总线端口后先将所有的redis关闭,然后去每个redis节点文件夹里面(本文是redis01-redis06)删除appendonly.aof 、nodes.conf 、dump.rdb(此文件有则删除无则不管)三个文件。然后重新启动所有节点,重新开启集群。
redis集群测试
我们的redis集群就创建好了,然后我们可以测试一下自己集群是否正常工作,我们通过redis-cli随便连接一个redis
[root@inode-2 redis02]# ./redis-cli -c -p 7003
127.0.0.1:7003> set redis_key redis_value
OK
127.0.0.1:7003>
通过Ctrl+C跳出7003端口的redis连接,然后我们去端口7005的redis获取redis_key看能不能获得redis_value
[root@inode-2 redis02]# ./redis-cli -c -p 7005
127.0.0.1:7005> get redis_key
-> Redirected to slot [12262] located at 192.168.231.13:7003
"redis_value"
192.168.231.13:7003>
在redis集群里面我们可以连接到任何一个节点就可以访问所有redis的数据,这样,redis集群就是创建成功了