一、理论部分
1.haproxy与lvs的区别:lvs负载能力更强、性能好但是搭建相对复杂;haproxy可以补充nginx的一些缺点,nginx的upstream模块支持群集功能但是对健康检查的功能不强性能没有haproxy好
2.http请求方式有post和get post的方式更安全
3.常用的调度算法
RR 轮询:第一个A 第二个B 第三个C
LC 最小连接:将请求分配给来连接点最小的,如当前个节点的连接数为:A:4 B:5 C:6 则第一个给A第二个给A 第三个给B 第四个给A
SH 基于来源访问调度算法,使用基于IP的调度算法,如第一个用户的访问指派到A 则第一个用户的所有访问都会交给A
4.web集群器 分为软件(LVS haproxy nginx)硬件 (F5 梭子鱼 绿盟)
二、实验使用一台haproxy 两台nginx,搭建方法如下
安装nginx
1.yum -y install pcre-devel zlib-devel gcc gcc-c++
2.useradd -M -s /sbin/nologin nginx
3.tar zxvf nginx-1.12.0.tar.gz
4. cd nginx-1.12.0
5./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
6.make && make install
7.vim /usr/local/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>aaaaaaaaaa.com</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
8./usr/local/nginx/sbin/nginx //开启服务
访问效果如下
第二台重复以上步骤
访问效果如下
haproxy的配置
- tar zxvf haproxy-1.5.19.tar.gz
- cd haproxy-1.5.19/
- make TARGET=linux26 //64位的操作系统
- make install
- mkdir /etc/haproxy
- cp examples/haproxy.cfg /etc/haproxy/
- vim /etc/haproxy/haproxy.cfg
global //全局配置
log /dev/log local0 info //local0为日志设备默认存放在系统日志
log /dev/log local0 notice //将info和notice的日志分别记录到不同的日志文件中 notice为日志级别
#log loghost local0 info
maxconn 4096 //最大连接数defaults中的最大连接数不能超过global中定义的
uid 99
gid 99
daemon //守护进程模式
#debug
#quiet
defaults
log global //定义日志为global配置中的日志记录
mode http
option httplog //采用http日志模式记录
option dontlognull
retries 3 //检查节点服务器失败的次数连续达到失败三次则认为节点不可用
redispatch //当服务器负载很高时,自动结束当队列处理久的连接
maxconn 2000 //最大连接数
contimeout 5000 //连接超时时间
clitimeout 50000 //客户端超时时间
srvtimeout 50000 //服务器超时时间
listen webcluster 0.0.0.0:80 //定义一个webcluster的应用
option httpchk GET /index.html //检查服务器的index.html文件
balance roundrobin //采用轮询算法
server inst1 192.168.168.131:80 check inter 2000 fall 3
server inst2 192.168.168.132:80 check inter 2000 fall 3
- cp ~/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
- ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
- chmod +x /etc/init.d/haproxy
- chkconfig --add /etc/init.d/haproxy
- /etc/init.d/haproxy start
创建haproxy日志
-
touch /etc/rsyslog.d/haproxy.conf
-
vim /etc/rsyslog.d/haproxy.conf 2.vim /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log & ~ //表示当日志写入日志文件后rsyslog停止处理这个信息 if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log & ~
-
systemctl restart rsyslog
-
/etc/init.d/haproxy restart
访问
刷新
haproxy搭建完成
tailf /var/log/haproxy/haproxy-info.log //查看haproxy日志
Haproxy性能优化