haproxy
源码安装
源码安装
//安装编译环境
[root@ray ~]# dnf -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//创建haproxy用户
[root@ray ~]# useradd -rMs /sbin/nologin haproxy
//解压安装
[root@ray ~]# ls
anaconda-ks.cfg haproxy-2.1.3.tar.gz
[root@ray ~]# tar -zxf haproxy-2.1.3.tar.gz
[root@ray ~]# ls
anaconda-ks.cfg haproxy-2.1.3 haproxy-2.1.3.tar.gz
[root@ray ~]# cd haproxy-2.1.3
[root@ray haproxy-2.1.3]# make clean
[root@ray haproxy-2.1.3]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
> TARGET=linux-glibc \
> USE_OPENSSL=1 \
> USE_ZLIB=1 \
> USE_PCRE=1 \
> USE_SYSTEMD=1
[root@ray haproxy-2.1.3]# make install PREFIX=/usr/local/haproxay
[root@ray haproxy-2.1.3]# cp haproxy /usr/sbin/
//设置Linux内核参数
[root@ray haproxy-2.1.3]# vim /etc/sysctl.conf
[root@ray haproxy-2.1.3]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//配置haproxy服务
[root@ray haproxy-2.1.3]# mkdir /etc/haproxy
[root@ray haproxy-2.1.3]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 172.16.103.130:80 check inter 2000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
//启动haproxy,配置service服务单元文件启动
[root@ray ~]# vim /usr/lib/systemd/system/haproxy.service
[root@ray ~]# cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfgv-p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
[root@ray ~]# systemctl daemon-reload
[root@ray ~]# systemctl restart haproxy.service
[root@ray ~]# systemctl enable haproxy.service
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
//配置日志信息
[root@ray ~]# vim /etc/rsyslog.conf
local0.info /var/log/haproxy.log
[root@ray ~]# systemctl restart rsyslog
[root@ray ~]# systemctl enable rsyslog
haproxy搭建http负载均衡
主机都关闭防火墙和selinux
[root@ray ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@ray ~]# setenforce 0
[root@ray ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
rs1 rs2部署httpd
[root@RS1 ~]# dnf -y install httpd
[root@RS1 ~]# echo RS1 > /var/www/html/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS2 ~]# dnf -y install httpd
[root@RS2 ~]# echo RS2 > /var/www/html/index.html
[root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
修改ray内核参数
[root@ray ~]# vim /etc/sysctl.conf
[root@ray ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
修改haproxy配置文件
[root@ray~]# vim /etc/haproxy/haproxy.cfg
global
daemon
maxconn 256
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server web01 192.168.183.138:80
server web02 192.168.183.139:80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
5、重新启动haproxy服务
[root@ray ~]# systemctl restart haproxy.service
1
6、客户端验证
[root@client ~]# curl http://192.168.183.137
RS1
[root@client ~]# curl http://192.168.183.137
RS2
[root@client ~]# curl http://192.168.183.137
RS1
[root@client ~]# curl http://192.168.183.137
RS2
1
2
3
4
5
6
7
8
使用WEB网页访问测试
[root@ray ~]# vim /etc/haproxy/haproxy.cfg
[root@ray ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats //访问网页后缀URL
stats realm Haproxy\ Statistics
stats auth admin:admin //用户名和密码
#stats hide-version
stats admin if TRUE
stats refresh 30s
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 192.168.183.138:80 check inter 2000 fall 5
server web02 192.168.183.139:80 check inter 2000 fall 5
[root@ray ~]# systemctl restart haproxy.service
[root@ray ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*