使用redis-cli部署集群

目标

  • 使用3台服务器A、B、C,每台服务器部署一套主从(1主2从),三个主服务来平分redis槽。

安装redis服务

  • 安装gcc                yum install -y gcc
  • 安装wget               yum install -y wget 
  • 下载redis包           wget http://download.redis.io/releases/redis-5.0.8.tar.gz
  • 解压并安装redis命令      cd  $REDIS_HOME/src && make
  • 安装redis服务      make install PREFIX=/opt/redis
  • 添加redis环境变量   
vi /etc/profile
export REDIS_HOME=/opt/redis
export PATH=$PATH:$REDIS_HOME/bin
source /etc/profile

 

  • 使用utils路径下的 install_server.sh安装3个端口配置文件,下面以6379为例
  • [root@node01 utils]# pwd
    /home/redis-5.0.8/utils
    [root@node01 utils]# sh install_server.sh 
    Welcome to the redis service installer
    This script will help you easily set up a running redis server
    
    Please select the redis port for this instance: [6379] 
    Selecting default: 6379
    Please select the redis config file name [/etc/redis/6379.conf] 
    Selected default - /etc/redis/6379.conf
    Please select the redis log file name [/var/log/redis_6379.log] 
    Selected default - /var/log/redis_6379.log
    Please select the data directory for this instance [/var/lib/redis/6379] 
    Selected default - /var/lib/redis/6379
    Please select the redis executable path [/opt/redis/bin/redis-server] 
    Selected config:
    Port           : 6379
    Config file    : /etc/redis/6379.conf
    Log file       : /var/log/redis_6379.log
    Data dir       : /var/lib/redis/6379
    Executable     : /opt/redis/bin/redis-server
    Cli Executable : /opt/redis/bin/redis-cli
    Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    Copied /tmp/6379.conf => /etc/init.d/redis_6379
    Installing service...
    Successfully added to chkconfig!
    Successfully added to runlevels 345!
    Starting Redis server...
    Installation successful!
    
  • 自行安装三个端口以后分别修改配置文件,其中cluster-config-file 需要区分开
  • #打开集群模式
    cluster-enabled yes
    #集群配置文件
    cluster-config-file nodes_6379.conf
    #集群节点超时时间
    cluster-node-timeout 5000
    #允许其他服务器连接
    protected-mode no
    #如果需要添加连接白名单的话,默认关联本机,这里为了测试方便把他注释
    #bind 127.0.0.1
    #打开aof持久化模式
    appendonly yes
  •  
  • 三个配置都修改完以后,以同样的方式再添加B,C两台配置作为集群备用
  • 开始部署集群。
  • 第一步 启动三个服务器的 实例
  • [root@node01 redis]# redis-server /etc/redis/6379.conf 
    [root@node01 redis]# redis-server /etc/redis/6380.conf 
    [root@node01 redis]# redis-server /etc/redis/6381.conf
    [root@node02 create-cluster]# redis-server /etc/redis/6382.conf 
    [root@node02 create-cluster]# redis-server /etc/redis/6383.conf 
    [root@node02 create-cluster]# redis-server /etc/redis/6384.conf 
    [root@node03 redis]# redis-server /etc/redis/6385.conf 
    [root@node03 redis]# redis-server /etc/redis/6386.conf 
    [root@node03 redis]# redis-server /etc/redis/6387.conf 
    

     

  • 第二步 使用redic-cli部署集群

  • redis-cli --cluster create 192.168.25.66:6379 192.168.25.66:6380 192.168.25.66:6381 192.168.25.67:6382 192.168.25.67:6383 192.168.25.67:6384 192.168.25.68:6385 192.168.25.68:6386 192.168.25.68:6387 --cluster-replicas 2
    

     

  • 查看集群状态

[root@node02 create-cluster]# redis-cli --cluster info 192.168.25.66:6379
192.168.25.66:6379 (5037dd57...) -> 0 keys | 5461 slots | 2 slaves.
192.168.25.68:6385 (b58897ae...) -> 0 keys | 5461 slots | 2 slaves.
192.168.25.67:6382 (ea76244b...) -> 0 keys | 5462 slots | 2 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
  • 查看集群结构

  • [root@node02 create-cluster]# redis-cli --cluster check 192.168.25.66:6379
    192.168.25.66:6379 (5037dd57...) -> 0 keys | 5461 slots | 2 slaves.
    192.168.25.68:6385 (b58897ae...) -> 0 keys | 5461 slots | 2 slaves.
    192.168.25.67:6382 (ea76244b...) -> 0 keys | 5462 slots | 2 slaves.
    [OK] 0 keys in 3 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.25.66:6379)
    M: 5037dd57380dc70776b8fb7ad67d6a1b029957e5 192.168.25.66:6379
       slots:[0-5460] (5461 slots) master
       2 additional replica(s)
    M: b58897ae1de663edba1f3d262ff416f0fdba2346 192.168.25.68:6385
       slots:[10923-16383] (5461 slots) master
       2 additional replica(s)
    S: cca6d3f83c1ddfa120f9cfc5fce9327abc175fb3 192.168.25.67:6384
       slots: (0 slots) slave
       replicates b58897ae1de663edba1f3d262ff416f0fdba2346
    S: f71218b1ba884a65c083db3b4ec30888778212c2 192.168.25.68:6387
       slots: (0 slots) slave
       replicates ea76244b226257b9ea893aa82ac203d656afe84f
    S: 60e740c4c5d7b15e7ebaf573122f71f46aa6cb42 192.168.25.66:6381
       slots: (0 slots) slave
       replicates ea76244b226257b9ea893aa82ac203d656afe84f
    M: ea76244b226257b9ea893aa82ac203d656afe84f 192.168.25.67:6382
       slots:[5461-10922] (5462 slots) master
       2 additional replica(s)
    S: 8501a8d0767b7466ac17e0f75bf36f2e244686e8 192.168.25.67:6383
       slots: (0 slots) slave
       replicates 5037dd57380dc70776b8fb7ad67d6a1b029957e5
    S: fd1d61efcfcd12810e73d3f735dc11ca0f764c14 192.168.25.68:6386
       slots: (0 slots) slave
       replicates 5037dd57380dc70776b8fb7ad67d6a1b029957e5
    S: 38ea4787e22d007b609ee2523597ac6887f3e22a 192.168.25.66:6380
       slots: (0 slots) slave
       replicates b58897ae1de663edba1f3d262ff416f0fdba2346
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.

     

部署方式 2  使用create-cluster脚本

  •  进入任意一台REDIS_HOME/src/utils/create-cluster目录,查看README。

  • 使用:
    1.第一步,编辑create-cluster脚本,修改启用和结束的端口范围,这个依赖于你要创建的实例。也可以创建一个config.sh脚本 通过-a config.sh来指定端口范围,这样可以保留脚本的初始属性。
    2.使用./create-cluster start来启动实例
    3.使用./create-cluster create来执行,也可以redis-cli --cluster create,这样来创建一个redis集群
    4.每个实例的AOF文件和日志将会再当前目录创建。
    
    停止:
    1.使用./create-cluster stop来停止所有的实例,停止后可以使用./create-cluster start来重启
    2.使用./create-cluster clean来溢出所有的AOF文件和日志.
    帮助文档 ./create-cluster help
    
    

     

  • 创建脚本config.sh
  • #!/bin/bash
    
    # Settings
    
    PORT=6378
    TIMEOUT=2000
    NODES=9
    REPLICAS=2

你可能感兴趣的:(redis)