# High-available 1C web-xml with HAproxy
Цель: сделать подключение к 1C для получения xml высокодоступным.
Нам нужно:
- безболезненно обновлять как балансеры так и backend серверы
- безболезненно обновлять и быстро менять версии 1С на backend серверах
Данная статья покажет как реализовать эти задачи.
У нас будет 2 общих ip на три ноды. haproxy привязан к ip.
## Файл hosts на всех нодах haproxy кластера
```
#cat /etc/hosts
10.0.0.141 co-1c-web-balancer-1
10.0.0.142 co-1c-web-balancer-2
10.0.0.143 co-1c-web-balancer-3
```

## Ставим нужные пакеты на всех нодах haproxy кластера
```
#timedatectl set-timezone Europe/Moscow
#dnf config-manager --set-enabled ha
#dnf install pcs pacemaker fence-agents-all -y
```
Rocky 9
```
#dnf config-manager --set-enabled highavailability
```
## Открываем нужные порты на всех нодах haproxy кластера
```
#firewall-cmd --permanent --add-service=high-availability
#firewall-cmd --reload
```
## На всех нодах haproxy кластера:
```
#systemctl enable pcsd --now
#passwd hacluster
```
## На первой ноде co-1c-web-balancer-1 создаем кластер и запускаем его:
```
#pcs host auth co-1c-web-balancer-1 co-1c-web-balancer-2 co-1c-web-balancer-3
Username: hacluster
Password:
co-1c-web-balancer-2: Authorized
co-1c-web-balancer-1: Authorized
co-1c-web-balancer-3: Authorized
#pcs cluster setup --start co-1c-web-1-haproxy-cluster co-1c-web-balancer-1 co-1c-web-balancer-2 co-1c-web-balancer-3
#pcs cluster enable --all
co-1c-web-balancer-1: Cluster Enabled
co-1c-web-balancer-2: Cluster Enabled
co-1c-web-balancer-3: Cluster Enabled
#pcs cluster start --all
co-1c-web-balancer-3: Starting Cluster...
co-1c-web-balancer-1: Starting Cluster...
co-1c-web-balancer-2: Starting Cluster...
#pcs property set stonith-enabled=false
#pcs property set symmetric-cluster=true
```
## Ставим haproxy на каждой ноде
```
#dnf install haproxy -y
```
## На первой ноде добавляем конфиг для haproxy /etc/haproxy/haproxy.cfg
```yaml=
global
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 5000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode tcp
timeout connect 5s
timeout client 50s
timeout server 50s
frontend http
bind :8080
default_backend stats
backend stats
mode http
stats enable
stats refresh 30s
stats enable
stats uri /
stats refresh 1s
stats show-legends
stats auth admin:AaKm924m1
frontend www-https
bind :443 ssl crt /etc/ssl/your_domain.pem
# Redirect HTTP to HTTPS
#redirect scheme https code 301 if !{ ssl_fc }
mode http
use_backend webservice83-https if { hdr(host) -i webservice83.your_domain.ru }
use_backend webservice82-https if { hdr(host) -i webservice82.your_domain.ru }
#Backend to use if no URL specified
default_backend webservice83-https
backend webservice83-https
mode http
balance roundrobin
option forwardfor
reqadd X-Forwarded-Proto:\ https
server co-1c-web-linux-1 10.0.0.144:80
server co-1c-web-linux-2 10.0.0.145:80
backend webservice82-https
mode http
option forwardfor
reqadd X-Forwarded-Proto:\ https
server co-1c-web-linux-3 10.0.0.146:80
frontend www-http
bind :80
mode http
use_backend webservice83-http if { hdr(host) -i webservice83.your_domain.ru }
use_backend webservice82-http if { hdr(host) -i webservice82.your_domain.ru }
#Backend to use if no URL specified
default_backend webservice83-http
backend webservice83-http
mode http
balance roundrobin
option forwardfor
server co-1c-web-linux-1 10.0.0.144:80
server co-1c-web-linux-2 10.0.0.145:80
backend webservice82-http
mode http
option forwardfor
server co-1c-web-linux-3 10.0.0.146:80
```
## На всех нодах создаем файл с сертификатом
```
#vi etc/ssl/your_domain.pem
#
```
## Далее нам нужно настроить синхронизацию конфигурации haproxy между нодами
Для данной задачи будем использовать lsyncd. Ставим его на первой ноде
```
#dnf install lsyncd -y
#vi ~/.ssh/root_web
#chmod 0600 ~/.ssh/root_web
#ssh -i ~/.ssh/root_web root@co-1c-web-balancer-2
#ssh -i ~/.ssh/root_web root@co-1c-web-balancer-3
#touch /etc/lsyncd.exclude
#vi /etc/lsyncd.conf
```
Конфигурационный файл lsyncd
```
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
nodaemon = false
}
sync {
default.rsyncssh,
source="/etc/haproxy",
excludeFrom ="/etc/lsyncd.exclude",
host="co-1c-web-balancer-2",
targetdir="/etc/haproxy",
rsync = {
archive=true,
compress=true,
temp_dir="/etc/lsyncd",
update=true,
links=true,
times=true,
protect_args=true
},
delay= 160,
ssh = {
port = 22,
identityFile = "~/.ssh/root_web",
options = { User = 'root', StrictHostKeyChecking = 'no' }
}
}
sync
{
default.rsyncssh,
source="/etc/haproxy",
excludeFrom ="/etc/lsyncd.exclude",
host="co-1c-web-balancer-3",
targetdir="/etc/haproxy",
rsync = {
archive=true,
compress=true,
temp_dir="/etc/lsyncd",
update=true,
links=true,
times=true,
protect_args=true
},
delay= 180,
ssh = {
port = 22,
identityFile = "~/.ssh/root_web",
options = { User = 'root', StrictHostKeyChecking = 'no' }
}
}
```
## На второй и третьей нодах
```
#mkdir /etc/lsyncd/
```
## Запускаем lsyncd на первой ноде и ставим его в автозагрузку
```
#systemctl enable lsyncd --now
```
## На всех нодах кластера прописываем дополнительные правила в firewalld
```
#firewall-cmd --zone=public --permanent --add-port=8080/tcp
#firewall-cmd --permanent --add-service=http
#firewall-cmd --permanent --add-service=https
#firewall-cmd --reload
```
## На первой ноде добавляем ресурсы cluster_ip_1 cluster_ip_2 в кластер
```
#pcs resource create cluster_ip_1 ocf:heartbeat:IPaddr2 ip=10.0.0.26 cidr_netmask=24 op monitor interval=10s --group co-1c-web-1_haproxy_cluster
#pcs resource create cluster_ip_2 ocf:heartbeat:IPaddr2 ip=10.0.0.29 cidr_netmask=24 op monitor interval=10s --group co-1c-web-1_haproxy_cluster
```
## Меняем приоритеты
```
#pcs constraint location co-1c-web-1_haproxy_cluster prefers co-1c-web-balancer-1=30 co-1c-web-balancer-2=20 co-1c-web-balancer-3=10
```
## На первой ноде добавляем ресурс systemd:haproxy в кластер
```
#pcs resource create haproxy systemd:haproxy op monitor interval=5s --group co-1c-web-1_haproxy_cluster
```
## Zabbix на всех нодах
Добавляем на балансере шаблон Template App Pacemaker
```
#vi /etc/zabbix/zabbix_agent2.d/pacemaker.conf
UserParameter=pacemakerd.status, sudo /usr/sbin/crm_mon --as-xml
#chown zabbix:zabbix /etc/zabbix/zabbix_agent2.d/pacemaker.conf
#mcedit /etc/sudoers
zabbix ALL=NOPASSWD:/usr/sbin/crm_mon --as-xml
#systemctl restart zabbix-agent2.service
```
## Миграция ресурсов для обслуживания балансеров
```
#pcs resource move co-1c-web-1_haproxy_cluster co-1c-web-balancer-2
```
Вернуть назад
```
#pcs resource move co-1c-web-1_haproxy_cluster co-1c-web-balancer-1
```
## Logging
Нам нужно видеть логи от haproxy
На каждой ноде
```
#dnf install rsyslog -y
#systemctl enable rsyslog --now
```
Редактируем конфигурацию haproxy
```
#vi /etc/haproxy/haproxy.cfg
```
Нам нужно добавить в секцию global
```
log /dev/log local0 notice
```
В секцию default
```
log global
```
В секции frontend www-https и frontend www-http
```
log /dev/log local0 info
option httplog
```
Создадим каталог dev и перезапустим haproxy
```
#mkdir /var/lib/haproxy/dev
#systemctl restart haproxy.service
```
Настроим rsyslog
```
#vi /etc/rsyslog.d/99-haproxy.conf
$AddUnixListenSocket /var/lib/haproxy/dev/log
# Send HAProxy messages to a dedicated logfile
:programname, startswith, "haproxy" {
/var/log/haproxy.log
stop
}
#systemctl restart rsyslog
```
Документация:
https://www.haproxy.com/blog/introduction-to-haproxy-logging/
https://www.digitalocean.com/community/tutorials/how-to-configure-haproxy-logging-with-rsyslog-on-rocky-linux-8-quickstart
SSL:
```
option forwardfor
reqadd X-Forwarded-Proto:\ https
```
https://www.haproxy.com/blog/ssl-offloading-impact-on-web-applications/
Healthcheck:
https://www.haproxy.com/blog/how-to-enable-health-checks-in-haproxy/