systemd를 사용하여 CentOS에서 HAProxy를 시작하는 방법

systemd를 사용하여 CentOS에서 HAProxy를 시작하는 방법

CentOS 7.5(64비트, 커널 3.10), HAProxy 1.8.13 및 systemd를 사용하여 간단한 웹 로드 밸런싱을 설정하려고 합니다. HAProxy 구성은 괜찮은 것 같지만 애플리케이션을 시작하면 머리가 아픕니다. init.d로 한 번 시도했지만 시스템 방향이 지정되어 현재 멈췄습니다. 거의 이틀 동안 원인을 찾고 있는데 Linux의 한계에 도달했으며 이 특정 동작의 원인을 어디에서도 찾을 수 없습니다. 이와 같은 대부분의 경우 haproxy 구성에 몇 가지 문제가 있지만 여기서는 그렇지 않습니다.

HAProxy 구성 파일 확인 출력

haproxy -f /etc/haproxy/haproxy.conf -c

Configuration file is valid

systemd를 사용하여 시작하려고 하면 Journalctl에 다음과 같은 출력이 표시됩니다.

journalctl -u haproxy.service

Aug 13 17:07:50 localhost.localdomain systemd[1]: Starting HAProxy Load Balancer...
Aug 13 17:07:50 localhost.localdomain systemd[1]: Started HAProxy Load Balancer.
Aug 13 17:07:50 localhost.localdomain systemd[1]: haproxy.service holdoff time over, scheduling restart.
Aug 13 17:07:50 localhost.localdomain systemd[1]: Starting HAProxy Load Balancer...
Aug 13 17:07:50 localhost.localdomain systemd[1]: Started HAProxy Load Balancer.
Aug 13 17:07:50 localhost.localdomain systemd[1]: haproxy.service holdoff time over, scheduling restart.
Aug 13 17:07:50 localhost.localdomain systemd[1]: Starting HAProxy Load Balancer...
Aug 13 17:07:50 localhost.localdomain systemd[1]: Started HAProxy Load Balancer.
Aug 13 17:07:50 localhost.localdomain systemd[1]: haproxy.service holdoff time over, scheduling restart.
Aug 13 17:07:50 localhost.localdomain systemd[1]: Starting HAProxy Load Balancer...
Aug 13 17:07:50 localhost.localdomain systemd[1]: Started HAProxy Load Balancer.
Aug 13 17:07:51 localhost.localdomain systemd[1]: haproxy.service holdoff time over, scheduling restart.
Aug 13 17:07:51 localhost.localdomain systemd[1]: Starting HAProxy Load Balancer...
Aug 13 17:07:51 localhost.localdomain systemd[1]: Started HAProxy Load Balancer.
Aug 13 17:07:51 localhost.localdomain systemd[1]: haproxy.service holdoff time over, scheduling restart.
Aug 13 17:07:51 localhost.localdomain systemd[1]: start request repeated too quickly for haproxy.service
Aug 13 17:07:51 localhost.localdomain systemd[1]: Failed to start HAProxy Load Balancer.
Aug 13 17:07:51 localhost.localdomain systemd[1]: Unit haproxy.service entered failed state.
Aug 13 17:07:51 localhost.localdomain systemd[1]: haproxy.service failed.

Systemctl은 다음 상태를 표시합니다.

systemctl status haproxy.service

[root@localhost sbin]# systemctl status haproxy.service
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Mo 2018-08-13 17:33:46 CEST; 24min ago
  Process: 1557 ExecStart=/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid (code=exited, status=0/SUCCESS)
  Process: 1556 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q (code=exited, status=0/SUCCESS)
 Main PID: 1557 (code=exited, status=0/SUCCESS)

Aug 13 17:33:46 localhost.localdomain systemd[1]: Started HAProxy Load Balancer.
Aug 13 17:33:46 localhost.localdomain systemd[1]: haproxy.service holdoff time over, scheduling restart.
Aug 13 17:33:46 localhost.localdomain systemd[1]: start request repeated too quickly for haproxy.service
Aug 13 17:33:46 localhost.localdomain systemd[1]: Failed to start HAProxy Load Balancer.
Aug 13 17:33:46 localhost.localdomain systemd[1]: Unit haproxy.service entered failed state.
Aug 13 17:33:46 localhost.localdomain systemd[1]: haproxy.service failed.

이 서비스의 systemd 파일은 대부분 haproxy 1.8.13과 함께 제공되는 샘플 파일에서 가져옵니다. 일부 구성 요소가 상대 경로를 처리할 수 없기 때문에 일부 자리 표시자/변수를 고정 값으로 대체했습니다.

nano /lib/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=network.target

[Service]
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/var/run/haproxy.pid"
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify

# The following lines leverage SystemD's sandboxing options to provide
# defense in depth protection at the expense of restricting some flexibility
# in your setup (e.g. placement of your configuration files) or possibly
# reduced performance. See systemd.service(5) and systemd.exec(5) for further
# information.

# NoNewPrivileges=true
# ProtectHome=true
# If you want to use 'ProtectSystem=strict' you should whitelist the PIDFILE,
# any state files and any other files written using 'ReadWritePaths' or
# 'RuntimeDirectory'.
# ProtectSystem=true
# ProtectKernelTunables=true
# ProtectKernelModules=true
# ProtectControlGroups=true
# If your SystemD version supports them, you can add: @reboot, @swap, @sync
# SystemCallFilter=~@cpu-emulation @keyring @module @obsolete @raw-io

[Install]
WantedBy=multi-user.target

앱을 시작했다가 다시 중지했다가 어느 시점에서 포기할 때까지 다시 시작하려고 시도하는 것 같습니다.

Bash에서 수동으로 애플리케이션을 시작하려고 하면 아무 일도 일어나지 않는 것 같습니다.

[root@localhost sbin]# /usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
[root@localhost sbin]#

ps 또는 top을 통해 표시되는 프로세스가 없습니다. 일부 로그를 얻으려고 했지만 rsyslog에서 내가 지정한 파일에 쓰고 싶지 않았기 때문에 rsyslog에서 아무것도 얻을 수 없었습니다.

haproxy 구성 파일을 부록으로 추가하지만 haproxy 자체를 기반으로 하면 괜찮은 것 같습니다.

# Rev1
# HAProxy Global and Default definitions
global
    log 127.0.0.1 local2
    chroot /var/lib/haproxy
    pidfile /var/run/haproxy.pid
    maxconn 4000
    user haproxy
    group haproxy
    daemon
        stats socket /var/run/haproxy.sock
        stats timeout 2m

defaults
    log                     global
    option                  dontlognull
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          300s
    timeout server          300s
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

frontend LB_https_vs
    bind *:443
    mode tcp
    option tcplog
    default_backend SP_https_group

backend LB_https_group
    mode tcp
    balance source
    option httpchk GET /MyPage/Logon.aspx
    hash-type consistent
    stick-table type ip size 200k expire 2000s
    stick on src table SP_https_group
    default-server inter 15000
    #server srv1 192.168.1.1:443 id 1 port 443 check
    server srv1 192.168.1.1:443 id 1 check
    server srv2 192.168.1.2:443 id 2 check
    server srv3 192.168.1.3:443 id 3 check
    server srv4 192.168.1.4:443 id 4 check
    server srv5 192.168.1.5:443 id 5 check

listen stats
    mode http
    bind :9000
    stats enable
    stats hide-version
    stats realm HAproxy-Statistics
    stats uri /haproxy_stats
    stats auth admin:default

어떤 아이디어가 있나요?

미리 감사드립니다

답변1

포그라운드에서 실행되는 모드에서 haproxy를 시작해야 합니다. 또한 Type=notifysystemd 알림 데몬을 사용하려면 haproxy가 필요합니다.

.from -Ws뿐만 아니라 해당 옵션을 사용하여 실행 해야 하는 것 같습니다 .-W--소스 코드의 도움말 출력:

#if defined(USE_SYSTEMD)
        "        -Ws master-worker mode with systemd notify support.\n"
#endif

또한 이는 haproxy가 systemd 지원으로 구축된 경우에만 작동한다는 점에 유의하세요. 이것이 귀하에게 해당되는지는 확실하지 않지만 --help 출력을 보면 확인할 수 있습니다(직접 지원되지 않는 것 같지만 --help알 수 없는 인수에 대한 사용법 정보를 인쇄하므로 --help도움이 될 것입니다. )

한 번 살펴보고 싶을 수도 있습니다버전 1.8에서 권장되는 haproxy.service 템플릿그리고GitHub의 최신, 최신 버전에 가까운 것을 사용하고 있는 것 같지만 약간의 차이점이 있습니다(예: -W... -Ws).

관련 정보