Go to file
Darshil Chanpura 3e76eeca76 build: fix in mage.go 2021-11-20 18:56:37 +05:30
checker metrics: added metrics for reload and states 2021-11-20 18:22:54 +05:30
init Added systemd configurations 2021-11-20 14:52:15 +05:30
sample Added samples and README.md 2021-11-20 14:29:04 +05:30
test Added working code 2021-11-20 14:12:23 +05:30
.gitignore Added magefile and minor edits for linter fixes 2021-11-20 16:02:44 +05:30
LICENSE Added license 2021-11-20 16:08:51 +05:30
README.md docs: updated usage 2021-11-20 18:29:29 +05:30
go.mod go: ran go get -u 2021-11-20 18:52:01 +05:30
go.sum go: ran go get -u 2021-11-20 18:52:01 +05:30
mage.go build: fix in mage.go 2021-11-20 18:56:37 +05:30
main.go metrics: added metrics for reload and states 2021-11-20 18:22:54 +05:30

README.md

Nginx Upstream Check

This project checks if the upstreams in mentioned file are up or not. It does a simple healthcheck periodically and updates the final file which is included in actual Nginx configuration.

Usage

$ nginx-upstream-check -h
Usage of ./nginx-upstream-check:
  -check-interval duration
        Interval period to check (default 10s)
  -check-path string
        Health Check path (default "/")
  -check-scheme string
        Health Check scheme (default "http")
  -match-response-code int
        Response code match (default 200)
  -metrics
        Enables metrics
  -metrics-addr string
        Address to listen on for metrics (default ":9137")
  -metrics-path string
        Path for metrics endpoint (default "/metrics")
  -nginx-pid-file string
        File containing Nginx PID (default "/run/nginx.pid")
  -upstream-config-file-final string
        Nginx File containing list of upstream servers that will be used
  -upstream-config-file-source string
        Nginx File containing list of upstream servers
  -version
        Prints version

Example

In most of the cases one can have basic nginx.conf that includes the upstreams, for example.

# nginx.conf
...

http {
    ...

    upstream some_upstream {
        include snippets/upstreams.conf
    }
    server {
        listen 80;
        server_name test.localhost;
        location / {
            proxy_pass http://some_upstream;
        }
    }

    ...
}

If the files are structured in following way, it will be static.

.
├── nginx.conf
└── snippets
    ├── upstreams.conf -> upstreams-without-healthcheck.conf
    ├── upstreams-with-healthcheck.conf
    └── upstreams-without-healthcheck.conf

If we keep upstreams-without-healthcheck.conf as static that means we can take that as source and do healthcheck on it. To have a filtered servers from upstream configuration we can run the nginx-upstream-checker with upstreams-with-healthcheck.conf as final and upstreams-without-healthcheck.conf as our source.

# (cd snippets/; ln -sf upstreams-with-healthcheck.conf upstreams.conf)
# ./nginx-upstream-check -upstream-config-file-final snippets/upstreams-with-healthcheck.conf -upstream-config-file-source snippets/upstreams-without-healthcheck.conf

Goals

  • Perform health check periodically
  • Update configuration based on the health check result, only if there is a difference
  • Reload configuration, only if there is a difference
  • Metrics
    • Healthy instances (gauge)
    • Number of Reloads (counter)
  • Longer health check like consider instance healthy if 5/5 checks pass and 2/5 to unhealthy

License

MIT