Redis集群搭建

  1. 下载redis包
    http://download.redis.io/releases/redis-5.0.5.tar.gz

  2. 安装gcc包

1
yum install -y gcc-c++
  1. 各自安装redis
1
tar -zxvf redis-5.0.5.tar.gz && cd redis-5.0.5 && make && make install PREFIX=/usr/local/redis
  1. 配置redis
1
cd .. && mkdir /usr/local/redis/etc && cp redis.conf /usr/local/redis/etc && mkdir -p /data/db/redis/ && cd /usr/local/redis/etc && vim redis.conf
1
2
3
4
5
6
7
8
9
10
11
port  6379
bind 0.0.0.0
protected-mode no
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000
appendonly yes
logfile /data/logs/redis.log
dir /data/db/redis/
  1. 复制redis-trib.rb文件
1
cp /data/software/redis/redis-5.0.5/src/redis-trib.rb /usr/local/redis/bin/
  1. 启动各个Redis节点
1
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
  1. 查看启动日志
1
cat /data/logs/redis.log
  1. 创建集群
1
/usr/local/redis/bin/redis-cli --cluster create 10.154.8.208:6379 10.154.8.149:6379 10.154.8.18:6379 10.154.8.130:6379 10.154.8.112:6379 10.154.8.113:6379 --cluster-replicas 1

其他命令

  1. 添加新主节点
1
redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-master-id node_id
  1. hash槽重新分配
    添加完新节点后,需要对新添加的主节点进行hash槽重新分配,这样该主节点才能存储数据,redis共有16384个槽。
1
redis-cli --cluster reshard host:port --cluster-from node_id --cluster-to node_id --cluster-slots <args> --cluster-yes
  1. 添加新从节点
1
redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-slave --cluster-master-id node_id
  1. 删除节点
1
redis-cli --cluster  del-node host:port node_id