加粗样式KeepAlived+Redis+Haproxy实现主从热备、负载均衡、秒级切换实战
- Redis+Keepalived+Haproxy 的集群架构,分别用六个端口,实现多路复用,最终实现主从热备、负载均衡、秒级切换。
修改主机名,域名解析(三台)
hostnamectl --static set-hostname redis1
hostnamectl --static set-hostname redis2
hostnamectl --static set-hostname redis3
[root@redis3 ~]# cat >>/etc/hosts <<eof
10.11.59.125 redis1
10.11.59.126 redis2
10.11.59.254 redis3
eof
修改系统参数(三台)
[root@redis1 ~]# cat >> /etc/security/limits.conf << EOF
- soft nofile 102400
- hard nofile 102400
EOF
[root@redis1 ~]# echo “net.core.somaxconn = 32767” >> /etc/sysctl.conf
[root@redis1 ~]# sysctl -p
vm.max_map_count = 262144
net.core.somaxconn = 32767
[root@redis1 ~]# echo “vm.overcommit_memory=1” >> /etc/sysctl.conf
[root@redis1 ~]# sysctl -p
vm.max_map_count = 262144
net.core.somaxconn = 32767
vm.overcommit_memory = 1
[root@redis1 ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@redis1 ~]# echo “echo never > /sys/kernel/mm/transparent_hugepage/enabled” >> /etc/rc.local
[root@redis1 ~]# chmod +x /etc/rc.local
编译安装redis(三台)
[root@redis01 ~]# yum -y install gcc glibc glibc-kernheaders glibc-common glibc-devel make
[root@redis01 ~]# yum -y install centos-release-scl
[root@redis01 ~]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@redis01 ~]# scl enable devtoolset-9 bash
[root@redis01 ~]# echo “source /opt/rh/devtoolset-9/enable” >>/etc/profile
[root@redis01 ~]# cd /usr/local/src
[root@redis01 ~]# wget http://download.redis.io/releases/redis-6.0.8.tar.gz
[root@redis01 ~]# tar -zxvf redis-6.0.8.tar.gz
[root@redis01 ~]# cd redis-6.0.8/
[root@redis01 ~]# make
[root@redis01 ~]# make install PREFIX=/usr/local/redis-cluster
[root@redis01 ~]# mkdir -p /redis/{6001,6002}/{conf,data,log}
配置redis1,redis2,redis3:
[root@redis01 ~]#cd /redis/6001/conf/
[root@redis01 conf]# cat >> redis.conf << EOF
bind 0.0.0.0
protected-mode no
port 6001
dir /redis/6001/data
cluster-enabled yes
cluster-config-file /redis/6001/conf/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
requirepass redis
pidfile /redis/6001/redis.pid
logfile /redis/6001/log/redis.log
EOF
[root@redis01 conf]# sed ‘s/6001/6002/g’ redis.conf > /redis/6002/conf/redis.conf
[root@redis01 ~]# cat >/usr/local/redis-cluster/start-redis-cluster.sh<<-EOF
#!/bin/bash
REDIS_HOME=/usr/local/redis-cluster
REDIS_CONF=/redis
$REDIS_HOME/bin/redis-server $REDIS_CONF/6001/conf/redis.conf
$REDIS_HOME/bin/redis-server $REDIS_CONF/6002/conf/redis.conf
EOF
[root@redis01 ~]# chmod +x /usr/local/redis-cluster/start-redis-cluster.sh
[root@redis1 conf]# bash /usr/local/redis-cluster/start-redis-cluster.sh
创建redis-cluster并验证
[root@redis1 ~]# ln -s /usr/local/redis-cluster/bin/redis-cli /bin/
[root@redis1 ~]# redis-cli --cluster create 10.11.59.125:6001 10.11.59.125:6002 10.11.59.126:6001 10.11.59.126:6002 10.11.59.254:6001 10.11.59.254:6002 --cluster-replicas 1
安装keepalived(redis1+redis2)
[root@redis1 ~]# yum install -y keepalived
keepalived 主备配置文件:
[root@redis1 ~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@redis1 ~]#cat >>/etc/keepalived/keepalived.conf <<eof
! Configuration File for keepalived
global_defs {
router_id redis-master
}
vrrp_script check_run {
script “/etc/keepalived/keepalived_check_redis.sh”
interval 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 89
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.11.59.110/24
}
track_script {
check_run
}
}
eof
slave 10.11.59.126 配置:
[root@redis1 ~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@redis1 ~]# cat >>/etc/keepalived/keepalived.conf <<eof
! Configuration File for keepalived
global_defs {
router_id redis-slave
}
vrrp_script check_run {
script “/etc/keepalived/keepalived_check_mysql.sh”
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface ens33
virtual_router_id 89
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.11.59.110/24
}
track_script {
check_run
}
}
Eof
haproxy状态检测脚本(两台都写)
[root@redis1 ~]# cat >> /etc/keepalived/keepalived_check_haproxy.sh <<eof
#!/bin/bash
systemctl status haproxy &>/dev/null
if [ $? -ne 0 ] ;then
systemctl stop keepalived
fi
eof
[root@redis2 ~]# cat >> /etc/keepalived/keepalived_check_haproxy.sh <<eof
#!/bin/bash
systemctl status haproxy &>/dev/null
if [ $? -ne 0 ] ;then
systemctl stop keepalived
fi
eof
两边均启动keepalived
[root@redis1 ~]#systemctl start keepalived
[root@redis2 ~]#systemctl start keepalived
[root@redis1 ~]# systemctl enable keepalived
[root@redis2 ~]# systemctl enable keepalived
[root@redis1 ~]#ip a
安装配置haproxy
[root@redis1 ~]#yum -y install haproxy
[root@redis1 ~]#cp -rf /etc/haproxy/haproxy.cfg{,.bak}
[root@redis1 ~]#cat >/etc/haproxy/haproxy.cfg <<eof
global
log 127.0.0.1 local3
maxconn 4096
user haproxy
group haproxy
daemon
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
nbproc 1
defaults
log global
mode tcp
maxconn 2048
retries 3
timeout connect 5000
timeout client 50000
timeout server 50000
listen stats
bind *:3366
mode http
stats enable
stats hide-version
stats uri /haproxy
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE
listen redis
bind *:6379
mode tcp
balance roundrobin
server redis1 10.11.59.125:6001 check
server redis2 10.11.59.126:6001 check
server redis3 10.11.59.254:6001 check
server redis1 10.11.59.125:6002 check
server redis2 10.11.59.126:6002 check
server redis3 10.11.59.254:6002 check
eof
rsyslog配置
[root@redis1 ~]# vim /etc/rsyslog.conf
Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
local3.* /redis/6001/log/redis.log