HAproxy负载均衡集群部署实例 tls加密负载均衡: HAProxy TLS 终止负载均衡架构详解 HAProxy TLS 终止(也称为 TLS 卸载)是指 HAProxy 作为反向代理,在客户端与 HAProxy 之间建立加密的 HTTPS 连接,而在 HAProxy 与后端真实服务器之间建立未加密的 HTTP 连接。如下图:
获取ssl证书 在修改配置之前,需要先生成一个https的证书,当前测试我们可以使用自签证书。
1 2 3 4 5 6 7 root@centos-manager tls] [root @centos -manager tls ] Generating a 2048 bit RSA private key .......................................+++ ......................................................+++ writing new private key to '/etc/pki/tls/certs/test.key' -----
可以看到再当前目录的certs目录生成两个证书文件,接下来需要将两个证书文件合并成一个 PEM 文件。
1 [root @centos -manager tls ]
修改haproxy配置文件 1 2 3 4 5 6 7 8 9 10 [root @centos -manager tls ] global maxsslconn 655350 tune.ssl.default-dh-param 2048 frontend http-in bind *:80 redirect scheme https if !{ ssl_fc } bind *:443 ssl crt /etc/pki/tls/certs/zhangbin.pem [root @centos -manager tls ]
在global和frontend http-in 中添加相关配置,并重启haproxy
测试 在浏览器中使用https的方式访问HAproxy负载均衡器IP地址看,是否还能正常显示后端服务器web页面。
Haproxy TCP 负载 HAproxy 除了支持HTPP层的负载均衡之外,还支持底层的TCP模式负载均衡,例如数据库、缓存、ssh服务等
环境准备 首先先搭建两台数据库服务器。mariadb
1 [root @node1 yum.repos.d ]
修改数据库密码并设置允许root 用户远程登录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root @node2 ~] Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5 .60 -MariaDB MariaDB Server Copyright (c) 2000 , 2018 , Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none )]> GRANT ALL PRIVILEGES ON *.* TO 'root' @'%' IDENTIFIED BY '1234' -> ; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
再两台数据库服务器中分别创建一个数据库为data1和data2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root @node2 ~] [root @node2 ~] +-------------------- + | Database | +-------------------- + | information_schema | | data2 | | mysql | | performance_schema | | test | +-------------------- + [root @node1 yum.repos.d ] [root @node1 yum.repos.d ] +-------------------- + | Database | +-------------------- + | information_schema | | data1 | | mysql | | performance_schema | | test | +-------------------- +
修改HAproxy配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 [root @centos -manager ~] global log 127.0 .0.1 local2 info chroot /usr/local/haproxy pidfile /usr/local/haproxy/haproxy.pid maxconn 455350 user haproxy group haproxy daemon maxsslconn 655350 tune.ssl.default-dh-param 2048 defaults mode tcp log global option httplog timeout connect 10 s timeout client 30 s timeout server 30 s frontend mysql-in bind *:3306 default_backend backend_servers option forwardfor option httpclose backend backend_servers balance roundrobin server db1 10.201 .9.133 :3306 cookie 1 check inter 5000 fall 3 rise 2 weight 1 server db2 10.201 .9.140 :3306 cookie 2 check inter 5000 fall 3 rise 2 weight 1 listen stats mode http bind *:10000 stats enable stats uri /haproxy stats realm HAProxy\ Statistics stats auth admin:123 .com
测试 重启HAproxy并测试 使用mysql的客户端访问HAProxy服务器的IP地址。 因为是轮询的算法,所以当我们每次都去访问负载IP的时候,都会转到不同的数据库中。
Haproxy 负载后端https协议 如上图:除了用户访问负载均衡器是https加密之外,从负载均衡器到 后端服务器的链路也需要是加密的https传输注意:在配置后端服务器的https访问的时候,后端服务器的ssl证书需要是同一个证书
环境准备 使用之前搭建的两台nginx服务器,并且配置ssl证书,实现https访问,在实现第一个实例的时候,我们已经生成了一个ssl证书,接下来还是使用该ssl证书。将合并前的crt和key证书复制到两台后端服务器中。
1 2 3 4 5 6 7 8 [root @centos -manager certs ] root@10.201 .9.133 's password: test.crt 100% 1220 1.3MB/s 00:00 test.key 100% 1704 2.0MB/s 00:00 [root@centos-manager certs]# scp test.crt test.key root@10.201.9.140:/etc/pki/tls/certs root@10.201.9.140' s password: test.crt 100 % 1220 1.6 MB/s 00 :00 test.key 100 % 1704 2.6 MB/s 00 :00
配置后端的nginx服务器https访问 node1:
1 2 3 4 5 6 7 8 9 [root @node1 html ] server { listen 443 ssl; server_name _; root /usr/share/nginx/html; ssl_certificate "/etc/pki/tls/certs/test.crt" ; ssl_certificate_key "/etc/pki/tls/certs/test.key" ; }
node2:
1 2 3 4 5 6 7 8 [root @node2 ~] server { listen 443 ssl; server_name _; root /usr/share/nginx/html; ssl_certificate "/etc/pki/tls/certs/test.crt" ; ssl_certificate_key "/etc/pki/tls/certs/test.key" ; }
添加ssl 的配置
修改HAproxy配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 [root @centos -manager ~] global log 127.0 .0.1 local2 info chroot /usr/local/haproxy pidfile /usr/local/haproxy/haproxy.pid maxconn 455350 user haproxy group haproxy daemon maxsslconn 655350 tune.ssl.default-dh-param 2048 defaults mode tcp log global option httplog timeout connect 10 s timeout client 30 s timeout server 30 s frontend tcp-in bind *:443 default_backend backend_servers option forwardfor option httpclose backend backend_servers balance roundrobin server web1 10.201 .9.133 :443 cookie 1 check inter 5000 fall 3 rise 2 weight 1 server web2 10.201 .9.140 :443 cookie 2 check inter 5000 fall 3 rise 2 weight 1 listen stats mode http bind *:10000 stats enable stats uri /haproxy stats realm HAProxy\ Statistics stats auth admin:123 .com
重启HAproxy进行测试 1 2 [root @centos -manager ~] Restarting haproxy (via systemctl): [ 确定 ]
访问HAproxy的https的443端口进行测试