upsync: NGINX-Modul zum Synchronisieren von Upstreams von Consul oder etcd
Installation
Sie können dieses Modul in jeder RHEL-basierten Distribution installieren, einschließlich, aber nicht beschränkt auf:
- RedHat Enterprise Linux 7, 8, 9 und 10
- CentOS 7, 8, 9
- AlmaLinux 8, 9
- Rocky Linux 8, 9
- Amazon Linux 2 und Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-upsync
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-upsync
Aktivieren Sie das Modul, indem Sie Folgendes an den Anfang von /etc/nginx/nginx.conf hinzufügen:
load_module modules/ngx_http_upsync_module.so;
Dieses Dokument beschreibt nginx-module-upsync v2.1.3, das am 20. November 2020 veröffentlicht wurde.
Nginx C-Modul, das Upstreams von Consul oder anderen synchronisieren kann. Es modifiziert dynamisch die Attribute der Backend-Server (Gewicht, max_fails,...), ohne dass NGINX neu geladen werden muss.
Es ist möglicherweise nicht immer praktisch, Konfigurationsdateien zu ändern und NGINX neu zu starten. Wenn Sie beispielsweise mit großen Verkehrsaufkommen und hoher Last zu kämpfen haben, erhöht das Neustarten von NGINX und das Neuladen der Konfiguration zu diesem Zeitpunkt die Last auf dem System weiter und kann die Leistung vorübergehend beeinträchtigen.
Das Modul ermöglicht es, ohne Beeinträchtigung der Leistung zu erweitern und zu skalieren.
Ein weiteres Modul, nginx-stream-upsync-module, unterstützt das NGINX-Stream-Modul (TCP-Protokoll), bitte beachten Sie dies.
Synopsis
nginx-consul:
http {
upstream test {
upsync 127.0.0.1:8500/v1/kv/upstreams/test/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
include /usr/local/nginx/conf/servers/servers_test.conf;
}
upstream bar {
server 127.0.0.1:8090 weight=1 fail_timeout=10 max_fails=3;
}
server {
listen 8080;
location = /proxy_test {
proxy_pass http://test;
}
location = /bar {
proxy_pass http://bar;
}
location = /upstream_show {
upstream_show;
}
}
}
http {
upstream test {
upsync 127.0.0.1:2379/v2/keys/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=etcd strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
include /usr/local/nginx/conf/servers/servers_test.conf;
}
upstream bar {
server 127.0.0.1:8090 weight=1 fail_timeout=10 max_fails=3;
}
server {
listen 8080;
location = /proxy_test {
proxy_pass http://test;
}
location = /bar {
proxy_pass http://bar;
}
location = /upstream_show {
upstream_show;
}
}
}
http {
upstream test {
least_conn; //hash $uri consistent;
upsync 127.0.0.1:8500/v1/kv/upstreams/test/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
upsync_lb least_conn; //hash_ketama;
include /usr/local/nginx/conf/servers/servers_test.conf;
}
upstream bar {
server 127.0.0.1:8090 weight=1 fail_timeout=10 max_fails=3;
}
server {
listen 8080;
location = /proxy_test {
proxy_pass http://test;
}
location = /bar {
proxy_pass http://bar;
}
location = /upstream_show {
upstream_show;
}
}
}
HINWEIS: Es wird empfohlen, strong_dependency auf "off" zu konfigurieren und die beim ersten Mal eingebundene Datei alle Server einzuschließen.
Beschreibung
Dieses Modul bietet eine Methode zur Entdeckung von Backend-Servern. Es unterstützt das dynamische Hinzufügen oder Löschen von Backend-Servern über Consul oder etcd und das dynamische Anpassen des Gewichts der Backend-Server. Das Modul zieht rechtzeitig die neue Backend-Serverliste von Consul oder etcd, um den NGINX-IP-Router zu synchronisieren. Nginx muss nicht neu geladen werden. Es hat einige Vorteile gegenüber anderen:
-
zeitnah
Das Modul sendet den Schlüssel an Consul/etcd mit einem Index. Consul/etcd vergleicht ihn mit seinem Index. Wenn sich der Index nicht ändert, bleibt die Verbindung fünf Minuten bestehen. In dieser Zeit führt jede Operation am Schlüssel-Wert sofort eine Rückmeldung durch.
-
Leistung
Das Abrufen von Consul/etcd entspricht einer Anfrage an NGINX. Das Aktualisieren des IP-Routers erfordert kein Neuladen von NGINX, sodass die Auswirkungen auf die NGINX-Leistung gering sind.
-
Stabilität
Selbst wenn ein Abruf fehlschlägt, wird der nächste upsync_interval abgerufen, sodass gewährleistet ist, dass der Backend-Server stabil Dienste bereitstellt. Und es unterstützt das Dumpen der neuesten Konfiguration an den Speicherort, sodass selbst wenn Consul/etcd ausfällt, NGINX jederzeit neu geladen werden kann.
-
health_check
Das nginx-upsync-module unterstützt das Hinzufügen oder Löschen von Server-Health-Checks und benötigt das nginx_upstream_check_module. Es wird empfohlen, nginx-upsync-module + nginx_upstream_check_module zu verwenden.
Direktiven
upsync
syntax: upsync $consul/etcd.api.com:$port/v1/kv/upstreams/$upstream_name/ [upsync_type=consul/etcd] [upsync_interval=seconds/minuten] [upsync_timeout=seconds/minuten] [strong_dependency=off/on]
kontext: upstream
beschreibung: Ziehen Sie Upstream-Server von Consul/etcd... .
Die Bedeutungen der Parameter sind:
-
upsync_interval
Zeitintervall zum Abrufen von Servern von Consul/etcd.
-
upsync_timeout
Zeitüberschreitung für die Anfrage zum Abrufen von Servern von Consul/etcd.
-
upsync_type
Typ des Servers, von dem die Server abgerufen werden.
-
strong_dependency
Wenn strong_dependency aktiviert ist, zieht NGINX bei jedem Start oder Neuladen Server von Consul/etcd.
upsync_dump_path
syntax: upsync_dump_path $path
standard: /tmp/servers_$host.conf
kontext: upstream
beschreibung: Dumpen Sie die Upstream-Backends in den $path.
upsync_lb
syntax: upsync_lb $load_balance
standard: round_robin/ip_hash/hash modula
kontext: upstream
beschreibung: Hauptsächlich für least_conn und konsistente Hashes. Wenn Sie eines von ihnen verwenden, müssen Sie angeben, dass Sie upsync_lb verwenden.
upstream_show
syntax: upstream_show
standard: keine
kontext: upstream
beschreibung: Zeigt alle Backend-Server eines bestimmten Upstreams an.
location /upstream_list {
upstream_show;
}
curl http://127.0.0.1:8500/upstream_list?test;
curl http://127.0.0.1:8500/upstream_list;
zeigt alle Upstreams an.
Consul_interface
Daten können aus dem Schlüssel/Wert-Speicher oder dem Dienstkatalog abgerufen werden. Im ersten Fall muss der Parameter upsync_type der Direktive consul sein. Zum Beispiel
upsync 127.0.0.1:8500/v1/kv/upstreams/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
Im zweiten Fall muss es consul_services sein.
upsync 127.0.0.1:8500/v1/catalog/service/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul_services strong_dependency=off;
Im dritten Fall muss es consul_health sein:
upsync 127.0.0.1:8500/v1/health/service/test upsync_timeout=6m upsync_interval=500ms upsync_type=consul_health strong_dependency=off;
Dienste mit fehlerhaften Health-Checks werden im Health-API als down markiert.
Sie können Backend-Server über consul_ui oder http_interface hinzufügen oder löschen. Unten sind Beispiele für den Schlüssel/Wert-Speicher.
http_interface Beispiel:
- hinzufügen
standard: gewicht=1 max_fails=2 fail_timeout=10 down=0 backup=0;
curl -X PUT http://$consul_ip:$port/v1/kv/upstreams/$upstream_name/$backend_ip:$backend_port
curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port
oder
curl -X PUT -d '{"weight":1, "max_fails":2, "fail_timeout":10}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port
-
löschen
curl -X DELETE http://$consul_ip:$port/v1/kv/upstreams/$upstream_name/$backend_ip:$backend_port -
gewicht-anpassen
curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port oder curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port -
Server als down markieren
curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10, \"down\":1}" http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port oder curl -X PUT -d '{"weight":2, "max_fails":2, "fail_timeout":10, "down":1}' http://$consul_ip:$port/v1/kv/$dir1/$upstream_name/$backend_ip:$backend_port -
überprüfen
curl http://$consul_ip:$port/v1/kv/upstreams/$upstream_name?recurse
Etcd_interface
Sie können Backend-Server über http_interface hinzufügen oder löschen.
Ähnlich wie etcd, http_interface Beispiel:
- hinzufügen
standard: gewicht=1 max_fails=2 fail_timeout=10 down=0 backup=0;
curl -X PUT http://$etcd_ip:$port/v2/keys/upstreams/$upstream_name/$backend_ip:$backend_port
curl -X PUT -d value="{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://$etcd_ip:$port/v2/keys/$dir1/$upstream_name/$backend_ip:$backend_port
-
löschen
curl -X DELETE http://$etcd_ip:$port/v2/keys/upstreams/$upstream_name/$backend_ip:$backend_port -
gewicht-anpassen
curl -X PUT -d "{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10}" http://$etcd_ip:$port/v2/keys/$dir1/$upstream_name/$backend_ip:$backend_port -
Server als down markieren
curl -X PUT -d value="{\"weight\":2, \"max_fails\":2, \"fail_timeout\":10, \"down\":1}" http://$etcd_ip:$port/v2/keys/$dir1/$upstream_name/$backend_ip:$backend_port -
überprüfen
curl http://$etcd_ip:$port/v2/keys/upstreams/$upstream_name
Check_module
Check-Modulunterstützung.
check-conf:
http {
upstream test {
upsync 127.0.0.1:8500/v1/kv/upstreams/test/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
check interval=1000 rise=2 fall=2 timeout=3000 type=http default_down=false;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
upstream bar {
server 127.0.0.1:8090 weight=1 fail_timeout=10 max_fails=3;
}
server {
listen 8080;
location = /proxy_test {
proxy_pass http://test;
}
location = /bar {
proxy_pass http://bar;
}
location = /upstream_show {
upstream_show;
}
location = /upstream_status {
check_status;
access_log off;
}
}
}
Code-Stil
Der Code-Stil basiert hauptsächlich auf style
siehe auch
- das nginx_upstream_check_module: https://github.com/alibaba/tengine/blob/master/src/http/ngx_http_upstream_check_module.c
- der nginx_upstream_check_module Patch: https://github.com/yaoweibin/nginx_upstream_check_module
- oder basierend auf https://github.com/xiaokai-wang/nginx_upstream_check_module
Quellabhängigkeit
- Cjson: https://github.com/kbranigan/cJSON
- http-parser: https://github.com/nodejs/http-parser
GitHub
Sie finden möglicherweise zusätzliche Konfigurationstipps und Dokumentationen für dieses Modul im GitHub-Repository für nginx-module-upsync.