基于Haproxy+Keepalived构建高可用负载均衡集群

一、网络结构图
基于Haproxy+Keepalived构建高可用负载均衡集群_第1张图片
二、主备服务器设置(设置相同,不同会指出)

  • 安装haproxy
[root@localhost ~]# yum -y install haproxy
  • 配置haproxy
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg 
global
#   log 127.0.0.1   local0
#   log 127.0.0.1   local1 notice
    log /dev/log    local0 info
    log /dev/log    local0 notice
    maxconn 4096
    uid 99
    gid 99
    daemon
 
defaults
    log global
    mode    http
    option  httplog
    retries 3
    maxconn 4096
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000
 
listen  webcluster 0.0.0.0:80
    option  httpchk GET /index.html
    balance roundrobin
    server  inst1 192.168.200.113:80 check inter 2000 fall 3
    server  inst1 192.168.200.114:80 check inter 2000 fall 3
 
listen admin_stats
    bind 0.0.0.0:8000
    mode http
    option httplog
    maxconn 100
    stats refresh 30s
    stats uri /stats
    stats realm Crushlinux\ Haproxy
        stats auth admin:admin
    stats hide-version
  • 安装keepalived
[root@localhost ~]# yum -y install keepalived
  • 配置keepalived
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
 
vrrp_script chk_http_port {
	script "/etc/keepalived/check_haproxy.sh"
	interval 2
 
	global_defs {
		router_id LVS_DEVEL
	}
	
	vrrp_instance VI_1 {
		state MASTER    #备服务器要改为BACKUP
		interface eno16777728
		virtual_router_id 51 
		priority 100  #备服务器优先级要降低
		advert_int 1
		
		authentication {
			auth_type PASS
			auth_pass 1111
		}
 
		track_script {
			chk_http_port
		}
 
		virtual_ipaddress {
			192.168.200.254 
		}
	}
}
  • 准备检测脚本
[root@localhost ~]# vim /etc/keepalived/check_haproxy.sh 
#!/bin/bash
num=`ps -C haproxy --no-header |wc -l`
if [ $num -eq 0 ]
then
    systemctl restart haproxy
    sleep 3
    if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]
    then
        systemctl stop keepalived
    fi
fi

#添加执行权限
[root@localhost ~]# chmod +x /etc/keepalived/check_haproxy.sh 

三、Apache服务器配置

  • 安装Apache
[root@localhost ~]# yum -y install httpd
  • 准备测试文件
#Apache1
[root@localhost ~]# echo "111" > /var/www/html/index.html 
#Apache2
[root@localhost ~]# echo "222" > /var/www/html/index.html 

四、测试

  • 关闭防火墙,安全机制
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
  • 启动服务
#主备
[root@localhost ~]# systemctl restart haproxy
[root@localhost ~]# systemctl restart keepalived
#Apache
[root@localhost ~]# systemctl restart httpd
  • 测试
    基于Haproxy+Keepalived构建高可用负载均衡集群_第2张图片
    基于Haproxy+Keepalived构建高可用负载均衡集群_第3张图片
  • 测试脚本
[root@localhost ~]# systemctl stop haproxy   #关闭服务
[root@localhost ~]# systemctl status haproxy  #查看发现开启,证明没问题
haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled)
   Active: active (running) since Sat 2019-09-28 00:58:13 CST; 7s ago
 Main PID: 12025 (haproxy-systemd)
  • 测试主备
#关闭主服务器
[root@localhost ~]# systemctl stop keepalived
#查看备服务器网卡
[root@localhost ~]# ip a | grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
    inet 192.168.200.112/24 brd 192.168.200.255 scope global eno16777728
    inet 192.168.200.254/32 scope global eno16777728
 #发现254的VIP地址,说明没问题

基于Haproxy+Keepalived构建高可用负载均衡集群_第4张图片
基于Haproxy+Keepalived构建高可用负载均衡集群_第5张图片

你可能感兴趣的:(基于Haproxy+Keepalived构建高可用负载均衡集群)