redis集群搭建

1、到官网下载Redis版本,我下载的是4.0.9当前最新版本,3.0版本以上才支持集群。

2、到官网下载Ruby,我下载的是2.3.7版本,必须下载2.2.2版本以上不然使用集群脚本的时候会提示 requires Ruby version >= 2.2.2。如果使用

apt-get install rubygems 

命令还需要升级ruby,所以我选择使用tar.gz包的方式安装。

3、解压Redis和Ruby


tar -xvf redis-4.0.9.tar.gz
tar -xvf ruby-2.3.7.tar.gz

4、安装Redis

cd redis-4.0.9
make

make后出现以下命令表示安装成功

  INSTALL redis-check-rdb
  INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/home/redis-4.0.9/src'

接着执行

make install
Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/home/redis-4.0.9/src'

5、要集群必须至少要有3个主节点,另外在创建3个从节点。端口分别配置6379、6380、6381、6382、6383、6384端口。

mkdir redis_cluster
cd redis_cluster
mkdir 6379 6380 6381 6382 6383 6384
cp ../redis.conf ./6379
cp ../redis.conf ./6380
cp ../redis.conf ./6381
cp ../redis.conf ./6382
cp ../redis.conf ./6383
cp ../redis.conf ./6384

分别修改redis.conf,已6379端口为例

port  6379                                        
bind  127.0.0.1                                    
daemonize yes 
pidfile  /var/run/redis_6379.pid
cluster-enabled  yes    
cluster-config-file nodes_6379.conf
cluster-node-timeout 15000               
appendonly yes
redis-server /home/redis-4.0.9/redis_cluster/6379/redis.conf   //启动6379端口,其他5个端口同样方式启动

然后分别启动,执行

ps -ef | grep redis
root@iZ25xpxpcnmZ:/home/redis-4.0.9# ps -ef | grep redis
root      5397     1  0 21:05 ?        00:00:00 redis-server 127.0.0.1:6379 [cluster]             
root      5402     1  0 21:05 ?        00:00:00 redis-server 127.0.0.1:6380 [cluster]             
root      5407     1  0 21:05 ?        00:00:00 redis-server 127.0.0.1:6381 [cluster]             
root      5412     1  0 21:05 ?        00:00:00 redis-server 127.0.0.1:6382 [cluster]             
root      5417     1  0 21:05 ?        00:00:00 redis-server 127.0.0.1:6383 [cluster]             
root      5422     1  0 21:05 ?        00:00:00 redis-server 127.0.0.1:6384 [cluster]             
root      5429  5186  0 21:07 pts/0    00:00:00 grep --color=auto redis

查看是否都正常启动,如果正常接下来就可以配置集群了。

6、在/ruby/src目录下有一个redis-trib.rb脚本,这个脚本可以帮助我们快速配置集群。接下来执行

gem install redis
root@iZ25xpxpcnmZ:/home/redis-4.0.9# gem install redis
The program 'gem' can be found in the following packages:
 * ruby1.9.1
 * rubygems
Try: apt-get install <selected package>

原因是没有安装ruby。

cd /home/ruby-2.3.7
./configure
make
sudo make install

安装成功后再次执行,出现了新的错误。

root@iZ25xpxpcnmZ:/home/ruby-2.3.7# gem install redis
ERROR:  Loading command: install (LoadError)
        cannot load such file -- zlib
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args' for nil:NilClass

然后执行以下命令

apt-get install zlib1g-dev  
cd /home/ruby-2.3.7/ext/zlib  
ruby ./extconf.rb  
make  
make install  

zlib安装成功后再次执行

root@iZ25xpxpcnmZ:/home/ruby-2.3.7/ext/zlib# gem install redis
ERROR:  While executing gem ... (Gem::Exception)
    Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources

原因是没有找到openssl

tar -xzvf openssl-1.0.2o.tar.gz
cd openssl-1.0.2o
./config -fPIC --prefix=/usr/local/openssl enable-shared  
./config -t  
make && make install

然后进去ruby的openssl目录,执行脚本

cd /home/ruby-2.3.7/ext/openssl
ruby extconf.rb --with-openssl-include=/usr/local/openssl/include/ --with-openssl-lib=/usr/local/openssl/lib

执行成功后现以下日志

checking for EVP_CTRL_GCM_GET_TAG in openssl/evp.h... yes  
creating extconf.h  
creating Makefile  

复制include到openssl

ln -s /usr/local/src/ruby-2.2.3/include /

然后执行make

make

如果出现以下错误,修改Makefile文件,因为生成Makefile的时候少了变量声明,所以加上 top_srcdir = ../..

make: *** No rule to make target `/include/ruby.h', needed by `ossl.o'.  Stop.
make install

成功会出现以下日志

/usr/bin/install -c -m 0755 openssl.so /usr/local/lib/ruby/site_ruby/2.3.0/i686-linux
installing default openssl libraries

然后再次执行,WOC! 可以了。

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

最后执行

cd /home/redis-4.0.9/src
./redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384

创建成功日志如下

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 9f486484e102a5247dc9733524a2d507c7b0acad 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: d211301b7f13d372ae5181ec25ad98581a884b33 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: fad3a17d92a0bceba1eafd8727a5c51b674170e4 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: a39b0f28ce9f4bcefeab0a2633c1cff7e80349c2 127.0.0.1:7004
   replicates d211301b7f13d372ae5181ec25ad98581a884b33
S: 5fa801a3e624900b8649cb9b6aaecaf765e2bbea 127.0.0.1:7005
   replicates fad3a17d92a0bceba1eafd8727a5c51b674170e4
S: a5d909b412859490e0c16ba7313b2986847bf4b5 127.0.0.1:7006
   replicates 9f486484e102a5247dc9733524a2d507c7b0acad
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 127.0.0.1:7001)
M: 9f486484e102a5247dc9733524a2d507c7b0acad 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d211301b7f13d372ae5181ec25ad98581a884b33 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 5fa801a3e624900b8649cb9b6aaecaf765e2bbea 127.0.0.1:7005
   slots: (0 slots) slave
   replicates fad3a17d92a0bceba1eafd8727a5c51b674170e4
S: a5d909b412859490e0c16ba7313b2986847bf4b5 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 9f486484e102a5247dc9733524a2d507c7b0acad
M: fad3a17d92a0bceba1eafd8727a5c51b674170e4 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: a39b0f28ce9f4bcefeab0a2633c1cff7e80349c2 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d211301b7f13d372ae5181ec25ad98581a884b33
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
阅读更多

没有更多推荐了,返回首页