CentOS7搭建redis6高可用主从集群(3主3从1哨兵)

Redis集群中有投票容错机制,超过半数的节点检测认为某一节点已经失效了,那么就会放弃此节点,所以至少需要3个节点。为了保证高可用加入了主从模式,则一共就有六个节点实例(3个主节点和3个从节点,开启1个哨兵模式)


CentOS7搭建redis6高可用主从集群(3主3从1哨兵)



IPmaster节点salve节点
192.168.3.210192.168.3.210:7000192.168.3.210:7001
192.168.3.220192.168.3.220:7000192.168.3.220:7001
192.168.3.230192.168.3.230:7000192.168.3.230:7001



系统准备

#修改计算机名
#主节点1
hostnamectl --static set-hostname Redis1 && su
#主节点2
hostnamectl --static set-hostname Redis2 && su
#主节点3
hostnamectl --static set-hostname Redis3 && su

#关闭防火墙和修改内核参数
setenforce 0 && sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld.service && systemctl disable firewalld.service && systemctl status firewalld.service
echo 1 > /proc/sys/vm/overcommit_memory
echo 1024 > /proc/sys/net/core/somaxconn
echo never > /sys/kernel/mm/transparent_hugepage/enabled

#添加hosts
cat >>/etc/hosts <<EOF
192.168.3.210 Redis1
192.168.3.220 Redis2
192.168.3.230 Redis3
EOF

#安装必要组件
yum install epel-release wget gcc gcc-c++ make tcl net-tools vim ntp -y 
yum install centos-release-scl -y 
yum install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils -y 
scl enable devtoolset-9 bash
echo -e "\nsource /opt/rh/devtoolset-9/enable" >>/etc/profile
ntpdate ntp.aliyun.com


编译安装

cd /usr/local/src/
wget https://download.redis.io/releases/redis-6.0.9.tar.gz
tar xzf redis-6.0.9.tar.gz
cd /usr/local/src/redis-6.0.9
make && make test
make install PREFIX=/usr/local/redis
#测试
/usr/local/redis/bin/redis-server

#修改环境变量
mkdir -p /usr/local/redis/redis_cluster/7000/conf/
mkdir -p /usr/local/redis/redis_cluster/7000/logs/
mkdir -p /usr/local/redis/redis_cluster/7000/data/
mkdir -p /usr/local/redis/redis_cluster/7001/conf/
mkdir -p /usr/local/redis/redis_cluster/7001/logs/
mkdir -p /usr/local/redis/redis_cluster/7001/data/
echo 'export PATH=$PATH:/usr/local/redis/bin'>>/etc/profile 
source /etc/profile


修改配置

vim /usr/local/redis/redis_cluster/7000/conf/redis.conf
改为
bind 192.168.3.210
port 7000
daemonize yes  
pidfile /var/run/redis-7000.pid    
logfile /usr/local/redis/redis_cluster/7000/logs/redis.log
dir /usr/local/redis/redis_cluster/7000/data
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000
appendonly yes
appendfsync always
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb

vim /usr/local/redis/redis_cluster/7001/conf/redis.conf
改为
bind 192.168.3.210
port 7001
daemonize yes
pidfile /var/run/redis-7001.pid
logfile /usr/local/redis/redis_cluster/7001/logs/redis.log
dir /usr/local/redis/redis_cluster/7001/data
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 15000
appendonly yes
appendfsync always
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb


分发配置文件

#免密
ssh-keygen -t rsa -b 1200
ssh-copy-id -i ~/.ssh/id_rsa.pub root@Redis1
ssh-copy-id -i ~/.ssh/id_rsa.pub root@Redis2
ssh-copy-id -i ~/.ssh/id_rsa.pub root@Redis3

#分发
scp -r /usr/local/redis/redis_cluster/7000/conf/redis.conf root@Redis2:/usr/local/redis/redis_cluster/7000/conf/redis.conf
scp -r /usr/local/redis/redis_cluster/7001/conf/redis.conf root@Redis2:/usr/local/redis/redis_cluster/7001/conf/redis.conf
scp -r /usr/local/redis/redis_cluster/7000/conf/redis.conf root@Redis3:/usr/local/redis/redis_cluster/7000/conf/redis.conf
scp -r /usr/local/redis/redis_cluster/7001/conf/redis.conf root@Redis3:/usr/local/redis/redis_cluster/7001/conf/redis.conf

节点2执行
cd /usr/local/redis/redis_cluster/7000/conf/
sed -i "s/^bind 192.168.3.210/bind 192.168.3.220/g" redis.conf
cd /usr/local/redis/redis_cluster/7001/conf/
sed -i "s/^bind 192.168.3.210/bind 192.168.3.220/g" redis.conf

节点3执行
cd /usr/local/redis/redis_cluster/7000/conf/
sed -i "s/^bind 192.168.3.210/bind 192.168.3.230/g" redis.conf
cd /usr/local/redis/redis_cluster/7001/conf/
sed -i "s/^bind 192.168.3.210/bind 192.168.3.230/g" redis.conf


添加脚本

#启动脚本
vim cluster_start.sh
添加
#!/bin/bash
redis-server /usr/local/redis/redis_cluster/7000/conf/redis.conf
redis-server /usr/local/redis/redis_cluster/7001/conf/redis.conf
执行chmod +x cluster_start.sh
./cluster_start.sh

#关闭脚本
vim cluster_shutdown.sh
添加
#!/bin/bash
pgrep redis-server | xargs -exec kill -9
执行chmod +x cluster_shutdown.sh

#测试
ps -ef | grep redis
netstat -luntp

CentOS7搭建redis6高可用主从集群(3主3从1哨兵)


启动集群

#cluster-replicas 1代表3主3从,前3个代表3个master,后3个代表3个slave
redis-cli --cluster create 192.168.3.210:7000 192.168.3.220:7000 192.168.3.230:7000 192.168.3.210:7001 192.168.3.220:7001 192.168.3.230:7001 --cluster-replicas 1

CentOS7搭建redis6高可用主从集群(3主3从1哨兵)

输入yes

CentOS7搭建redis6高可用主从集群(3主3从1哨兵)

#如果遇到错误,删除集群
#rm -rf /usr/local/redis/redis_cluster/7000/data/nodes_7000.conf
#rm -rf /usr/local/redis/redis_cluster/7000/data/appendonly.aof 
#rm -rf /usr/local/redis/redis_cluster/7000/data/dump.rdb 
#rm -rf /usr/local/redis/redis_cluster/7001/data/nodes_7001.conf
#rm -rf /usr/local/redis/redis_cluster/7001/data/appendonly.aof 
#rm -rf /usr/local/redis/redis_cluster/7001/data/dump.rdb


查看集群状态

 redis-cli --cluster check redis3:7001

CentOS7搭建redis6高可用主从集群(3主3从1哨兵)


#Radis1连接Radis3上的7001节点并添加一个数据
redis-cli -c -h 192.168.3.230 -p 7001
set key001 hi~VOS
get key001

CentOS7搭建redis6高可用主从集群(3主3从1哨兵)


Radis2连接Radis1上的7001节点查询刚添加的数据
redis-cli -c -h 192.168.3.210 -p 7001
get key001

CentOS7搭建redis6高可用主从集群(3主3从1哨兵)


设置集群自启

vim /etc/rc.local
添加
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7000/conf/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7001/conf/redis.conf

执行
chmod +x /etc/rc.d/rc.local


添加哨兵监控

#创建目录
mkdir -p /usr/local/redis/redis_cluster/27001/conf/
mkdir -p /usr/local/redis/redis_cluster/27001/logs/
mkdir -p /usr/local/redis/redis_cluster/27001/data/

#修改配置
vim /usr/local/redis/redis_cluster/27001/conf/sentinel.conf
改为
bind 192.168.3.210
port 27001
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile /usr/local/redis/redis_cluster/27001/logs/redis-sentinel.log
dir /usr/local/redis/redis_cluster/27001/data
sentinel monitor mymaster1 192.168.3.210 7000 2
sentinel monitor mymaster2 192.168.3.220 7000 2
sentinel monitor mymaster3 192.168.3.230 7000 2
sentinel down-after-milliseconds mymaster1 10000
sentinel down-after-milliseconds mymaster2 10000
sentinel down-after-milliseconds mymaster3 10000
sentinel parallel-syncs mymaster1 1
sentinel parallel-syncs mymaster2 1
sentinel parallel-syncs mymaster3 1
sentinel failover-timeout mymaster1 15000
sentinel failover-timeout mymaster2 15000
sentinel failover-timeout mymaster3 15000

#分发配置
scp -r /usr/local/redis/redis_cluster/27001/conf/sentinel.conf root@Redis2:/usr/local/redis/redis_cluster/27001/conf/sentinel.conf
scp -r /usr/local/redis/redis_cluster/27001/conf/sentinel.conf root@Redis3:/usr/local/redis/redis_cluster/27001/conf/sentinel.conf

#节点2执行
cd /usr/local/redis/redis_cluster/27001/conf/
sed -i "s/^bind 192.168.3.210/bind 192.168.3.220/g" sentinel.conf
#节点3执行
cd /usr/local/redis/redis_cluster/27001/conf/
sed -i "s/^bind 192.168.3.210/bind 192.168.3.230/g" sentinel.conf

#启动
redis-sentinel /usr/local/redis/redis_cluster/27001/conf/sentinel.conf
tail -f /usr/local/redis/redis_cluster/27001/logs/redis-sentinel.log
ps -ef|grep sentinel

CentOS7搭建redis6高可用主从集群(3主3从1哨兵)


更新日期: 2020年12月11日
文章标签: LinuxRedis
文章链接: https://www.vos.cn/db/463.html  
版权说明:如非注明,本站文章均为vOS原创,转载请注明出处和附带本文链接。