HAProxy杂记(1)

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/452354.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

编解码标准H264 与 AVS 变换矩阵比较

在编解码中&#xff0c;变换是最重要的一步&#xff0c;从开始的模拟离散变换&#xff0c;到现在国际和中国标准中的整数变换&#xff0c;变换取的压缩是最重要的&#xff0c;在 DV等其他编解码中&#xff0c;只使用变换进行压缩&#xff0c; 下面对H264 和AVS使用的变换矩阵进…

计算机图画大赛作品六年级,打字能手显本领,电脑绘画展风采——记陆埠二小举行电脑绘画和电脑打字比赛...

为了提高小学生的计算机应用水平&#xff0c;培养学生动手能力和综合素质&#xff0c;提升学生的信息素养&#xff0c;2019年5月23日、24日中午&#xff0c;陆埠镇第二小学举行了三四年级电脑打字和五六年级电脑绘画比赛。本次比赛&#xff0c;3--6年级每班中选出3名学生参加&a…

数据库角色

数据库角色&#xff1a;被命名的一组与数据库操作相关的权限1.角色是权限的集合 2.可以为一组具有相同权限的用户创建一个角色 3.简化授权的过程 一个角色的权限&#xff1a;直接授予这个角色的全部权限加上其他角色 授予这个角色的全部权限

变量在原型链中的查找顺序

js原型链 下面是一道js题目&#xff1a;[javascript] view plaincopy function C1(name){ if(name){ this.name name; } } function C2(name){ this.name name; } function C3(name){ this.name name || "John"; } C1.p…

基于SpringBoot + Vue的图书管理系统

功能概述 该图书管理系统提供了一系列功能&#xff0c;包括图书管理、图书类型管理、读者借阅归还图书、用户管理和重置密码等。 在图书管理功能中&#xff0c;管理员可以方便地进行图书信息的管理。他们可以添加新的图书记录&#xff0c;包括书名、作者、出版社、ISBN等信息&a…

交换机的工作转发原理

交换机通常是运行在网络OSI七层模型的第二层数据链路层&#xff0c;如图中&#xff0c;第三层网络层通常是路由器运行在该层 今天我们来看看&#xff0c;交换机的工作转发原理是什么样的。 交换机既然是利用端口进行网络数据传输&#xff0c;那么它是如何识别数据是谁给谁的呢…

[UWP小白日记-14]正则表达式

原文:[UWP小白日记-14]正则表达式匹配2位浮点数&#xff1a; ^(([1-9][0-9]*\.{1}[0-9]{1,2})|([0]\.{1}[1-9][0-9]{1,2})|([0]\.\d{1,2})|([1-9][0-9]{1,2})|[1-9]\d*|([0][.][0-9][1-9]{1,2}))$

视图机制对于数据库的安全意义

视图机制可以把要保密的数据对无权存取这些数据的用户隐藏起来&#xff0c;对数据提供一定程度的安全保护&#xff0c;间接地实现支持存取谓词的用户权限定义。

2017计算机考试题上机,2017年计算机二级上机考试试题及答案

2017年计算机二级上机考试试题及答案20世纪60年代中期之前的第一代计算机网络是以单个计算机为中心的远程联机系统。下面是小编整理的关于计算机二级上机考试试题&#xff0c;希望大家认真练习!1[单选题] 一棵二叉树中共有80个叶子结点与70个度为1的结点&#xff0c;则该二叉树…

八个最好的轻量级Linux发行版

如果你在苦恼老旧的硬件无法利用&#xff0c;如果你想要一个能够在不是很大的记忆棒上运行的系统&#xff0c;如果你想要在桌面端上运行200个虚拟机&#xff0c;那么你可以考虑一些“迷你”的Linux发行版。 曾经在08年介绍过当时的十大轻量级Linux&#xff0c;现在已经是2010年…

面向对象——三层架构(表现层、业务层、持久层)

① 持久层&#xff1a;采用DAO模式&#xff0c;建立实体类和数据库表映射&#xff08;ORM映射&#xff09;。也就是哪个类对应哪个表&#xff0c;哪个属性对应哪个列。持久层 的目的就是&#xff0c;完成对象数据和关系数据的转换。 ② 业务层&#xff1a;采用事务脚本模式。将…

VMware安装Centos7后有线线缆被拔出

背景&#xff1a;在win10 系统中的虚机软件VMware Workstation中安装CentOS7桌面版&#xff0c;安装过程中没有设置网络 1.确认你win10系统打开了这两个服务&#xff1a;VMware DHCP Service和VMware NAT Service 方法&#xff1a;电脑——右键——管理——服务和应用程序——服…

SpringCloud |第二篇: 服务消费者(Ribbon)

2019独角兽企业重金招聘Python工程师标准>>> 一、Ribbon简介 Ribbon是Netflix发布的开源项目&#xff0c;主要功能是提供客户端的软件负载均衡算法&#xff0c;将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时&#xff0c;重试…

数据库审计

启用一个专用的审计日志&#xff08;Audit Log&#xff09;将用户对数据库的所有操作记录在上面。审计员利用审计日志监控数据库中的各种行为&#xff0c;找出非法存取数据的人、时间和内容。 审计很费时间和空间 DBA可以根据应用对安全性的要求&#xff0c;灵活地打开或关闭…

北海市计算机等级考试,2021上半年北海市计算机二级报名时间|网上报名入口【已开通】...

&nbsp&nbsp[导读]:2021上半年北海市计算机二级报名时间|网上报名入口【已开通】&#xff0c;更多广西等级考试报名时间、考试时间以及考试模拟试题&#xff0c;请访问易考吧广西等级考试栏目2021上半年北海市计算机二级报名时间|网上报名入口【已开通】一、报名时间网上…

Java实体对象为什么一定要实现Serializable接口呢?

文章目录Java对象为什么要实现Serializable接口&#xff1f;Serializable接口概述Java对象为什么要实现Serializable接口&#xff1f; 最近这段时间一直在忙着编写Java业务代码&#xff0c;麻木地搬着Ctrl-C、Ctrl-V的砖&#xff0c;在不知道重复了多少次定义Java实体对象时“…

C3P0连接池工具类使用

c3p0的基本连接配置文件 c3p0-config.xml <c3p0-config><default-config><property name"driverClass">com.mysql.jdbc.Driver</property><property name"jdbcUrl">jdbc:mysql:///mybase</property><property name…

项目经理常见的沟通坏习惯

沟通失败有很多原因&#xff0c;每个项目经理都必须熟悉这些原因、了解其中的行为、并且有责任避免沟通失败的发生。在一些团队中&#xff0c;会产生失败的沟通、失败的项目是因为团队经理本身的坏习惯行为或者他本人容忍组员有些行为&#xff0c;而这些行为和坏习惯无意中会导…

Android屏幕适配

Android屏幕适配一直是Android开发们的一个痛点&#xff0c;各种各样的屏幕分辨率等&#xff0c;对Android的屏幕适配带来了很大的麻烦&#xff0c;而谷歌的解决方案也并不被所有人满意&#xff0c;所以笔者结合Android官方文档&#xff0c;来谈谈这个话题。 术语和基本概念 本…

万维网www

WWW是环球信息网的缩写&#xff0c;&#xff08;亦作“Web”、“WWW”、“W3”&#xff0c;英文全称为“World Wide Web”&#xff09;&#xff0c;中文名字为“万维网”&#xff0c;"环球网"等&#xff0c;常简称为Web。 分为Web客户端和Web服务器程序。 WWW可以让W…