{"id":13775792,"url":"https://github.com/hashicorp/consul-haproxy","last_synced_at":"2025-10-01T13:31:32.726Z","repository":{"id":18277169,"uuid":"21442406","full_name":"hashicorp/consul-haproxy","owner":"hashicorp","description":"Consul HAProxy connector for real-time configuration","archived":true,"fork":false,"pushed_at":"2014-10-21T20:51:40.000Z","size":420,"stargazers_count":277,"open_issues_count":0,"forks_count":43,"subscribers_count":320,"default_branch":"master","last_synced_at":"2025-01-08T13:07:06.556Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hashicorp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-02T22:30:00.000Z","updated_at":"2024-10-03T05:10:05.000Z","dependencies_parsed_at":"2022-08-28T10:11:33.057Z","dependency_job_id":null,"html_url":"https://github.com/hashicorp/consul-haproxy","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fconsul-haproxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fconsul-haproxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fconsul-haproxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fconsul-haproxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hashicorp","download_url":"https://codeload.github.com/hashicorp/consul-haproxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234874641,"owners_count":18900006,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T17:01:50.161Z","updated_at":"2025-10-01T13:31:27.450Z","avatar_url":"https://github.com/hashicorp.png","language":"Go","funding_links":[],"categories":["\u003ca id=\"d03d494700077f6a65092985c06bf8e8\"\u003e\u003c/a\u003e工具"],"sub_categories":["\u003ca id=\"d3069cac6097830d12f5933c9c8b7a77\"\u003e\u003c/a\u003eHAProxy"],"readme":"# DEPRECATED: consul-haproxy\n\n**Deprecated!** This project is deprecated. Consul HAProxy has been replaced by [Consul Template](https://github.com/hashicorp/consul-template). This repository is kept for history and legacy purposes. Please use Consul Template instead.\n\n---\n\nThis project provides `consul-haproxy`, a daemon for dynamically\nconfiguring HAProxy using data from Consul.\n\nThe daemon watches any number of backends for updates, and when\nappropriate renders an configuration template for HAProxy and then\ninvokes a reload command, which can gracefully reload HAProxy. This\nallows for a zero-downtime reload of HAProxy which is populated by\nConsul.\n\n## Download \u0026 Compilation\n\nDownload a release from the [releases page](https://github.com/hashicorp/consul-haproxy/releases), or compile from source:\n\n```\n$ make\n$ ./bin/consul-haproxy -h\n```\n\n## Usage\n\nThe `consul-haproxy` command takes a number of CLI flags:\n\n* `-addr` - Provides the HTTP address of a Consul agent. By default this\n  assumes a local agent at \"127.0.0.1:8500\".\n\n* `-backend` - Backend specification. Can be provided multiple times.\n  The specification of a backend is documented below.\n\n* `-dry` - Dry run. Emit config file to stdout.\n\n* `-f` - Path to config file, overwrites CLI flags. The format of the\n  file is documented below.\n\n* `-in`- Path to a template file. This is the template that is rendered\n  to generate the configuration file at `-out`. It uses the Golang templating\n  system. Docs for that are [here](http://golang.org/pkg/text/template/).\n  Can be provided multiple times. If specified multiple times, specify the\n  same number of paths with `-out`.\n\n* `-out` - Path to output configuration file. This path must be writable\n  by `consul-haproxy` or the file cannot be updated. This can be specified\n  multiple times.\n\n* `-reload` - Command to invoke to reload configuration. This command can\n  be any executable, and should be used to reload HAProxy. This is invoked\n  only after the configuration file is updated.\n\n* `-quiet` - Quiet specifies a duration of time to wait for no updates\n  before writing out the new configuration. This allows for waiting until\n  a service stabilizes to prevent many different reloads.\n\n* `-max-wait` - Max wait is used to limit how waiting is done for a quiet\n  period before forcing a reload. This defaults to 4x the `-quiet` value.\n  As an example, if `-quiet=30s` but the backends are constantly flapping,\n  a refresh will be forced after 2 minutes.\n\nIn addition to using CLI flags, `consul-haproxy` can be configured using a\nfile given the `-f` flag. A configuration file overrides any values given by\nthe CLI unless otherwise specified. The configuration file should be a JSON\nobject with the following keys:\n\n* `address` - Same as `-addr` CLI flag.\n* `backends` - A list of backend specifications. This is merged with any\n  backends provided via the CLI.\n* `dry_run` - Same as `-dry` CLI flag.\n* `paths` - Same as `-out` CLI flag. . This value should be a list of paths and\n  is merged with any paths provided via the CLI.\n* `reload_command` - Same as `-reload` CLI flag.\n* `templates` - Same as `-in` CLI flag. This value should be a list of templates\n  and is merged with any paths provided via the CLI.\n* `quiet` - Same as `-quiet` CLI flag.\n* `max_wait` - Same as `-max-wait` CLI flag.\n\n## Backend Specification\n\nOne of the key configuration values to `consul-haproxy` is the backends that\nit should monitor. These are the service entries that are watched for changes\nand used to populate the configuration file. The syntax for a backend is:\n\n    backend_name=tag.service@datacenter:port\n\nThe specification provides a variable name for the template `backend_name`,\nwhich is defined as the entries that match the given tag and service for a specific\ndatacenter. A port can also be provided, which overrides the port specified by\nthe service. The tag, datacenter, and port are all optional and can be omitted.\n\nBelow are a few examples:\n\n* `app=release.webapp` - This defines a template variable `app` which watches for\n  the `webapp` service, filtering on the `release` tag.\n\n* `db=mysql@east-aws:5500` - This defines a template variable `db` which watches for\n  the `mysql` service in the `east-aws` datacenter, using port 5500.\n\nA useful features is the ability to specify multiple backends with the same variable\nname. This causes the nodes to be merged. This can be used to merge nodes with various\ntags, or different datacenters together. As an example, we can define:\n\n    app=webapp@dc1\n    app=webapp@dc2\n    app=webapp@dc3\n\nThis backend specification sets `app` variable to be the union of the servers\nin the `dc1`, `dc2`, and `dc3` datacenters.\n\n## Template Language\n\nThe template language is the Golang text/template package, which is\n[fully documented here](http://golang.org/pkg/text/template/). However, the\nbasic usage is quite simple.\n\nAs an example, suppose we define a simple backends with:\n\n    app=webapp@east-aws:8000\n    cache=redis\n\n\nWe might provide a very basic template like:\n\n    global\n        daemon\n        maxconn 256\n\n    defaults\n        mode tcp\n        timeout connect 5000ms\n        timeout client 60000ms\n        timeout server 60000ms\n\n    listen http-in\n        bind *:80{{range .app}}\n        {{.}} maxconn 32{{end}}\n\n    listen cache-in\n        bind *:4444{{range .cache}}\n        {{.}} maxconn 64{{end}}\n\nThis will populate the `http-in` block with the servers\nin the `app` backend, and the `cache-in` with the servers\nin the `cache` backend. This template will be re-rendered when\nany of those servers changing, allowing for dynamic updates.\n\n## Example\n\nWe run the example below against our\n[NYC demo server](http://nyc3.demo.consul.io). This lets you set\nquickly test consul-haproxy.\n\nFirst lets create a simple template:\n\n    global\n        daemon\n        maxconn 256\n\n    defaults\n        mode tcp\n        timeout connect 5000ms\n        timeout client 60000ms\n        timeout server 60000ms\n\n    listen http-in\n        bind *:8000{{range .c}}\n        {{.}}{{end}}\n\nNow, we can run the following to get our output configuration:\n\n    consul-haproxy -addr=demo.consul.io -in in.conf -backend \"c=consul@nyc3:80\" -backend \"c=consul@sfo1:80\" -dry\n\nWhen this runs, we should see something like the following:\n\n    global\n        daemon\n        maxconn 256\n\n    defaults\n        mode tcp\n        timeout connect 5000ms\n        timeout client 60000ms\n        timeout server 60000ms\n\n    listen http-in\n        bind *:8000\n        server 0_nyc3-consul-1_consul 192.241.159.115:80\n        server 0_nyc3-consul-2_consul 192.241.158.205:80\n        server 0_nyc3-consul-3_consul 198.199.77.133:80\n        server 1_sfo1-consul-2_consul 162.243.155.82:80\n        server 1_sfo1-consul-1_consul 107.170.195.169:80\n        server 1_sfo1-consul-3_consul 107.170.195.158:80\n\n## Varnish Example\n\n\nderived from the varnish director example from the varnish page: https://www.varnish-cache.org/docs/trunk/users-guide/vcl-backends.html#directors\n\n\timport directors;    # load the directors\n\t{{range .c}}\n\tbackend {{.Node}}_{{.ID}} { \n\t    .host = \"{{.IP}}\";\n\t    .port = \"{{.Port}}\";\n\t}{{end}}\n\n\tsub vcl_init {\n\t    new bar = directors.round_robin();\n\t{{range .c}}\n\t    bar.add_backend({{.Node}}_{{.ID}});{{end}}\n\t}\n\n\tsub vcl_recv {\n\t    # send all traffic to the bar director:\n\t    set req.backend_hint = bar.backend();\n\t}\n\nNow, we run the following command:\n\n    consul-haproxy -addr=demo.consul.io -in in.conf -backend \"c=consul@nyc1:80\" -backend \"c=consul@sfo1:80\" -dry\n\nThe following should return:\n\n\tbackend 0_nyc1-server-1_consul {\n\t    .host = \"192.241.159.115\";\n\t    .port = \"80\";\n\t}\n\tbackend 0_nyc1-server-3_consul {\n\t    .host = \"198.199.77.133\";\n\t    .port = \"80\";\n\t}\n\tbackend 0_nyc1-server-2_consul {\n\t    .host = \"162.243.162.228\";\n\t    .port = \"80\";\n\t}\n\tbackend 1_sfo1-server-3_consul {\n\t    .host = \"107.170.196.151\";\n\t    .port = \"80\";\n\t}\n\tbackend 1_sfo1-server-2_consul {\n\t    .host = \"107.170.195.154\";\n\t    .port = \"80\";\n\t}\n\tbackend 1_sfo1-server-1_consul {\n\t    .host = \"162.243.153.242\";\n\t    .port = \"80\";\n\t}\n\n\tsub vcl_init {\n\t    new bar = directors.round_robin();\n\n\t    bar.add_backend(0_nyc1-server-1_consul);\n\t    bar.add_backend(0_nyc1-server-3_consul);\n\t    bar.add_backend(0_nyc1-server-2_consul);\n\t    bar.add_backend(1_sfo1-server-3_consul);\n\t    bar.add_backend(1_sfo1-server-2_consul);\n\t    bar.add_backend(1_sfo1-server-1_consul);\n\t}\n\n\tsub vcl_recv {\n\t    # send all traffic to the bar director:\n\t    set req.backend_hint = bar.backend();\n\t}\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fconsul-haproxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhashicorp%2Fconsul-haproxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fconsul-haproxy/lists"}