Nginx VTS Stats, Mockify - simulacro de API, comparaci贸n de Wiremock y Mockify

En esta publicaci贸n quer铆a hacer una demostraci贸n de Nginx VTS + Prometheus + Grafana. Para la demostraci贸n, era necesario que upstream pudiera emitir diferentes c贸digos http. Los siguientes proyectos podr铆an hacer esto: Mockify , escrito en Golang, y WireMock , escrito en Java.


Una peque帽a tabla de contenido.


  • instalaci贸n y configuraci贸n de Nginx VTS + Prometheus + Grafana;
  • Mockify es una API de emulaci贸n ligera y configurable escrita en Golang;
  • Comparaci贸n del uso de CPU para Mockify, escrito en Golang, y WireMock, escrito en Java.

M谩quina virtual de banco de pruebas:


inxi CPU: 8x Single Core Intel Xeon E312xx (Sandy Bridge) (-SMP-) speed: 2594 MHz Kernel: 3.10.0-957.1.3.el7.x86_64 x86_64 Up: 58m Mem: 474.9/32011.6 MiB (1.5%) Storage: 80.00 GiB (2.7% used) Procs: 149 Shell: bash 4.2.46 inxi: 3.0.35 

Config prometheus:


 global: scrape_interval: 5s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 5s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'nginx_vts_exporter' static_configs: - targets: ['localhost:9913'] 

Config Grafana est谩ndar. Tablero ID 2949.


Para Nginx VTS, necesita compilar nginx con el m贸dulo nginx-module-vts. Hacemos esto usando Nginx-builder . Su config:


 nginx_version: 1.16.1 output_package: rpm modules: - module: name: nginx-module-vts git_url: https://github.com/vozlt/nginx-module-vts.git git_tag: v0.1.18 

Instale el nginx ensamblado. Aqu铆 est谩 su configuraci贸n principal (no olvide especificar vhost_traffic_status_zone;):


 user nginx; worker_processes auto; worker_rlimit_nofile 40960; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { use epoll; worker_connections 1024; multi_accept on; } http { vhost_traffic_status_zone; include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log /var/log/nginx/access.log main; access_log off; sendfile on; tcp_nodelay on; tcp_nopush on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; open_file_cache max=200000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; } 

Crear configuraciones de host virtual:


 touch vhost1.conf vhost2.conf vhost3.conf vhost4.conf vhost5.conf 

Sus contenidos:


vhost1.conf:


 server { listen 80; server_name vhost1; location / { proxy_pass http://127.0.0.1:8001/102; } } 

vhost2.conf:


 server { listen 80; server_name vhost2; location / { proxy_pass http://127.0.0.1:8001/204; } } 

vhost3.conf:


 server { listen 80; server_name vhost3; location / { proxy_pass http://localhost:8001/304; } } 

vhost4.conf:


 server { listen 80; server_name vhost4; location / { proxy_pass http://localhost:8001/403; } } 

vhost5.conf:


 server { listen 80; server_name vhost5; location / { proxy_pass http://localhost:8001/503; } } 

Instale nginx-vts-exporter:


 wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz tar zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz cp nginx-vts-exporter-0.10.3.linux-amd64/nginx-vts-exporter /usr/local/bin/nginx-vts-exporter 

Cree el archivo /etc/systemd/system/nginx_vts_exporter.service:


 [Unit] Description=Nginx vts exporter Wants=network-online.target After=network-online.target [Service] ExecStart=/usr/local/bin/nginx-vts-exporter -nginx.scrape_uri=http://localhost:7070/status/format/json Restart=always RestartSec=3 [Install] WantedBy=default.target 

Cree el archivo de configuraci贸n VTS /etc/nginx/conf.d/vts-exporter.conf


 server { listen 7070; listen [::]:7070; location / { } location /status { vhost_traffic_status_display; vhost_traffic_status_display_format html; } } 

Agregue DNS de hosts virtuales a / etc / hosts:


 127.0.0.1 vhost1 127.0.0.1 vhost2 127.0.0.1 vhost3 127.0.0.1 vhost4 127.0.0.1 vhost5 

Estamos optimizando el kernel para las pruebas, ya que Wiremock dio un tiempo de espera con una gran cantidad de conexiones. Ahora puede no ser necesario, ya que reduce el n煤mero de conexiones al iniciar el punto de referencia de Apache. Cree el archivo /etc/sysctl.d/90-nginx.conf con el contenido:


 fs.file-max=100000 net.netfilter.nf_conntrack_max=1548576 net.ipv4.ip_local_port_range=10000 65000 net.ipv4.tcp_tw_reuse=1 net.core.somaxconn=15600 net.ipv4.tcp_fin_timeout=15 net.ipv4.tcp_tw_recycle=1 net.core.rmem_default=31457280 net.core.rmem_max=12582912 net.core.wmem_default=31457280 net.core.wmem_max=12582912 net.core.netdev_max_backlog=65536 net.core.optmem_max=25165824 net.ipv4.tcp_rmem=8192 87380 16777216 net.ipv4.udp_rmem_min=16384 net.ipv4.tcp_wmem=8192 65536 16777216 

Aplicar configuraci贸n


 sysctl -p /etc/sysctl.d/90-nginx.conf 

Instalar mockify-rpm


 yum -y install yum-plugin-copr yum copr enable antonpatsev/mockify-rpm yum -y install mockify systemctl start mockify 

Instalar Apache Benchmark:


 yum install -y httpd-tools 

Comenzamos una peque帽a prueba de nginx:


 while true; do ab -c 1 -n 1 -t 1 http://vhost1/; sleep 2; done while true; do ab -c 1 -n 1 -t 1 http://vhost2/; sleep 2; done while true; do ab -c 1 -n 1 -t 1 http://vhost3/; sleep 2; done while true; do ab -c 1 -n 1 -t 1 http://vhost4/; sleep 2; done while true; do ab -c 1 -n 1 -t 1 http://vhost5/; sleep 2; done 

Capturas de pantalla






Instalar wiremock:


 yum -y install yum-plugin-copr yum copr enable antonpatsev/wiremock-rpm yum -y install wiremock wiremock-popular-json systemctl start wiremock 

Tambi茅n en los archivos vhost1-vhost5 en nginx necesita cambiar el puerto de 8001 a 8080.


Pido disculpas por las capturas de pantalla irregulares.


A continuaci贸n se muestra la carga simulada de CPU y MEM al probar vhost1-vhost5



A continuaci贸n se muestra la carga de la CPU y la memoria MEM al probar vhost1



A continuaci贸n se muestra la carga de la CPU y la memoria MEM al probar vhost1-vhost2



A continuaci贸n se muestra la carga de la CPU y la memoria MEM al probar vhost1-vhost3



A continuaci贸n se muestra la carga de CPU y la memoria MEM al probar vhost1-vhost4



A continuaci贸n se muestra la carga de la CPU y la memoria MEM al probar vhost1-vhost5. A veces, la carga en la CPU creci贸 hasta un 700%.



Conclusiones:


Seg煤n Nginx VTS, me gustar铆a tener m谩s m茅tricas sin editar las configuraciones.


Por Wiremock vs Mockify: use Mockify. Utiliza menos CPU y MEM.


Y finalmente, el ensamblaje de las aplicaciones de Golang en Fedora Copr usando el ejemplo de Mockify.


Utilice el repositorio https://github.com/patsevanton/mockify-rpm como ejemplo.

Source: https://habr.com/ru/post/476428/


All Articles