86754tr 发表于 2017-9-11 10:51:29

haproxy服务配置

环境说明-两台服务器:
server1: 192.168.10.71
server2: 192.168.10.72
server1配置HAProxy,server2配置lamp并搭建WordPress博客,php测试页为test.php

配置haproxy
yum install haproxy
haproxy -v
HA-Proxy version 1.5.18 2016/05/10
haproxy -vv//显示详细信息
haproxy -h

配置rsyslog

1
2
3
4
5
vi /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
local2.*      /var/log/haproxy.log
systemctl restart rsyslog




配置haproxy

1
2
3
4
5
6
vi /etc/haproxy/haproxy.cfg
frontend web *:80
    default_backend myweb
backend myweb
    balance roundrobin
    server web1 192.168.10.72 check




检查配置   
haproxy -c -f /etc/haproxy/haproxy.cfg
启动服务
systemctl start haproxy
查看日志信息
more /var/log/haproxy.log

测试代理访问:
http://192.168.10.71
curl 127.0.0.1/test.php//访问后端lamp的测试页
lamp-wp-test-php-10.72

新增一台httpd服务器server3-10.73,创建一个测试页
echo "lamp-wp-test-php-10.73" > /var/www/html/test.php

======================
修改haproxy配置,实现负载均衡

1
2
3
4
5
6
7
vi /etc/haproxy/haproxy.cfg
frontend web *:80
    default_backend myweb
backend myweb
    balance roundrobin
    server web1 192.168.10.72:80 check
    server web2 192.168.10.73:80 check




访问测试,可以实现轮询
for i in {1..10};do curl 192.168.10.71/test.php;done
more /var/log/haproxy.log

======================
一致性hash算法配置:

1
2
3
4
5
backend myweb
    balance uri
    server web1 192.168.10.72:80 check
    server web2 192.168.10.73:80 check
    hash-typeconsistent




======================   
配置压缩

1
2
    compression algo gzip
    compression type text/html text/plain application/xml




======================
定义sorry server配置:

1
    server web2 192.168.10.71:80 check backup




======================
配置状态检测和管理页面

1
2
3
4
5
6
7
8
9
10
11
12
vi /etc/haproxy/haproxy.cfg增加如下配置段
listen stats
    bind :8888
    stats enable
    stats url /myproxy?stats
    stats realm HAPorxy\ Stats\ Page
    stats auth admin:admin
    stats admin if TRUE   
    stats refresh 3
    acl allowip src 192.168.10.72
    block if !allowip//只允许上面定义的IP访问
    errorloc 403 http://192.168.10.72/errorip.html




10.72上定义错误页面:
echo "sorry you can't see it" > /var/www/html/errorip.html
10.72访问:
http://192.168.10.71/haproxy?stats

10.73上测试显示403错误页面:
elinks http://192.168.10.71:8888/myproxy?stats
sorry you can't see it

======================
tcp的负载均衡,实现ssh代理

1
2
3
4
5
6
7
8
9
10
vi /etc/haproxy/haproxy.cfg//需要更改mode模式为tcp
defaults
    mode                  tcp
    option                  tcplog
listen ssh
    mode tcp
    bind *:2222
    balance leastconn
    server ssh1 192.168.10.72:22 check
    server ssh2 192.168.10.73:22 check




测试:
haproxy -c -f /etc/haproxy/haproxy.cfg
systemctl restart haproxy
ssh 192.168.10.71 -p 2222

======================
haproxy实现动静分离代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vi /etc/haproxy/haproxy.cfg
frontend web *:80
    acl static path_end .jpg .png .txt
    acl static path_begin /images /css /imgs
    use_backend imgsrv if staic//如果是匹配上面定义的static-acl则代理到imgsrv服务器组
    default_backend myweb//默认访问的是myweb服务器组
backend myweb
    balance roundrobin
    server web1 192.168.10.72:80 check
    server web2 192.168.10.73:80 check
backend imgsrv
    balance roundrobin
    server web1 192.168.10.72:8080 check
    server web2 192.168.10.73:8080 check




测试访问:
http://192.168.10.71/messages.txt
http://192.168.10.71/test.php


页: [1]
查看完整版本: haproxy服务配置