环境:
主机1 192.168.2.204
主机2 192.168.2.201
步骤:
安装redis
参考 初试redis主从同步
主机1 启动3台redis
mkdir /install/redisconf /install/redisconf/7000 /install/redisconf/7001 /install/redisconf/7002
cp /install/redis/redis.conf /install/redisconf/7000
cp /install/redis/redis.conf /install/redisconf/7001
cp /install/redis/redis.conf /install/redisconf/7002
修改三个配置文件
port 7000(7001/7002)
bind 192.168.2.204
daemonize yes
pidfile /var/run/redis_7000(7001/7002).pid
cluster-enabled yes
cluster-config-file nodes_7000(7001/7002).conf
appendonly yes
redis-server /install/redisconf/7000/redis.conf
redis-server /install/redisconf/7001/redis.conf
redis-server /install/redisconf/7002/redis.conf
主机2同上开启三个redis分别为7003、7004、7005
创建集群
cp /download/redis-3.2.1/src/redis-trub.rb /usr/local/bin
主从对应
$ruby redis-trib.rb create --replicas 1 192.168.2.204:7000 192.168.2.204:7001 192.168.2.204:7002 192.168.2.201:7003 192.168.2.201:7004 192.168.2.201:7005
此处报错
因为ruby没有安装 另外 此处有坑,ruby需安装1.8以上版本,否则需要安装gem
cd /soft
wget https://cache.ruby-china.org/pub/ruby/ruby-2.4.1.tar.gz
tar zxvf ruby-2.4.1
cd ruby-2.4.1
./configure --with-openssl-dir=/usr/local/ssl
make && make install
cp /usr/local/bin/ruby /usr/bin
重新执行
$ruby redis-trib.rb create --replicas 1 192.168.2.204:7000 192.168.2.204:7001 192.168.2.204:7002 192.168.2.201:7003 192.168.2.201:7004 192.168.2.201:7005
再次报错
Waiting for the cluster to join......................................................................................................................
...........................................................
检查防火墙接口正常
原来 redis 集群创建的时候会默认使用集群端口来通信,端口号为对应redis通信接口+10000 于是 开启防火墙
再次执行
$ruby redis-trib.rb create --replicas 1 192.168.2.204:7000 192.168.2.204:7001 192.168.2.204:7002 192.168.2.201:7003 192.168.2.201:7004 192.168.2.201:7005
至此集群搭建成功
增加master节点
配置文件同上
删除这两个文件
重新执行
为新节点分配 slot
redis-trib.rb reshard 192.168.2.204:7007
增加slave节点
redis-trib.rb add-node --slave --master-id d8153430246886589ec68436c4ccfb7770c003ad 192.168.2.201:7006 192.168.2.204:7000
redis-trib.rb check 192.168.2.201:7006