13 KiB
📌 Установка HAProxy на Ubuntu 20.04
На основе инструкции из https://itsecforu.ru/2019/07/15/⏳-настройка-балансировщика-нагрузки-h/
sudo apt-get update && sudo apt-get install haproxy -y
Создаём резервную копию файла:
sudo cp /etc/haproxy/haproxy.cfg{,.bak}
Редактируем конфигурацию (Описание конфигурационного файла HAProxy https://habr.com/ru/sandbox/34354/)
mcedit /etc/haproxy/haproxy.cfg
В конец файла добавляем пока только для перенаправления (для балансироки больше IP адресов):
frontend frontend-http
bind *:80
mode http
# ACL для определения запросов на проверку Let's Encrypt
acl is_certbot path_beg /.well-known/acme-challenge/
# Если это не запрос Let's Encrypt, перенаправляем на HTTPS
http-request redirect scheme https code 301 unless is_certbot
# Отправляем запросы Let's Encrypt на backend-certbot
use_backend backend-certbot if is_certbot
frontend LOADBALANCER-01
bind *:80
bind *:443 ssl crt /etc/haproxy/ssl/mqtt.kz.pem crt /etc/ssl/certs/bigfoottrade_kz.pem
mode http #режим работы HAProxy, в http режиме происходит анализ Layer 7 трафика
option httpclose #Закрывает пассивные соединения
http-request set-header x-Forwarded-for %[src]
http-request set-header x-Forwarded-uri %[url]
acl v_geoserver2 hdr(host) -i geoserver2.ccalm.org
use_backend geoserver2_ccalm_org if v_geoserver2
#Если различные нестандартные порты то так
acl v_locust_kz hdr_reg(host) -i ^locust\.kz(:.*)?$
use_backend b_locust_kz if v_locust_kz
#Перенаправление одной страницы по адресу http://geoserver2.ccalm.org/data/ на другой backend
acl v_geo_data hdr(host) -i geoserver2.ccalm.org/data
use_backend WEBSERVERS-01 if v_geo_data
default_backend WEBSERVERS-01
backend WEBSERVERS-01
balance roundrobin
server web10 127.0.0.1:8081 check inter 5s ssl verify none
option httpchk
backend geoserver2_ccalm_org
balance roundrobin
server web1 192.168.0.90:80 check
option httpchk
listen stats
bind *:8989
stats enable
stats uri /
stats realm Haproxy\ Statistics
stats auth igor:i123456
frontend f-RabbitMQ
mode tcp
bind 10.1.7.73:21000
default_backend b-RabbitMQ
backend b-RabbitMQ
mode tcp
server srv1 10.10.16.21:20000
Для перенаправления незащищённого HTTP трафика можно: xxxxxxxxxxxxxxx
Для использования SSL и перенаправляние по обычному сокету для начала нужно настроить pem файл объеденив crt и key (и незабыть изменить порт Apache с 433) cat /etc/ssl/certs/bigfoottrade_kz.crt /etc/ssl/certs/bigfoottrade_kz_ca.crt /etc/ssl/private/bigfoottrade_kz.key > /etc/haproxy/ssl/bigfoottrade_kz.pem cat AAA_Certificate_Services.crt GoGetSSL_RSA_DV_CA.crt istransit_kz.crt istransit_kz.key > istransit_kz.pem
SSLCertificateFile
SSLCertificateKeyFile
Для включения WEB статистики на 9000 порту добавить в конец конфигурации: listen stats bind *:8989 stats enable stats uri /stats stats realm Haproxy\ Statistics stats auth igor:i123456 Тестирую файл конфигурации:
haproxy -f /etc/haproxy/haproxy.cfg -c
Также можно в журнале посмотреть что написал HAProxy:
sudo journalctl -u haproxy --no-pager | tail -n 50
Перезагружаем:
sudo systemctl restart haproxy
Посмотреть что в журнале можно так: Посмотреть что в журнале можно так:
journalctl -u haproxy --since "1 minutes ago"
journalctl -u haproxy --since "30 minutes ago" | grep "backend"
administrator@app:~$ journalctl -u haproxy --since "30 minutes ago" | grep "backend" backend 'transit_acquiring_v01_kz' has no server available! backend 'transit_translation_v02_kz' has no server available! backend 'transit_mobile_kz' has no server available! backend 'transit_manager_kz' has no server available! backend 'transit_warehouse_kz' has no server available! backend 'transit_kgd_kz' has no server available! backend 'transit_monitoring_kz' has no server available!
И теперь должно открываться но адресу: http://data.ccalm.org:8989/ Обязательно проверить как установился SSL чекером: https://www.leaderssl.ru/tools/ssl_checker
📌 Бесплатный SSL сертификат Let’s Encrypt для HAPROXY
Бесплатный SSL сертификат Let’s Encrypt для HAPROXY https://serversforhackers.com/c/letsencrypt-with-haproxy
sudo apt-get update
sudo apt-get install -y certbot
Для удаления PPA нужно выполнить:
sudo apt-get remove certbot
sudo add-apt-repository --remove ppa:certbot/certbot
apt-get install -f
apt autoremove
Если включён файрволл то разрешаем порт:
sudo ufw allow 9080/tcp
Проверяем что автообновление в certbot работает для этого выполняем команду:
sudo systemctl status certbot.timer
Затем пытаемся выполнить пробный прогон при помощи команды:
sudo certbot renew --dry-run
Согласно инструкции модифицируем конфигурационный файл haproxy добавив во frontend это:
# ACL for detecting Let's Encrypt validtion requests
acl is_certbot path_beg /.well-known/acme-challenge/
use_backend backend-certbot if is_certbot
А также ещё один backend:
# Certbot backend
# Contains certbot stand-alone webserver
backend backend-certbot
mode http
server certbot 127.0.0.1:9080
Перезагрузить и выполнить команду:
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d locust.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d gotify.locust.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d git.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d ru.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d rug.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d kz.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d kzg.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d locust.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d test.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d data.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d geoserver2.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d geoserver.ccalm.org --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d stations.istt.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d uspdmanager.istt.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d tourist.istt.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d monitoring.infracos.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d aistransit.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d istransit.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d test.istransit.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d main.istransit.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d transit.istt.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d geovizor.com --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d mqtt.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d observer.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d rigor.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d pal.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d elektronnaya-ochered.kz --email irigm@mail.ru --agree-tos --non-interactive
certbot certonly --rsa-key-size 2048 --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 -d mcp.kz --email irigm@mail.ru --agree-tos --non-interactive
После генерации файлы будут в: /etc/letsencrypt/live/geoserver2.ccalm.org/fullchain.pem /etc/letsencrypt/live/geoserver2.ccalm.org/privkey.pem
Для обновления создаём файл /etc/letsencrypt/renew.sh (по моему этот скрипт не нужен так как рабтает сервис certbot.timer): #!/bin/bash certbot renew --standalone --preferred-challenges http --http-01-address 127.0.0.1 --http-01-port 9080 --post-hook "/etc/haproxy/prepare.sh" --quiet
Но для HAProxy нужно чтобы 2 файла были объединены поэтому добавляю спец скрипт /etc/haproxy/prepare.sh: #!/bin/bash
# Loop through all Let's Encrypt certificates
for CERTIFICATE in `find /etc/letsencrypt/live/* -type d`; do
CERTIFICATE=`basename $CERTIFICATE`
# Combine certificate and private key to single file
cat /etc/letsencrypt/live/$CERTIFICATE/fullchain.pem /etc/letsencrypt/live/$CERTIFICATE/privkey.pem > /etc/haproxy/ssl/$CERTIFICATE.pem
done
systemctl reload haproxy.service
Обновляем конфигурацию HAProxy добавив в frontend: bind *:443 ssl crt /etc/haproxy/ssl/mqtt.kz.pem
Устанавливаем права chmod +x /etc/haproxy/renew.sh chmod +x /etc/haproxy/prepare.sh И добавляем задание в cron "crontab -e" ниже текст это раз в 24 часа в 00:00:00 ночи: 0 0 * * * /bin/sh /etc/letsencrypt/renew.sh