Question

haproxy - cannot bind socket error - ubuntu 20.04

I followed the instructions here https://kifarunix.com/install-and-setup-haproxy-on-ubuntu-20-04/ & have almost set up HTTPS. The function beam.smp which is specific to couchdb was bound to 5984 & hence not allowing haproxy to start.

Error log from terminal:

Oct 04 08:11:58 AgentDost systemd[1]: haproxy.service: Scheduled restart job, restart counter is at 4.
Oct 04 08:11:58 AgentDost systemd[1]: Stopped HAProxy Load Balancer.
Oct 04 08:11:58 AgentDost systemd[1]: Starting HAProxy Load Balancer...
Oct 04 08:11:59 AgentDost haproxy[31476]: [ALERT] 276/081158 (31476) : Starting frontend couch.tezi.app: cannot bind socket [174.138.120.68:5984]
Oct 04 08:11:59 AgentDost haproxy[31476]: Proxy webapps started.
Oct 04 08:11:59 AgentDost haproxy[31476]: Proxy webapps started.
Oct 04 08:11:59 AgentDost haproxy[31476]: [ALERT] 276/081158 (31476) : Starting proxy stats: cannot bind socket [174.138.120.68:5984]
Oct 04 08:11:59 AgentDost systemd[1]: haproxy.service: Main process exited, code=exited, status=1/FAILURE
Oct 04 08:11:59 AgentDost systemd[1]: haproxy.service: Failed with result 'exit-code'.
Oct 04 08:11:59 AgentDost systemd[1]: Failed to start HAProxy Load Balancer.

So I stopped the service to be able to bind the haproxy service to the port. Now beam.smp is not bound to the port but still haproxy is not able to takeover. What could be the reason here?

Oct 04 08:11:58 AgentDost systemd[1]: haproxy.service: Scheduled restart job, restart counter is at 4.
Oct 04 08:11:58 AgentDost systemd[1]: Stopped HAProxy Load Balancer.
Oct 04 08:11:58 AgentDost systemd[1]: Starting HAProxy Load Balancer...
Oct 04 08:11:59 AgentDost haproxy[31476]: [ALERT] 276/081158 (31476) : Starting frontend couch.tezi.app: cannot bind socket [174.138.120.68:5984]
Oct 04 08:11:59 AgentDost haproxy[31476]: Proxy webapps started.
Oct 04 08:11:59 AgentDost haproxy[31476]: Proxy webapps started.
Oct 04 08:11:59 AgentDost haproxy[31476]: [ALERT] 276/081158 (31476) : Starting proxy stats: cannot bind socket [174.138.120.68:5984]
Oct 04 08:11:59 AgentDost systemd[1]: haproxy.service: Main process exited, code=exited, status=1/FAILURE
Oct 04 08:11:59 AgentDost systemd[1]: haproxy.service: Failed with result 'exit-code'.
Oct 04 08:11:59 AgentDost systemd[1]: Failed to start HAProxy Load Balancer.
Oct 04 08:11:59 AgentDost systemd[1]: haproxy.service: Scheduled restart job, restart counter is at 5.
Oct 04 08:11:59 AgentDost systemd[1]: Stopped HAProxy Load Balancer.
Oct 04 08:11:59 AgentDost systemd[1]: haproxy.service: Start request repeated too quickly.
Oct 04 08:11:59 AgentDost systemd[1]: haproxy.service: Failed with result 'exit-code'.
Oct 04 08:11:59 AgentDost systemd[1]: Failed to start HAProxy Load Balancer.
root@AgentDost:~# sudo ss -4 -tlnp | grep 80
LISTEN    0         511                0.0.0.0:80               0.0.0.0:*        users:(("nginx",pid=751,fd=6),("nginx",pid=750,fd=6))                          
root@AgentDost:~# ss -4 -tlnp | grep 5984

I tried changing the frontend to port 6984 but even that doesn’t help.

Error log from terminal:

Oct 04 08:21:26 AgentDost systemd[1]: haproxy.service: Scheduled restart job, restart counter is at 4.
Oct 04 08:21:26 AgentDost systemd[1]: Stopped HAProxy Load Balancer.
Oct 04 08:21:26 AgentDost systemd[1]: Starting HAProxy Load Balancer...
Oct 04 08:21:27 AgentDost haproxy[31747]: [ALERT] 276/082126 (31747) : Starting frontend couch.tezi.app: cannot bind socket [174.138.120.68:6984]
Oct 04 08:21:27 AgentDost haproxy[31747]: Proxy webapps started.
Oct 04 08:21:27 AgentDost haproxy[31747]: Proxy webapps started.
Oct 04 08:21:27 AgentDost haproxy[31747]: [ALERT] 276/082126 (31747) : Starting proxy stats: cannot bind socket [174.138.120.68:5984]
Oct 04 08:21:27 AgentDost systemd[1]: haproxy.service: Main process exited, code=exited, status=1/FAILURE
Oct 04 08:21:27 AgentDost systemd[1]: haproxy.service: Failed with result 'exit-code'.
Oct 04 08:21:27 AgentDost systemd[1]: Failed to start HAProxy Load Balancer.
Oct 04 08:21:27 AgentDost systemd[1]: haproxy.service: Scheduled restart job, restart counter is at 5.
Oct 04 08:21:27 AgentDost systemd[1]: Stopped HAProxy Load Balancer.
Oct 04 08:21:27 AgentDost systemd[1]: haproxy.service: Start request repeated too quickly.
Oct 04 08:21:27 AgentDost systemd[1]: haproxy.service: Failed with result 'exit-code'.
Oct 04 08:21:27 AgentDost systemd[1]: Failed to start HAProxy Load Balancer.
root@AgentDost:~# ss -4 -tlnp | grep 6984
root@AgentDost:~# ss -4 -tlnp | grep 5984
LISTEN    0         128                0.0.0.0:5984             0.0.0.0:*        users:(("beam.smp",pid=31658,fd=16))     

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
July 28, 2023

Heya,

Based on the provided error logs, it seems that HAProxy is having trouble binding to the specified ports, whether it’s port 5984 or 6984. The error message “cannot bind socket” indicates that something is already using those ports, preventing HAProxy from taking over.

Here are some steps you can take to troubleshoot the issue:

  1. Identify Processes Using the Ports: Run the following commands to check which processes are currently using the ports 5984 and 6984:
sudo ss -4 -tlnp | grep ':5984' 
sudo ss -4 -tlnp | grep ':6984'

These commands will show you if any processes are bound to those ports. Make sure to stop or reconfigure the processes using these ports to free them up for HAProxy.

  1. Check CouchDB Configuration: Since you mentioned that “beam.smp” is specific to CouchDB and was previously bound to port 5984, ensure that CouchDB is correctly configured and not attempting to bind to the port anymore. Review CouchDB’s configuration files and logs to verify this.

  2. Check HAProxy Configuration: Double-check your HAProxy configuration file to ensure that there are no syntax errors or conflicting configurations. It’s possible that there might be a misconfiguration in the frontend section, causing the issue. Make sure you’ve set the correct backend server(s) and port(s) in the HAProxy configuration.

  3. Restart HAProxy: After verifying and making necessary changes in the configuration, restart the HAProxy service:

sudo systemctl restart haproxy
  1. Check Systemd Journal: If the issue persists, check the full HAProxy service logs in the systemd journal to get more detailed information about the error:
sudo journalctl -u haproxy -xe

This command will display the HAProxy service logs, including any additional error messages or information that might help you identify the problem.

  1. Verify Port Availability: Ensure that no other applications or services are currently using the desired ports (5984 and 6984) on your system. If needed, you can change the frontend ports in the HAProxy configuration to available ports.

Remember to always back up your configuration files before making changes, so you can easily revert to the previous state if necessary.

By following these steps and investigating the mentioned points, you should be able to identify the cause of the issue and resolve it. If you’re still facing difficulties, feel free to provide more details about your HAProxy and CouchDB configurations, and I’ll be glad to assist further.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel