一个小业务系统的配置:
安装:
tar -xvf haproxy-1.4.21.tar.gz ;cd haproxy-1.4.21;make TARGET=linux26 PREFIX=/usr/local/haproxy;make install PREFIX=/usr/local/haproxy;cp examples/haproxy.cfg /usr/local/haproxy/;cp -rp examples/errorfiles/ /usr/local/haproxy/
启动:
/usr/local/haproxy/sbin/haproxy -c -f /usr/local/haproxy/haproxy.cfg
HAproxy配置:
global
maxconn 20480
log 127.0.0.1 local3
chroot /usr/local/haproxy
uid 502
gid 502
daemon
nbproc 4
pidfile /var/run/haproxy.pid
ulimit-n 65535
defaults
log global
mode http
maxconn 20480
retries 3
contimeout 5000
clitimeout 50000
srvtimeout 50000
timeout check 2000
listen admin_status
bind :9000
mode http
log 127.0.0.1 local3 err
stats refresh 5s
stats uri /admin?stats
stats realm Haproxy\ Statistics
stats auth test:test
stats hide-version
stats admin if TRUE
errorfile 403 /usr/local/haproxy/errorfiles/403.http
errorfile 500 /usr/local/haproxy/errorfiles/500.http
errorfile 502 /usr/local/haproxy/errorfiles/502.http
errorfile 503 /usr/local/haproxy/errorfiles/503.http
errorfile 504 /usr/local/haproxy/errorfiles/504.http
capture request header Host len 40
capture request header Content-Length len 10
capture request header Referer len 200
capture response header Server len 40
capture response header Content-Length len 10
capture response header Cache-Control len 8
########################### HTTP #############################
listen Telemedicine
bind 10.10.18.109:80
mode http
balance source
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
option abortonclose
stats refresh 30
cookie ServerID prefix
cookie JSESSIONID prefix
capture request header Cookie len 200
capture request header X-Forwarded-For len 15
capture request header Host len 15
capture request header Referrer len 15
appsession JSESSIONID len 52 timeout 1080000
option httpchk GET /Telemedicine/index.html
server tomcat1 10.10.18.108:8080 cookie web1 check inter 1500 rise 3 fall 3 weight 1
server tomcat2 10.10.18.108:8081 cookie web2 check inter 1500 rise 3 fall 3 weight 2
########################### FTP #############################
listen xkftp
bind 10.10.18.109:21
bind 10.10.18.109:10001-15000
mode tcp
option tcpka
balance source
server 108-ftp 10.10.18.108 weight 2 check port 21 inter 10s rise 1 fall 2
server 245-ftp 10.10.18.245 weight 1 check port 21 inter 10s rise 1 fall 2
keepalived双主配置
安装
tar -zxvf keepalived-1.2.15.tar.gz;cd keepalived-1.2.15;./configure --prefix=/usr/local/keepalived --disable-fwmark;make;make install
文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/;cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/;mkdir -p /etc/keepalived;cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/;cp /usr/local/keepalived/sbin/keepalived /usr/sbin/;chkconfig --add keepalived;chkconfig keepalived on;chkconfig --list keepalived;ln -s /etc/keepalived/keepalived.conf /usr/local/keepalived/;
启动:
service keepalived start
配置
global_defs {
router_id LVS_DEVEL
}
#监测haproxy进程状态,每2秒执行一次
vrrp_script chk_haproxy {
script "/etc/keepalived/chk_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
mcast_src_ip 10.10.18.109
authentication {
auth_type PASS
auth_pass 111111
}
track_script {
chk_haproxy
}
virtual_ipaddress {
10.10.18.110 #虚拟IP
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 99
advert_int 1
mcast_src_ip 10.10.18.109
authentication {
auth_type PASS
auth_pass 111111
}
track_script {
chk_haproxy
}
virtual_ipaddress {
10.10.18.114
}
}
说明:
1、haproxy负责WEB的请求的负载均衡, keepalived负责双线(电信/联通)VIP的切换。VIP由 keepalived控制(自己写检测脚本)。
2、FTP后端数据可以用同步软件同步,也可以共享存储,图中略了。
2、TOMCAT的SESSION 会话保持建议用REDIS或者MYSQL做,我原来公司是用MYSQL做的。熙康采用Terracotta做的,避免依赖第三方软件,直接与TOMCAT集成在一起。
如果想交流可以进群:沈阳linux运维技术群:362033008