没有 ruby 你如何搭建一套 redis 集群
资源准备
- 准备三台 Linux 服务器:
- 192.168.9.1
-
192.168.9.2
-
192.168.9.3
-
下载 redis-3.2.9
-
redis cluster 因为选举算法的因素所以要求 master 节点数必须为奇数
-
这里我们演示的是搭建 3 主 3 从,共 6 节点的 redis 集群:
- 192.168.9.1:6379
- 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
服务器初始化及 redis 编译
- 创建 redis 用户并指定 gid 为:redis
- 三台服务器分别执行:root@XXXX# useradd redis
- 三台服务器分别执行:redis@XXXX$ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
- 在192.168.9.1上执行:redis@XXXX$ ssh-copy-id 192.168.9.2
- 在192.168.9.1上执行:redis@XXXX$ ssh-copy-id 192.168.9.3
- 编译 redis
- 将准备好的 redis-3.2.9.tar.gz 上传到 192.168.9.1 服务器的 /home/redis 宿主目录下
- redis@192.168.9.1$ tar -xvf redis-3.2.9.tar.gz
- redis@192.168.9.1$ cd redis-3.2.9 && make && cd redis-3.2.9/src && make PREFIX=../../ install
redis 集群手工搭建及配置
- 目录结构规划:
redis@192.168.9.1$ mkdir -p /home/redis/6379/{config,data,logs} && mkdir -p /home/redis/6380/{config,data,logs}
- redis cluster 配置
- 在 192.168.9.1 服务器中编辑 /home/redis/6379/config/redis-6379.conf,并保存如下内容:
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"
- 在 192.168.9.1 服务器中编辑 /home/redis/6380/config/redis-6380.conf,并保存如下内容:
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"
- 将 192.168.9.1 服务器上 /home/redis/bin、/home/redis/6379、/home/redis/6380,分发到另外 2 台服务器中
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
- 启动所有 redis 节点
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
- 节点间握手:
这一步可以在任意节点上进行,这里以 192.168.9.1:6379 为例
-
redis@192.168.9.1$ ./redis-cli -h 192.168.9.1 -p 6379
-
192.168.9.1:6379> cluster meet 192.168.9.1 6380
-
192.168.9.1:6379> cluster meet 192.168.9.2 6379
-
192.168.9.1:6379> cluster meet 192.168.9.2 6380
-
192.168.9.1:6379> cluster meet 192.168.9.3 6379
-
192.168.9.1:6379> cluster meet 192.168.9.3 6380
-
分配 hash 槽:
这里我们规划三台服务器上端口奇数节点为 master 节点,端口偶数节点为 slave 节点
-
redis@192.168.9.1$ cd ~/bin && ./redis-cli -h 192.168.9.1 -p 6379 cluster addslots {0..5461}
-
redis@192.168.9.2$ cd ~/bin && ./redis-cli -h 192.168.9.2 -p 6379 cluster addslots {5462..10922}
-
redis@192.168.9.3$ cd ~/bin && ./redis-cli -h 192.168.9.3 -p 6379 cluster addslots {10923..16383}
-
从节点复制:
- 查看各个节点的 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
- 登陆各个从节点对主节点进行复制:** 这里我们规划 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**
- redis@192.168.9.1$ ./redis-cli -h 192.168.9.1 -p 6380
- 192.168.9.1:6380> cluster replicate NODEID 注意这里的 NODEID 并不是 redis 节点的 ip:port,而是从节点要复制的 redis master 节点的 hash 值即 NODEID
- redis@192.168.9.2$ ./redis-cli -h 192.168.9.2 -p 6380
- 192.168.9.2:6380> cluster replicate NODEID
- redis@192.168.9.3$ ./redis-cli -h 192.168.9.3 -p 6380
- 192.168.9.3:6380> cluster replicate NODEID