Redis Cluster 集群搭建
一、 原生方式安装:
cd /opt
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar zxvf redis-4.0.11.tar.gz
cd redis-4.0.11
(如果编译时报: “zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录”,可以使用make MALLOC=libc解决。)
make && make install[或make PREFIX=/data/soft/redis install]
redis-cli –v
cp redis.conf redis.conf.bak
redis-cli -h 192.168.26.52 -p 6379
mkdir cluster
cp redis.conf ./cluster/redis-8001.conf
vim redis-8001.conf //先创建未有的目录
…
bind 192.168.xx.xx
port 8001
dir "/opt/redis-4.0.11/"
logfile "/opt/redis-4.0.11/log/redis-8001.log"
appendonly yes
daemonize yes //以独立服务启动
pidfile "/var/run/redis-8001.pid"
dbfilename "dump.rdb"
# requirepass 123456
appendfilename "appendonly.aof"
#集群模式设置
cluster-enabled yes
cluster-config-file /opt/redis-4.0.11/nodes/nodes-8001.conf
cluster-node-timeout 15000
复制5份配置文件,改好端口即可。
注意:
设置密码后,从节点三份配置文件中要多添加一条:
#从节点同步主节点的密码
masterauth puNE4d9wRvN+XUY=qPh7f
不然,同步会出错。(ERROR: ....AUTHENTICATED...)
cd ../src
ln -s redis-server /usr/local/bin/redis-server
ln -s redis-cli /usr/local/bin/redis-cli
ln -s redis-trib.rb /usr/local/bin/redis-trib.rb
cd ../cluster
redis-server redis-8001.conf &
redis-server redis-8002.conf &
redis-server redis-8003.conf &
redis-server redis-8004.conf &
redis-server redis-8005.conf &
redis-server redis-8006.conf &
ps -ef|grep redis
redis-cli -h 192.168.26.52 -p 8001
192.168.26.52:8001>cluster nodes
…>cluster meet 192.168.26.52 8002
…>cluster meet 192.168.26.52 8003
…>cluster meet 192.168.26.52 8004
…>cluster meet 192.168.26.52 8005
…>cluster meet 192.168.26.52 8006
…>cluster nodes
…>set aa bb
(error)CLUSTERDOWN...下面启动集群...
…>cluster info
…>exit
#redis-cli -h 192.168.26.52 -p 8001 -a 密码 cluster addslots {0..5461}
#redis-cli -h 192.168.26.52 -p 8002 -a 密码 cluster addslots {5462..10922}
#redis-cli -h 192.168.26.52 -p 8003 -a 密码 cluster addslots {10923..16383}
…>cluster info
…>exit
...>cluster nodes //查看主节点ID
#按照一个slave复制一个master来分配
192.168.26.52:8004>cluster replicate <要复制的主节点ID>
192.168.26.52:8005>cluster replicate <要复制的主节点ID>
192.168.26.52:8006>cluster replicate <要复制的主节点ID>
搭建完成。
二、 利用Ruby工具安装:
1、安装Ruby:
#wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.4.tar.gz
tar zxvf ruby-2.4.4.tar.gz
cd ruby-2.4.4
./configure --prefix=/usr/local/ruby
make && make install
cd /usr/local/ruby/
cp bin/ruby /usr/local/bin/
cp bin/gem /usr/local/bin/
2、安装rubygem redis依赖:
wget http://rubygems.org/downloads/redis-4.0.2.gem
gem install -l redis-4.0.2.gem
gem list check redis gem
gem –v
gem update --system
gem –v
gem list check redis gem
cp redis-4.0.11/src/redis-trib.rb /usr/local/bin/
redis-trib.rb
cd redis-4.0.11
mkdir cluster
vim redis.conf
…
…
cp redis.conf cluster/redis-8001.conf
cd cluster
...
redis-server redis-8001.conf &
redis-server redis-8002.conf &
redis-server redis-8003.conf &
redis-server redis-8004.conf &
redis-server redis-8005.conf &
redis-server redis-8006.conf &
ps -ef|grep redis
#redis-trib.rb create --replicas 1 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 12
7.0.0.1:8005 127.0.0.1:8006
###########如果配置文件设置了密码,这时会启动不成功,报“[ERR] Sorry, can't connect to node 192.168.57.3:7001”的错误。
解决=>修改ruby目录中的文件:/data/soft/ruby/lib/ruby/gems/2.4.0/gems/redis-4.0.2/lib/redis/client.rb
......
class Redis
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => “redis配置中的实际密码”,
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
......
然后,重新执行创建集群,yes,等待完成。
#redis-trib.rb create --replicas 1 192.168.57.3:7001 192.168.57.3:7002 192.168.57.3:7003 192.168.57.2:7004 192.168.57.2:7005 192.168.57.2:7006
//集群完整性检查(任一节点)
# redis-trib.rb check 127.0.0.1:8001
# redis-trib.rb check 127.0.0.1:8002
...
附录图片:
redis-trib.rb创建集群分配master.png