项目中用到了redis集群,趁机研究下redis单机集群的部署。用的虚拟机系统CentOS release 6.9 (Final)和redis-4.0.8。查看虚拟机系统的版本命令:
[root@nlfd local]# cat /etc/centos
其实可以先总结下基本步骤:
1、下载redis安装包,进行编译
2、对编译好的redis复制多份,每份即一个节点,修改每个节点的redis.conf文件。包括端口、绑定ip、打开集群、个性化的节点配置文件替换等(一般使用默认的节点配置文件)。
3、分别依次启动多个节点,利用redis自带的./redis-trib.rb进行节点创建,需要注意的是这个要依赖于ruby。
4、查看集群是否创建成功。
接下下进行具体的操作:
1、下载编译redis4.0.8
下载redis-4.0.8
[root@nlfd home]# http://download.redis.io/releases/redis-4.0.8.tar.gz
解压编译
[root@nlfd home]# tar -zxvf redis-4.0.8.tar.gz
[root@nlfd home]# cd redis-4.0.8
[root@nlfd redis-4.0.8]# make
[root@nlfd redis-4.0.8]# make test
[root@nlfd redis-4.0.8]# make install
2、配置结点
创建要部署结点的文件夹
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster/7000
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster/7001
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster/7002
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster/7003
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster/7004
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster/7005
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster/7006
[root@nlfd redis-4.0.8]# mkdir -p /usr/local/cluster/7007
复制编译好的redis到创建好的目录下面去
[root@nlfd redis-4.0.8]# cp -rf /home/redis-4.0.8/ /usr/local/cluster/7000/
[root@nlfd redis-4.0.8]# cp -rf /home/redis-4.0.8/ /usr/local/cluster/7001/
[root@nlfd redis-4.0.8]# cp -rf /home/redis-4.0.8/ /usr/local/cluster/7002/
[root@nlfd redis-4.0.8]# cp -rf /home/redis-4.0.8/ /usr/local/cluster/7003/
[root@nlfd redis-4.0.8]# cp -rf /home/redis-4.0.8/ /usr/local/cluster/7004/
[root@nlfd redis-4.0.8]# cp -rf /home/redis-4.0.8/ /usr/local/cluster/7005/
[root@nlfd redis-4.0.8]# cp -rf /home/redis-4.0.8/ /usr/local/cluster/7006/
[root@nlfd redis-4.0.8]# cp -rf /home/redis-4.0.8/ /usr/local/cluster/7007/
修改配置文件
[root@nlfd redis-4.0.8]# vi /usr/local/cluster/7000/redis.conf
daemonize yes
port 7000
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
用7000/redis.conf覆盖7001,7002,7003,7004,7005,7006,7007目录下的redis.conf
[root@nlfd redis-4.0.8]# cp /usr/local/cluster/7000/redis.conf /usr/local/cluster/7001
[root@nlfd redis-4.0.8]# cp /usr/local/cluster/7000/redis.conf /usr/local/cluster/7002
[root@nlfd redis-4.0.8]# cp /usr/local/cluster/7000/redis.conf /usr/local/cluster/7003
[root@nlfd redis-4.0.8]# cp /usr/local/cluster/7000/redis.conf /usr/local/cluster/7004
[root@nlfd redis-4.0.8]# cp /usr/local/cluster/7000/redis.conf /usr/local/cluster/7005
[root@nlfd redis-4.0.8]# cp /usr/local/cluster/7000/redis.conf /usr/local/cluster/7006
[root@nlfd redis-4.0.8]# cp /usr/local/cluster/7000/redis.conf /usr/local/cluster/7007
然后分别修改7001-7007里面的配置文件中的端口为7001-7007.
3、启动8个结点
[root@nlfd src]# cd /usr/local/cluster/7000/src
[root@nlfd src]# redis-server ../redis.conf
[root@nlfd src]# cd /usr/local/cluster/7001/src
[root@nlfd src]# redis-server ../redis.conf
[root@nlfd src]# cd /usr/local/cluster/7002/src
[root@nlfd src]# redis-server ../redis.conf
[root@nlfd src]# cd /usr/local/cluster/7003/src
[root@nlfd src]# redis-server ../redis.conf
[root@nlfd src]# cd /usr/local/cluster/7004/src
[root@nlfd src]# redis-server ../redis.conf
[root@nlfd src]# cd /usr/local/cluster/7005/src
[root@nlfd src]# redis-server ../redis.conf
[root@nlfd src]# cd /usr/local/cluster/7006/src
[root@nlfd src]# redis-server ../redis.conf
[root@nlfd src]# cd /usr/local/cluster/7007/src
[root@nlfd src]# redis-server ../redis.conf
[root@nlfd src]# ps -aux|grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 9610 1.3 0.6 150064 11880 ? Ssl 16:02 0:01 redis-server 127.0.0.1:7000 [cluster]
root 9644 0.8 0.6 150064 11876 ? Ssl 16:04 0:00 redis-server 127.0.0.1:7001 [cluster]
root 9657 0.8 0.6 150064 11876 ? Ssl 16:04 0:00 redis-server 127.0.0.1:7002 [cluster]
root 9670 0.7 0.6 150064 11876 ? Ssl 16:04 0:00 redis-server 127.0.0.1:7003 [cluster]
root 9683 0.5 0.6 150064 11864 ? Ssl 16:04 0:00 redis-server 127.0.0.1:7004 [cluster]
root 9698 0.4 0.6 150064 11872 ? Ssl 16:04 0:00 redis-server 127.0.0.1:7005 [cluster]
root 9713 0.5 0.6 150064 11876 ? Ssl 16:04 0:00 redis-server 127.0.0.1:7006 [cluster]
root 9728 0.3 0.6 150064 11872 ? Ssl 16:04 0:00 redis-server 127.0.0.1:7007 [cluster]
root 9735 0.0 0.0 103336 900 pts/0 S+ 16:04 0:00 grep redis
可以看到8个几点已经全部启动成功,接下来就是创建集群了。
4 创建集群
[root@nlfd src]# cd /home/redis-4.0.8/src/
[root@nlfd src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 127.0.0.1:7007 127.0.0.1:7007
出现如下情况,输入yes
Creating cluster
Performing hash slots allocation on 8 nodes...
Using 4 masters:
127.0.0.1:7000
127.0.0.1:7002
127.0.0.1:7003
127.0.0.1:7004
Adding replica 127.0.0.1:7006 to 127.0.0.1:7000
Adding replica 127.0.0.1:7007 to 127.0.0.1:7002
Adding replica 127.0.0.1:7001 to 127.0.0.1:7003
Adding replica 127.0.0.1:7005 to 127.0.0.1:7004
Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 741c0bf753e65c8e6deca11b8efb994519303daa 127.0.0.1:7000
slots:0-4095 (4096 slots) master
M: 7c4e1dae5c6ce5660156fb727d36f34fb2c65423 127.0.0.1:7002
slots:4096-8191 (4096 slots) master
M: 3329c221b5f689b1bfe26b5ac6996c4a29dea0b3 127.0.0.1:7003
slots:8192-12287 (4096 slots) master
M: faea5db1781470acf660962414ef4772f6e7c28b 127.0.0.1:7004
slots:12288-16383 (4096 slots) master
S: 727abe639adfdea836c0132e2b61d5a5bdbaf967 127.0.0.1:7005
replicates 3329c221b5f689b1bfe26b5ac6996c4a29dea0b3
S: 5fc6364410bae6b2dfe31c8896658738a39f2d7f 127.0.0.1:7006
replicates faea5db1781470acf660962414ef4772f6e7c28b
S: e76e4989e8bb94aa23302002a1934e8ac90fbcd9 127.0.0.1:7007
replicates 7c4e1dae5c6ce5660156fb727d36f34fb2c65423
S: 97f2f9750fb571c6884ca01aedc5cf74d2e254ea 127.0.0.1:7001
replicates 741c0bf753e65c8e6deca11b8efb994519303daa
Can I set the above configuration? (type 'yes' to accept):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 127.0.0.1:7000)
M: 741c0bf753e65c8e6deca11b8efb994519303daa 127.0.0.1:7000
slots:0-4095 (4096 slots) master
1 additional replica(s)
M: faea5db1781470acf660962414ef4772f6e7c28b 127.0.0.1:7004
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: 3329c221b5f689b1bfe26b5ac6996c4a29dea0b3 127.0.0.1:7003
slots:8192-12287 (4096 slots) master
1 additional replica(s)
S: 5fc6364410bae6b2dfe31c8896658738a39f2d7f 127.0.0.1:7006
slots: (0 slots) slave
replicates faea5db1781470acf660962414ef4772f6e7c28b
S: e76e4989e8bb94aa23302002a1934e8ac90fbcd9 127.0.0.1:7007
slots: (0 slots) slave
replicates 7c4e1dae5c6ce5660156fb727d36f34fb2c65423
S: 97f2f9750fb571c6884ca01aedc5cf74d2e254ea 127.0.0.1:7001
slots: (0 slots) slave
replicates 741c0bf753e65c8e6deca11b8efb994519303daa
S: 727abe639adfdea836c0132e2b61d5a5bdbaf967 127.0.0.1:7005
slots: (0 slots) slave
replicates 3329c221b5f689b1bfe26b5ac6996c4a29dea0b3
M: 7c4e1dae5c6ce5660156fb727d36f34fb2c65423 127.0.0.1:7002
slots:4096-8191 (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.
至此redis节点集群创建完成。
5、在创建集群是有可能遇到的问题及解决方法
/usr/bin/env: ruby: 没有那个文件或目录
解决方案:# yum install ruby
./redis-trib.rb:24:in ‘require’: no such file to load -- rubygems (LoadError) from ./redis-trib.rb:24
解决方案:# yum install rubygems
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in ‘gem_original_require': no such file to load -- redis (LoadError) from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in ‘require' from ./redis-trib.rb:25
解决方案:# gem install redis
redis requires Ruby version >= 2.2.2
解决方案:
1).安装curl
sudo yum install curl
2). 安装RVM
curl -L get.rvm.io | bash -s stable
3).
source /usr/local/rvm/scripts/rvm
4). 查看rvm库中已知的ruby版本
rvm list known
5). 安装一个ruby版本
rvm install 2.3.3
6). 使用一个ruby版本
rvm use 2.3.3
7). 设置默认版本
rvm remove 2.0.0
8). 卸载一个已知版本
ruby --version
9). 再安装redis就可以了
gem install redis
转载于:https://blog.51cto.com/12122148/2355812