systemdを使用してCentOSでHAProxyを起動する方法

systemdを使用してCentOSでHAProxyを起動する方法

CentOS 7.5(64ビット、カーネル3.10)、HAProxy 1.8.13、およびsystemdを使用して単純なWebロードバランシングを設定しようとしています。 HAProxyの設定は問題ないようですが、アプリケーションを起動すると頭が痛くなります。 init.dで一度試しましたが、システムの向きが指定され、現在停止しています。ほぼ2日間、原因を探していましたが、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)。

おすすめ記事