HAProxy无法绑定socket [0.0.0.0:8888]。

24

我在CentOS 7上构建了一个HAProxy,并开启了端口8080的统计页面。它似乎正常工作。

当我将端口设置为8888时,HAProxy不起作用并给了我一些反馈。
之后,我尝试了许多方法来解决这个问题,但问题仍然存在。

有人可以帮我处理这个问题吗?

以下是系统信息

haproxy.cfg

/etc/haproxy/haproxy.cfg
端口8080可以使用,8888无法工作。

    # [HAPROXY DASHBOARD]
        listen  stats :8888
        mode http
        stats enable
        stats hide-version
        stats realm Haproxy\ Statistics
        stats uri /
        stats auth haproxy:haproxy
        stats refresh 10s

服务状态

运行命令:service haproxy status

systemd[1]: Started HAProxy Load Balancer.
haproxy-systemd-wrapper[2358]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cf...id -Ds
haproxy-systemd-wrapper[2358]: [ALERT] 012/095413 (2359) : Starting proxy stats: cannot bind socket [0.0.0.0:8888]
haproxy-systemd-wrapper[2358]: haproxy-systemd-wrapper: exit, haproxy RC=256

/etc/sysctl.conf
有人说这可能是一个虚拟 IP 的问题,所以我按照指示添加以下设置,然后运行 sysctl -p。

net.ipv4.ip_nonlocal_bind=1

网络配置

ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
    link/ether 00:15:5d:0a:09:05 brd ff:ff:ff:ff:ff:ff
    inet 192.168.4.117/24 brd 192.168.4.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe0a:905/64 scope link
       valid_lft forever preferred_lft forever

监听端口

使用命令:ss --listening

[root@localhost ~]# ss --listening
Netid State      Recv-Q Send-Q                                                                                  Local Address:Port                                                                                      Peer Address:Port
nl    UNCONN     0      0                                                                                                rtnl:NetworkManager/792                                                                                               *
nl    UNCONN     0      0                                                                                                rtnl:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                rtnl:avahi-daemon/671                                                                                               *
nl    UNCONN     0      0                                                                                                rtnl:4195096                                                                                               *
nl    UNCONN     4352   0                                                                                             tcpdiag:ss/3772                                                                                               *
nl    UNCONN     768    0                                                                                             tcpdiag:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                   6:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                   7:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                   7:systemd/1                                                                                               *      
nl    UNCONN     0      0                                                                                                   7:dbus-daemon/680                                                                                               *
nl    UNCONN     0      0                                                                                                   9:auditd/640                                                                                               *     
nl    UNCONN     0      0                                                                                                   9:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                   9:systemd/1                                                                                               *      
nl    UNCONN     0      0                                                                                                  10:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                  11:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                  15:iprdump/723                                                                                               *    
nl    UNCONN     0      0                                                                                                  15:systemd/1                                                                                               *      
nl    UNCONN     0      0                                                                                                  15:-4124                                                                                                 *
nl    UNCONN     0      0                                                                                                  15:systemd-logind/679                                                                                               *
nl    UNCONN     0      0                                                                                                  15:NetworkManager/792                                                                                               *
nl    UNCONN     0      0                                                                                                  15:iprinit/713                                                                                               *    
nl    UNCONN     0      0                                                                                                  15:-4107                                                                                                 *
nl    UNCONN     0      0                                                                                                  15:-4125                                                                                                 *
nl    UNCONN     0      0                                                                                                  15:-4119                                                                                                 *
nl    UNCONN     0      0                                                                                                  15:iprupdate/710                                                                                               *  
nl    UNCONN     0      0                                                                                                  15:-4118                                                                                                 *
nl    UNCONN     0      0                                                                                                  15:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                  15:-4117                                                                                                 *
nl    UNCONN     0      0                                                                                                  15:tuned/676                                                                                               *      
nl    UNCONN     0      0                                                                                                  16:kernel                                                                                                *
nl    UNCONN     0      0                                                                                                  18:kernel                                                                                                *
u_str LISTEN     0      128                                                                           /run/lvm/lvmetad.socket 11542                                                                                                * 0
u_str LISTEN     0      128                                                                       /run/systemd/journal/stdout 6697                                                                                                 * 0
u_dgr UNCONN     0      0                                                                         /run/systemd/journal/socket 6700                                                                                                 * 0
u_dgr UNCONN     0      0                                                                                            /dev/log 6702                                                                                                 * 0
u_dgr UNCONN     0      0                                                                              /run/systemd/shutdownd 11321                                                                                                * 0
u_dgr LISTEN     0      128                                                                                 /run/udev/control 11338                                                                                                * 0
u_str LISTEN     0      100                                                                                      public/flush 18726                                                                                                * 0
u_str LISTEN     0      100                                                                                      public/showq 18741                                                                                                * 0
u_str LISTEN     0      30                                                               /var/run/NetworkManager/private-dhcp 17003                                                                                                * 0
u_dgr UNCONN     0      0                                                                   @/org/freedesktop/systemd1/notify 11259                                                                                                * 0
u_str LISTEN     0      100                                                                                    private/tlsmgr 18708                                                                                                * 0
u_str LISTEN     0      30                                                                    /var/run/NetworkManager/private 16518                                                                                                * 0
u_str LISTEN     0      128                                                                      /var/run/avahi-daemon/socket 13986                                                                                                * 0
u_str LISTEN     0      128                                                                   /var/run/dbus/system_bus_socket 13998                                                                                                * 0
u_str LISTEN     0      100                                                                                   private/rewrite 18711                                                                                                * 0
u_str LISTEN     0      100                                                                                    private/bounce 18714                                                                                                * 0
u_str LISTEN     0      100                                                                                     private/defer 18717                                                                                                * 0
u_str LISTEN     0      100                                                                                     private/trace 18720                                                                                                * 0
u_str LISTEN     0      100                                                                                    private/verify 18723                                                                                                * 0
u_str LISTEN     0      100                                                                                  private/proxymap 18729                                                                                                * 0
u_str LISTEN     0      100                                                                                private/proxywrite 18732                                                                                                * 0
u_str LISTEN     0      100                                                                                      private/smtp 18735                                                                                                * 0
u_str LISTEN     0      100                                                                                     private/relay 18738                                                                                                * 0
u_str LISTEN     0      100                                                                                     private/error 18744                                                                                                * 0
u_str LISTEN     0      100                                                                                     private/retry 18747                                                                                                * 0
u_str LISTEN     0      100                                                                                   private/discard 18750                                                                                                * 0
u_str LISTEN     0      100                                                                                     private/local 18753                                                                                                * 0
u_str LISTEN     0      100                                                                                   private/virtual 18756                                                                                                * 0
u_str LISTEN     0      100                                                                                      private/lmtp 18759                                                                                                * 0
u_str LISTEN     0      100                                                                                     private/anvil 18762                                                                                                * 0
u_str LISTEN     0      100                                                                                    private/scache 18765                                                                                                * 0
u_str LISTEN     0      100                                                                                     public/pickup 18697                                                                                                * 0
u_str LISTEN     0      100                                                                                    public/cleanup 18701                                                                                                * 0
u_str LISTEN     0      100                                                                                       public/qmgr 18704                                                                                                * 0
u_str LISTEN     0      30                                                                               /run/systemd/private 11261                                                                                                * 0
u_dgr UNCONN     0      0                                                                                                   * 14733                                                                                                * 6700
u_dgr UNCONN     0      0                                                                                                   * 15011                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 12659                                                                                                * 12658
u_dgr UNCONN     0      0                                                                                                   * 18818                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 15244                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 16991                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 12644                                                                                                * 6700
u_dgr UNCONN     0      0                                                                                                   * 12658                                                                                                * 12659
u_dgr UNCONN     0      0                                                                                                   * 19513                                                                                                * 6700
u_dgr UNCONN     0      0                                                                                                   * 29994                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 13899                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 16528                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 30457                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 18632                                                                                                * 6702
u_dgr UNCONN     0      0                                                                                                   * 16504                                                                                                * 6702
raw   UNCONN     0      0                                                                                                  :::ipv6-icmp                                                                                             :::*     
tcp   UNCONN     0      0                                                                                                   *:ipproto-5353                                                                                              *:*  
tcp   UNCONN     0      0                                                                                                   *:ipproto-50900                                                                                              *:* 
tcp   LISTEN     0      100                                                                                         127.0.0.1:smtp                                                                                                 *:*
tcp   LISTEN     0      128                                                                                                 *:ssh                                                                                                  *:*
tcp   LISTEN     0      100                                                                                               ::1:smtp                                                                                                :::*
tcp   LISTEN     0      128                                                                                                :::ssh                                                                                                 :::*

2
你检查过端口是否已经被占用了吗?netstat -anp | grep ":8888" - kwarunek
我已经检查过了。未使用。 - Code Man
1
检查 SELinux 规则,查看 /var/log/audit/audit.log。 - Rhim
10个回答

81

首先感谢你们。
我用以下命令解决了这个问题。
setsebool -P haproxy_connect_any=1

对我有用!


10
如果 Stack Overflow 上有一个「捐赠一美元」的按钮,你绝对会得到一美元 =)。实际上,这将是一个很棒的功能 - 人们会更多地使用这个平台并赚取钱! - John Humphreys
5
我希望我能给这条评论一美元。 - user2818782
参考链接:https://www.systutorials.com/docs/linux/man/8-haproxy_selinux/请返回翻译后的文本。 - NoOne
不幸的是,在FreeBSD和OpnSense上无法运行(没有这样的命令)。 :( - NoOne
我想更好的方法是使用audit2allow创建自定义SELinux策略模块。参考链接 - Christoph Lösch
显示剩余2条评论

15

在/etc/sysctl.conf文件中添加net.ipv4.ip_nonlocal_bind=1。

运行sysctl -p命令。

重启haproxy服务(service restart haproxy)。它将会生效。


3
我猜这是一个 SELinux 的问题。 尝试使用 setenforce 0 命令,然后重启 HAProxy 服务。如果此更改后 HAProxy 可以正常工作,则恢复强制执行状态,并使用 setsebool -P haproxy_connect_any 命令更改可能影响该问题的 SELinux 布尔值,再次重启服务即可。

2

对我有效的方法是关闭运行在我想使用的端口(6443)上的任何服务:

$fuser -k 6443/tcp

然后我运行了:

$sudo systemctl restart haproxy

1

setsebool可以指导正确方向。 这是一个SELinux问题。尝试安装SELinux的工具链: yum install policycoreutils policycoreutils-python selinux-policy selinux-policy-targeted libselinux-utils setroubleshoot-server setools setools-console mcstrans

通过键入“selinux permissive”按下“录制按钮”,然后尝试启动服务。它们会失败。 然后: grep haprox /var/log/audit/audit.log | audit2allow -M haproxy 并通过激活haproxy权限来激活它。

semodule -i haproxy.pp

完成!


1
你并不总是需要执行dosetsebool -P haproxy_connect_any=1 对我来说,无法绑定的端口实际上正在运行。所以在我的情况下(在Ubuntu上),我使用fuser tcp/8888它显示它正在某个XXXX端口上运行。因此,通过执行fuser -k tcp/8888来清除它。在我的情况下,它成功地启动了。

0

我在OpnSense防火墙(FreeBSD)上收到了类似的消息:Starting frontend <FrontendName>: cannot bind socket <IP:port>

我绑定了一个域名,结果发现它指向我的公共地址,这是个问题。使用回环地址使HAProxy正常工作。

这个与Linux相关的答案也给出了一些提示:https://dev59.com/vOk6XIcBkEYKwwoYDPoQ#41009557......因为我记得这在pfSense上可以工作。这里可能还有更多的魔法等待我们去发现!


0

Code Man的答案是可行的,但你也可以使用以下方法来管理端口的selinux:

yum -y install policycoreutils-python
semanage port -m -t http_port_t -p tcp 8080
systemctl restart haproxy
systemctl status haproxy

0

延续Code Man的答案

setsebool -P haproxy_connect_any=1

我正在运行RHEL,设置SELinux布尔值的方式如下:

setsebool -P haproxy_connect_any on


-1

/etc/sysconfig/selinux SELINUX=permissive

重新启动

对我有用


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接