Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Created June 1, 2021 21:44
Show Gist options
  • Save haproxytechblog/d8d02f5ddc01d14e0d05f0d9b5f34e9e to your computer and use it in GitHub Desktop.
Save haproxytechblog/d8d02f5ddc01d14e0d05f0d9b5f34e9e to your computer and use it in GitHub Desktop.
Consul Service Discovery for HAProxy
datacenter = "dc1"
server = true
data_dir = "/var/lib/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"
$ /usr/bin/consul agent -config-dir=/etc/consul.d/
$ sudo add-apt-repository -y ppa:vbernat/haproxy-2.4
$ sudo apt update
$ sudo DEBIAN_FRONTEND=noninteractive apt install -y haproxy
$ wget https://github.com/haproxytech/dataplaneapi/releases/download/v2.3.0/dataplaneapi_2.3.0_Linux_x86_64.tar.gz
$ tar -zxvf dataplaneapi_2.3.0_Linux_x86_64.tar.gz
$ sudo cp build/dataplaneapi /usr/local/bin/
$ sudo chmod +x /usr/local/bin/dataplaneapi
config_version: 2
name: haproxy1
dataplaneapi:
host: 192.168.50.20
port: 5555
user:
- name: dataplaneapi
password: mypassword
insecure: true
program api
command /usr/local/bin/dataplaneapi -f /etc/haproxy/dataplaneapi.yaml
no option start-on-reload
$ sudo systemctl restart haproxy
$ curl -u dataplaneapi:mypassword \
-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
datacenter = "dc1"
server = false
data_dir = "/var/lib/consul/"
bind_addr = "192.168.50.22"
retry_join = ["192.168.50.21"]
enable_syslog = true
log_level = "INFO"
{
"service": {
"name": "web",
"port": 80
}
}
$ consul services register ./web.json
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment