Redis集群搭建(三主三从)


  集群版本和环境

Redis 集群要求至少3 个主节点3 个从节点,共计6 个节点,规划如下

操作系统

CentOS 7.4

Redis 版本

6.0.5

主节点

192.168.239.143 7000

192.168.239.147 7000

192.168.239.149 7000

从节点

192.168.239.143 7001

192.168.239.147 7001

192.168.239.149 7001

 

  修改系统配置

2.1 关闭NetworkManager

[root@localhost ~]# systemctl disable NetworkManager      -- 关闭自启动

[root@localhost ~]# systemctl stop NetworkManager

[root@localhost ~]# systemctl status NetworkManager

 

2.2 关闭防火墙

systemctl stop firewalld.service

systemctl disable firewalld.service     -- 关闭自启动

firewall-cmd --state

 

vi /etc/selinux/config

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

#       enforcing - SELinux security policy is enforced.

#       permissive - SELinux prints warnings instead of enforcing.

#       disabled - SELinux is fully disabled.

SELINUX= disabled

# SELINUXTYPE= type of policy in use. Possible values are:

#       targeted - Only targeted network daemons are protected.

#       strict - Full SELinux protection.

SELINUXTYPE=targeted

 

2.3 安装依赖包

安装gcc 套装

yum install -y cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make 

 

升级gcc

yum -y install centos-release-scl

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils

scl enable devtoolset-9 bash

 

设置永久升级

echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

 

2.4 创建Redis 节点

2.4.1 创建Redis 目录并解压

mkdir /redis

tar xzf redis-6.0.5.tar.gz

 

2.4.2 安装Redis

cd /redis/redis-6.0.5

make install

 

2.4.3 创建节点目录

cd /redis/redis-6.0.5

mkdir cluster

cd cluster

mkdir 7000 7001

 

2.4.4 修改Redis 配置文件

192.168.239.143 为例,其他节点步骤相同

将配置文件拷贝到创建好的端口文件夹下

cp /redis/redis-6.0.5/redis.config /redis/redis-6.0.5/cluster/7000

cp /redis/redis-6.0.5/redis.config /redis/redis-6.0.5/cluster/7001

 

每个节点的主从节点都需要配置文件

主:

port 7000

bind 192.168.239.143

cluster-enabled yes

cluster-config-file nodes_7000.conf 

cluster-node-timeout 5000

appendonly yes

daemonize yes

logfile "/redis/redis-6.0.5/cluster/redis_7000.log"

pidfile /var/run/redis_7000.pid

 

从:

port 7001

bind 192.168.239.143

cluster-enabled yes

cluster-config-file nodes_7001.conf 

cluster-node-timeout 5000

appendonly yes

daemonize yes

logfile "/redis/redis-6.0.5/cluster/redis_7001.log"

pidfile /var/run/redis_7001.pid

    

2.4.5 启动Redis 节点服务

redis-server /redis/redis-6.0.5/cluster/7000/redis.conf 

redis-server /redis/redis-6.0.5/cluster/7001/redis.conf 


  创建Redis 集群

3.1 创建Redis 集群  

目前为止,我们已经创建了Redis 节点并且启动服务,接下来就要创建Redis 集群了。在Redis 6 中,使用redis-cli 命令来创建,其中--cluster-replicas 1 的意思是每个主节点需要1 个从节点。

 

例如:

redis-cli --cluster create 192.168.239.143:7000 192.168.239.147:7000 \

192.168.239.149:7000 192.168.239.143:7001 192.168.239.147:7001 \

192.168.239.149:7001 --cluster-replicas 1

    

我们最初的目标是创建3 个主节点加3 个从节点的集群,显然这条命令并没有指定哪些主哪些是从,其实在执行后,Redis 会建议主从节点的分配,M 表示主(Master )节点,S 表示从(Slave )节点,你只要选择yes 即可。

    

到此为止,Redis 集群就创建完毕了,在每个节点的日志中可能看到改节点在集群中的唯一标识。

 

3.2 修改集群密码

需要注意的是各个节点的密码都必须一致

masterauth 作用:主要是针对master 对应的slave 节点设置的,在slave 节点数据同步的时候用到。

requirepass 作用:对登录权限做限制,redis 每个节点的requirepass 可以是独立、不同的。

 

192.168.239.143 为例,所有节点都要执行

redis-cli -h 192.168.239.143 -p 7000

192.168.239.143:7000> config set requirepass redis123

192.168.239.143:7000> config set masterauth redis123

 

redis-cli -h 192.168.239.143 -p 7001

192.168.239.143:7001> config set requirepass redis123

192.168.239.143:7001> config set masterauth redis123

 


请使用浏览器的分享功能分享到微信等