HAProxy

haproxy基础

1、安装haproxy

[root@master1 ~]# yum -y install haproxy
[root@master2 ~]# yum -y install haproxy查看haproxy生成的文件 :
[root@master1 ~]# rpm -ql haproxy备份配置文件:
[root@master1 haproxy]# cp haproxy.cfg{,.back}
[root@master1 haproxy]# ls
haproxy.cfg  haproxy.cfg.back

haproxy演示

实验环境:1台haproxy,2台httpd

1、两台网页服务器安装httpd

[root@master2 ~]# yum install -y httpd
[root@master3 ~]# yum install -y httpd设置首页,启动服务:
[root@master2 ~]# echo "<h1>Web1</h1>" > /var/www/html/index.html
[root@master2 ~]# systemctl start httpd.service
[root@master2 ~]#[root@master3 ~]# echo "<h1>Web2</h1>" > /var/www/html/index.html
[root@master3 ~]# systemctl start httpd.service
[root@master3 ~]# 

2、配置haproxy文件,将用户请求转发到后端去

[root@master1 haproxy]# vim haproxy.cfg#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80default_backend            websrvs#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     roundrobinserver web1 10.201.106.132:80 checkserver web2 10.201.106.133:80 check
~          启动服务:
[root@master1 haproxy]# systemctl start haproxy.service
[root@master1 haproxy]# systemctl status haproxy.service
● haproxy.service - HAProxy Load BalancerLoaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)Active: active (running) since Sun 2017-01-15 22:30:40 CST; 5s agoMain PID: 4924 (haproxy-systemd)CGroup: /system.slice/haproxy.service├─4924 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid├─4925 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds└─4926 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -DsJan 15 22:30:40 master1.com systemd[1]: Started HAProxy Load Balancer.
Jan 15 22:30:40 master1.com systemd[1]: Starting HAProxy Load Balancer...
Jan 15 22:30:40 master1.com haproxy-systemd-wrapper[4924]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
[root@master1 haproxy]# 访问:http://10.201.106.131/
可以在两个web站点轮流跳转

3、健康状态监测测试

3.1 停止一个web节点服务

[root@master2 ~]# systemctl stop httpd现在只能访问剩下的那个节点了;

4、开启日志功能

4.1 开启本地日志服务

配置日志文件:
[root@master1 ~]# vim /etc/rsyslog.conf # Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514# Save boot messages also to boot.loglocal2.*                                                /var/log/haproxy.log重启日志服务:
[root@master1 ~]# systemctl restart rsyslog.service
[root@master1 ~]# systemctl status rsyslog.service
● rsyslog.service - System Logging ServiceLoaded: loaded (/usr/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)Active: active (running) since Sun 2017-01-15 22:47:46 CST; 13s agoMain PID: 5073 (rsyslogd)CGroup: /system.slice/rsyslog.service└─5073 /usr/sbin/rsyslogd -nJan 15 22:47:46 master1.com systemd[1]: Starting System Logging Service...
Jan 15 22:47:46 master1.com systemd[1]: Started System Logging Service.
[root@master1 ~]# 查看是否监听UDP 514端口:
[root@master1 ~]# ss -unlp
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port
UNCONN      0      0                                                       *:40858                                                               *:*                   users:(("haproxy",pid=4926,fd=6),("haproxy",pid=4925,fd=6))
UNCONN      0      0                                                       *:514                                                                 *:*                   users:(("rsyslogd",pid=5104,fd=3))
UNCONN      0      0                                                      :::514                                                                :::*                   users:(("rsyslogd",pid=5104,fd=4))
[root@master1 ~]# 

4.2 查看haproxy日志

[root@master1 ~]# tail /var/log/haproxy.log Feb  5 23:21:29 localhost haproxy[4926]: Server websrvs/web1 is UP, reason: Layer4 check passed, check duration: 0ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
Feb  5 23:21:31 localhost haproxy[4926]: 10.201.106.1:56402 [05/Feb/2017:23:21:31.495] main websrvs/web2 5/0/9/2/16 304 141 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Feb  5 23:21:32 localhost haproxy[4926]: 10.201.106.1:56402 [05/Feb/2017:23:21:31.512] main websrvs/web1 727/0/1/4/732 200 273 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Feb  5 23:21:32 localhost haproxy[4926]: 10.201.106.1:56402 [05/Feb/2017:23:21:32.243] main websrvs/web2 738/0/3/6/747 200 273 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Feb  5 23:21:33 localhost haproxy[4926]: 10.201.106.1:56402 [05/Feb/2017:23:21:32.989] main websrvs/web1 572/0/1/2/575 200 273 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
[root@master1 ~]# 

haproxy 高级配置

修改haproxy调度算法为source

vim haproxy.cfg
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     source重载服务:
[root@master1 haproxy]# systemctl reload haproxy.service访问测试后:只在一个web上面停留了,不会跳转至另一个web;

修改为uri算法

[root@master1 haproxy]# vim haproxy.cfg#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     urihash-type   consistent重载服务:
[root@master1 haproxy]# systemctl reload haproxy.service生成10个测试页面:
[root@master2 ~]# for i in {1..10};do echo "<h1>Page $i on Web1</h1>" > /var/www/html/test$i.html;done[root@master3 ~]# for i in {1..10};do echo "<h1>Page $i on Web2</h1>" > /var/www/html/test$i.html;done
[root@master3 ~]#
[root@master3 ~]# ls /var/www/html/
index.html   test1.html  test3.html  test5.html  test7.html  test9.html
test10.html  test2.html  test4.html  test6.html  test8.html访问测试:http://10.201.106.131/test2.html
只固定在一个web服务器上;[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]#
[root@master3 ~]# curl http://10.201.106.131/test5.html
<h1>Page 5 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test5.html
<h1>Page 5 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test5.html
<h1>Page 5 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test6.html
<h1>Page 6 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test6.html
<h1>Page 6 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test6.html
<h1>Page 6 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test8.html
<h1>Page 8 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test8.html
<h1>Page 8 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test9.html
<h1>Page 9 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test9.html
<h1>Page 9 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test3.html
<h1>Page 3 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test3.html
<h1>Page 3 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test8.html
<h1>Page 8 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test8.html
<h1>Page 8 on Web1</h1>
[root@master3 ~]# 

hdr调度算法

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     hdr(User-Agent)hash-type   consistent通过curl模拟别的浏览器访问:
[root@master3 ~]# curl -A 'hello' http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]#
[root@master3 ~]# curl -A 'IE' http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl -A 'IE' http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl -A '360' http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl -A '360' http://10.201.106.131/test4.html

设置监听多个端口

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  mainbind *:80  bind *:8080default_backend            websrvs重启服务:
[root@master1 haproxy]# systemctl restart haproxy.service
[root@master1 haproxy]# ss -tnl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128           *:8080                      *:*
LISTEN      0      128           *:80                        *:*
LISTEN      0      128           *:22                        *:*
LISTEN      0      100    127.0.0.1:25                        *:*
LISTEN      0      128          :::22                       :::*
LISTEN      0      100         ::1:25                       :::*
[root@master1 haproxy]# 

修改权重测试

[root@master1 haproxy]# vim haproxy.cfg#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     roundrobinserver web1 10.201.106.132:80 check weight 1server web2 10.201.106.133:80 check weight 3[root@master1 haproxy]# systemctl reload haproxy.service测试访问网页结果是,访问3次web2,才访问一次web1;

转载于:https://blog.51cto.com/zhongle21/2087359

HAProxy杂记(1)相关推荐

  1. Haproxy Nginx cluster构建

    -----client---------haproxy-------nginx1---------nginx2------ 192.168.1.250 192.168.1.1 192.168.1.10 ...

  2. [原创]商城系统下单库存管控系列杂记(二)(并发安全和性能部分延伸)

      商城系统下单库存管控系列杂记(二)(并发安全和性能部分延伸)     前言   参与过几个中小型商城系统的开发,随着时间的增长,以及对系统的深入研究和测试,发现确实有很多值得推敲和商榷的地方(总有 ...

  3. redis sentinel集群配置及haproxy配置

    ip分布情况: sentinel-1/redis 主 10.11.11.5 sentinel-2/redis 从 10.11.11.7 sentinel-3/redis 从 10.11.11.8 ha ...

  4. 负载均衡工具haproxy安装,配置,使用

    一,什么是haproxy HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点 ...

  5. 千万级并发HAproxy均衡负载系统介绍

    Haproxy介绍及其定位 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.根据官方数据,其最高极限支持10G的并发. HA ...

  6. Linux自学笔记——haproxy

    HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠地一套解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持 ...

  7. 烂泥:haproxy学习之手机规则匹配

    2019独角兽企业重金招聘Python工程师标准>>> 本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 今天我们来介 ...

  8. haproxy ssl_我们如何微调HAProxy以实现2,000,000个并发SSL连接

    haproxy ssl by Sachin Malhotra 由Sachin Malhotra 我们如何微调HAProxy以实现2,000,000个并发SSL连接 (How we fine-tuned ...

  9. saltstack实现haproxy+keepalived负载均衡+高可用(二)

    一键部署haproxy+keepalived实现负载均衡+高可用 实验环境: !!!!    特别注意: www.westos.org为test1的minion名字 test1: 172.25.1.1 ...

最新文章

  1. 异步通知是什么意思_一次相亲经历,我彻底搞懂了阻塞非阻塞、同步异步
  2. handlerinterceptoradapter 获取请求参数_SSM框架防止重复请求
  3. 1、数据库为什么需要备份?
  4. ios开发 UITableView with xib 以及自定义TableViewCell
  5. PyTorch基础(12)-- torch.nn.BatchNorm2d()方法
  6. Android socket 学习记录 之 执行new socket(ip, port)程序崩溃
  7. js/vue 高德地图绘制驾车路线图
  8. CSS中em和px单位的区别(转)
  9. 台式机电脑配置单_2020年电脑配置单重点硬件参考
  10. java重命名package_AndroidStudio怎么重命名java目录下的包名(如cn.zsn.app)
  11. go的优势--链表与结构体使用
  12. 0基础学python做什么工作好-零基础学了8个月的Python,到底有啥感悟
  13. 电脑文件同步备份软件哪个好用?
  14. CMOS和TTL的区别?
  15. Bootstrap学习笔记02
  16. xp隐藏桌面计算机图标不见了怎么办,XP系统桌面IE图标不见了怎么办?IE图标消失了怎么恢复?...
  17. 我在 B 站挖到了 9 款深藏不露的工具,每个都好用到爆!
  18. C/C++读取txt
  19. 开发者能力大赏,谁是技术知识达人?
  20. 中国最大在线保健品供应商“健康中国”停业

热门文章

  1. 服务器pg信号指的是什么信号,关于atx电源PG信号检测和分析
  2. html 浮动窗口置顶,jQuery简单实现页面元素置顶时悬浮效果示例
  3. 关于“三门问题”的一些想法
  4. leetcode132. 分割回文串 II
  5. leetcode15 三数之和
  6. python基础技巧总结(三)
  7. gdb调试多进程和多线程命令
  8. 深度学习(10)-- Capsules Networks(CapsNet)
  9. php ajax队列,AJAX请求队列实现
  10. STL源码剖析 __type_traits