Consul Service Discovery
This section will guide you through integrating HAProxy Enterprise and Consul.
You can:
query Consul for the IP addresses of your internal services, and
fill these addresses into HAProxy Enterprise's configuration in real time.

We will pull configuration changes from the Consul service discovery registry through the HAProxy Enterprise Data Plane API.
See also
Example setup
We will consider an example setup with 3 machines with the following characteristics.
Hostname | IP address | Role | |
---|---|---|---|
hapee-lb | 192.168.50.20 | HAProxy Enterprise load balancer | |
consul-server | 192.168.50.21 | Consul server | |
webserver | 192.168.50.22 | Web server |
See also
Install the Consul Server
We will install a Consul server on a machine and run it as a Systemd service.
-
Download and install Consul on your Consul server machine.
For example, on a Debian-based Linux distribution:
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - $ sudo apt-add-repository \ "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" $ sudo apt-get update && sudo apt-get install consul
-
Replace the content of the /etc/consul.d/consul.hcl configuration file with the following.
datacenter = "dc1" server = true data_dir = "/opt/consul/" bind_addr = "192.168.50.21" client_addr = "192.168.50.21" bootstrap = true bootstrap_expect = 1 ui_config { enabled = true } enable_syslog = true log_level = "INFO"
Notable fields
Value
Description
datacenter
Consul datacenter name
The private networking environment all Consul agents belong to.
server
true
This Consul agent will run in server mode.
bind_addr and client_addr
Consul server machine's static IP address
IP address Consul agents will connect to when they join the cluster.
-
Start Consul.
$ sudo systemctl enable consul $ sudo systemctl start consul
-
To check if the Consul server is up and running, open the Consul web UI at http://<Consul Server IP address>:8500 in a web browser.
Check available nodes and services.
See also
Install a Consul agent on your web server machine
We will install and run a Consul agent on the web server machine.
-
Install Consul and create the following /etc/consul.d/consul.hcl file.
datacenter = "dc1" server = false data_dir = "/opt/consul/" bind_addr = "192.168.50.22" retry_join = ["192.168.50.21"] enable_syslog = true log_level = "INFO"
Notable fields
Value
Description
datacenter
Consul datacenter name
The private networking environment all Consul agents belong to.
server
false
This Consul agent will not run in server mode.
bind_addr
Web server machine's static IP address
IP address Consul agents will connect to when they join the cluster.
retry_join
Web server machine's static IP address
IP address of the Consul server, which the agent uses to join the Consul cluster.
-
Start the Consul agent.
$ sudo systemctl enable consul $ sudo systemctl start consul
-
To check if the Consul agent is up and running, open the Consul web UI at http://<Consul Server IP address>:8500 in a web browser.
Check available nodes.
The webserver node was created but does not contain any service as yet.
Register the web server as a service
We will register the web server as a Consul service.
-
Create a Consul service configuration file on the web server machine.
For example, create the web.json file with the following contents:
{ "service": { "name": "web", "port": 80 } }
Field
Description
port
Port of the listening web server.
name
Name that will display in the Consul registry.
To add more servers to the same backend, register more services with Consul. Keep the same name, web, in their JSON files.
-
Register your web server as a Consul service.
$ consul services register web.json
-
To check if the service was registered, open the Consul web UI at http://<Consul Server IP address>:8500 in a web browser.
To check the details of the service, click its name in the Services tab.
See also
Enable service discovery in HAProxy Enterprise
We will install the HAProxy Enterprise Data Plane API on the load balancer machine.
Follow the steps to install the HAProxy Data Plane API on your HAProxy Enterprise server.
-
To enable HAProxy Enterprise to use the Consul API, send a POST request to the HAProxy Enterprise Data Plane API's /v2/services_discovery/consul endpoint.
$ curl -u admin:adminpwd \ -H 'Content-Type: application/json' \ -d '{ "address": "192.168.50.21", "port": 8500, "enabled": true, "retry_timeout": 10 }' http://192.168.50.20:5555/v2/service_discovery/consul
The body of the request is a JSON string with the following fields:
Field
Description
address
The Consul server's IP address.
port
The Consul server's HTTP API port (i.e. 8500).
enabled
Whether to enable this integration.
retry_timeout
Duration in seconds in-between requesting data from the Consul server.
The HAProxy Enterprise Data Plane API now begins monitoring Consul's service catalog.
-
Check the /etc/hapee-1.6/hapee-lb.cfg file.
A new backend section contains the web server.
backend consul-backend-192.168.50.21-8500-web server SRV_V2u4y 192.168.50.22:80 check weight 128 server SRV_1NQXA 127.0.0.1:80 disabled weight 128 server SRV_DWcAP 127.0.0.1:80 disabled weight 128 server SRV_IIcqg 127.0.0.1:80 disabled weight 128
The HAProxy Enterprise Data Plane API has also added several disabled
server
lines.When you register more instances of the same service, HAProxy Enterprise fills in disabled server slots. You can thus scale up or down without a reload, in most cases.
You can now configure a frontend
section that routes traffic to this backend pool of servers.
See also
Next up
DNS Service Discovery