目录
实验环境
haproxy:192.168.115.130
web1:192.168.115.131
web2:192.168.115.132
一、内核配置
vim /etc/sysctl.conf
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65023
net.ipv4.tcp_max_syn_backlog = 10240
net.ipv4.tcp_max_tw_buckets = 400000
net.ipv4.tcp_max_orphans = 60000
net.ipv4.tcp_synack_retries = 3
net.core.somaxconn = 10000
二、编译安装
编译前先产看内核版本
- linux22 for Linux 2.2
- linux24 for Linux 2.4 and above (default)
- linux24e for Linux 2.4 with support for a working epoll (> 0.21)
- linux26 for Linux 2.6 and above
- linux2628 for Linux 2.6.28, 3.x, and above (enables splice and tproxy
各版本对应的数字
uname -r
查看内核版本
wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.2.tar.gz
make PREFIX=/usr/local/haproxy TARGET=linux2628
make install PREFIX=/usr/local/haproxy
三、建立配置文件
从haproxy的源码包中的examples下的init.haproxy中获得配置文件的路径“/etc/haproxy/haproxy.cfg”
mkdir /etc/haproxy
touch /etc/haproxy/haproxy.cfg
vim /etc/haproxy/haproxy.cfg
配置项:
global:用于配置全局参数
default:用于配置所有frontend和backend的默认属性
frontend:用于配置前端服务(即HAProxy自身提供的服务)实例
backend:用于配置后端服务(即HAProxy后面接的服务)实例组
listen:frontend+backend的组合配置,可以理解成更简洁的配置方法
七层模型配置如下:
global
daemon
maxconn 256
pidfile /var/run/haproxy/haproxy.pid
defaults
mode http
timeout connect 5000ms