单机redis cluster集群,使用端口7001-7003模拟主节点,7004-7006模拟从节点

安装redis组件

[root@localhost ~]# dnf -y install redis

81836-0z6mnzs9psh.png

创建一个目录,用来存放模拟节点的配置文件,我这里创建文件夹在/etc/redis下(位置可以自定),
分别创建了7001-7006六个目录,以端口号来区分

[root@localhost etc]# mkdir redis
[root@localhost etc]# cd redis
[root@localhost redis]# mkdir {7001..7006}

将默认的redis.conf配置文件,复制到7001下,修改配置作为模板

[root@localhost redis]# cp /etc/redis.conf /etc/redis/7001/
[root@localhost redis]# cd /etc/redis/7001

修改配置文件,主要修改以下配置,注意这里是7001端口节点,所有7001与实际端口、文件夹路径相匹配

[root@localhost 7001]# vim redis.conf
bind 0.0.0.0
protected-mode yes
port 7001
daemonize yes
pidfile /var/run/redis_7001.pid
dir /etc/redis/7001
appendonly yes

cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000

将修改完的7001节点下的redis.conf配置,拷贝到其余目录下

[root@localhost redis]# cp 7001/redis.conf 7003
cp: overwrite '7003/redis.conf'? y
[root@localhost redis]# cp 7001/redis.conf 7004
cp: overwrite '7004/redis.conf'? y
[root@localhost redis]# cp 7001/redis.conf 7005
cp: overwrite '7005/redis.conf'? y
[root@localhost redis]# cp 7001/redis.conf 7006

将所有节点下的配置文件,使用sed将配置中的端口进行替换,别的不需要修改,启动六个redis-server

[root@localhost redis]# cd 7001
[root@localhost 7001]# redis-server redis.conf
[root@localhost redis]# cd 7002
[root@localhost 7002]# sed -i 's/7001/7002/g' redis.conf
[root@localhost 7002]# redis-server redis.conf
[root@localhost redis]# cd 7003
[root@localhost 7003]# sed -i 's/7001/7003/g' redis.conf
[root@localhost 7003]# redis-server redis.conf
[root@localhost redis]# cd 7004
[root@localhost 7004]# sed -i 's/7001/7004/g' redis.conf
[root@localhost 7004]# redis-server redis.conf
[root@localhost redis]# cd 7005
[root@localhost 7005]# sed -i 's/7001/7005/g' redis.conf
[root@localhost 7005]# redis-server redis.conf
[root@localhost redis]# cd 7006
[root@localhost 7006]# sed -i 's/7001/7006/g' redis.conf
[root@localhost 7006]# redis-server redis.conf

查看服务运行端口,7001-7006正常
74368-1thqmhjlio8.png

启动cluster集群

[root@localhost 7001]# redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006  --cluster-replicas 1

95586-or5204myajk.png
等待集群创建完成,可以看到7001-7003为master节点,7004-7006为slave节点
39413-316d496tjzj.png

验证,进入redis-cli交互,查看cluster nodes节点,如需求所示

[root@localhost ~]# redis-cli -c -p 7001
127.0.0.1:7001> cluster nodes

80271-1l61472r7n.png