linux系统haproxy负载均衡器使用keepalived做高可用

haproxy负载均衡器做高可用

      • haproxy+keepalived

haproxy+keepalived

两台负载均衡器都安装
yum -y install haproxy

vim /etc/haproxy/haproxy.cfg

global
    log         127.0.0.1 local2 info
    pidfile     /var/run/haproxy.pid
    maxconn     4000   #优先级低
    user        haproxy
    group       haproxy
    daemon               #以后台形式运行ha-proxy
    nbproc 1		    #工作进程数量  cpu内核是几就写几
defaults
    mode                    http  #工作模式 http ,tcp 是 4 层,http是 7 层	
    log                     global
    retries                 3   #健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查
    option                  redispatch  #服务不可用后重定向到其他健康服务器。
    maxconn                 4000  #优先级中
    contimeout	            5000  #ha服务器与后端服务器连接超时时间,单位毫秒ms
    clitimeout	            50000 #客户端超时
    srvtimeout	            50000 #后端服务器超时
listen stats
    bind			*:80    #这个端口要和监听的端口不一致,要不然会端口冲突
    stats                   	enable
    stats uri              	/haproxy  #使用浏览器访问 http://192.168.246.169/haproxy,可以看到服务器状态  
    stats auth           	qianfeng:123  #用户认证,客户端使用elinks浏览器的时候不生效
frontend  web
    mode                   	http  
    bind                    	    *:80   #监听哪个ip和什么端口
    option                  httplog		#日志类别 http 日志格式
    acl html url_reg  -i  \.html$  #1.访问控制列表名称html。规则要求访问以html结尾的url
    use_backend httpservers if  html #2.如果满足acl html规则,则推送给后端服务器httpservers
    default_backend    httpservers   #默认使用的服务器组
backend httpservers    #名字要与上面的名字必须一样
    balance     roundrobin  #负载均衡的方式
    server  http1 后台web服务器ip:端口 maxconn 2000 weight 1  check inter 1s rise 2 fall 2
    server  http2 后台web服务器ip:端口 maxconn 2000 weight 1  check inter 1s rise 2 fall 2
    
    
systemctl start haproxy    //启动haproxy
浏览器分别访问两台负载均衡器
浏览器访问ip:81/haproxy      //查看haproxy后台
两台后台rs集群安装web服务器

yum -y install nginx
systemctl start nginx
两台负载均衡器都安装
yum install -y keepalived

主负载均衡器
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id director1
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 80
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.188.17/24      #vip
    }
}


备用负载均衡器
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id directory2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    nopreempt
    virtual_router_id 80
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.188.17/24      #vip
    }
}


启动keepalived
systemctl start keepalived

对调度器Haproxy健康检查,两台负载均衡器都添加
vim /etc/keepalived/check_haproxy_status.sh
#!/bin/bash                                                                                      /usr/bin/curl -I http://localhost &>/dev/null   
if [ $? -ne 0 ];then                                                                            
#       /etc/init.d/keepalived stop
        systemctl stop keepalived
fi		

chmod a+x /etc/keepalived/check_haproxy_status.sh

给keepalived配置文件添加
vrrp_script check_haproxy {
   script "/etc/keepalived/check_haproxy_status.sh" #脚本位置
   interval 5
}

vrrp_instance VI_1 {
	track_script {
        check_haproxy
    }
}


注:必须先启动haproxy,再启动keepalived
两台机器都配置haproxy的日志:需要打开注释并添加
vim /etc/rsyslog.conf 

# Provides UDP syslog reception  #由于haproxy的日志是用udp传输的,所以要启用rsyslog的udp监听
$ModLoad imudp
$UDPServerRun 514

找到  #### RULES ####   下面添加
local2.*                       /var/log/haproxy.log

systemctl restart rsyslog
systemctl restart haproxy

你可能感兴趣的:(linux,linux,负载均衡,运维)