Redis集群搭建(三主三从)

Redis集群搭建

1 角色规划

角色 ip 端口
Master1 192.168.0.230 7000
Salve1 192.168.0.230 7001
Master2 192.168.0.231 7000
Salve2 192.168.0.231 7001
Master3 192.168.0.232 7000
Salve3 192.168.0.232 7001

2 步骤

2.1 安装

# 安装C语言环境GCC
yum install -y gcc-c++
# 安装用于下载Redis
yum install -y wget
# 下载Redis
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
cd /opt/packages/
mkdir /opt/redis/ -p
tar -zxf redis-5.0.5.tar.gz -C /opt/redis/
# 编译Redis源码
cd /opt/redis/redis-5.0.5/src
make
# 安装Redis
make install PREFIX=/opt/redis/redis-5.0.5/

2.2 创建节点

2.3.1 redis-cluster配置文件夹

# 创建redis-cluster节点配置文件
mkdir -p /opt/redis/redis-cluster/7000mkdir -p /opt/redis/redis-cluster/7001
# 复制节点配置文件
cp /opt/redis/redis-5.0.5/redis.conf /opt/redis/redis-cluster/7000/
cp /opt/redis/redis-5.0.5/redis.conf /opt/redis/redis-cluster/7001/

2.3.2 7000(master)节点配置

# 修改190.168.0.230:7000节点配置文件
cd /opt/redis/redis-cluster/7000/
sed -i 's/bind 127.0.0.1/bind 192.168.0.230/g' redis.conf
sed -i '92c port 7000' redis.conf
sed -i 's/daemonize no/daemonize yes/g' redis.conf
sed -i 's/pidfile \/var\/run\/redis_6379.pid/pidfile\/var\/run\/redis_7000.pid/g' redis.conf
sed -i 's/logfile ""/logfile \/opt\/redis\/redis-cluster\/7000\/redis.log/g'redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/g' redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7000.conf/g' redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/g' redis.conf
sed -i 's/appendonly no/appendonly yes/g' redis.conf
sed -i 's/# appendfsync always/appendfsync always/g' redis.conf
# 校验配置文件修改
diff redis.conf /opt/redis/redis-5.0.5/redis.conf

2.3.3 7001(slave)节点配置

# 修改190.168.0.230:7001节点配置文件
cd /opt/redis/redis-cluster/7001/
sed -i 's/bind 127.0.0.1/bind 192.168.0.230/g' redis.conf
sed -i '92c port 7001' redis.conf
sed -i 's/daemonize no/daemonize yes/g' redis.conf
sed -i 's/pidfile \/var\/run\/redis_6379.pid/pidfile\/var\/run\/redis_7001.pid/g' redis.conf
sed -i 's/logfile ""/logfile \/opt\/redis\/redis-cluster\/7001\/redis.log/g'redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/g' redis.conf
sed -i 's/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-7001.conf/g' redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 15000/g' redis.conf
sed -i 's/appendonly no/appendonly yes/g' redis.conf
sed -i 's/# appendfsync always/appendfsync always/g' redis.conf
# 校验配置文件修改
diff redis.conf /opt/redis/redis-5.0.5/redis.conf

2.3.4 校验配置文件

diff redis.conf /opt/redis/redis-5.0.5/redis.conf

56c56
< # bind 192.168.0.230 ::1
---
> # bind 127.0.0.1 ::1
69c69
< bind 192.168.0.230
---
> # bind 127.0.0.1
88c88
< protected-mode yes
---
> protected-mode no
92c92
< port 7001
---
> port 6379
158c158
< pidfile /var/run/redis_7000.pid
---
> pidfile /var/run/redis_6379.pid
171c171
< logfile /opt/redis/redis-cluster/7000/redis.log
---
> logfile ""
699c699
< appendonly yes
---
> appendonly no
728c728
< appendfsync always
---
> # appendfsync always
832c832
< cluster-enabled yes
---
> # cluster-enabled yes
840c840
< cluster-config-file nodes-7000.conf
---
> # cluster-config-file nodes-6379.conf
846c846
< cluster-node-timeout 15000
---
> # cluster-node-timeout 15000

2.3 分发节点

scp -r redis root@192.168.0.231:/opt/
scp -r redis root@192.168.0.232:/opt/

2.4 修改绑定服务器

​ ip:192.168.0.231/232

# 192.168.0.231
cd /opt/redis/redis-cluster/7000/
sed -i 's/bind 192.168.0.230/bind 192.168.0.231/g' redis.conf
cd /opt/redis/redis-cluster/7001/
sed -i 's/bind 192.168.0.230/bind 192.168.0.231/g' redis.conf
# 192.168.0.232
cd /opt/redis/redis-cluster/7000/
sed -i 's/bind 192.168.0.230/bind 192.168.0.232/g' redis.conf
cd /opt/redis/redis-cluster/7001/
sed -i 's/bind 192.168.0.230/bind 192.168.0.232/g' redis.conf

2.5 配置环境变量

vim /etc/profile
# 键入如下信息
# Redis
export Redis_HOME=/opt/redis/redis-5.0.5
export PATH=$PATH:$Redis_HOME/bin
# 生效
source /etc/profile

2.6 启动

# 192.168.0.230
redis-server /opt/redis/redis-cluster/7000/redis.conf
redis-server /opt/redis/redis-cluster/7001/redis.conf
# 192.168.0.231
redis-server /opt/redis/redis-cluster/7000/redis.conf
redis-server /opt/redis/redis-cluster/7001/redis.conf
# 192.168.0.232
redis-server /opt/redis/redis-cluster/7000/redis.conf
redis-server /opt/redis/redis-cluster/7001/redis.conf

2.7 创建集群

redis-cli --cluster create 192.168.0.230:7000 192.168.0.231:7000 192.168.0.232:7000 192.168.0.230:7001 192.168.0.231:7001 192.168.0.232:7001 --cluster-replicas 1
[root@mr00 redis]# redis-cli --cluster create 192.168.0.230:7000 192.168.0.231:7000 192.168.0.232:7000 192.168.0.230:7001 192.168.0.231:7001 192.168.0.232:7001 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.231:7001 to 192.168.0.230:7000
Adding replica 192.168.0.232:7001 to 192.168.0.231:7000
Adding replica 192.168.0.230:7001 to 192.168.0.232:7000
M: 710c7a5cabf64b2a602709fc250ac24bf4bd4adb 192.168.0.230:7000
slots:[0-5460] (5461 slots) master
M: deadb71b00291d7f9aaf17eb88b709db34abc440 192.168.0.231:7000
slots:[5461-10922] (5462 slots) master
M: 16a3cad1e4441d5590a51b768af39cf6ac4dc412 192.168.0.232:7000
slots:[10923-16383] (5461 slots) master
S: 459f1f1f4f223a4c9e6cf77568ffb6446cbecaa4 192.168.0.230:7001
replicates 16a3cad1e4441d5590a51b768af39cf6ac4dc412
S: e01cb1fe4d731bdf323179160038333aa81d9218 192.168.0.231:7001
replicates 710c7a5cabf64b2a602709fc250ac24bf4bd4adb
S: fed45204d404eeb0ea0b1b16c921224b3022d104 192.168.0.232:7001
replicates deadb71b00291d7f9aaf17eb88b709db34abc440
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.0.230:7000)
M: 710c7a5cabf64b2a602709fc250ac24bf4bd4adb 192.168.0.230:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: fed45204d404eeb0ea0b1b16c921224b3022d104 192.168.0.232:7001
slots: (0 slots) slave
replicates deadb71b00291d7f9aaf17eb88b709db34abc440
S: e01cb1fe4d731bdf323179160038333aa81d9218 192.168.0.231:7001
slots: (0 slots) slave
replicates 710c7a5cabf64b2a602709fc250ac24bf4bd4adb
M: deadb71b00291d7f9aaf17eb88b709db34abc440 192.168.0.231:7000
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 459f1f1f4f223a4c9e6cf77568ffb6446cbecaa4 192.168.0.230:7001
slots: (0 slots) slave
replicates 16a3cad1e4441d5590a51b768af39cf6ac4dc412
M: 16a3cad1e4441d5590a51b768af39cf6ac4dc412 192.168.0.232:7000
slots:[10923-16383] (5461 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.

2.8 验证

redis-cli -c -h 192.168.0.230 -p 7000
# 查看集群信息
cluster info
# 查看集群列表
cluster nodes

集群信息:

节点信息:

posted @ 2023-05-23 09:44  书中黄金屋  阅读(939)  评论(0编辑  收藏  举报