没有 ruby 你如何搭建一套 redis 集群

资源准备

  1. 准备三台 Linux 服务器:
  1. 192.168.9.1
  1. 192.168.9.2

  2. 192.168.9.3

  3. 下载 redis-3.2.9

  4. redis cluster 因为选举算法的因素所以要求 master 节点数必须为奇数

  5. 这里我们演示的是搭建 3 主 3 从,共 6 节点的 redis 集群:

  1. 192.168.9.1:6379
  1. 192.168.9.1:6380
  2. 192.168.9.2:6379
  3. 192.168.9.2:6380
  4. 192.168.9.3:6379
  5. 192.168.9.3:6380

服务器初始化及 redis 编译

  1. 创建 redis 用户并指定 gid 为:redis
  1. 三台服务器分别执行:root@XXXX# useradd redis
  1. 三台服务器分别执行:redis@XXXX$ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
  2. 在192.168.9.1上执行:redis@XXXX$ ssh-copy-id 192.168.9.2
  3. 在192.168.9.1上执行:redis@XXXX$ ssh-copy-id 192.168.9.3
  4. 编译 redis
  1. 将准备好的 redis-3.2.9.tar.gz 上传到 192.168.9.1 服务器的 /home/redis 宿主目录下
  1. [email protected]$ tar -xvf redis-3.2.9.tar.gz
  2. [email protected]$ cd redis-3.2.9 && make && cd redis-3.2.9/src && make PREFIX=../../ install

redis 集群手工搭建及配置

  1. 目录结构规划:

[email protected]$ mkdir -p /home/redis/6379/{config,data,logs} && mkdir -p /home/redis/6380/{config,data,logs}

  1. redis cluster 配置
  2. 在 192.168.9.1 服务器中编辑 /home/redis/6379/config/redis-6379.conf,并保存如下内容:

[email protected]$ vi /home/redis/6379/config/redis-6379.conf
####### redis server 配置信息
port 6379
maxmemory 4gb
protected-mode no
daemonize yes
dir /home/redis/6379/data
pidfile /home/redis/6379/data/redis-6379.pid

logfile /home/redis/6379/logs/redis-6379.log
tcp-backlog 511
timeout 300
tcp-keepalive 60
loglevel notice
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file "/home/redis/6379/nodes-6379.conf"

  1. 在 192.168.9.1 服务器中编辑 /home/redis/6380/config/redis-6380.conf,并保存如下内容:

[email protected]$ vi /home/redis/6380/config/redis-6380.conf
####### redis server 配置信息

port 6380
maxmemory 4gb
protected-mode no
daemonize yes
dir /home/redis/6380/data
pidfile /home/redis/6380/data/redis-6380.pid
logfile /home/redis/6380/logs/redis-6380.log
tcp-backlog 511
timeout 300
tcp-keepalive 60
loglevel notice
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file "/home/redis/6380/nodes-6380.conf"

  1. 将 192.168.9.1 服务器上 /home/redis/bin、/home/redis/6379、/home/redis/6380,分发到另外 2 台服务器中

[email protected]$ scp -r /home/redis/bin /home/redis/6379 /home/redis/6380 [email protected]:/home/redis
[email protected]$ scp -r /home/redis/bin /home/redis/6379 /home/redis/6380 [email protected]:/home/redis

  1. 启动所有 redis 节点

[email protected]$ cd ~/bin && ./redis-server ../6379/config/redis-6379.conf
[email protected]$ cd ~/bin && ./redis-server ../6380/config/redis-6380.conf

[email protected]$ cd ~/bin && ./redis-server ../6379/config/redis-6379.conf
[email protected]$ cd ~/bin && ./redis-server ../6380/config/redis-6380.conf
[email protected]$ cd ~/bin && ./redis-server ../6379/config/redis-6379.conf
[email protected]$ cd ~/bin && ./redis-server ../6380/config/redis-6380.conf

  1. 节点间握手:

这一步可以在任意节点上进行,这里以 192.168.9.1:6379 为例

  1. [email protected]$ ./redis-cli -h 192.168.9.1 -p 6379

  2. 192.168.9.1:6379> cluster meet 192.168.9.1 6380

  3. 192.168.9.1:6379> cluster meet 192.168.9.2 6379

  4. 192.168.9.1:6379> cluster meet 192.168.9.2 6380

  5. 192.168.9.1:6379> cluster meet 192.168.9.3 6379

  6. 192.168.9.1:6379> cluster meet 192.168.9.3 6380

  7. 分配 hash 槽:

这里我们规划三台服务器上端口奇数节点为 master 节点,端口偶数节点为 slave 节点

  1. [email protected]$ cd ~/bin && ./redis-cli -h 192.168.9.1 -p 6379 cluster addslots {0..5461}

  2. [email protected]$ cd ~/bin && ./redis-cli -h 192.168.9.2 -p 6379 cluster addslots {5462..10922}

  3. [email protected]$ cd ~/bin && ./redis-cli -h 192.168.9.3 -p 6379 cluster addslots {10923..16383}

  4. 从节点复制:

  1. 查看各个节点的 nodeid
1. [email protected]$ ./redis-cli -h 192.168.9.1 -p 6379
1. 192.168.9.1:6379> cluster nodes
没有 ruby 你如何搭建一套 redis 集群_第1张图片
image.png
  1. 登陆各个从节点对主节点进行复制:** 这里我们规划 192.168.9.1:6380 复制 192.168.9.2:6379,192.168.9.2:6380 复制 192.168.9.3:6379,192.168.9.3:6380 复制 192.168.9.1:6379**
    1. [email protected]$ ./redis-cli -h 192.168.9.1 -p 6380
    2. 192.168.9.1:6380> cluster replicate NODEID 注意这里的 NODEID 并不是 redis 节点的 ip:port,而是从节点要复制的 redis master 节点的 hash 值即 NODEID
    3. [email protected]$ ./redis-cli -h 192.168.9.2 -p 6380
    4. 192.168.9.2:6380> cluster replicate NODEID
    5. [email protected]$ ./redis-cli -h 192.168.9.3 -p 6380
    6. 192.168.9.3:6380> cluster replicate NODEID
如果不出意外,一个基础的 3 主 3 从的 redis cluster 已经搭建完毕 _

你可能感兴趣的:(没有 ruby 你如何搭建一套 redis 集群)