HAproxy负载均衡

概述:

1、HAProxy是一个免费的负载均衡软件,可以运行于大部分主流的Linux操作系统上(CentOS、Ubuntu、Debian、OpenSUSE、Fedora、麒麟、欧拉、UOS)。

2、HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力,具备丰富的功能。HAProxy具备媲美商用负载均衡器的性能和稳定性。

目录

一、准备环境

二、编译安装

1、安装

2、建立配置文件

3、添加为系统服务

4、启动haproxy

三、修改主配置文件

1、配置负载

四、web节点配置

1、安装httpd并启动

2、配置httpd主页文件


一、准备环境

HAproxy IP:192.168.115.119
web1 IP:192.168.115.202
web2 IP:192.168.115.203
systemctl stop firewalld            关闭防火墙
setenforce 0                        宽容模式

二、编译安装

1、安装

wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.2.tar.gz
tar xf haproxy-1.7.2.tar.gz
cd haproxy-1.7.2
make PREFIX=/usr/local/haproxy TARGET=linux2628    后面数字看自己版本,用uname -r看
make install PREFIX=/usr/local/haproxy
可能会需要gcc编译环境

2、建立配置文件

从haproxy的源码包中的examples下的init.haproxy中获得配置文件的路径“/etc/haproxy/haproxy.cfg”

mkdir /etc/haproxy

touch /etc/haproxy/haproxy.cfg

3、添加为系统服务

cp /root/haproxy-1.7.2/examples/haproxy.init /etc/init.d/haproxy

修改: 35行 内容为 BIN=/usr/local/haproxy/sbin/$BASENAME

chmod +x /etc/init.d/haproxy

chkconfig --add /etc/init.d/haproxy

4、启动haproxy

service haproxy  start      启动

systemctl  daemon-reload    后台程序从新加载

三、修改主配置文件

1、配置负载

global 
    daemon  
    maxconn 256  
    pidfile /var/run/haproxy.pid  
 
 
defaults 
    mode http  
    timeout connect 5000ms  
    timeout client 50000ms  
    timeout server 50000ms 
 
 
frontend http-in 
    bind *:80  
    default_backend servers  
 
 
backend servers 
    server server1 192.168.115.202:80 maxconn 32  
    server server2 192.168.115.203:80 maxconn 32

重启haproxy

四、web节点配置

1、安装httpd并启动

yum -y install httpd

systemctl start httpd

2、配置httpd主页文件

web1
echo web1 > /var/www/html/index.html

web2
echo web2 > /var/www/html/index.html
两台web节点都是安装好httpd之后启动,然后配置一下主页文件就好了

验证:

curl 192.168.115.199

 

或打开自带浏览器输入ip地址查看,用ctrl+F5刷新

你可能感兴趣的:(HAproxy,负载均衡,linux,服务器)