Created
June 1, 2021 21:44
-
-
Save haproxytechblog/d8d02f5ddc01d14e0d05f0d9b5f34e9e to your computer and use it in GitHub Desktop.
Consul Service Discovery for HAProxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ /usr/bin/consul agent -config-dir=/etc/consul.d/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo add-apt-repository -y ppa:vbernat/haproxy-2.4 | |
$ sudo apt update | |
$ sudo DEBIAN_FRONTEND=noninteractive apt install -y haproxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config_version: 2 | |
name: haproxy1 | |
dataplaneapi: | |
host: 192.168.50.20 | |
port: 5555 | |
user: | |
- name: dataplaneapi | |
password: mypassword | |
insecure: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program api | |
command /usr/local/bin/dataplaneapi -f /etc/haproxy/dataplaneapi.yaml | |
no option start-on-reload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo systemctl restart haproxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"service": { | |
"name": "web", | |
"port": 80 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ consul services register ./web.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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