设为首页 收藏本站
查看: 1239|回复: 0

[经验分享] HAProxy杂记(1)

[复制链接]

尚未签到

发表于 2019-1-1 11:37:07 | 显示全部楼层 |阅读模式
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 "Web1" > /var/www/html/index.html
[root@master2 ~]# systemctl start httpd.service
[root@master2 ~]#
[root@master3 ~]# echo "Web2" > /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 *:80
default_backend            websrvs
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvs
balance     roundrobin
server web1 10.201.106.132:80 check
server 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 Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2017-01-15 22:30:40 CST; 5s ago
Main 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 -Ds
Jan 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.log
local2.*                                                /var/log/haproxy.log
重启日志服务:
[root@master1 ~]# systemctl restart rsyslog.service
[root@master1 ~]# systemctl status rsyslog.service
● rsyslog.service - System Logging Service
Loaded: loaded (/usr/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2017-01-15 22:47:46 CST; 13s ago
Main PID: 5073 (rsyslogd)
CGroup: /system.slice/rsyslog.service
└─5073 /usr/sbin/rsyslogd -n
Jan 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 websrvs
balance     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 websrvs
balance     uri
hash-type   consistent
重载服务:
[root@master1 haproxy]# systemctl reload haproxy.service
生成10个测试页面:
[root@master2 ~]# for i in {1..10};do echo "Page $i on Web1" > /var/www/html/test$i.html;done
[root@master3 ~]# for i in {1..10};do echo "Page $i on Web2" > /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
Page 2 on Web1
[root@master3 ~]# curl http://10.201.106.131/test2.html
Page 2 on Web1
[root@master3 ~]# curl http://10.201.106.131/test2.html
Page 2 on Web1
[root@master3 ~]# curl http://10.201.106.131/test2.html
Page 2 on Web1
[root@master3 ~]# curl http://10.201.106.131/test2.html
Page 2 on Web1
[root@master3 ~]#
[root@master3 ~]# curl http://10.201.106.131/test5.html
Page 5 on Web1
[root@master3 ~]# curl http://10.201.106.131/test5.html
Page 5 on Web1
[root@master3 ~]# curl http://10.201.106.131/test5.html
Page 5 on Web1
[root@master3 ~]# curl http://10.201.106.131/test6.html
Page 6 on Web1
[root@master3 ~]# curl http://10.201.106.131/test6.html
Page 6 on Web1
[root@master3 ~]# curl http://10.201.106.131/test6.html
Page 6 on Web1
[root@master3 ~]# curl http://10.201.106.131/test8.html
Page 8 on Web1
[root@master3 ~]# curl http://10.201.106.131/test8.html
Page 8 on Web1
[root@master3 ~]# curl http://10.201.106.131/test9.html
Page 9 on Web2
[root@master3 ~]# curl http://10.201.106.131/test9.html
Page 9 on Web2
[root@master3 ~]# curl http://10.201.106.131/test3.html
Page 3 on Web2
[root@master3 ~]# curl http://10.201.106.131/test3.html
Page 3 on Web2
[root@master3 ~]# curl http://10.201.106.131/test4.html
Page 4 on Web2
[root@master3 ~]# curl http://10.201.106.131/test4.html
Page 4 on Web2
[root@master3 ~]# curl http://10.201.106.131/test8.html
Page 8 on Web1
[root@master3 ~]# curl http://10.201.106.131/test8.html
Page 8 on Web1
[root@master3 ~]#
hdr调度算法

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvs
balance     hdr(User-Agent)
hash-type   consistent
通过curl模拟别的浏览器访问:
[root@master3 ~]# curl -A 'hello' http://10.201.106.131/test4.html
Page 4 on Web2
[root@master3 ~]#
[root@master3 ~]# curl -A 'IE' http://10.201.106.131/test4.html
Page 4 on Web2
[root@master3 ~]# curl -A 'IE' http://10.201.106.131/test4.html
Page 4 on Web2
[root@master3 ~]# curl -A '360' http://10.201.106.131/test4.html
Page 4 on Web2
[root@master3 ~]# curl -A '360' http://10.201.106.131/test4.html
设置监听多个端口

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main
bind *:80  
bind *:8080
default_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 websrvs
balance     roundrobin
server web1 10.201.106.132:80 check weight 1
server web2 10.201.106.133:80 check weight 3
[root@master1 haproxy]# systemctl reload haproxy.service
测试访问网页结果是,访问3次web2,才访问一次web1;



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-658226-1-1.html 上篇帖子: Haproxy 线上配置文档 下篇帖子: haproxy调度算法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表