KeepAlived+Redis+Haproxy实现主从热备、负载均衡、秒级切换实战

KeepAlived+Redis+Haproxy实现主从热备、负载均衡、秒级切换实战

  • Redis+Keepalived+Haproxy 的集群架构,分别用六个端口,实现多路复用,最终实现主从热备、负载均衡、秒级切换。

一、部署Redis集群

1、环境

  • 3台虚拟机模拟6个节点,一台机器2个节点,创建出3 master、3 salve 环境

  • redis1: 192.168.166.130

  • redis2: 192.168.166.134

  • redis3: 192.168.166.135

    配置主机名 本地解析

2、安装 redis 实例 (6节点)

用脚本运行搭建6个节点

192.168.166.130:6001
192.168.166.130:6002
192.168.166.134:6001
192.168.166.134:6002
192.168.166.135:6001
192.168.166.135:6002

脚本1:只在node1执行此脚本即可 
[root@localhost ~]# cat redis_cluster.sh 
#!/bin/bash
stty erase ^H
#此脚本仅适用于rediscluster的部署
#--初始化引用
host_ip=`ip a show ens33 |grep 'scope global dynamic' |awk '{print $2}'|awk -F '/' '{print$1}'`
original(){
	systemctl stop firewalld
	systemctl disable firewalld
	setenforce 0
	sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config 
}
nopass_login(){
	for i in `(seq 1 $num1)`
	do
		read -p "Please enter your redis ip:  " ip
		echo "$ip" >> ./ip.txt
		arr[$i]=`awk "NR==$i{print $1}" ./ip.txt`
		cat >> /etc/hosts <<-EOF
		${arr[$i]} redis$i
		EOF
	if [[ "${arr[$i]}" == "$host_ip" ]];then
		yum install -y expect
		rm -rf /root/.ssh/*
		echo "开始生成密钥"
		sleep 1
		                echo "正在生成密钥"
		rm -rf ~/.ssh/{known_hosts,id_rsa*}
		ssh-keygen -f ~/.ssh/id_rsa -t rsa -N '' &>/dev/null
		if [ $? -eq 0 ];then
		                echo "密钥生成成功"
		else
		                echo "密钥生成失败"
		fi
	fi
	if [[ "${arr[$i]}" != "$host_ip" ]];then
		for k in ${arr[$i]}
		do
		   echo "开始传送密钥"
		expect <<-EOF
		spawn ssh-copy-id -i redis$i 
		expect "(yes/no)?" {send "yes\r"}
		expect "password:" {send "123\r"}
		expect "#" {send "exit\r"}
		EOF
		if [ $? -eq 0 ];then
		        echo "密钥传送成功 redis$i" 
		        continue
		        else
		        echo "密钥传送失败 redis$i"
		fi
		
		done
	fi
	done
}
check(){
	if [[ $? -eq 0 ]];then
		echo "SUCCESSFUL"
	else
		echo "FAIELD"
	fi
}
install_redis_all(){
	i=1
	count=`echo $(( ($num2 + $num3) * $num1 ))`
	echo "Your redis has $count"
	echo "Modify host name"
	while read host
	do
		if [[ "$host_ip" == "$host" ]];then
			hostnamectl --static set-hostname redis$i
			echo "The host name of the current server is: redis$i"
		fi
		let i++
	done <./ip.txt
}

other_node(){
	cd ~
	grep "redis" /etc/hosts >> /root/hosts.txt
	for l in `seq 2 ${num1}` 
	do
	echo $l
	expect <<-EOF &>/dev/null
	spawn scp ./hosts.txt ./ip.txt /   root@redis$l:/root/
	expect "(yes/no)?" {send "yes\r"}
	expect "password:" {send "123\r"}
	EOF
	if [ $? -eq 0 ];then
		echo "File transfer successful redis$l" 
	else
		echo "File transfer failed  redis$l"
	fi
	scp -r /redis/ root@redis$l:/ 
	scp /usr/local/redis-cluster/start-redis-cluster.sh root@redis$l:/root/
done
}
install_redis_persion(){
	install_redis_all
	cd /usr/local/src
	yum install -y wget &> /dev/null
	wget http://download.redis.io/releases/redis-6.0.5.tar.gz
	tar -zxvf redis-6.0.5.tar.gz -C /usr/local
	echo "Modify the maximum number of open files"
	sleep 0.5
	 cat >> /etc/security/limits.conf <<-EOF
	* soft nofile 102400
	* hard nofile 102400
	EOF
	echo "Modify TCP listening queue size and Modify OOM"
	sleep 0.5
	echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf
	echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
	sysctl -p
	echo "Turn on the transparent tiger pages (THP) feature of the kernel"
	echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled"  >> /etc/rc.local
	chmod +x /etc/rc.local
	yum -y install gcc glibc glibc-kernheaders  glibc-common glibc-devel make
	yum -y install centos-release-scl
	yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
	source /opt/rh/devtoolset-9/enable
	echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
	echo "即将编译"
	cd /usr/local/redis-6.0.5/
	echo "make"
	make
	make install PREFIX=/usr/local/redis-cluster
	echo "加入配置文件"
	cat >>/usr/local/redis-cluster/start-redis-cluster.sh <<-EOF
	#!/bin/bash
	REDIS_HOME=/usr/local/redis-cluster
	REDIS_CONF=/redis
	EOF
	o=`echo $(($num2+$num3))`
	for i in `seq 1 $o`
	do
		mkdir -p /redis/600$i/{conf,data,log}
		cd /redis/600$i/conf/
		cat >> redis.conf <<-EOF
		bind 0.0.0.0
		protected-mode no
		daemonize yes
		port 600$i
		dir /redis/600$i/data
		cluster-enabled yes
		cluster-config-file /redis/600$i/conf/nodes.conf
		cluster-node-timeout 5000
		appendonly yes
		pidfile /redis/600$i/redis.pid
		logfile /redis/600$i/log/redis.log
		EOF
	cat >>/usr/local/redis-cluster/start-redis-cluster.sh<<-EOF
	\$REDIS_HOME/bin/redis-server \$REDIS_CONF/600$i/conf/redis.conf
	EOF
	done
	chmod +x /usr/local/redis-cluster/start-redis-cluster.sh
	echo "向其它节点传送配置文件 启动脚本"
	other_node
	echo "开始执行启动脚本"
	bash /usr/local/redis-cluster/start-redis-cluster.sh
	ps -ef | grep [r]edis &>/dev/null
	check
}
other_install(){
	for i in `seq 2 $num1`
	do
	scp /root/redis_other.sh root@redis$i:/root/
	ssh root@redis$i "/usr//bin/bash /root/redis_other.sh"
	done
}
cluster_create(){
	cd /usr/local/redis-cluster/bin
	ms=`echo $(( $num2 + $num3))`
	for s in `seq 1 $num1`
	do
		node=`grep "redis$s" /root/hosts.txt | awk '{print $1}'`
		for f in `seq 1 $ms`
		do
		        echo $node":600"$f >> /root/node_ip.txt
		done
	done
	echo "您添加的节点为:"
	cat /root/node_ip.txt
	echo "即将添加节点,请稍后"
	index=0
	while read nodeip
	do
		arr[$index]=$nodeip
		let index++
	done</root/node_ip.txt
expect <<EOF
spawn ./redis-cli --cluster create ${arr[@]}   --cluster-replicas $num3
expect 'Can I set the above configuration? (type 'yes' to accept):' {send "yes\r"}
EOF
}

#-------------------------------集群部署前下载启动各节点------------------------------------
read -p "Please enter the number of rediss in your cluster(example: 3 1 1: )" num1 num2 num3
echo "About to initialize..."
original
sleep 0.5
echo "Do secret free login transmission..."
nopass_login
sleep 0.5
echo "Global configuration of all nodes..."
echo "Each node is configured to start redis"
install_redis_persion
echo "Beginning other"
other_install
#--------------------------------集群部署----------------------------------------------------
echo "After the node is ready, the redis cluster will be created......"
echo "Please wait!"
cluster_create
echo "Cluster set successful!"

脚本2:与脚本1在同一个目录下即可
[root@localhost ~]# cat redis_other.sh 
#!/bin/bash
stty erase ^H
#此脚本仅适用于redis 其它节点的部署
#--初始化引用
host_ip=`ip a show ens33 |grep 'scope global dynamic' |awk '{print $2}'|awk -F '/' '{print$1}'`
original(){
	systemctl stop firewalld
	systemctl disable firewalld
	setenforce 0
	sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config 
}
check(){
	if [[ $? -eq 0 ]];then
		echo "SUCCESSFUL"
	else
		echo "FAIELD"
	fi
}
install_redis_all(){
	echo i=1
	echo "Modify host name"
	while read host
	do
		if [[ "$host_ip" == "$host" ]];then
			hostnamectl --static set-hostname redis$i
			echo "The host name of the current server is: redis$i"
		fi
		let i++
	done </root/ip.txt
	echo "Configure local resolution"
	while read host
	do
	echo "$host" >> /etc/hosts
	done</root/hosts.txt
}
install_redis_persion(){
	install_redis_all
	cd /usr/local/src
	yum install -y wget &> /dev/null
	wget http://download.redis.io/releases/redis-6.0.5.tar.gz
	tar -zxvf redis-6.0.5.tar.gz -C /usr/local
	echo "Modify the maximum number of open files"
	sleep 0.5
	 cat >> /etc/security/limits.conf <<-EOF
	* soft nofile 102400
	* hard nofile 102400
	EOF
	echo "Modify TCP listening queue size and Modify OOM"
	sleep 0.5
	echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf
	echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
	sysctl -p
	echo "Turn on the transparent tiger pages (THP) feature of the kernel"
	echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled"  >> /etc/rc.local
	chmod +x /etc/rc.local
	yum -y install gcc glibc glibc-kernheaders  glibc-common glibc-devel make
	yum -y install centos-release-scl
	yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
	source /opt/rh/devtoolset-9/enable
	echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
	echo "即将编译"
	cd /usr/local/redis-6.0.5/
	echo "make"
	make
	make install PREFIX=/usr/local/redis-cluster
	echo "加入配置文件"
	mv /root/start-redis-cluster.sh /usr/local/redis-cluster/start-redis-cluster.sh
	echo "开始执行启动脚本"
	bash /usr/local/redis-cluster/start-redis-cluster.sh
	ps -ef | grep [r]edis &>/dev/null
	check
}
#-------------------------------集群部署前下载启动各节点------------------------------------
echo "About to initialize..."
original
sleep 0.5
echo "Other node begining"
install_redis_persion

3、检查 redis 启动情况

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

4、创建集群

脚本自动创建的集群

在这里插入图片描述

在这里插入图片描述

5、集群验证

在这里插入图片描述

二、部署Keepalived,实现主从热备、秒级切换

1、环境

  • 两台虚拟机或者选择集群中的任意两个节点配置
  • keepalived1:192.168.166.131
  • keepalived2:192.168.166.132
  • VIP地址:192.168.166.160

2、安装keepalived

在这里插入图片描述
在这里插入图片描述

3、修改配置文件

1、keepalived1 配置

[root@localhost log]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id directory1
}

vrrp_script check_run {
   script "/etc/keepalived/keepalived_check_haproxy.sh"
   interval 5
}
vrrp_instance VI_1 {
    state MASTER
    nopreempt
    interface ens33
    virtual_router_id 80
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_run
    }
    virtual_ipaddress {
        192.168.166.160/24
    }
}

2、keepalived2 配置

[root@localhost log]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id directory2
}

vrrp_script check_run {
   script "/etc/keepalived/keepalived_check_haproxy.sh"
   interval 5
}
vrrp_instance VI_1 {
    state BACKUP
    nopreempt
    interface ens33
    virtual_router_id 80
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_run
    }
    virtual_ipaddress {
        192.168.166.160/24
    }
}

3、健康检测脚本 haproxy_chk.sh

[root@localhost keepalived]# cat keepalived_check_haproxy.sh 
#!/bin/bash                                                                      
/usr/bin/curl -I http://localhost &>/dev/null   
if [ $? -ne 0 ];then                                                         
        /usr/bin/systemctl stop keepalived                                        
fi 

4、开启服务验证是VIP

可以看到vip在192.168.166131keepalived上

在这里插入图片描述

而192.168.166.132上没有vip
在这里插入图片描述

三、部署haproxy,实现访问6379端口时,轮询访问六个节点

1、安装 haproxy 主192.168.166.131 备192.168.166.132

主:

在这里插入图片描述

备:
在这里插入图片描述

2、创建 haproxy.conf

主:

global
    log                     127.0.0.1 local2
    chroot                  /var/lib/haproxy
    pidfile                 /var/run/haproxy.pid
    maxconn                 4000
    user                    haproxy
    group                   haproxy
    daemon

defaults
    mode                     http
    log                      global
    option                   dontlognull
    retries                  3
    maxconn                  3000
    contimeout               50000
    clitimeout               50000
    srvtimeout               50000

listen stats
    bind                    *:8888
    stats                   enable
    stats                    hide-version
    stats uri               /haproxystats
    stats realm             Haproxy\ stats
    stats auth              admin:admin
    stats admin             if TRUE

frontend web
    option                   httplog
    option                   http-server-close
    option forwardfor        except 127.0.0.0/8
    mode                     tcp
    bind                     *:6379
    default_backend          redisservers

backend redisservers
    balance roundrobin    
    server  redis1 192.168.166.130:6001 weight 1  check maxconn 2000
    server  redis2 192.168.166.130:6002 weight 1  check maxconn 2000
    server  redis3 192.168.166.134:6001 weight 1  check maxconn 2000
    server  redis4 192.168.166.134:6002 weight 1  check maxconn 2000
    server  redis5 192.168.166.135:6001 weight 1  check maxconn 2000
    server  redis6 192.168.166.135:6002 weight 1  check maxconn 2000

在这里插入图片描述

备:

[root@localhost log]# cat /etc/haproxy/haproxy.cfg
global
    log                     127.0.0.1 local2
    chroot                  /var/lib/haproxy
    pidfile                 /var/run/haproxy.pid
    maxconn                 4000
    user                    haproxy
    group                   haproxy
    daemon

defaults
    mode                     http
    log                      global
    option                   dontlognull
    retries                  3
    maxconn                  3000
    contimeout               50000
    clitimeout               50000
    srvtimeout               50000

listen stats
    bind                    *:8888
    stats                   enable
    stats                    hide-version
    stats uri               /haproxystats
    stats realm             Haproxy\ stats
    stats auth              admin:admin
    stats admin             if TRUE

frontend web
    option                   httplog
    option                   http-server-close
    option forwardfor        except 127.0.0.0/8
    mode                     tcp
    bind                     *:6379
    default_backend          redisservers

backend redisservers
    balance roundrobin    
    server  redis1 192.168.166.130:6001 weight 1  check maxconn 2000
    server  redis2 192.168.166.130:6002 weight 1  check maxconn 2000
    server  redis3 192.168.166.134:6001 weight 1  check maxconn 2000
    server  redis4 192.168.166.134:6002 weight 1  check maxconn 2000
    server  redis5 192.168.166.135:6001 weight 1  check maxconn 2000
    server  redis6 192.168.166.135:6002 weight 1  check maxconn 2000

在这里插入图片描述

3、Haproxy rsyslog 日志配置

主备配置相同:

[root@localhost log]# vim /etc/rsyslog.conf

#开启接收功能
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514


#添加在这个下边#### RULES ####
local2.*                                                /var/log/haproxy.log


#重启rsyslog服务
[root@localhost ~]# systemctl restart rsyslog
#可以看到我们在指定的目录下的haproxy.log日志
[root@localhost log]# ls
anaconda  boot.log  chrony  dmesg      firewalld           haproxy.log  maillog   rhsm    spooler   tuned  yum.log
audit     btmp      cron    dmesg.old  grubby_prune_debug  lastlog      messages  secure  tallylog  wtmp
#查看日志内容
[root@localhost log]# cat haproxy.log 
Sep 18 17:43:07 localhost haproxy[1589]: Proxy stats started.
Sep 18 17:43:07 localhost haproxy[1589]: Proxy web started.
Sep 18 17:43:07 localhost haproxy[1589]: Proxy redisservers started.
Sep 18 17:46:37 localhost haproxy[1590]: Connect from 192.168.166.1:64428 to 192.168.166.131:8888 (stats/HTTP)
Sep 18 17:46:37 localhost haproxy[1590]: Connect from 192.168.166.1:64429 to 192.168.166.131:8888 (stats/HTTP)
Sep 18 17:47:09 localhost haproxy[1590]: Connect from 192.168.166.1:64435 to 192.168.166.131:8888 (stats/HTTP)
Sep 18 17:47:09 localhost haproxy[1590]: Connect from 192.168.166.1:64436 to 192.168.166.131:8888 (stats/HTTP)

4、haproxy 监控页面访问验证

  • 端口8888 账号密码 admin:admin

  • 有以下效果

在这里插入图片描述

主:

在这里插入图片描述

备:

在这里插入图片描述

访问vip:

在这里插入图片描述

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页