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

資源準(zhǔn)備

  1. 準(zhǔn)備三臺 Linux 服務(wù)器:
  1. 192.168.9.1
  1. 192.168.9.2

  2. 192.168.9.3

  3. 下載 redis-3.2.9

  4. redis cluster 因為選舉算法的因素所以要求 master 節(jié)點數(shù)必須為奇數(shù)

  5. 這里我們演示的是搭建 3 主 3 從,共 6 節(jié)點的 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

服務(wù)器初始化及 redis 編譯

  1. 創(chuàng)建 redis 用戶并指定 gid 為:redis
  1. 三臺服務(wù)器分別執(zhí)行:root@XXXX# useradd redis
  1. 三臺服務(wù)器分別執(zhí)行:redis@XXXX$ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
  2. 在192.168.9.1上執(zhí)行:redis@XXXX$ ssh-copy-id 192.168.9.2
  3. 在192.168.9.1上執(zhí)行:redis@XXXX$ ssh-copy-id 192.168.9.3
  4. 編譯 redis
  1. 將準(zhǔn)備好的 redis-3.2.9.tar.gz 上傳到 192.168.9.1 服務(wù)器的 /home/redis 宿主目錄下
  1. redis@192.168.9.1$ tar -xvf redis-3.2.9.tar.gz
  2. redis@192.168.9.1$ cd redis-3.2.9 && make && cd redis-3.2.9/src && make PREFIX=../../ install

redis 集群手工搭建及配置

  1. 目錄結(jié)構(gòu)規(guī)劃:

redis@192.168.9.1$ mkdir -p /home/redis/6379/{config,data,logs} && mkdir -p /home/redis/6380/{config,data,logs}

  1. redis cluster 配置
  2. 在 192.168.9.1 服務(wù)器中編輯 /home/redis/6379/config/redis-6379.conf,并保存如下內(nèi)容:

redis@192.168.9.1$ 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 服務(wù)器中編輯 /home/redis/6380/config/redis-6380.conf,并保存如下內(nèi)容:

redis@192.168.9.1$ 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 服務(wù)器上 /home/redis/bin、/home/redis/6379、/home/redis/6380,分發(fā)到另外 2 臺服務(wù)器中

redis@192.168.9.1$ scp -r /home/redis/bin /home/redis/6379 /home/redis/6380 redis@192.168.9.2:/home/redis
redis@192.168.9.1$ scp -r /home/redis/bin /home/redis/6379 /home/redis/6380 redis@192.168.9.3:/home/redis

  1. 啟動所有 redis 節(jié)點

redis@192.168.9.1$ cd ~/bin && ./redis-server ../6379/config/redis-6379.conf
redis@192.168.9.1$ cd ~/bin && ./redis-server ../6380/config/redis-6380.conf

redis@192.168.9.2$ cd ~/bin && ./redis-server ../6379/config/redis-6379.conf
redis@192.168.9.2$ cd ~/bin && ./redis-server ../6380/config/redis-6380.conf
redis@192.168.9.3$ cd ~/bin && ./redis-server ../6379/config/redis-6379.conf
redis@192.168.9.3$ cd ~/bin && ./redis-server ../6380/config/redis-6380.conf

  1. 節(jié)點間握手:

這一步可以在任意節(jié)點上進行,這里以 192.168.9.1:6379 為例

  1. redis@192.168.9.1$ ./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 槽:

這里我們規(guī)劃三臺服務(wù)器上端口奇數(shù)節(jié)點為 master 節(jié)點,端口偶數(shù)節(jié)點為 slave 節(jié)點

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

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

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

  4. 從節(jié)點復(fù)制:

  1. 查看各個節(jié)點的 nodeid
1. redis@192.168.9.1$ ./redis-cli -h 192.168.9.1 -p 6379
1. 192.168.9.1:6379> cluster nodes
image.png
  1. 登陸各個從節(jié)點對主節(jié)點進行復(fù)制:** 這里我們規(guī)劃 192.168.9.1:6380 復(fù)制 192.168.9.2:6379,192.168.9.2:6380 復(fù)制 192.168.9.3:6379,192.168.9.3:6380 復(fù)制 192.168.9.1:6379**
    1. redis@192.168.9.1$ ./redis-cli -h 192.168.9.1 -p 6380
    2. 192.168.9.1:6380> cluster replicate NODEID 注意這里的 NODEID 并不是 redis 節(jié)點的 ip:port,而是從節(jié)點要復(fù)制的 redis master 節(jié)點的 hash 值即 NODEID
    3. redis@192.168.9.2$ ./redis-cli -h 192.168.9.2 -p 6380
    4. 192.168.9.2:6380> cluster replicate NODEID
    5. redis@192.168.9.3$ ./redis-cli -h 192.168.9.3 -p 6380
    6. 192.168.9.3:6380> cluster replicate NODEID
如果不出意外,一個基礎(chǔ)的 3 主 3 從的 redis cluster 已經(jīng)搭建完畢 _
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容