LINUX.ORG.RU
решено ФорумAdmin

Haproxy не запускается при загрузке сервера

 , ,


1

1

Тестовая система из пяти виртуальных машин. На 192.168.56.147 Debian x86_64 GNU/Linux, на котором Haproxy слушает порт 7100.

После загрузки у службы haproxy статус failed. Но если запустить её командой «sudo systemctl start haproxy service», всё работает. Journalctl после загрузки пишет:

$ sudo journalctl | grep -i haproxy
Jun 26 16:00:43 debian systemd[1]: Starting HAProxy Load Balancer...
Jun 26 16:00:43 debian systemd[1]: Started HAProxy Load Balancer.
Jun 26 16:00:43 debian haproxy-systemd-wrapper[293]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Jun 26 16:00:43 debian haproxy-systemd-wrapper[293]: Note: setting global.maxconn to 2000.
Jun 26 16:00:43 debian haproxy-systemd-wrapper[293]: [ALERT] 176/160043 (296) : Starting frontend localnodes: cannot bind socket [192.168.56.147:7100]
Jun 26 16:00:43 debian haproxy-systemd-wrapper[293]: haproxy-systemd-wrapper: exit, haproxy RC=1
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Main process exited, code=exited, status=1/FAILURE
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Unit entered failed state.
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Failed with result 'exit-code'.
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Service hold-off time over, scheduling restart.
Jun 26 16:00:43 debian systemd[1]: Stopped HAProxy Load Balancer.
Jun 26 16:00:43 debian systemd[1]: Starting HAProxy Load Balancer...
Jun 26 16:00:43 debian systemd[1]: Started HAProxy Load Balancer.
Jun 26 16:00:43 debian haproxy-systemd-wrapper[319]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Jun 26 16:00:43 debian haproxy-systemd-wrapper[319]: Note: setting global.maxconn to 2000.
Jun 26 16:00:43 debian haproxy-systemd-wrapper[319]: [ALERT] 176/160043 (322) : Starting frontend localnodes: cannot bind socket [192.168.56.147:7100]
Jun 26 16:00:43 debian haproxy-systemd-wrapper[319]: haproxy-systemd-wrapper: exit, haproxy RC=1
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Main process exited, code=exited, status=1/FAILURE
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Unit entered failed state.
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Failed with result 'exit-code'.
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Service hold-off time over, scheduling restart.
Jun 26 16:00:43 debian systemd[1]: Stopped HAProxy Load Balancer.
Jun 26 16:00:43 debian systemd[1]: Starting HAProxy Load Balancer...
Jun 26 16:00:43 debian systemd[1]: Started HAProxy Load Balancer.
Jun 26 16:00:43 debian haproxy-systemd-wrapper[352]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Jun 26 16:00:43 debian haproxy-systemd-wrapper[352]: Note: setting global.maxconn to 2000.
Jun 26 16:00:43 debian haproxy-systemd-wrapper[352]: [ALERT] 176/160043 (355) : Starting frontend localnodes: cannot bind socket [192.168.56.147:7100]
Jun 26 16:00:43 debian haproxy-systemd-wrapper[352]: haproxy-systemd-wrapper: exit, haproxy RC=1
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Main process exited, code=exited, status=1/FAILURE
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Unit entered failed state.
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Failed with result 'exit-code'.
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Service hold-off time over, scheduling restart.
Jun 26 16:00:43 debian systemd[1]: Stopped HAProxy Load Balancer.
Jun 26 16:00:43 debian systemd[1]: Starting HAProxy Load Balancer...
Jun 26 16:00:43 debian systemd[1]: Started HAProxy Load Balancer.
Jun 26 16:00:43 debian haproxy-systemd-wrapper[388]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Jun 26 16:00:43 debian haproxy-systemd-wrapper[388]: Note: setting global.maxconn to 2000.
Jun 26 16:00:43 debian haproxy-systemd-wrapper[388]: [ALERT] 176/160043 (391) : Starting frontend localnodes: cannot bind socket [192.168.56.147:7100]
Jun 26 16:00:43 debian haproxy-systemd-wrapper[388]: haproxy-systemd-wrapper: exit, haproxy RC=1
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Main process exited, code=exited, status=1/FAILURE
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Unit entered failed state.
Jun 26 16:00:43 debian systemd[1]: haproxy.service: Failed with result 'exit-code'.
Jun 26 16:00:44 debian systemd[1]: haproxy.service: Service hold-off time over, scheduling restart.
Jun 26 16:00:44 debian systemd[1]: Stopped HAProxy Load Balancer.
Jun 26 16:00:44 debian systemd[1]: Starting HAProxy Load Balancer...
Jun 26 16:00:44 debian systemd[1]: Started HAProxy Load Balancer.
Jun 26 16:00:44 debian haproxy-systemd-wrapper[434]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Jun 26 16:00:44 debian haproxy-systemd-wrapper[434]: Note: setting global.maxconn to 2000.
Jun 26 16:00:44 debian haproxy-systemd-wrapper[434]: [ALERT] 176/160044 (435) : Starting frontend localnodes: cannot bind socket [192.168.56.147:7100]
Jun 26 16:00:44 debian haproxy-systemd-wrapper[434]: haproxy-systemd-wrapper: exit, haproxy RC=1
Jun 26 16:00:44 debian systemd[1]: haproxy.service: Main process exited, code=exited, status=1/FAILURE
Jun 26 16:00:44 debian systemd[1]: haproxy.service: Unit entered failed state.
Jun 26 16:00:44 debian systemd[1]: haproxy.service: Failed with result 'exit-code'.
Jun 26 16:00:44 debian systemd[1]: haproxy.service: Service hold-off time over, scheduling restart.
Jun 26 16:00:44 debian systemd[1]: Stopped HAProxy Load Balancer.
Jun 26 16:00:44 debian systemd[1]: haproxy.service: Start request repeated too quickly.
Jun 26 16:00:44 debian systemd[1]: Failed to start HAProxy Load Balancer.
Jun 26 16:00:44 debian systemd[1]: haproxy.service: Unit entered failed state.
Jun 26 16:00:44 debian systemd[1]: haproxy.service: Failed with result 'exit-code'.

Конфиг:

$ cat /etc/haproxy/haproxy.cfg
global
        debug

frontend localnodes
        bind 192.168.56.147:7100
        mode tcp
        default_backend nodes
        timeout client          1m
        acl block_1 src 192.168.56.1
        acl block_2 src 192.168.56.149

        use_backend ls1 if block_1
        use_backend ls2 if block_2

backend ls1
        mode tcp
        balance roundrobin
        server mytestweb1 192.168.56.150:7189
        timeout connect        10s
        timeout server          1m

backend ls2
        mode tcp
        balance roundrobin
        server mytestweb2 192.168.56.151:7189
        timeout connect        10s
        timeout server          1m

backend nodes
        mode tcp
        balance roundrobin
        server web01 192.168.56.150:7189
        server web02 192.168.56.151:7189
        timeout connect        10s
        timeout server          1m

После запуска вручную journalctl показывает:

Jun 26 16:11:04 debian systemd[1]: Starting HAProxy Load Balancer...
Jun 26 16:11:04 debian systemd[1]: Started HAProxy Load Balancer.
Jun 26 16:11:04 debian haproxy-systemd-wrapper[677]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
Jun 26 16:11:04 debian haproxy-systemd-wrapper[677]: Note: setting global.maxconn to 2000.

В чём проблема? Почему ошибка «cannot bind socket [192.168.56.147:7100]» возникает при автозагрузке и не возникает при ручном запуске?

Ответ: Haproxy запускается до того, как машина получает адрес по DHCP. Помогло сменить адрес на 0.0.0.0:

frontend localnodes
        bind 0.0.0.0:7100

Либо можно в /etc/systemd/system/multi-user.target.wants/haproxy.service в строку After добавить network-online.target

★★

Starting frontend localnodes: cannot bind socket [192.168.56.147:7100]

не может забиндиться на этот адрес. посмотри, вероятнее всего соответствующий сетевой интерфейс недоступен на момент запуска сервиса

eternal_sorrow ★★★★★ ()
Ответ на: комментарий от eternal_sorrow

Как это определить точно? И как заставить службу запускаться позднее?

Просмотрел journalctl — есть строки «Starting Raise network interfaces.» и «Started Raise network interfaces.», но «started» строк на 10 выше первого упоминания Haproxy.

olegd ★★ ()
Ответ на: комментарий от eternal_sorrow
$ cat /etc/systemd/system/multi-user.target.wants/haproxy.service
[Unit]
Description=HAProxy Load Balancer
Documentation=man:haproxy(1)
Documentation=file:/usr/share/doc/haproxy/configuration.txt.gz
After=network.target syslog.service
Wants=syslog.service

[Service]
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid"
EnvironmentFile=-/etc/default/haproxy
ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecStart=/usr/sbin/haproxy-systemd-wrapper -f $CONFIG -p $PIDFILE $EXTRAOPTS
ExecReload=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always

[Install]
WantedBy=multi-user.target

Не поможет ли добавить network.target в Wants? — Не помогло.

olegd ★★ ()
Последнее исправление: olegd (всего исправлений: 1)