一、HAProxy介绍

         专业反向代理,支持双机热备支持虚拟主机,配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端节点出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服务器加入,基于TCP和HTTP应用的代理软件,开源免费、快速并且可靠的一种方案。

二、HAProxy优点

1、专业做反向代理负载均衡的软件
2、负载均衡算法多,大概8种
3、性能优于nginx
4、支持动态管理、通过和haproxy的sock进行通信管理
5、有比较丰富的dashboard页面
6、强大的七层功能

三、HAProxy应用场景

1、"tcp"即4层代理(大多用于邮件服务器、内部协议通信服务器等)

2、HAProxy可以作为MySQL、邮件或其它的非web的负载均衡,我们常用于它作为MySQL(读)负载均衡

3、特别适用于负载特大的web站点,这些站点通常又需要会话保持或者七层处理

四、HAProxy服务部署

1、环境

  1. #haproxy01
  2. [root@haproxy01 ~]# cat /etc/redhat-release
  3. CentOS release 6.9 (Final)
  4. [root@haproxy01 ~]# uname -r
  5. 2.6.-.el6.x86_64
  6. [root@haproxy01 ~]# getenforce
  7. Disabled
  8. [root@haproxy01 ~]# /etc/init.d/iptables status
  9. iptables: Firewall is not running.
  10. [root@haproxy01 ~]# hostname -I
  11. 172.19.5.3 172.16.1.3
  1. #haproxy02
  2. [root@haproxy02 ~]# cat /etc/redhat-release
  3. CentOS release 6.9 (Final)
  4. [root@haproxy02 ~]# uname -r
  5. 2.6.-.el6.x86_64
  6. [root@haproxy02 ~]# getenforce
  7. Disabled
  8. [root@haproxy02 ~]# /etc/init.d/iptables status
  9. iptables: Firewall is not running.
  10. [root@haproxy02 ~]# hostname -I
  11. 172.19.5.4 172.16.1.4

2、编译安装HAProxy

  1. ##打开IP转发
  2. echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf && sysctl -p
  3. yum -y install gcc systemd-devel #安装依赖
  4. tar xf haproxy-1.8..tar.gz #解压
  5. cd haproxy-1.8./
  6. make TARGET=linux2628 PREFIX=/usr/local/haproxy
  7. make install PREFIX=/usr/local/haproxy
  8. cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
  9. cp ./examples/haproxy.init /etc/init.d/haproxy
  10. chmod /etc/init.d/haproxy
  11. useradd -r haproxy
  12. mkdir /etc/haproxy

3、设置HAProxy日志(/etc/rsyslog.conf)

  1. $ModLoad imudp
  2. $UDPServerRun
  3. local3.* /var/log/haproxy.log

systemctl restart rsyslog

4、创建配置文件

/etc/haproxy/haproxy.cfg

  1. #---------------------------------------------------------------------
  2. global #全局配置
  3. log 127.0.0.1 local3 info #指定服务器的日志级别
  4. chroot /usr/local/haproxy #改变工作目录
  5. user haproxy #用户组和用户
  6. group haproxy
  7. daemon #以守护进程的方式运行
  8. maxconn #最大连接数
  9. defaults #默认配置
  10. log global
  11. mode http #7层http;4层tcp 如果要让haproxy支持虚拟主机,mode 必须设为http
  12. option httplog #http日志格式
  13. timeout connect #连接超时(毫秒)
  14. timeout client #客户端超时(毫秒)
  15. timeout server #服务器超时(毫秒)
  16. listen stats
  17. mode http
  18. bind 192.168.56.11:
  19. stats enable
  20. stats hide-version
  21. stats uri /stats
  22. stats admin if TRUE
  23. frontend web_front #前端配置 web_front名称可自定义
  24. bind 192.168.56.11: #发起的http请求到80端口,会转发到设置的ip及端口
  25. mode http
  26. log global
  27. option httplog # 启用http日志
  28. default_backend http_back
  29. backend http_back #后端配置,http_back名称可自定义
  30. option httpchk GET /index.jsp #设置健康检查页面
  31. option forwardfor header X-Forwarded-For #传递客户端真实IP
  32. balance roundrobin #roundrobin 轮询方式
  33. # 需要转发的ip及端口
  34. server web01 192.168.56.12: check inter rise fall weight
  35. server web02 192.168.56.13: check inter rise fall weight

5、实现HAProxy负载均衡

  1. #分别在haproxy01和haproxy02上面装了http服务,修改默监听端口为8080
  2. #测试负载均衡数据
  3. [root@haproxy01 ~]# curl 172.19.5.3
  4. http01
  5. [root@haproxy01 ~]# curl 172.19.5.3
  6. http02
  7. [root@haproxy01 ~]# curl 172.19.5.3
  8. http01
  9. [root@haproxy01 ~]# curl 172.19.5.3
  10. http02
  11. [root@haproxy01 ~]# curl 172.19.5.3
  12. http01
  13. [root@haproxy01 ~]# curl 172.19.5.3
  1. [root@haproxy02 ~]# curl 172.19.5.4
  2. http01
  3. [root@haproxy02 ~]# curl 172.19.5.4
  4. http02
  5. [root@haproxy02 ~]# curl 172.19.5.4
  6. http01
  7. [root@haproxy02 ~]# curl 172.19.5.4
  8. http02
  9. [root@haproxy02 ~]# curl 172.19.5.4
  10. http01
  11. [root@haproxy02 ~]# curl 172.19.5.4
  12. http02 

五、HAProxy动态管理

1、安装socat命令

  1. yum install -y socat

2、使用方法

  1. [root@haproxy01 ~]# echo "help"|socat stdio /var/lib/haproxy/haproxy.sock
  2. Unknown command. Please enter one of the following commands only :
  3. help : this message
  4. prompt : toggle interactive mode with prompt
  5. quit : disconnect
  6. set maxconn global : change the per-process maxconn setting
  7. set rate-limit : change a rate limiting value
  8. set timeout : change a timeout setting
  9. show env [var] : dump environment variables known to the process
  10. show stat resolvers [id]: dumps counters from all resolvers section and
  11. associated name servers
  12. add acl : add acl entry
  13. clear acl <id> : clear the content of this acl
  14. del acl : delete acl entry
  15. get acl : report the patterns matching a sample for an ACL
  16. show acl [id] : report available acls or dump an acl's contents
  17. add map : add map entry
  18. clear map <id> : clear the content of this map
  19. del map : delete map entry
  20. get map : report the keys and values matching a sample for a map
  21. set map : modify map entry
  22. show map [id] : report available maps or dump a map's contents
  23. show pools : report information about the memory pools usage
  24. show sess [id] : report the list of current sessions or dump this session
  25. shutdown session : kill a specific session
  26. shutdown sessions server : kill sessions on a server
  27. clear counters : clear max statistics counters (add 'all' for all counters)
  28. show info : report information about the running process
  29. show stat : report counters for each proxy and server
  30. show errors : report last request and response errors for each proxy
  31. clear table : remove an entry from a table
  32. set table [id] : update or create a table entry's data
  33. show table [id]: report table usage stats or dump this table's contents
  34. disable frontend : temporarily disable specific frontend
  35. enable frontend : re-enable specific frontend
  36. set maxconn frontend : change a frontend's maxconn setting
  37. show servers state [id]: dump volatile server information (for backend <id>)
  38. show backend : list backends in the current running config
  39. shutdown frontend : stop a specific frontend
  40. disable agent : disable agent checks (use 'set server' instead)
  41. disable health : disable health checks (use 'set server' instead)
  42. disable server : disable a server for maintenance (use 'set server' instead)
  43. enable agent : enable agent checks (use 'set server' instead)
  44. enable health : enable health checks (use 'set server' instead)
  45. enable server : enable a disabled server (use 'set server' instead)
  46. set maxconn server : change a server's maxconn setting
  47. set server : change a server's state, weight or address
  48. get weight : report a server's current weight
  49. set weight : change a server's weight (deprecated)

3、监控数据

  1. [root@haproxy01 ~]# echo "show info"|socat stdio /var/lib/haproxy/haproxy.sock
  2. Name: HAProxy
  3. Version: 1.7.9
  4. Release_date: 2017/08/18
  5. Nbproc: 1
  6. Process_num: 1
  7. Pid: 3897
  8. Uptime: 0d 2h57m54s
  9. Uptime_sec: 10674
  10. Memmax_MB: 0
  11. PoolAlloc_MB: 0
  12. PoolUsed_MB: 0
  13. PoolFailed: 0
  14. Ulimit-n: 4031
  15. Maxsock: 4031
  16. Maxconn: 2000
  17. Hard_maxconn: 2000
  18. CurrConns: 0
  19. CumConns: 3563
  20. CumReq: 3575
  21. Maxpipes: 0
  22. PipesUsed: 0
  23. PipesFree: 0
  24. ConnRate: 1
  25. ConnRateLimit: 0
  26. MaxConnRate: 2
  27. SessRate: 1
  28. SessRateLimit: 0
  29. MaxSessRate: 2
  30. CompressBpsIn: 0
  31. CompressBpsOut: 0
  32. CompressBpsRateLim: 0
  33. Tasks: 5
  34. Run_queue: 1
  35. Idle_pct: 100
  36. node: haproxy01

4、管理后端节点

①后端节点正常状态

②关闭后端节点haproxy01

  1. echo "disable server web_back/haproxy01"|socat stdio /var/lib/haproxy/haproxy.sock

③打开后端节点haproxy01又恢复正常

  1. [root@haproxy01 ~]# echo "enable server web_back/haproxy01"|socat stdio /var/lib/haproxy/haproxy.sock

六、HAProxy结合Keepalived实现高可用

 1、安装keepalived

  1. yum install keepalived -y

2、keepalived配置文件

#HAProxy01-keepalived配置#

global_defs {

router_id HAProxy-HA01

}

vrrp_instance HAProxy_1 {

state MASTER

interface eth0

virtual_router_id 55

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.19.5.16/24

172.19.5.17/24

}

}

#HAProxy02-keepalived配置#

global_defs {

router_id HAProxy-HA02

}

vrrp_instance HAProxy_1 {

state BACKUP

interface eth0

virtual_router_id 55

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.19.5.16/24

172.19.5.17/24

}

}

3、使用tcpdump检测主发包

  1. [root@haproxy01 ~]# tcpdump -n 'host 224.0.0.18'
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on eth0, link-type EN10MB (Ethernet), capture size bytes
  4. ::52.240031 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  5. ::53.241106 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  6. ::54.242203 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  7. ::55.243301 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  8. ::56.244382 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  9. ::57.245453 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  10. ::58.245809 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  11. ::59.246893 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  12. ::00.247984 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length
  13. ::01.249098 IP 172.19.5.3 > 224.0.0.18: VRRPv2, Advertisement, vrid , prio , authtype simple, intvl 1s, length

 4、测试keepalived的高可用,故障切换

①使用vip访问测试

  1. [root@m01 ~]# curl 172.19.5.16
  2. http01
  3. [root@m01 ~]# curl 172.19.5.16
  4. http02
  5. [root@m01 ~]# curl 172.19.5.17
  6. http01
  7. [root@m01 ~]# curl 172.19.5.17
  8. http02

②haproxy01上查看vip

  1. [root@haproxy01 ~]# ip a s eth0
  2. : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
  3. link/ether ::::: brd ff:ff:ff:ff:ff:ff
  4. inet 172.19.5.3/ brd 172.19.5.255 scope global eth0
  5. inet 172.19.5.16/ scope global secondary eth0
  6. inet 172.19.5.17/ scope global secondary eth0
  7. inet6 fe80:::31ff:fe57:/ scope link
  8. valid_lft forever preferred_lft forever

③停掉haproxy01的keepalived服务

  1. [root@haproxy01 ~]# /etc/init.d/keepalived stop
  2. Stopping keepalived: [ OK ]

④haproxy02上查看vip

  1. [root@haproxy02 ~]# ip a s eth0
  2. : eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
  3. link/ether f6:4f::db:f5:d8 brd ff:ff:ff:ff:ff:ff
  4. inet 172.19.5.4/ brd 172.19.5.255 scope global eth0
  5. inet 172.19.5.16/ scope global secondary eth0
  6. inet 172.19.5.17/ scope global secondary eth0
  7. inet6 fe80::f44f:56ff:fedb:f5d8/ scope link
  8. valid_lft forever preferred_lft forever

linux运维、架构之路-HAProxy反向代理的更多相关文章

  1. linux运维架构师职业规划

    1.假如你从来未接触过Linux的话,首先要做的就找一本指导书来学习.现在公认的Linux的入门书籍是“鸟哥的私房菜”,讲的很全面,鸟哥的私房菜一共分为两部,一部是基础篇,一部是服务器篇.“鸟哥的私房 ...

  2. linux运维、架构之路-Nginx反向代理

    一. Nginx负载均衡和反向代理知识 1.集群概念   一堆服务器合作做同一件事,这些机器可能需要整个技术团队架构.设计和统一协调管理,这些机器可以分布在一个机房,也可以分布在全国各个地区的多个机房 ...

  3. centos6.5环境自动化运维之puppet实现nginx反向代理功能及puppet安装配置详解

    puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppet把这些系统实体称之为资 ...

  4. 运维工作笔记-------nginx的反向代理

    1.nginx的反向代理意义 一般来说,我们在项目中,不会直接让项目服务器ip与外网做直接映射,这样一则是不安全,二是客户直接去访问项目服务器,对项目服务器带来的压力太大,从而导致项目运行速度变慢,程 ...

  5. Linux运维企业架构实战系列

    Linux运维企业架构项目实战系列 项目实战1-LNMP的搭建.nginx的ssl加密.权限控制的实现 项目实战2-LVS.nginx实现负载均衡系列 2.1 项目实战2.1-实现基于LVS负载均衡集 ...

  6. 从苦逼到牛逼,详解Linux运维工程师的打怪升级之路

    做运维也快四年多了,就像游戏打怪升级,升级后知识体系和运维体系也相对变化挺大,学习了很多新的知识点. 运维工程师是从一个呆逼进化为苦逼再成长为牛逼的过程,前提在于你要能忍能干能拼,还要具有敏锐的嗅觉感 ...

  7. Linux运维企业架构项目实战系列

    Linux运维企业架构项目实战系列 项目实战1—LNMP的搭建.nginx的ssl加密.权限控制的实现 项目实战2—LVS.nginx实现负载均衡系列2.1 项目实战2.1—实现基于LVS负载均衡集群 ...

  8. Linux 运维入门到跑路书单推荐

    一.基础入门 <鸟哥的Linux私房菜基础学习篇>:最具知名度的Linux入门书<鸟哥的Linux私房菜基础学习篇>,全面而详细地介绍了Linux操作系统. https://b ...

  9. Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Discuz!

    Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Disc ...

随机推荐

  1. mysql下载与安装过程

    1:下载MySql 官网下载地址:https://dev.mysql.com/downloads/mysql/ 选择对应的下载文件.(我电脑是64位,所以这下载的是64位的下载文件) 2:解压mysq ...

  2. 关于migration build failed的问题

    首先一定要执行dotnet restore 查看网站的依赖关系(有时候生成是不报错的但是restore会找不到文件路径) 检查执行命令的路径是否是正确的当前网站路径 build failed一定是生成 ...

  3. python下对mysql数据库的链接操作

    参考网址: https://blog.csdn.net/guofeng93/article/details/53994112 https://blog.csdn.net/Chen_Eris/artic ...

  4. docker--docker 的web可视化管理工具

    12 docker 的web可视化管理工具 12.1 常用工具介绍 当 Docker 部署规模逐步变大后,可视化监控容器环境的性能和健康状态将会变得越来越 重要. Docker的图形化管理工具,提供状 ...

  5. [JS] 鼠标点击文本框清空默认值,离开文本框恢复默认值

    在使用文本框的时候,若设定了初始值,选择文本框进行输入的时候要将本来的内容进行删除,会显得非常麻烦 可以在文本框属性定义触发onfocus和onblur两个事件时对应的js功能 下面以asp.net代 ...

  6. Jmeter响应数据显示乱码问题

    Jmeter在访问接口的时候,响应内容如果有中文可能会显示乱码,原因应该是响应页面没有做编码处理,jmeter默认按照ISO-8859-1编码格式进行解析. 解决步骤: 现象:jmeter访问本地文件 ...

  7. 安装OpenStack计算服务(nova)

    1. 配置数据库 数据库安装在控制节(controller)点上 $ mysql -u root -p 2.创建 glance 数据库 CREATE DATABASE nova; GRANT ALL ...

  8. Boostrap4 li列表橫向

    Boostrap3 li元素橫向: <ul class="nav navbar-nav list-inline"> <li class="list-in ...

  9. 获取程序所有加载的dll名称

    1.在任务管理器输入如下指令.输出到文件:tasklist /m >c:\dll.txt 输出到命令行 tasklist /m 2.使用工具软件https://files.cnblogs.com ...

  10. freemarker进阶--项目中使用

    1.工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...