我们都是架构师!
关注架构师(JiaGouX),添加“星标”
获取每天技术干货,一起成为牛逼架构师
技术群请加若飞:1321113940 进架构师群
投稿、合作、版权等邮箱:admin@137x.com
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65023
net.ipv4.tcp_max_syn_backlog = 10240
net.ipv4.tcp_max_tw_buckets = 400000
net.ipv4.tcp_max_orphans = 60000
net.ipv4.tcp_synack_retries = 3
net.core.somaxconn = 10000
为HAProxy创建用户和用户组,此例中用户和用户组都是"ha"。
注意,如果想要让HAProxy监听1024以下的端口,则需要以root用户来启动
下载并解压
wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.2.tar.gz
tar -xzf haproxy-1.7.2.tar.gz
make PREFIX=/home/ha/haproxy TARGET=linux2628
make install PREFIX=/home/ha/haproxy
- linux22 for Linux 2.2
- linux24 for Linux 2.4 and above (default)
- linux24e for Linux 2.4 with support for a working epoll (> 0.21)
- linux26 for Linux 2.6 and above
- linux2628 for Linux 2.6.28, 3.x, and above (enables splice and tproxy)
mkdir -p /home/ha/haproxy/conf
vi /home/ha/haproxy/conf/haproxy.cfg
global #全局属性
daemon #以daemon方式在后台运行
maxconn 256 #最大同时256连接
pidfile /home/ha/haproxy/conf/haproxy.pid #指定保存HAProxy进程号的文件
defaults #默认参数
mode http #http模式
timeout connect 5000ms #连接server端超时5s
timeout client 50000ms #客户端响应超时50s
timeout server 50000ms #server端响应超时50s
frontend http-in #前端服务http-in
bind *:8080 #监听8080端口
default_backend servers #请求转发至名为"servers"的后端服务
backend servers #后端服务servers
server server1 127.0.0.1:8000 maxconn 32 #backend servers中只有一个后端服务,名字叫server1,起在本机的8000端口,HAProxy同时最多向这个服务发起32个连接
vi /etc/init.d/haproxy
#! /bin/sh
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/ha/haproxy/sbin
PROGDIR=/home/ha/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/conf/$PROGNAME.cfg
PIDFILE=$PROGDIR/conf/$PROGNAME.pid
DESC="HAProxy daemon"
SCRIPTNAME=/etc/init.d/$PROGNAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
start()
{
echo -e "Starting $DESC: $PROGNAME\n"
$DAEMON -f $CONFIG
echo "."
}
stop()
{
echo -e "Stopping $DESC: $PROGNAME\n"
haproxy_pid="$(cat $PIDFILE)"
kill $haproxy_pid
echo "."
}
restart()
{
echo -e "Restarting $DESC: $PROGNAME\n"
$DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
echo "."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
运行
service haproxy start
service haproxy stop
service haproxy restart
global
...
log 127.0.0.1 local0 info
log 127.0.0.1 local1 warning
...
defaults
...
log global
...
vi /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
$FileCreateMode 0644 #日志文件的权限
$FileOwner ha #日志文件的owner
local0.* /var/log/haproxy.log #local0接口对应的日志输出文件
local1.* /var/log/haproxy_warn.log #local1接口对应的日志输出文件
vi /etc/sysconfig/rsyslog
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
SYSLOGD_OPTIONS="-c 2 -r -m 0"
service rsyslog restart
service haproxy restart
mkdir /root/logrotate
vi /root/logrotate/haproxy
/var/log/haproxy.log /var/log/haproxy_warn.log { #切分的两个文件名
daily #按天切分
rotate 7 #保留7份
create 0644 ha ha #创建新文件的权限、用户、用户组
compress #压缩旧日志
delaycompress #延迟一天压缩
missingok #忽略文件不存在的错误
dateext #旧日志加上日志后缀
sharedscripts #切分后的重启脚本只运行一次
postrotate #切分后运行脚本重载rsyslog,让rsyslog向新的日志文件中输出日志
/bin/kill -HUP $(/bin/cat /var/run/syslogd.pid 2>/dev/null) &>/dev/null
endscript
}
并配置在crontab中运行:
0 0 * * * /usr/sbin/logrotate /root/logrotate/haproxy
使用HAProxy搭建L7负载均衡器
ms1.srv1 - 192.168.8.111:8080
ms1.srv2 - 192.168.8.112:8080
ms2.srv1 - 192.168.8.111:8081
ms2.srv2 - 192.168.8.112:8081
def.srv1 - 192.168.8.111:8082
def.srv2 - 192.168.8.112:8082
在第一组中部署ms1/demo.html
在第二组中部署ms2/demo.html
在第三组中部署def/demo.html
Hello! This is ms1.srv1!
Hello! This is ms1.srv2!
global
daemon
maxconn 30000 #ulimit -n至少为60018
user ha
pidfile /home/ha/haproxy/conf/haproxy.pid
log 127.0.0.1 local0 info
log 127.0.0.1 local1 warning
defaults
mode http
log global
option http-keep-alive #使用keepAlive连接
option forwardfor #记录客户端IP在X-Forwarded-For头域中
option httplog #开启httplog,HAProxy会记录更丰富的请求信息
timeout connect 5000ms
timeout client 10000ms
timeout server 50000ms
timeout http-request 20000ms #从连接创建开始到从客户端读取完整HTTP请求的超时时间,用于避免类DoS攻击
option httpchk GET /healthCheck.html #定义默认的健康检查策略
frontend http-in
bind *:9001
maxconn 30000 #定义此端口上的maxconn
acl url_ms1 path_beg -i /ms1/ #定义ACL,当uri以/ms1/开头时,ACL[url_ms1]为true
acl url_ms2 path_beg -i /ms2/ #同上,url_ms2
use_backend ms1 if url_ms1 #当[url_ms1]为true时,定向到后端服务群ms1中
use_backend ms2 if url_ms2 #当[url_ms2]为true时,定向到后端服务群ms2中
default_backend default_servers #其他情况时,定向到后端服务群default_servers中
backend ms1 #定义后端服务群ms1
balance roundrobin #使用RR负载均衡算法
cookie HA_STICKY_ms1 insert indirect nocache #会话保持策略,insert名为"HA_STICKY_ms1"的cookie
#定义后端server[ms1.srv1],请求定向到该server时会在响应中写入cookie值[ms1.srv1]
#针对此server的maxconn设置为300
#应用默认健康检查策略,健康检查间隔和超时时间为2000ms,两次成功视为节点UP,三次失败视为节点DOWN
server ms1.srv1 192.168.8.111:8080 cookie ms1.srv1 maxconn 300 check inter 2000ms rise 2 fall 3
#同上,inter 2000ms rise 2 fall 3是默认值,可以省略
server ms1.srv2 192.168.8.112:8080 cookie ms1.srv2 maxconn 300 check
backend ms2 #定义后端服务群ms2
balance roundrobin
cookie HA_STICKY_ms2 insert indirect nocache
server ms2.srv1 192.168.8.111:8081 cookie ms2.srv1 maxconn 300 check
server ms2.srv2 192.168.8.112:8081 cookie ms2.srv2 maxconn 300 check
backend default_servers #定义后端服务群default_servers
balance roundrobin
cookie HA_STICKY_def insert indirect nocache
server def.srv1 192.168.8.111:8082 cookie def.srv1 maxconn 300 check
server def.srv2 192.168.8.112:8082 cookie def.srv2 maxconn 300 check
listen stats #定义监控页面
bind *:1080 #绑定端口1080
stats refresh 30s #每30秒更新监控数据
stats uri /stats #访问监控页面的uri
stats realm HAProxy\ Stats #监控页面的认证提示
stats auth admin:admin #监控页面的用户名和密码
service haproxy start
mv healthCheck.html healthCheck.html.bak
global
daemon
maxconn 30000 #ulimit -n至少为60018
user ha
pidfile /home/ha/haproxy/conf/haproxy.pid
log 127.0.0.1 local0 info
log 127.0.0.1 local1 warning
defaults
mode tcp
log global
option tcplog #开启tcplog
timeout connect 5000ms
timeout client 10000ms
timeout server 10000ms #TCP模式下,应将timeout client和timeout server设置为一样的值,以防止出现问题
option httpchk GET /healthCheck.html #定义默认的健康检查策略
frontend http-in
bind *:9002
maxconn 30000 #定义此端口上的maxconn
default_backend default_servers #请求定向至后端服务群default_servers
backend default_servers #定义后端服务群default_servers
balance roundrobin
server def.srv1 192.168.8.111:8082 maxconn 300 check
server def.srv2 192.168.8.112:8082 maxconn 300 check
balance roundrobin
balance source
check:指定此参数时,HAProxy将会对此server执行健康检查,检查方法在option httpchk中配置。同时还可以在check后指定inter, rise, fall三个参数,分别代表健康检查的周期、连续几次成功认为server UP,连续几次失败认为server DOWN,默认值是inter 2000ms rise 2 fall 3
cookie [value]:用于配合基于cookie的会话保持,如cookie ms1.srv1代表交由此server处理的请求会在响应中写入值为ms1.srv1的cookie(具体的cookie名则在backend域中的cookie设置中指定)
maxconn:指HAProxy最多同时向此server发起的连接数,当连接数到达maxconn后,向此server发起的新连接会进入等待队列。默认为0,即无限
maxqueue:等待队列的长度,当队列已满后,后续请求将会发至此backend下的其他server,默认为0,即无限
weight:server的权重,0-256,权重越大,分给这个server的请求就越多。weight为0的server将不会被分配任何新的连接。所有server默认weight为1
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
tar -xzf keepalived-1.2.19.tar.gz
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
chmod +x /etc/init.d/keepalived
mkdir -p /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
vi /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL #虚拟路由名称
}
#HAProxy健康检查配置
vrrp_script chk_haproxy {
script "killall -0 haproxy" #使用killall -0检查haproxy实例是否存在,性能高于ps命令
interval 2 #脚本运行周期
weight 2 #每次检查的加权权重值
}
#虚拟路由配置
vrrp_instance VI_1 {
state MASTER #本机实例状态,MASTER/BACKUP,备机配置文件中请写BACKUP
interface enp0s25 #本机网卡名称,使用ifconfig命令查看
virtual_router_id 51 #虚拟路由编号,主备机保持一致
priority 101 #本机初始权重,备机请填写小于主机的值(例如100)
advert_int 1 #争抢虚地址的周期,秒
virtual_ipaddress {
192.168.8.201 #虚地址IP,主备机保持一致
}
track_script {
chk_haproxy #对应的健康检查配置
}
}
yum intall psmisc
service keepalived start
ip addr sh enp0s25 (将enp0s25替换成主机的网卡名)
service haproxy stop
·END·
如果您喜欢本文,欢迎点击右上角,把文章分享到朋友圈~~
作者:kelgon
来源:https://www.jianshu.com/p/c9f6d55288c0
版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢!
我们都是架构师!
关注架构师(JiaGouX),添加“星标”
获取每天技术干货,一起成为牛逼架构师
技术群请加若飞:1321113940 进架构师群
投稿、合作、版权等邮箱:admin@137x.com