Redis 集群搭建

Redis 集群搭建 (伪集群)

软件环境

centos7.3 (虚拟机)

redis 5.0.5

搭建步骤

创建相关目录

  • 创建主文件夹redis-cluster 在此文件夹下面创建六个子文件夹
mkdir /opt/redis-cluster
cd /opt/redis-cluster
mkdir 7000 7001 7002 7003 7004 7005

安装redis 5.0.5

  • redis官网下载压缩包拷贝到/opt/redis-cluster中
  • 编译安装
cd /opt/redis-cluster/redis5.0.5
make MALLOC=libc
cd src && make install

启动六个redis实例

  • 移动redis.conf文件到每个实例中
cp /opt/redis-cluster/redis5.0.5/redis.conf /opt/redis-cluster/7000
  • 修改配置
# bind 192.168.1.187  //绑定服务器IP地址(注释掉开启远程连接)
port 7000  //绑定端口号,必须修改,以此来区分Redis实例

daemonize yes  //后台运行

pidfile /var/run/redis-7000.pid  //修改pid进程文件名,以端口号命名

logfile /root/application/program/redis-cluster/7000/redis.log  //修改日志文件名称,以端口号为目录来区分

dir /root/application/program/redis-cluster/7000/  //修改数据文件存放地址,以端口号为目录名来区分

cluster-enabled yes  //启用集群

cluster-config-file nodes-7000.conf  //配置每个节点的配置文件,同样以端口号为名称

cluster-node-timeout 15000  //配置集群节点的超时时间,可改可不改

appendonly yes  //启动AOF增量持久化策略

appendfsync always  //发生改变就记录日志

protected-mode no
  • 启动六个redis实例
cd /opt/redis-cluster/redis5.0.5/src
cp /redis-server ../
cp /redis-cli ../
cd ../
redis-server /opt/redis-cluster/7000/redis.conf
redis-server /opt/redis-cluster/7001/redis.conf
redis-server /opt/redis-cluster/7002/redis.conf
redis-server /opt/redis-cluster/7003/redis.conf
redis-server /opt/redis-cluster/7004/redis.conf
redis-server /opt/redis-cluster/7005/redis.conf

创建集群

  • 执行创建集群命令
cd /opt/redis-cluster/redis5.0.5
 redis-cli --cluster create 192.168.1.187:7000 192.168.1.187:7001 192.168.1.187:7002 192.168.1.187:7003 192.168.1.187:7004 192.168.1.187:7005 --cluster-replicas 1

全部重启

  • 删除所有实例下面的 appendonly.aof 和 nodes-700*.conf
cd /opt/redis-cluster/
 find . -name '*.aof' -type f -print -exec rm -rf {} \;
  find . -name 'nodes-700*' -type f -print -exec rm -rf {} \;
  • 杀掉所有redis实例进程
pkill -9 redis
  • 重新启动所有redis实例
redis-server /opt/redis-cluster/7000/redis.conf
redis-server /opt/redis-cluster/7001/redis.conf
redis-server /opt/redis-cluster/7002/redis.conf
redis-server /opt/redis-cluster/7003/redis.conf
redis-server /opt/redis-cluster/7004/redis.conf
redis-server /opt/redis-cluster/7005/redis.conf
  • 创建集群
 redis-cli --cluster create 192.168.1.187:7000 192.168.1.187:7001 192.168.1.187:7002 192.168.1.187:7003 192.168.1.187:7004 192.168.1.187:7005 --cluster-replicas 1

==注意:== 这种重启会丢失数据

你可能感兴趣的:(Redis 集群搭建)