#==============================================haproxy.cfg============================================
#此配置为haproxy透传前端IP地址到后端的配置
#==================================================================================================
global #全局属性
log 127.0.0.1 local0 info
#以daemon方式在后台运行
daemon
#HAProxy启动时作为守护运行可创建的进程数,
#配合daemon参数使用,默认只启动一个进程,该值应小于cpu核数。
nbproc 1
#最大同时*连接
maxconn 102400
#指定保存HAProxy进程号的文件
pidfile /var/lib/haproxy/haproxy.pid
#定义统计信息保存位置
stats socket /var/lib/haproxy/stats
defaults #默认参数
#tcp/http模式
mode tcp
retries 3
#连接server端超时5s
timeout connect 5s
#客户端响应超时50s
timeout client 300s
#server端响应超时50s
timeout server 300s
#设置对后端服务器检测超时时间,即心跳50s
timeout check 300s
#source 0.0.0.0 usesrc clientip
frontend front_https
mode tcp
bind 0.0.0.0:443
log global
default_backend back_https
backend back_https
balance roundrobin
server server1 192.168.1.2:443 maxconn 10000 check inter 3000 rise 2 fall 3
#统计web页面配置, frontend和backend的组合体, 监控组的名称可按需自定义
listen admin_status
#配置监控运行模式
mode http
#配置统计页面访问端口
bind 0.0.0.0:1080
#统计页面默认最大连接数
maxconn 10
log 127.0.0.1 local0 err
#开启统计
stats enable
#监控页面自动刷新时间
stats refresh 30s
#统计页面访问url
stats uri /stats
#统计页面密码框提示文本
stats realm welcome login\ Haproxy
#监控页面的用户和密码:admin, 可设置多个用户名
stats auth admin:admin
#手工启动/禁用后端服务器, 可通过web管理节点
stats admin if TRUE
#===========================================keepalived_master.conf==================================
! Configuration File for keepalived
global_defs {
script_user root
enable_script_security
#notification_email {
# [email protected]
#}
#notification_email_from [email protected]
#smtp_server 192.168.133.4
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
#HAProxy健康检查配置
vrrp_script chk_haproxy {
#使用killall -0检查haproxy实例是否存在,性能高于ps命令
script "killall -0 nginx"
interval 2 #脚本运行周期
weight 2 #每次检查的加权权重值
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.1
}
track_interface {
eth0
}
track_script {
chk_haproxy
}
}
#==============================================keepalived_slave.conf================================
! Configuration File for keepalived
global_defs {
script_user root
enable_script_security
#notification_email {
# [email protected]
#}
#notification_email_from [email protected]
#smtp_server 192.168.133.4
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
#HAProxy健康检查配置
vrrp_script chk_haproxy {
#使用killall -0检查haproxy实例是否存在,性能高于ps命令
script "killall -0 nginx"
interval 2 #脚本运行周期
weight 2 #每次检查的加权权重值
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.1
}
track_interface {
eth0
}
track_script {
chk_haproxy
}
}
#================================================iptable设置=======================================
#!/bin/bash
/sbin/iptables -F
/sbin/iptables -t mangle -N DIVERT
/sbin/iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT
/sbin/iptables -t mangle -A DIVERT -j MARK --set-mark 1
/sbin/iptables -t mangle -A DIVERT -j ACCEPT
/sbin/ip rule add fwmark 1 lookup 100
/sbin/ip route add local 0.0.0.0/0 dev lo table 100
#============================================check_haproxy.sh=========================================
#!/bin/bash
ret=`ps -C haproxy --no-header | wc -l`
#if [ $ret -eq 0 ];then
# /etc/init.d/haproxy start
# sleep 3
if [ `ps -C haproxy --no-header | wc -l ` -eq 0 ];then
/etc/init.d/keepalived stop
fi
#fi