Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Last active May 27, 2020 00:37
Show Gist options
  • Save juanpabloaj/f7dc576924f8e6c49222bcc098edd619 to your computer and use it in GitHub Desktop.
Save juanpabloaj/f7dc576924f8e6c49222bcc098edd619 to your computer and use it in GitHub Desktop.
traefik consul header matching

traefik and consul, manual register

consul and traefik up

docker-compose up -d

services up

docker-compose -f docker-compose-services.yaml up -d

Register manually

curl --request PUT --data @consul_register_whoami1.json http://127.0.0.1:8500/v1/agent/service/register
curl --request PUT --data @consul_register_whoami2.json http://127.0.0.1:8500/v1/agent/service/register

curl to whoami passing by traefik, filter with host

curl -H "Host:whoami" 0.0.0.0

Filter with prefix

curl http://127.0.0.1/whoami-alpha

Deregister

curl --request PUT http://127.0.0.1:8500/v1/agent/service/deregister/whoami1
curl --request PUT http://127.0.0.1:8500/v1/agent/service/deregister/whoami2

References

{
"ID": "whoami1",
"Name": "whoami",
"Address": "whoami1",
"Check": {
"DeregisterCriticalServiceAfter": "5m",
"http": "http://whoami1",
"Interval": "10s",
"Timeout": "5s"
},
"Port": 80
}
{
"ID": "whoami2",
"Name": "whoami-alpha",
"Address": "whoami2",
"tags": [
"traefik.enable=true",
"traefik.http.routers.whoami-alpha.rule=PathPrefix(`/whoami-alpha`)",
"traefik.http.routers.whoami-alpha.middlewares=whoami-alpha",
"traefik.http.middlewares.whoami-alpha.stripprefix.prefixes=/whoami-alpha"
],
"Port": 80
}
version: '3'
services:
whoami1:
image: containous/whoami
whoami2:
image: containous/whoami
version: '3'
services:
traefik:
image: traefik:v2.1 # The official Traefik docker image
command: |
--log.level=DEBUG
-c traefik.toml #--docker # Enables the web UI and tells Traefik to listen to docker
--api.insecure=true
#--providers.consulcatalog=true
#--providers.consulcatalog.refreshInterval=5s
#--providers.consulcatalog.endpoint.address=http://consul-server-bootstrap:8500
#--providers.consulcatalog.exposedByDefault=true
ports:
- "80:80" # The HTTP port
- "8080:8080" # The Web UI (enabled by --api)
volumes:
#- /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
- ${PWD}/traefik.toml:/traefik.toml
consul-agent-1: &consul-agent
image: consul:latest
command: "agent -retry-join consul-server-bootstrap -client 0.0.0.0"
consul-agent-2:
<<: *consul-agent
consul-agent-3:
<<: *consul-agent
consul-server-1: &consul-server
<<: *consul-agent
command: "agent -server -retry-join consul-server-bootstrap -client 0.0.0.0"
consul-server-2:
<<: *consul-server
consul-server-bootstrap:
<<: *consul-agent
ports:
- "8400:8400"
- "8500:8500"
- "8600:8600"
- "8600:8600/udp"
command: "agent -server -bootstrap-expect 3 -ui -client 0.0.0.0"
[api]
insecure = true
[providers.consulCatalog]
exposedByDefault = true
refreshInterval = "500ms"
#defaultRule = "PathPrefix(`/{{ normalize .Name }}`)"
[providers.consulCatalog.endpoint]
address = "consul-server-bootstrap:8500"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment