Search code examples
kuberneteskubernetes-helmconsul

how to expose ingress for Consul


I'm trying to add consul ingress to my project, and I'm using this GitHub repo as a doc for ui and ingress: here and as you can see unfortunately there is no ingress in doc, there is an ingressGateways which is not useful because doesn't create ingress inside Kubernetes(it can just expose URL to outside)

I have searched a lot, there are 2 possible options:

1: create extra deployment for ingress

2: create consul helm chart to add ingress deploy

(unfortunately I couldn't find a proper solution for this on the Internet)


Solution

  • Here is an example Docker compose file which configures Traefik to expose an entrypoint named web which listens on TCP port 8000, and integrates Traefik with Consul's service catalog for endpoint discovery.

    # docker-compose.yaml
    ---
    version: "3.8"
    services:
      consul:
        image: consul:1.8.4
        ports:
          - "8500:8500/tcp"
      traefik:
        image: traefik:v2.3.1
        ports:
          - "8000:8000/tcp"
        environment:
          TRAEFIK_PROVIDERS_CONSULCATALOG_CACHE: 'true'
          TRAEFIK_PROVIDERS_CONSULCATALOG_STALE: 'true'
          TRAEFIK_PROVIDERS_CONSULCATALOG_ENDPOINT_ADDRESS: http://consul:8500
          TRAEFIK_PROVIDERS_CONSULCATALOG_EXPOSEDBYDEFAULT: 'false'
          TRAEFIK_ENTRYPOINTS_web: 'true'
          TRAEFIK_ENTRYPOINTS_web_ADDRESS: ":8000"
    
    

    Below is a Consul service registration file which registers an application named web which is listening on port 80. The service registration includes a couple tags which instructs Traefik to expose traffic to the service (traefik.enable=true) over the entrypoint named web, and creates the associated routing config for the service.

    service {
      name = "web"
      port = 80
      tags = [
        "traefik.enable=true",
        "traefik.http.routers.web.entrypoints=web",
        "traefik.http.routers.web.rule=Host(`example.com`) && PathPrefix(`/myapp`)"
      ]
    }
    

    This can be registered into Consul using the CLI (consul service register web.hcl). Traefik will then discover this via the catalog integration, and configure itself based on the routing config specified in the tags.

    HTTP requests received by Traefik on port 8000 with an Host header of example.com and path of /myapp will be routed to the web service that was registered with Consul.

    Example curl command.

    curl --header "Host: example.com" http://127.0.0.1:8000/myapp
    

    This is a relatively basic example that is suitable for dev/test. You will need to define additional Traefik config parameters if you are deploying into a production Consul environment which is typically secured by access control lists (ACLs).