Skip to content

Adding Services to Consul

Henry Le Grys edited this page Apr 8, 2020 · 1 revision

ok so you've got your shiny VM, but now you need to actually receive some traffic! fortunately, we have the power of Consul service discovery.

setting up consul

  1. Create /var/lib/consul and /etc/consul
  2. Create /etc/consul/local.json. Fill in and replace the placeholders signified by <>:
{
	"data_dir": "/var/lib/consul",
	"node_name": "<hostname (without domain)>",
	"client_addr": "127.0.0.1",
	"join": "appserver.catgirls.dev"
}
  1. Download consul, stick it in your path (/usr/local/bin is a good spot) and make a service to start the agent:

consul agent -config-dir=/etc/consul

Example systemd unit:

[Unit]
Description=Consul agent
After=network.target

[Service]
# you might need User= and Group= keys.
ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul
ExecReload=/usr/local/bin/consul reload

[Install]
WantedBy=multi-user.target
  1. Start up the agent and check everything's gucci with a consul members

adding a new service

once consul is set up, adding services is super easy!

you just need to create a JSON file in /etc/consul. name it something descriptive, like my-cool-service.json, and fill it in:

{
	"service": {
		"name": "my-cool-service",
		"tags": [
			"see the below section"
		],
		"port": 8000
	}
}

What you fill in here is, of course, based on what you need.

HTTP proxying

If you've got a HTTP service, you'll want to add tags for Traefik. You can specify all sorts of tags, as detailed in the traefik docs, but the simplest setup looks like this:

"tags": [
	"traefik.enable=true"
]

With the above configuration, Traefik will proxy my-cool-service.thonk.dev to your node's local address, port 8000.

If you want to customise the domain (bearing in mind that for HTTPS, we currently use the DNS challenge, and until we upgrade to traefik 2 it will stay that way, so you must use a domain on the genprog account) you can specify the traefik.frontend.rule=Host:my-cool-subdomain.generalprogramming.org tag. Check the traefik docs linked above for more options.

Port forwarding

NOTE: will be enabled shortly, but currently not yet deployed. just gotta finish implementing some features first

Thanks to thonkwall, you can now proxy arbitrary traffic pointing at hv0.generalprogramming.org to your VM. You'll need to add the enabling tag for thonkwall:

"tags": [
	"thonkwall.enable=true"
]

This will add a rule redirecting traffic from hv0.generalprogramming.org:PORT to your-vm-address:PORT. If you want to change settings, you should first use the service keys address and port, but you can also use tags:

"tags": [
	"thonkwall.enable=true",
	"thonkwall.host=destination.address.or.ip",
	"thonkwall.port=8008",
	"thonkwall.protocol=udp"
]
Clone this wiki locally