介绍

使用软件层面做ADFS 反向代理以及负载均衡

需求准备

  • 2 Ubuntu 20.04 Servers
  • 3 available IP Addresses (Here we are using the 10.0.0.0/24 subnet)
    • 10.0.0.100 for keepalived
    • 10.0.0.101 for the MASTER server
    • 10.0.0.102 for the BACKUP server
  • An ADFS Cluster

Keepalived 配置

MASTER: /etc/keepalived/keepalived.conf

  1. global_defs {
  2. router_id haproxy
  3. enable_script_security
  4. }
  5. vrrp_script chk_haproxy {
  6. script "/usr/bin/pgrep haproxy"
  7. interval 2
  8. weight 2
  9. }
  10. vrrp_instance haproxy {
  11. interface ens160
  12. state MASTER
  13. # MASTER requires a higher priority number than the SLAVE
  14. priority 101
  15. virtual_router_id 51
  16. authentication {
  17. auth_type AH
  18. auth_pass 12345678
  19. }
  20. virtual_ipaddress {
  21. 10.0.0.100
  22. }
  23. track_script {
  24. chk_haproxy
  25. }
  26. }

BACKUP: /etc/keepalived/keepalived.conf

  1. global_defs {
  2. router_id haproxy
  3. enable_script_security
  4. }
  5. vrrp_script chk_haproxy {
  6. script "/usr/bin/pgrep haproxy"
  7. interval 2
  8. weight 2
  9. }
  10. vrrp_instance haproxy {
  11. interface ens160
  12. state BACKUP
  13. # BACKUP requires a lower priority number than the MASTER
  14. priority 100
  15. virtual_router_id 51
  16. authentication {
  17. auth_type AH
  18. auth_pass 12345678
  19. }
  20. virtual_ipaddress {
  21. 10.0.0.100
  22. }
  23. track_script {
  24. chk_haproxy
  25. }
  26. }

HAProxy 配置

需要将必要的证书放置于 /etc/ssl/domain.com/certificate.pem

根据需要修改以下配置文件内容:
/etc/haproxy/haproxy.cfg

  1. global
  2. log /dev/log local0
  3. log /dev/log local1 notice
  4. chroot /var/lib/haproxy
  5. stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
  6. user haproxy
  7. group haproxy
  8. maxconn 40000
  9. ulimit-n 81000
  10. daemon
  11. ssl-default-bind-ciphers EECDH+AESGCM:EDH+AESGCM
  12. ssl-default-bind-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets
  13. ssl-default-server-ciphers EECDH+AESGCM:EDH+AESGCM
  14. ssl-default-server-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets
  15. tune.ssl.default-dh-param 2048
  16. defaults
  17. log global
  18. mode tcp
  19. option httplog
  20. option dontlognull
  21. option forwardfor
  22. option http-use-htx
  23. timeout client 30s
  24. timeout server 30s
  25. timeout connect 5s
  26. errorfile 400 /etc/haproxy/errors/400.http
  27. errorfile 403 /etc/haproxy/errors/403.http
  28. errorfile 408 /etc/haproxy/errors/408.http
  29. errorfile 500 /etc/haproxy/errors/500.http
  30. errorfile 502 /etc/haproxy/errors/502.http
  31. errorfile 503 /etc/haproxy/errors/503.http
  32. errorfile 504 /etc/haproxy/errors/504.http
  33. # Page to view statistics with username User and password Password
  34. listen stats
  35. bind :9000
  36. mode http
  37. stats enable
  38. stats hide-version
  39. stats realm Haproxy\ Statistics
  40. stats uri /haproxy_stats
  41. stats auth Username:Password
  42. # Frontend for HTTP to HTTPS redirect
  43. frontend fe_http
  44. bind *:80
  45. mode http
  46. option httplog
  47. log global
  48. redirect scheme https code 301
  49. # Frontend for SNI Passthrough
  50. frontend fe_spt
  51. bind *:443
  52. option forwardfor header X-Forwarded-For
  53. tcp-request inspect-delay 5s
  54. tcp-request content accept if { req_ssl_hello_type 1 }
  55. # An entry is required per FQDN
  56. use_backend spt_sts if { req_ssl_sni -i sts.domain.com }
  57. # This will be the webpage that is displayed if no matching FQDN is detected
  58. default_backend spt_sts
  59. # Each SNI Passthrough backend requires a port to use for the normal frontend
  60. backend spt_sts
  61. option forwardfor header X-Forwarded-For
  62. server localhost 127.0.0.1:20000 check
  63. # Normal frontend
  64. frontend fe_sts
  65. bind *:20000 ssl crt /etc/ssl/domain.com/certificate.pem
  66. mode http
  67. http-request set-header X-MS-Forwarded-Client-IP %[src]
  68. use_backend be_sts
  69. # Normal backend
  70. backend be_sts
  71. mode http
  72. log global
  73. balance roundrobin
  74. option httpchk GET /adfs/ls/IdpInitiatedSignon.aspx HTTP/1.1\r\nHost:\ sts.domain.com
  75. option forwardfor header X-Client
  76. http-check expect status 200
  77. http-request add-header X-Forwarded-Proto https if { ssl_fc }
  78. server adfs01 10.0.0.98:443 ssl verify none check check-sni sts.domain.com sni ssl_fc_sni inter 3s rise 2 fall 3
  79. server adfs02 10.0.0.99:443 ssl verify none check check-sni sts.domain.com sni ssl_fc_sni inter 3s rise 2 fall 3