You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
Darshil Chanpura 3e76eeca76 build: fix in mage.go 2 months ago
checker metrics: added metrics for reload and states 2 months ago
init Added systemd configurations 2 months ago
sample Added samples and README.md 2 months ago
test Added working code 2 months ago
.gitignore Added magefile and minor edits for linter fixes 2 months ago
LICENSE Added license 2 months ago
README.md docs: updated usage 2 months ago
go.mod go: ran go get -u 2 months ago
go.sum go: ran go get -u 2 months ago
mage.go build: fix in mage.go 2 months ago
main.go metrics: added metrics for reload and states 2 months ago

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