server:haproxy

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
server:haproxy [2018/07/12 20:39] – [Links] stserver:haproxy [2020/08/30 16:30] (aktuell) st
Zeile 1: Zeile 1:
 +====== haproxy ======
 +
 +haproxy ist ein [[server:Load-balancing|Load-Balancer]] (vor allem für HTTP(s) und TCP - [[https://github.com/haproxy/haproxy/issues/62#issuecomment-502362680|nicht UDP]]) der in der 'Programmiersprache C geschrieben ist. Er wird aufgrund seiner Performanz von großen Webseiten eingesetzt.
 +
 +:!: Diese Seite bezieht sich auf haproxy version 1.7.
 +
 +===== Links =====
 +
 +  * [[http://www.haproxy.org/|haproxy.org]]
 +  * [[https://jve.linuxwall.info/ressources/taf/haproxy-aws/|Guidelines for HAProxy termination in AWS]]
 +  * [[https://cbonte.github.io/haproxy-dconv/1.7/configuration.htm|1.7 Dokumentation]]
 +  * [[https://www.digitalocean.com/community/tutorials/how-to-use-haproxy-to-set-up-http-load-balancing-on-an-ubuntu-vps|How To Use HAProxy to Set Up HTTP Load Balancing on an Ubuntu VPS]] 
 +  * http://badberg-online.com/2017/05/01/haproxy-mit-mehreren-domains-und-ssl/
 +  * http://www.fromdual.com/making-haproxy-high-available-for-mysql-galera-cluster
 +  * [[https://github.com/wikimedia/puppet/tree/production/modules/haproxy|puppet module for haproxy]]
 +
 +
 +===== Installation =====
 +
 +Es sollte möglichst aktuelle Versionen benutzt werden, eine extra Paketquelle bietet sich an um die aktuellste stabile Version zu erhalten die noch nicht in der Distribution enthalten ist:
 +Für Debian z.B. die [[https://haproxy.debian.net/|HAProxy packages]].
 +
 +
 +===== Grundlagen =====
 +
 +  * global: Grundlegende Einstellungen die übergreifend gelten
 +  * defaults: vordefiniterte Einstellungen falls nicht explizit angegeben
 +  * frontend: client -> haproxy
 +  * backend: haproxy -> Server/node
 +  * acls: 
 +
 +===== Frontends =====
 +
 +[[https://github.com/Aidaho12/haproxy-wi|haproxy-wi]]
 +[[https://computingforgeeks.com/how-to-manage-haproxy-servers-from-a-web-interface/|How to Manage HAProxy servers from a Web Interface]]
 +
 +==== Statistiken ====
 +
 +...
 +
 +
 +===== Konfiguration =====
 +
 +Eine Beispiel-Webapp "WEBAPP1", drei Server/nodes (10.0.0.1, 10.0.0.2, 10.0.0.3), ein SSL-Bundle liegt in /etc/ssl/private/WEBAPP1-certbundle-crt-chain-and-privkey.pem .
 +
 +<file>
 +# Sample Config für haproxy 1.7.x - see documentation: https://cbonte.github.io/haproxy-dconv/1.7/configuration.html
 +global
 +  log /dev/log  local0
 +  log /dev/log  local1 notice
 +  chroot /var/lib/haproxy
 +  stats socket /run/haproxy/admin.sock mode 660 level admin
 +  stats timeout 30s
 +  user haproxy
 +  group haproxy
 +  daemon
 +
 +  # Default SSL material locations
 +  ca-base /etc/ssl/certs
 +  crt-base /etc/ssl/private
 +
 +  # default ciphers client <-> haproxy
 +  ssl-default-bind-ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA:EECDH:EDH+AESGCM:EDH:ECDH+AESGCM:ECDH+AES:ECDH:HIGH:MEDIUM:!RC4:!3DES:!CAMELLIA:!SEED:!aNULL:!MD5:!eNULL:!LOW:!EXP:!DSS:!PSK:!SRP
 +  # default ciphers haproxy <-> server
 +  ssl-default-server-ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA:EECDH:EDH+AESGCM:EDH:ECDH+AESGCM:ECDH+AES:ECDH:HIGH:MEDIUM:!RC4:!3DES:!CAMELLIA:!SEED:!aNULL:!MD5:!eNULL:!LOW:!EXP:!DSS:!PSK:!SRP
 +  ssl-default-bind-options no-sslv3 no-tls-tickets
 +  # SSL DH 2048 (default is 1024 but with warning):
 +  tune.ssl.default-dh-param 2048
 +
 +
 +defaults
 +  log   global
 +  mode  http
 +  option        httplog
 +  option        dontlognull
 +
 +  timeout connect 5000ms
 +  retries 2
 +  timeout client 10000ms
 +  timeout server 10000ms
 +  timeout queue 60000ms
 +  timeout http-request 15000ms
 +  timeout http-keep-alive 15000ms
 +  # assign client to other node if his current node dies:
 +  option redispatch
 +  # insert X-Forwarted-For-Header with client-IP:
 +  option forwardfor
 +  # close server-conn, but leave client-facing conn open ( https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#option%20http-server-close ):
 +  option http-server-close
 +
 +  errorfile 400 /etc/haproxy/errors/400.http
 +  errorfile 403 /etc/haproxy/errors/403.http
 +  errorfile 408 /etc/haproxy/errors/408.http
 +  errorfile 500 /etc/haproxy/errors/500.http
 +  errorfile 502 /etc/haproxy/errors/502.http
 +  errorfile 503 /etc/haproxy/errors/503.http
 +  errorfile 504 /etc/haproxy/errors/504.http
 +
 +  #frontend WEBAPP1-http
 +  #  bind *:80
 +  #  default_backend WEBAPP1
 +  #
 +  #frontend WEBAPP1-https
 +  #  bind *:443 ssl crt /etc/ssl/private/WEBAPP1-certbundle-crt-chain-and-privkey.pem
 +  #  default_backend WEBAPP1
 +  #  # HSTS:
 +  #  rspadd Strict-Transport-Security:\ max-age=31536000
 +
 +frontend WEBAPP1-http-with-redirect-to-https
 +  bind *:80
 +  bind *:443 ssl crt /etc/ssl/private/WEBAPP1-certbundle-crt-chain-and-privkey.pem
 +  # Redirect if HTTPS is *not* used
 +  redirect scheme https code 301 if !{ ssl_fc }
 +  # HSTS:
 +  rspadd Strict-Transport-Security:\ max-age=31536000 
 +
 +  # haproxy stats:
 +  stats enable
 +  stats uri /haproxy?stats
 +  stats realm haproxy
 +  stats auth MEIN_BENUTZER:GEHEIMES_Passwort
 +  # stats auth Another_User:passwd
 +  
 +  default_backend WEBAPP1
 +
 +
 +backend WEBAPP1
 +  balance roundrobin
 +      # other options: static Round Robin (static-rr), Least Connections (leastconn), Source (source), URI (uri) and URL parameter (url_param).
 +  # "maxconn 200" limits simultaneous requests to 200, see: https://www.haproxy.com/de/blog/play_with_maxconn_avoid_server_slowness_or_crash/
 +  server node1 10.0.0.1:80 check maxconn 200
 +  server node2 10.0.0.2:80 check maxconn 200
 +  server node3 10.0.0.3:80 check maxconn 200
 +
 +  # Nodes with SSL and verification of SSL-Traffic (possible: none|all)
 +  # server node1 10.0.0.1:443 check ssl verify all maxconn 200
 +  # server node2 10.0.0.2:443 check ssl verify all maxconn 200
 +  # server node3 10.0.0.1:443 check ssl verify all maxconn 200
 +</file>
 +
 +
 +
 +==== Konfiguration aufteilen ====
 +
 +Eine einzige Konfigurationsdatei haproxy.conf wäre allen Einstellungen ist nicht ideal für eine Automatisierung. Die Lösung ist der Parameter "-f", damit können Verzeichnisnamen angeben werden die dann *.cfg-Dateien in alphabetischer Reihenfolge einliest (siehe "-f <cfgfile|cfgdir>" in http://cbonte.github.io/haproxy-dconv/1.7/management.html ).
 +
 +In der Datei ''/etc/default/haproxy'':
 +<file>
 +# Change the config file location if needed, only *.cfg-files are loaded in lexical order!
 +CONFIG="/etc/haproxy/conf.d"
 +</file>
 +
 +Nun legen wir die Config-Dateien in /etc/haproxy/conf.d mit Endung .cfg ab und nach Neustart werden die von dort geladen.
 +
 +:!: die globale Konfiguration sollte als erstes eingelesen werden, z.B. bei einer Benennnung mit ''00_haproxy.cfg'', weitere Dateien könnten dann ''50_backend1.cfg'', 50_backend2.cfg usw. heißen.
 +==== SNI ====
 +
 +FIXME testen, Quelle: https://stuff-things.net/2016/11/30/haproxy-sni/
 +
 +
 +=== fallback für nicht-SNI Clients ===
 +
 +[[https://gist.github.com/PiBa-NL/6301624|komplexes SNI-fallback]]
 +
 +
 +=== wildcard-Angabe bei SNI ===
 +
 +https://stackoverflow.com/questions/31262448/can-i-use-wildcard-sni-matching-with-haproxy
 +
 +
 +=== unterschiedliche SSL-settings bei SNI ===
 +
 +https://serverfault.com/questions/662662/haproxy-with-sni-and-different-ssl-settings
 +
 +
 +  * use a different IP or port for each cert (so it is a different bind line and you can therefor apply the configuration you need on a per certificate/“bind” basis)
 +  * if you need everything on a single IP:port combination, the only way to achieve this currently with haproxy is to configure the frontend as TCP mode without any SSL and switching to different TCP backends based on SNI values and then configure those TCP backends to point to dedicated SSL frontends, where you have a different SSL configuration (with and without client certificates). You can use unix sockets or linux abstrace namespace sockets, instead of using the loopback TCP stack.
 +  * 
 +Quelle: https://discourse.haproxy.org/t/haproxy-1-6-with-sni-and-different-ssl-settings-per-hostname/698
 +
 +
 +<file>
 +acl domain_strong_SSL   req_ssl_sni -i www.superSecure.com
 +acl domain_standard_SSL req_ssl_sni -i www.normal.com
 +
 +use_backend strong_SSL if domain_strong_SSL
 +use_backend standard_SSL  if domain_standard_SSL
 +
 +
 +frontend strong_SSL
 +  bind *:443 ssl crt /path/to/cert force-tlsv12 ciphers MY_STRONG_CIPHERSTRING
 +  [...]
 +frontend standard_SSL
 +  bind *:443 ssl crt /path/to/cert force-tlsv12 ciphers MY_RELAXED_CIPHERSTRING
 +  [...]
 +</file>
 +
 +
 +==== letsencrypt integrieren ====
 +
 +https://gridscale.io/community/tutorials/haproxy-ssl/
 +===== Verwaltung =====
 +
 +...die integrierte Stats-Seite (s.o.)
 +
 +
 +==== hatop ====
 +
 +apt install hatop
 +
 +in der haproxy.cfg den stats-socket: <file>stats socket /run/haproxy/admin.sock mode 660 level admin</file>
 +und los gehts:<code bash>hatop -s /run/haproxy/admin.sock</code>