os: centos7.4
redis:4.0.9
ruby:2.3.4

ip 规划

redis1    192.168.56.101
redis2    192.168.56.102

用两台虚拟机模拟6个redis节点。
分别为
192.168.56.101 的 9001、9002、9003 端口,
192.168.56.102 的 9001、9002、9003 端口。

os安装

参考<<centos 7.x 安装 redis 的优化设置>>

使用 rvm 安装 ruby

下载、编译、安装

http://www.redis.cn/download.html
或者直接用wget
wget http://download.redis.io/releases/redis-4.0.9.tar.gz

redis1、redis2 两个节点都需要操作

# mkdir -p /usr/redis
# cd /usr/redis
# wget http://download.redis.io/releases/redis-4.0.9.tar.gz
# tar -zxvf ./redis-4.0.9.tar.gz
# cd redis-4.0.9
# make
# make test
# make install

执行 make install 后,会拷贝编译好的文件到 /usr/local/bin 目录下

# ls -l /usr/local/bin/
total 32748
-rwxr-xr-x 1 root root 4367368 May  8 12:01 redis-benchmark
-rwxr-xr-x 1 root root 8117952 May  8 12:01 redis-check-aof
-rwxr-xr-x 1 root root 8117952 May  8 12:01 redis-check-rdb
-rwxr-xr-x 1 root root 4807704 May  8 12:01 redis-cli
lrwxrwxrwx 1 root root      12 May  8 12:01 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8117952 May  8 12:01 redis-server

redis1 节点上操作,创建 cluster data 目录

# mkdir -p /var/lib/redis/redis-cluster-data
# cd /var/lib/redis/redis-cluster-data
# mkdir -p 9001 9002 9003

redis2 节点上操作,创建 cluster data 目录

# mkdir -p /var/lib/redis/redis-cluster-data
# cd /var/lib/redis/redis-cluster-data
# mkdir -p 9001 9002 9003

redis1、redis2 两个节点都需要操作
创建bin目录,添加到PATH里

# cd /usr/redis/redis-4.0.9
# mkdir bin
# cd src
# cp mkreleasehdr.sh \
redis-benchmark \
redis-check-aof \
redis-check-rdb \
redis-cli redis-server \
redis-sentinel redis-trib.rb ../bin

# vi /etc/profile
PATH=/usr/redis/redis-4.0.9/bin:$PATH

# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# source /etc/profile
# echo $PATH
/usr/redis/redis-4.0.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

redis cluster 配置

redis1 的 9001 9002 9003 目录下,
redis2 的 9001 9001 9003 目录下都需要创建 redis.conf

$ cd /var/lib/redis/redis-cluster-data/9001
$ vi redis.conf

redis.conf 配置文件请参考<<redis cluster 4.0.9 之七:redis.conf>>

# tree
.
├── 9001
│   └── redis.conf
├── 9002
│   └── redis.conf
└── 9003
    └── redis.conf

3 directories, 3 files

启动redis

redis1 上启动 redis

# cd /var/lib/redis/redis-cluster-data/9001
# redis-server /var/lib/redis/redis-cluster-data/9001/redis.conf

# cd /var/lib/redis/redis-cluster-data/9002
# redis-server /var/lib/redis/redis-cluster-data/9002/redis.conf

# cd /var/lib/redis/redis-cluster-data/9003
# redis-server /var/lib/redis/redis-cluster-data/9003/redis.conf

# tree
.
├── 9001
│   ├── appendonly.aof
│   ├── cluster_9001.conf
│   └── redis.conf
├── 9002
│   ├── appendonly.aof
│   ├── cluster_9002.conf
│   └── redis.conf
└── 9003
    ├── appendonly.aof
    ├── cluster_9003.conf
    └── redis.conf

3 directories, 9 files

redis2 上启动 redis

# cd /var/lib/redis/redis-cluster-data/9001
# redis-server /var/lib/redis/redis-cluster-data/9001/redis.conf

# cd /var/lib/redis/redis-cluster-data/9002
# redis-server /var/lib/redis/redis-cluster-data/9002/redis.conf

# cd /var/lib/redis/redis-cluster-data/9003
# redis-server /var/lib/redis/redis-cluster-data/9003/redis.conf

# tree
.
├── 9001
│   ├── appendonly.aof
│   ├── cluster_9001.conf
│   └── redis.conf
├── 9002
│   ├── appendonly.aof
│   ├── cluster_9002.conf
│   └── redis.conf
└── 9003
    ├── appendonly.aof
    ├── cluster_9003.conf
    └── redis.conf

3 directories, 9 files

查看redis 的启动情况

# ps -ef|grep -i redis
root     20531     1  0 May17 ?        00:00:17 redis-server 192.168.56.101:9001 [cluster]
root     20536     1  0 May17 ?        00:00:17 redis-server 192.168.56.101:9002 [cluster]
root     20541     1  0 May17 ?        00:00:17 redis-server 192.168.56.101:9003 [cluster]
root     30028  1851  0 09:14 pts/0    00:00:00 grep --color=auto -i redis

# netstat -antp|grep -i redis
tcp        0      0 192.168.56.101:9001     0.0.0.0:*               LISTEN      20531/redis-server
tcp        0      0 192.168.56.101:9002     0.0.0.0:*               LISTEN      20536/redis-server
tcp        0      0 192.168.56.101:9003     0.0.0.0:*               LISTEN      20541/redis-server
tcp        0      0 192.168.56.101:19001    0.0.0.0:*               LISTEN      20531/redis-server
tcp        0      0 192.168.56.101:19002    0.0.0.0:*               LISTEN      20536/redis-server
tcp        0      0 192.168.56.101:19003    0.0.0.0:*               LISTEN      20541/redis-server

正式创建

执行 redis-trib.rb 前必须安装ruby >= 2.2.2.

–replicas 1 代表每个master 有一个slave

# redis-trib.rb create --replicas 1 192.168.56.101:9001 192.168.56.101:9002 192.168.56.101:9003 192.168.56.102:9001 192.168.56.102:9002 192.168.56.102:9003
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.56.101:9001
192.168.56.102:9001
192.168.56.101:9002
Adding replica 192.168.56.102:9003 to 192.168.56.101:9001
Adding replica 192.168.56.101:9003 to 192.168.56.102:9001
Adding replica 192.168.56.102:9002 to 192.168.56.101:9002
M: 8240d131c58c8b687c11462e03825bdba00af53b 192.168.56.101:9001
   slots:0-5460 (5461 slots) master
M: 0bdd8387a662984e95f5103522f52e75eceaab69 192.168.56.101:9002
   slots:10923-16383 (5461 slots) master
S: e38d78fc68b69d4e5f347cf96f31a9cb6a02dccf 192.168.56.101:9003
   replicates 12e2353c9f0b085d2684da46ba0dc69575d68603
M: 12e2353c9f0b085d2684da46ba0dc69575d68603 192.168.56.102:9001
   slots:5461-10922 (5462 slots) master
S: 2b48fd08a8ec55eba5bfa0d163d02c8462637302 192.168.56.102:9002
   replicates 0bdd8387a662984e95f5103522f52e75eceaab69
S: cf22edbc6463b8b31536819297ac50e9e39175ad 192.168.56.102:9003
   replicates 8240d131c58c8b687c11462e03825bdba00af53b
Can I set the above configuration? (type 'yes' to accept): yes

>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 192.168.56.101:9001)
M: 8240d131c58c8b687c11462e03825bdba00af53b 192.168.56.101:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: cf22edbc6463b8b31536819297ac50e9e39175ad 192.168.56.102:9003
   slots: (0 slots) slave
   replicates 8240d131c58c8b687c11462e03825bdba00af53b
S: e38d78fc68b69d4e5f347cf96f31a9cb6a02dccf 192.168.56.101:9003
   slots: (0 slots) slave
   replicates 12e2353c9f0b085d2684da46ba0dc69575d68603
S: 2b48fd08a8ec55eba5bfa0d163d02c8462637302 192.168.56.102:9002
   slots: (0 slots) slave
   replicates 0bdd8387a662984e95f5103522f52e75eceaab69
M: 0bdd8387a662984e95f5103522f52e75eceaab69 192.168.56.101:9002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 12e2353c9f0b085d2684da46ba0dc69575d68603 192.168.56.102:9001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

slot 槽分布

M: 8240d131c58c8b687c11462e03825bdba00af53b 192.168.56.101:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 12e2353c9f0b085d2684da46ba0dc69575d68603 192.168.56.102:9001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s) 
M: 0bdd8387a662984e95f5103522f52e75eceaab69 192.168.56.101:9002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
  
# cd /var/lib/redis/redis-cluster-data
# tree
.
├── 9001
│   ├── appendonly.aof
│   ├── cluster_9001.conf
│   ├── dump.rdb
│   └── redis.conf
├── 9002
│   ├── appendonly.aof
│   ├── cluster_9002.conf
│   ├── dump.rdb
│   └── redis.conf
└── 9003
    ├── appendonly.aof
    ├── cluster_9003.conf
    ├── dump.rdb
    └── redis.conf

3 directories, 12 files

连接redis

# redis-cli -h 192.168.56.101 -p 9001 -c
192.168.56.101:9001> 
192.168.56.101:9001> 
192.168.56.101:9001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:23175
cluster_stats_messages_pong_sent:25275
cluster_stats_messages_sent:48450
cluster_stats_messages_ping_received:25270
cluster_stats_messages_pong_received:23175
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:48450

192.168.56.101:9001> cluster nodes
cf22edbc6463b8b31536819297ac50e9e39175ad 192.168.56.102:9003@19003 slave 8240d131c58c8b687c11462e03825bdba00af53b 0 1526649623970 6 connected
8240d131c58c8b687c11462e03825bdba00af53b 192.168.56.101:9001@19001 myself,master - 0 1526649623000 1 connected 0-5460
12e2353c9f0b085d2684da46ba0dc69575d68603 192.168.56.102:9001@19001 slave e38d78fc68b69d4e5f347cf96f31a9cb6a02dccf 0 1526649621000 7 connected
2b48fd08a8ec55eba5bfa0d163d02c8462637302 192.168.56.102:9002@19002 slave 0bdd8387a662984e95f5103522f52e75eceaab69 0 1526649624972 5 connected
0bdd8387a662984e95f5103522f52e75eceaab69 192.168.56.101:9002@19002 master - 0 1526649622000 2 connected 10923-16383
e38d78fc68b69d4e5f347cf96f31a9cb6a02dccf 192.168.56.101:9003@19003 master - 0 1526649622963 7 connected 5461-10922

参考:
https://www.cnblogs.com/kevingrace/p/7910692.html

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐