Redis7.X Centos集群搭建

发布时间 2023-08-31 15:36:33作者: Jruing

下载

https://download.redis.io/redis-stable.tar.gz

部署

  1. 上传解压

    tar -zxvf  redis-stable.tar.gz
    
  2. 编译安装

    cd redis-stable
    
    make && make install
    
  3. 创建6个节点的目录

    # 创建日志目录
    mkdir /usr/local/redis-cluster/{6379,6380,6381,6382,6383,6384}/log
    # 创建配置文件目录
    mkdir /usr/local/redis-cluster/{6379,6380,6381,6382,6383,6384}/config
    
  4. 写入配置文件

    # 6379节点配置文件
    cat << EOF > /usr/local/redis-cluster/6379/config/redis.conf
    protected-mode no
    bind 0.0.0.0
    port 6379 # 服务监听端口
    daemonize yes # 后台启动
    dir /usr/local/redis-cluster/6379/
    pidfile /usr/local/redis-cluster/6379/redis_6379.pid
    masterauth test-cluster # 启动 redis 密码验证,主要是针对 master 对应的 slave 节点设置的,在 slave 节点数据同步的时候用到
    requirepass test-cluster # redis密码,#启动 redis 密码验证,一定要 requirepass 和 masterauth 同时设置。对登录权限做限制,redis每个节点的requirepass可以是独立、不同的,但建议和 masterauth 设置成一样
    appendonly yes # 启用守护进程
    cluster-enabled yes # 启用集群
    cluster-config-file "nodes.conf" # 关联集群的配置文件
    cluster-node-timeout 5000 # 集群超时时间
    cluster-require-full-coverage no # 只要有结点宕机导致 16384 个槽没全被覆盖,整个集群就全部停止服务,所以一定要改为 no
    EOF
    
    # 6380节点配置文件
    cat << EOF > /usr/local/redis-cluster/6380/config/redis.conf
    protected-mode no
    bind 0.0.0.0
    port 6380
    daemonize yes
    dir /usr/local/redis-cluster/6380/
    pidfile /usr/local/redis-cluster/6380/redis_6380.pid
    masterauth test-cluster
    requirepass test-cluster 
    appendonly yes
    cluster-enabled yes
    cluster-config-file "nodes.conf"
    cluster-node-timeout 5000
    cluster-require-full-coverage no
    EOF
    
    # 6381,6382,6383,6384配置文件参考上面的
    
  5. 创建集群

    redis-cli -a "test-cluster" --cluster create 192.168.137.10:6379 192.168.137.10:6380 192.168.137.10:6381 192.168.137.10:6382 192.168.137.10:6383 192.168.137.10:6384 --cluster-replicas 1
    
  6. 验证集群

    redis-cli -h 192.168.137.10 -c -p 6380 -a "test-cluster"