学习Haproxy (七)
haproxy是个高性能的tcp和http的反向代理。它就是个代理。不像nginx还做web服务器
nginx的优点和缺点
1
2
3
4
5
6
7
8
9
10
11
|
优点: 1、web服务器,应用比较广泛,大家都会 2、可以作为7层负载均衡,location设置复杂的基于HTTP的负载均衡 3、性能强大,网络依赖小 4、安装配置简单 缺点: 1、健康检查单一,不支持基于url的健康检查(可以使用第三方插件实现) 2、负载均衡算法少 3、不能动态管理,比如踢出某个web节点,需要reload配置 4、没有集群upstream的状态页面 |
haproxy的优点和缺点
1
2
3
4
5
6
7
8
9
10
11
|
优点: 1、专门做反向代理负载均衡 2、负载均衡算法比较多,大于等于8种,比nginx丰富 3、性能不低于nginx,大于等于nginx 4、支持动态管理,通过和haproxy的sock进行通信,可以进行管理 5、有比较丰富的Dashboard的页面,监控方便。有管理页面 6、比较强大的7层反向代理功能,在7层方便,功能强大 7、会话保持比nginx丰富。可以基于cookie和源IP(nginx也能做到基于IP和cookie) 缺点: 配置没有Nginx简单(相对熟悉) |
先杀掉原先的nginx进程,防止80端口被占用,导致haproxy无法启动
1
2
3
4
|
[root@linux-node1 conf] # pkill nginx [root@linux-node1 conf] # ps aux | grep nginx root 27201 0.0 0.0 112664 972 pts /0 S+ 05:39 0:00 grep --colour=auto nginx [root@linux-node1 conf] # |
部署haproxy,这里是编译安装,版本是1.6.3,执行命令如下
1
2
3
4
5
6
7
8
|
cd /usr/local/src/ wget http: //www .haproxy.org /download/1 .6 /src/haproxy-1 .6.3. tar .gz tar xfz haproxy-1.6.3. tar .gz cd haproxy-1.6.3 make TARGET=linux2628 PREFIX= /usr/local/haproxy-1 .6.3 make install cp /usr/local/sbin/haproxy /usr/sbin/ haproxy - v |
1
2
3
4
5
|
[root@linux-node1 haproxy-1.6.3] # haproxy -v HA-Proxy version 1.6.3 2015 /12/25 Copyright 2000-2015 Willy Tarreau <willy@haproxy.org> [root@linux-node1 haproxy-1.6.3] # |
1
2
3
4
5
6
7
8
|
[root@linux-node1 haproxy-1.6.3] # pwd /usr/local/src/haproxy-1 .6.3 [root@linux-node1 haproxy-1.6.3] # cd examples/ [root@linux-node1 examples] # ls haproxy.init haproxy.init [root@linux-node1 examples] # cp haproxy.init /etc/init.d/haproxy [root@linux-node1 examples] # chmod +x /etc/init.d/haproxy [root@linux-node1 examples] # |
创建haproxy用户和相关目录
useradd -r表示创建系统账号
1
2
3
4
5
|
[root@linux-node1 examples] # useradd -r haproxy [root@linux-node1 examples] # [root@linux-node1 examples] # mkdir /etc/haproxy -p [root@linux-node1 examples] # mkdir /var/lib/haproxy -p [root@linux-node1 examples] # |

重启rsyslog
1
2
3
4
5
6
|
[root@linux-node1 ~] # vim /etc/rsyslog.conf [root@linux-node1 ~] # systemctl restart rsyslog [root@linux-node1 ~] # netstat -lnup | grep 514 udp 0 0 0.0.0.0:514 0.0.0.0:* 27509 /rsyslogd udp6 0 0 :::514 :::* 27509 /rsyslogd [root@linux-node1 ~] # |
关于mode http 你如果不写,默认继承defaults里面的
defaults默认不写好像也是http。
tcp的需要注明。
mode tcp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[root@linux-node1 ~] # cd /etc/haproxy/ [root@linux-node1 haproxy] # vim haproxy.cfg [root@linux-node1 haproxy] # cat haproxy.cfg global chroot /var/lib/haproxy daemon group haproxy user haproxy log 127.0.0.1:514 local3 info defaults log global #使用全局的日志配置 mode http option httplog option dontlognull #日志中不记录空连接,比如不记录健康检查的连接 timeout client 50000 timeout server 50000 timeout connect 5000 frontend http_front bind *:80 stats uri /haproxy ?stats default_backend http_back backend http_back balance roundrobin server linux-node1 10.0.1.105:8080 check server linux-node2 10.0.1.106:8080 check [root@linux-node1 haproxy] # |
启动haproxy
1
2
3
4
5
6
7
8
|
[root@linux-node1 ~] # /etc/init.d/haproxy start Reloading systemd: [ 确定 ] Starting haproxy (via systemctl): [ 确定 ] [root@linux-node1 ~] # [root@linux-node1 ~] # netstat -lntp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27556 /haproxy tcp6 0 0 :::8080 :::* LISTEN 20130 /httpd [root@linux-node1 ~] # |
1
2
3
4
5
|
[root@linux-node1 ~] # grep local3 /etc/rsyslog.conf local3.* /var/log/haproxy .log [root@linux-node1 ~] # [root@linux-node1 ~] # systemctl restart rsyslog [root@linux-node1 ~] # |
再次重启haproxy服务,就可以看到haproxy的日志文件生成了。可以看到启动过程
1
2
3
4
5
6
7
8
9
|
[root@linux-node1 ~] # /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ 确定 ] [root@linux-node1 ~] # tail -f /var/log/haproxy.log Feb 27 06:33:43 localhost haproxy[27648]: Stopping frontend http_front in 0 ms. Feb 27 06:33:43 localhost haproxy[27648]: Stopping backend http_back in 0 ms. Feb 27 06:33:43 localhost haproxy[27648]: Proxy http_front stopped (FE: 0 conns, BE: 0 conns). Feb 27 06:33:43 localhost haproxy[27648]: Proxy http_back stopped (FE: 0 conns, BE: 0 conns). Feb 27 06:33:43 localhost haproxy[27687]: Proxy http_front started. Feb 27 06:33:43 localhost haproxy[27687]: Proxy http_back started. |
继续优化更改下配置
haproxy可以自定义健康检查的url,这是nginx不具备的
check:启用健康检测
inter:健康检测间隔
rise:检测服务可用的连续次数
fall:检测服务不可用的连续次数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[root@linux-node1 ~] # cd /etc/haproxy/ [root@linux-node1 haproxy] # vim haproxy.cfg [root@linux-node1 haproxy] # cat haproxy.cfg global chroot /var/lib/haproxy daemon group haproxy user haproxy log 127.0.0.1:514 local3 info defaults log global mode http option httplog option dontlognull timeout client 50000 timeout server 50000 timeout connect 5000 frontend http_front mode http bind *:80 stats uri /haproxy ?stats default_backend http_back backend http_back option httpchk GET /index .html balance roundrobin server linux-node1 10.0.1.105:8080 check inter 2000 rise 3 fall 3 weight 1 server linux-node2 10.0.1.106:8080 check inter 2000 rise 3 fall 3 weight 1 [root@linux-node1 haproxy] # |
重启服务
1
2
3
4
5
6
|
[root@linux-node1 haproxy] # /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ 确定 ] [root@linux-node1 haproxy] # netstat -lntp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27849 /haproxy tcp6 0 0 :::8080 :::* LISTEN 20130 /httpd [root@linux-node1 haproxy] # |

页面测试,目前也是轮询的
多访问几次,健康页面有新的数据变化
sessions这里可以看到有没有失败的访问

结合haproxy的acl配置反向代理功能,先备份原先配置文件
设置acl
这样能支持多个域名,让不同的域名,访问不同的backend上面去
1
2
|
[root@linux-node1 conf] # cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.ori [root@linux-node1 conf] # vim /etc/haproxy/haproxy.cfg |
修改配置文件为如下
注意,配置文件中,前端和后端不要用特殊符号以及点。它对这些敏感。推荐使用下划线
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
[root@linux-node1 conf] # vim /etc/haproxy/haproxy.cfg [root@linux-node1 conf] # cat /etc/haproxy/haproxy.cfg global chroot /var/lib/haproxy daemon group haproxy user haproxy log 127.0.0.1:514 local3 info stats socket /var/lib/haproxy/haproxy .sock mode 600 level admin stats timeout 2m defaults log global mode http option httplog option dontlognull timeout client 50000 timeout server 50000 timeout connect 5000 frontend www_nmap_com mode http bind *:80 stats uri /haproxy ?stats default_backend www_nmap_com_backend acl is_other_nmap_com hdr_end(host) other.nmap-blog.com use_backend other_nmap_com_backend if is_other_nmap_com backend www_nmap_com_backend option forwardfor header X-REAL-IP option httpchk GET /index .html balance roundrobin server linux-node1 10.0.1.105:8080 check inter 2000 rise 3 fall 3 weight 1 backend other_nmap_com_backend option forwardfor header X-REAL-IP option httpchk GET /index .html balance roundrobin server linux-node2 10.0.1.106:8080 check inter 2000 rise 3 fall 3 weight 1 [root@linux-node1 conf] # |
重启haproxy
1
2
|
[root@linux-node1 conf] # /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ 确定 ] |
windows客户端配置host文件
1
|
10.0.1.105 www.nmap-blog.com other.nmap-blog.com |
这样也实现了haproxy的多域名反向代理
haproxy的acl,也可以根据正则,和后缀设置,下面2种方法。推荐第一种,正则方式匹配
1
2
|
acl is_static_reg url_reg /*.(css|jpg|png|js|jpeg|gif)$ acl is_static_path path_end .gif .png .js |
修改配置文件做基于正则的acl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
[root@linux-node1 conf] # cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.2 [root@linux-node1 conf] # vim /etc/haproxy/haproxy.cfg [root@linux-node1 conf] # cat /etc/haproxy/haproxy.cfg global chroot /var/lib/haproxy daemon group haproxy user haproxy log 127.0.0.1:514 local3 info stats socket /var/lib/haproxy/haproxy .sock mode 600 level admin stats timeout 2m defaults log global mode http option httplog option dontlognull timeout client 50000 timeout server 50000 timeout connect 5000 frontend www_nmap_com mode http bind *:80 stats uri /haproxy ?stats default_backend www_nmap_com_backend acl is_static_reg url_reg /*.(css|jpg|png|js|jpeg|gif)$ use_backend other_nmap_com_backend if is_static_reg #acl is_static_path path_end .gif .png .js #acl is_other_nmap_com hdr_end(host) other.nmap-blog.com #use_backend other_nmap_com_backend if is_other_nmap_com backend www_nmap_com_backend option forwardfor header X-REAL-IP option httpchk GET /index .html balance roundrobin server linux-node1 10.0.1.105:8080 check inter 2000 rise 3 fall 3 weight 1 backend other_nmap_com_backend option forwardfor header X-REAL-IP option httpchk GET /index .html balance roundrobin server linux-node2 10.0.1.106:8080 check inter 2000 rise 3 fall 3 weight 1 [root@linux-node1 conf] # |
重启服务
1
2
3
4
5
6
|
[root@linux-node1 conf] # /etc/init.d/haproxy restart Restarting haproxy (via systemctl): [ 确定 ] [root@linux-node1 conf] # lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE /OFF NODE NAME haproxy 48521 haproxy 5u IPv4 1371377 0t0 TCP *:http (LISTEN) [root@linux-node1 conf] # |
因为匹配后会连接到node2,这里就在node2上设置一个js文件,node1不做任何设置。
1
2
|
[root@linux-node2 ~] # echo 'test111' >/var/www/html/test.js [root@linux-node2 ~] # |
测试成功
关于后端web节点记录检查日志的问题,因为我设置检查check inter 2000 ,也就是2秒发一次检查包。后端节点日志这里也能看到
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@linux-node2 ~] # tail -f /var/log/httpd/access_log 10.0.1.105 - - [04 /Mar/2017 :00:35:46 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:35:48 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:35:50 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:35:52 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:35:54 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:35:56 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:35:58 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:36:00 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:36:02 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:36:04 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:36:06 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" 10.0.1.105 - - [04 /Mar/2017 :00:36:08 +0800] "GET /index.html HTTP/1.0" 200 24 "-" "-" |
关于怎么让后端apache不记录健康检查日志,以及如何记录真正的客户端IP,这里不做实验。
学习Haproxy (七)的更多相关文章
- C语言学习 第七次作业总结
C语言学习 第七次作业总结 数组可以分为数组和多下标数组(在传统的国内C语言书本中,将其称为二/多维数组). 数组名称 在之前的课程中,大家应该都有印象,对于int a这样的定义,会为变量 a 声明一 ...
- 前端学习 第七弹: Javascript实现图片的延迟加载
前端学习 第七弹: Javascript实现图片的延迟加载 为了实现图片进入视野范围才开始加载首先: <img src="" x-src="/acsascas ...
- MyBatis学习总结(七)——Mybatis缓存(转载)
孤傲苍狼 只为成功找方法,不为失败找借口! MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的 ...
- 八、Android学习第七天——XML文件解析方法(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 八.Android学习第七天——XML文件解析方法 XML文件:exten ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- 【转】MyBatis学习总结(七)——Mybatis缓存
[转]MyBatis学习总结(七)——Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualC ...
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
- python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍
目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...
- Python学习第七课
Python学习第七课 'Alex' "Alex"print('hello'*5) #重复输出字符串 print('hellowold'[2:]) #类似于切片操作:会取出 llo ...
- Typescript 学习笔记七:泛型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
随机推荐
- Python 单例模式的几种实现方式
单例模式的几种实现方式 先来看几个魔法方法的简单运用:__new__, __init__, __call__. class A(object): def __init__(self, x): prin ...
- iOS桌面小插件 Widget Extension
iOS桌面小插件 Widget Extension 这个插件时iOS14以后才出现的,基于SwiftUI 旧项目新建时可能一堆错误,其中一个时要把插件target 开发sdk版本设置为14.0以上 新 ...
- 第一次尝试3D建模和打印:色子
打印机前几天到了,除了打印了一半自带demo之外,还没开始打过啥模型.想自己从头打印个东西.那就做个色子吧. 因为机子上有SW2020,参考这个 solidworks2017怎么建模骰子? 教程来练习 ...
- CentOS8安装Geant4笔记(一):Geant4介绍、编译和安装
前言 在服务器CentOS8.2上安装geant4软件. GEANT4 介绍 Geant4 是一个用于模拟粒子穿过物质的工具包.其应用领域包括高能.核物理和加速器物理,以及医学和空间科学研 ...
- [k8s] k8s基于csi使用rbd存储
描述 ceph-csi扩展各种存储类型的卷的管理能力,实现第三方存储ceph的各种操作能力与k8s存储系统的结合.通过 ceph-csi 使用 ceph rbd块设备,它动态地提供rbd以支持 Kub ...
- 《Shader入门精要》第11章-11.3.1流动的河流中的offset.x的解释
在我学习入门精要的时候,经常遇到不解释api,甚至是关键代码的实现原理. 11.3.1流动的河流中的offset.x的sin函数查了一下好像大家也都是书上原话直接复制,现在好不容易想明白了希望能帮到和 ...
- ## [湖南省赛2019]Findme ###
[湖南省赛2019]Findme 1.题目概述 2.解题过程 010打开这几张图片 先简单分析一下这几张图片 简单分析 1.png 从外观上,1.png明显高度太低,需要更改 2.png 2.png末 ...
- 前端经典面试题vue面试题
1.什么是MVVM? MVVM是一种设计思想. Model 层代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑: View 代表UI 组件,它负责将数据模型转化成UI 展现出来,View ...
- 阿里一面,说说你对Mysql死锁的理解
又到了金三银四的时候,大家都按耐不住内心的躁动,我在这里给大家分享下之前面试中遇到的一个知识点(死锁问题),如有不足,欢迎大佬们指点指点. 1.什么是死锁? 死锁指的是在两个或两个以上不同的进程或线程 ...
- kubernetes内yaml格式
yaml格式的pod定义文件完整内容: apiVersion: v1 #必选,版本号,例如v1 可通过 kubectl api-versions 获取 kind: Pod #必选,Pod metada ...