Linux初学者学习笔记

20171101 HAProxy

2017-11-06  本文已影响83人  哈喽别样
  • HAProxy简介
  • HAProxy配置
  • ACL配置
  • TCP转发
  • SSL转发

一、HAProxy简介:

(一)HAProxy的主要功能

(二)程序环境

主程序:/usr/sbin/haproxy
配置文件:/etc/haproxy/haproxy.cfg
Unit file:/usr/lib/systemd/system/haproxy.service

(三)配置文件结构

(1)global:全局配置段
(2)proxies:代理配置段

二、HAProxy配置:

(一)global全局配置

(1)进程及安全管理:chroot, deamon, user, group, uid, gid
// 1. 配置haproxy
vim /etc/haproxy/haproxy.cfg
// global配置段下,日志设置
log         192.168.136.133 local2 
// proxies配置段下,反向代理设置
frontend web 172.18.58.230:80
        default_backend webservs
backend webservs
        balance roundrobin
        server websrv1 192.168.136.229:80 check
        server websrv2 192.168.136.129:80 check
systemctl restart haproxy

// 2. 配置RS
echo RS1 homepage > /var/www/http/index.html     // RS1上操作
echo RS2 homepage > /var/www/http/index.html     // RS2上操作
service httpd start

// 3. 配置日志记录主机
vim /etc/rsyslog.conf
// 打开UDP 514端口监听
$ModLoad imudp
$UDPServerRun 514
// 设置日志存放目录
local2.*                                                /var/log/haproxy.log
systemctl restart rsyslog

测试结果显示反向代理成功运行,日志成功生成

(2)日志系统

(二)proxies 代理配置

(1) bind:指定一个或多个前端侦听地址和端口
(2)balance:后端服务器组内的服务器调度算法
(3)hash-type:哈希算法
(4)default_backend <backend>
(5)default-server [param*]
(6)server:定义后端主机的各服务器及其选项
// haproxy服务器上安装httpd服务,并配置
yum -y install httpd
vim /etc/httpd/conf/httpd.conf
Listen 8001     // haproxy监听80端口,故sorry server服务需要通过其他端口提供服务
echo sorry page > /var/www/html/index.html
systemctl start httpd

// haproxy服务器上修改haproxy配置
vim /etc/haproxy/haproxy.cfg
frontend web 172.18.58.230:80
        default_backend webservs
backend webservs
        default-server inter 3000 weight 2     // 默认server的健康监测间隔3000ms,权重2
        balance roundrobin
        server websrv1 192.168.136.229:80 weight 3 maxconn 5000 check
        server websrv2 192.168.136.129:80 maxconn 3000 check
        server sorrysrv 192.168.136.230:8001 backup
systemctl restart haproxy

// 测试
for i in {1..10}; do curl 172.18.58.230; done

确实按照设置的权重转发

关闭RS1和RS2的httpd服务后,转至sorry server

// haproxy服务器上修改haproxy配置
vim /etc/haproxy/haproxy.cfg
frontend web 172.18.58.230:80
        default_backend webservs
backend webservs
        default-server inter 3000 weight 2
        cookie WEBSRV insert nocache     // 响应报文添加cookie,不进行缓存
        balance roundrobin
        server websrv1 192.168.136.229:80 weight 3 maxconn 5000 check cookie srv1
        server websrv2 192.168.136.129:80 maxconn 3000 check cookie srv2
        server sorrysrv 192.168.136.230:8001 backup
systemctl restart haproxy
// 测试
curl -I 172.18.58.230
curl -b WEBSRV=srv1 172.18.58.230
curl -b WEBSRV=srv2 172.18.58.230

第一次登录后的响应报文中包含了cookie信息

curl命令指定不同的cookie信息,会被转发至指定的RS

浏览器会在之后的请求报文中包含第一次响应报文中的cookie信息,从而实现转发绑定

(三)统计接口启用相关参数

vim /etc/haproxy/haproxy.cfg
listen hastats
        bind 192.168.136.230:8888
        stats enable
        stats uri /hastats
        stats auth hatest1:centos
        stats realm "haproxy auth"
        stats hide-version
        stats refresh 5
        stats admin if TRUE
systemctl restart haproxy
浏览器登录http://192.168.136.230:8888/hastats

(四)maxconn <conns>

(五)mode { tcp | http | health}

(六)options httpchk

(七)options forwardfor

// 配置haproxy
vim /etc/haproxy/haproxy.cfg
option forwardfor       except 127.0.0.0/8    // 此行在defaults语句段已经存在
// 配置httpd
vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog logs/access_log combined
service httpd restart

查看/var/log/httpd/access_log,可以看到httpd服务重启前后的日志记录格式的不同。之前日志记录的源IP为LB在内网的IP,修改配置后的日志记录的源IP为真实发起连接的客户端IP地址。

(八)为指定的MIME类型启用压缩传输功能

(九)errorfile, errorloc 自定义错误页

(十)修改报文首部

// 配置haproxy服务
vim /etc/haproxy/haproxy.cfg
// 在frontend, listen, backend段中均可以设置
reqadd x-via:\ host_node1
rspadd y-via:\ host_node2
rspdel Server
systemctl restart haproxy

// 配置RS的httpd服务
vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{x-via}i\"" combined
CustomLog logs/access_log combined
service httpd restart

// 测试
tail -f /var/log/httpd/access_log     // RS端执行
curl -I 172.18.58.230                 // 客户端执行

日志文件中显示了添加的请求报文信息

响应报文中显示添加的信息,并且服务器程序信息被删除

(十一)连接超时

三、ACL配置:

(一) value的类型:

(二) flags的选项

(三)operator

(四)criterion

(五)base string

(六)path string

(七)url string

(八)hdr string

(九) status : integer

(十)预定义ACL

(十一)ACL的使用

// 配置haproxy
vim /etc/haproxy/haproxy.cfg
frontend web 172.18.58.230:80
        acl url_static path_beg -i /images /javascript /static /stylesheets
        acl url_static path_end -i .jpg .jpeg .png .gif .html .htm .js .css .txt
        use_backend staticsrvs if url_static
        default_backend appsrvs
backend staticsrvs
        balance roundrobin
        server websrv1 192.168.136.229:80 check
backend appsrvs
        balance roundrobin
        server websrv2 192.168.136.129:80 check
        server websrv3 192.168.136.130:80 check
systemctl restart haproxy

// 配置RS1
mkdir /var/www/html/images
cp /usr/share/backgrounds/nature/FreshFlower.jpg /var/www/html/images/a.jpg
echo RS1 page > /var/www/html/index.html
yum -y install php
vim /var/www/html/index.php
node 1
<?php
phpinfo();
?>
service httpd restart

// 配置RS2
mkdir /var/www/html/images
cp /usr/share/backgrounds/nature/Wood.jpg /var/www/html/images/a.jpg
echo RS2 page > /var/www/html/index.html
yum -y install php
vim /var/www/html/index.php
node 2
<?php
phpinfo();
?>
service httpd restart

// 配置RS3
mkdir /var/www/html/images
cp /usr/share/backgrounds/night.jpg /var/www/html/images/a.jpg
echo RS3 page > /var/www/html/index.html
yum -y install php
vim /var/www/html/index.php
node 3
<?php
phpinfo();
?>
systemctl restart httpd

测试:在浏览器上测试,当输入http://172.18.58.230/images/a.jpg,无论如何强制刷新都是显示RS1的鲜花照片;证明jpg静态文件转发至RS1

当输入http://172.18.58.230/index.html,无论如何强制刷新都是输出RS1的网页信息"RS1 page";证明html静态文件转发至RS1

当输入http://172.18.58.230/index.php,多次刷新交替出现RS2和RS3上的php文件信息;证明php动态文件转发至RS2和RS3

查看RS1的httpd日志文件,全部请求都是jpg文件和html文件,进一步证实其接受调度过来的全部是静态文件

查看RS2和RS3的httpd日志文件,全部请求都是phpl文件,进一步证实其接受调度过来的全部是动态文件

四、TCP转发:

// 配置RS的mysql服务
yum -y install mysql-server
service mysqld start
mysql_secure_installation
mysql -uroot -pmagedu
mysql> create user test@'192.168.136.%' identified by 'centos';

// 配置haproxy
frontend mysql
        bind 172.18.58.230:3306
        mode tcp
        default_backend mysqlsrvs
backend mysqlsrvs
        mode tcp
        balance roundrobin
        server mysqlsrv1 192.168.136.229:3306 check
        server mysqlsrv2 192.168.136.129:3306 check
systemctl restart haproxy

// 测试
yum -y install mariadb
mysql -utest -pcentos -h172.18.58.230
MySQL [(none)]> show variables like 'hostname';

通过查询数据库的hostname变量名,确认转发成功

五、SSL转发:

// 配置haproxy
cd /etc/pki/tls/certs/
make /etc/haproxy/haproxy.pem     // 生成密钥和证书文件
vim /etc/haproxy/haproxy.cfg
frontend http
        bind 172.18.58.230:80
        bind 172.18.58.230:443 ssl crt /etc/haproxy/haproxy.pem
        redirect scheme https if !{ ssl_fc }     // 设置http连接自动跳转
        http-request set-header X-Forwarded-Port %[dst_port]             // 请求报文传递端口信息
        http-request add-header X-forwarded-Proto https if { ssl_fc }    // 请求报文传递协议信息
        default_backend httpsrvs
backend httpsrvs
        balance roundrobin
        server httpsrv1 192.168.136.229:80 check
        server httpsrv2 192.168.136.129:80 check
systemctl restart haproxy

// 配置RS
echo RS1 page > /var/www/html/index.html      // RS1上设置
echo RS2 page > /var/www/html/index.html      // RS2上设置
vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-Port}i\" \"%{X-Forwarded-Proto}i\"" combined
CustomLog logs/access_log combined
service httpd restart

// 测试
curl -k https://172.18.58.230        // 客户端测试https连接转发情况
curl -kL http://172.18.58.230        // 客户端测试http连接是否成功跳转https
tail -f /var/log/httpd/access_log    // RS上查看日志是否收到传递的客户端请求协议和端口信息

https连接转发成功

http连接自动跳转为https连接,并且转发成功

日志信息中显示了客户端请求的端口号和协议信息

上一篇 下一篇

猜你喜欢

热点阅读