赞
踩
实验用途 | IP地址 | 操作系统 |
---|---|---|
haproxy | 192.168.31.61 | Centos7.9 |
node1 | 192.168.31.62 | Centos7.9 |
node2 | 192.168.31.63 | Centos7.9 |
需要关闭selinux,防火墙,并清空Iptables规则。
软件名称 | 版本 |
---|---|
haproxy | 2.5 |
HAProxy 概述:
HAProxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。根据官方数据,其最高极限支持 10G 的并发。
HAProxy 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层处理。
HAProxy 运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的 web 服务器不被暴露到网络上。
其支持从 4 层至 7 层的网络交换,即覆盖所有的 TCP 协议。就是说,Haproxy 甚至还支持 Mysql的均衡负载。
www.haproxy.org #打不开
http://haproxy.com/ #收费
http://haproxy.1wt.eu/ 社区版地址, 打不开
https://github.com/haproxy/haproxy/releases/ 在 github 可以下载
实验拓扑图:
将GitHub下载的安装包上传到要安装haproxy服务器,并解压
[root@haproxy ~]# tar xvf haproxy-2.5-dev2.tar.gz
[root@master ~]# cd haproxy-2.5-dev2
编译之前首先安装编译需要的组件,这里使用yum进行安装
[root@haproxy ~]#yum -y install make gcc gcc-c++ openssl-devel
根据服务器的内核版本进行编译,最新版本参数为 ‘linux-glibc’ ,如果使用之前的
TARGET=linux2628 ,则会报以下错误。
[root@master haproxy-2.5-dev2]# make TARGET=linux2628
make: Warning: File `Makefile' has modification time 237859 s in the future
Target 'linux2628' was removed from HAProxy 2.0 due to being irrelevant and
often wrong. Please use 'linux-glibc' instead or define your custom target
by checking available options using 'make help TARGET=<your-target>'.
make: *** [all] 错误 1
因此,编译2.0以上的haproxy版本的时候需要使用:
[root@master haproxy-2.5-dev2]# make TARGET=linux-glibc PREFIX=/usr/local/haproxy #指定操作系统内核类型和安装的路径。也可以直接修改Makefile配置文件中这两个变量的值。如下:
[root@master haproxy-2.5-dev2]# vim Makefile
146 PREFIX = /usr/local #修改这个将制定haproxy安装路径
157 TARGET = #指定内核类型
#编译完成之后进行安装,指定安装目录为:/usr/local/haproxy
[root@master haproxy-2.5-dev2]# make install PREFIX=/usr/local/haproxy
安装完成后查看安装目录,发现没有配置文件,因此新建一个配置文件:
[root@haproxy ~]# ls /usr/local/haproxy doc sbin share [root@haproxy ~]# mkdir /usr/local/haproxy/etc #新建haproxy配置文件 [root@haproxy ~]# vim /usr/local/haproxy/etc/haprox.cfg global log 127.0.0.1 local0 maxconn 4096 chroot /usr/local/haproxy uid 99 gid 99 daemon defaults log global log 127.0.0.1 local3 mode http option httplog option httpclose option dontlognull option forwardfor option redispatch retries 2 maxconn 2000 balance roundrobin stats uri /haproxy-stats timeout connect 5000 timeout client 50000 timeout server 50000 mode http option httpchk GET /index.html frontend http bind 0.0.0.0:80 default_backend http_back backend http_back server s1 192.168.31.62:80 weight 3 check server s2 192.168.31.63:80 weight 3 check
保存并退出,之后启动haproxy
[root@haproxy ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
查看启动的进程
[root@haproxy ~]# ps -ef | grep haproxy
nobody 17644 1 0 00:00 ? 00:00:01 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
登录haproxy网页进行查看,地址是安装haproxy服务器的地址+/haproxy-stats,这里是http://192.168.31.61/haproxy-stats
由上图可以看出当前转发的后端服务器s1/s2异常,因为我们还没有在后端服务器配置服务。
配置web服务器:
[root@node1 ~]# yum install httpd php -y
生成测试文件:
root@node1 ~]# echo 192.168.31.62 > /var/www/html/index.html
启动apache服务器:
[root@node1 ~]# systemctl start httpd
node2同理,这里不再进行展示
再次查看haproxy,发现2台后端服务器已经已经正常,变成绿色。
[root@haproxy ~]# curl 192.168.31.61
192.168.31.62
[root@haproxy ~]# curl 192.168.31.61
192.168.31.63
可以查看到haproxy将访问请求平均分配到后端的服务器。
1.使用kill -9 <haproxy的进程号>
[root@haproxy ~]# ps -ef | grep haproxy
nobody 17644 1 0 00:00 ? 00:00:01 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
[root@haproxy ~]# kill -9 17644
2.使用killall进行停止,首先安装命令工具
[root@haproxy ~]# yum install -y psmisc
[root@haproxy ~]# killall haproxy
#查看进程,进程已经停止了
[root@master ~]# ps -ef | grep haproxy
root 17873 17685 0 01:02 pts/2 00:00:00 grep --color=auto haproxy
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。