nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

이 기사에서는 프로젝트에 대해 설명합니다. nginx-로그 수집기, nginx 로그를 읽고 이를 Clickhouse 클러스터로 보냅니다. 일반적으로 ElasticSearch는 로그에 사용됩니다. Clickhouse에는 더 적은 리소스(디스크 공간, RAM, CPU)가 필요합니다. Clickhouse는 데이터를 더 빠르게 기록합니다. Clickhouse는 데이터를 압축하여 디스크의 데이터를 더욱 컴팩트하게 만듭니다. 클릭하우스의 장점은 보고서의 2개 슬라이드에서 확인할 수 있습니다. VK가 수만 대의 서버에서 ClickHouse에 데이터를 삽입하는 방법.

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

로그 기반 분석을 보기 위해 Grafana용 대시보드를 생성하겠습니다.

관심 있으신 분은 고양이에 오신 것을 환영합니다.

nginx, grafana를 표준 방식으로 설치합니다.

ansible-playbook을 사용하여 클릭하우스 클러스터 설치 데니스 프로스쿠린.

Clickhouse에서 데이터베이스 및 테이블 생성

파일 Clickhouse에서 nginx-log-collector에 대한 데이터베이스 및 테이블을 생성하기 위한 SQL 쿼리가 설명되어 있습니다.

Clickhouse 클러스터의 각 서버에서 하나씩 요청을 수행합니다.

중요 사항. 이 줄에서logs_cluster는 "remote_servers"와 "shard" 사이에 있는 clickhouse_remote_servers.xml 파일의 클러스터 이름으로 바꿔야 합니다.

ENGINE = Distributed('logs_cluster', 'nginx', 'access_log_shard', rand())

nginx-log-collector-rpm 설치 및 구성

Nginx-log-collector에는 rpm이 없습니다. 여기 https://github.com/patsevanton/nginx-log-collector-rpm rpm을 생성하십시오. rpm은 다음을 사용하여 컴파일됩니다. 페도라 코퍼

rpm 패키지 nginx-log-collector-rpm 설치

yum -y install yum-plugin-copr
yum copr enable antonpatsev/nginx-log-collector-rpm
yum -y install nginx-log-collector
systemctl start nginx-log-collector

/etc/nginx-log-collector/config.yaml 구성을 편집합니다.

  .......
  upload:
    table: nginx.access_log
    dsn: http://ip-адрес-кластера-clickhouse:8123/

- tag: "nginx_error:"
  format: error  # access | error
  buffer_size: 1048576
  upload:
    table: nginx.error_log
    dsn: http://ip-адрес-кластера-clickhouse:8123/

nginx 설정

일반 nginx 구성:

user  nginx;
worker_processes  auto;

#error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    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"';

    log_format avito_json escape=json
                     '{'
                     '"event_datetime": "$time_iso8601", '
                     '"server_name": "$server_name", '
                     '"remote_addr": "$remote_addr", '
                     '"remote_user": "$remote_user", '
                     '"http_x_real_ip": "$http_x_real_ip", '
                     '"status": "$status", '
                     '"scheme": "$scheme", '
                     '"request_method": "$request_method", '
                     '"request_uri": "$request_uri", '
                     '"server_protocol": "$server_protocol", '
                     '"body_bytes_sent": $body_bytes_sent, '
                     '"http_referer": "$http_referer", '
                     '"http_user_agent": "$http_user_agent", '
                     '"request_bytes": "$request_length", '
                     '"request_time": "$request_time", '
                     '"upstream_addr": "$upstream_addr", '
                     '"upstream_response_time": "$upstream_response_time", '
                     '"hostname": "$hostname", '
                     '"host": "$host"'
                     '}';

    access_log     syslog_server=unix:/var/run/nginx_log.sock,nohostname,tag=nginx avito_json; #ClickHouse
    error_log      syslog_server=unix:/var/run/nginx_log.sock,nohostname,tag=nginx_error; #ClickHouse

    #access_log  /var/log/nginx/access.log  main;

    proxy_ignore_client_abort on;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

하나의 가상 호스트:

vhost1.conf:

upstream backend {
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
}

server {
    listen   80;
    server_name vhost1;
    location / {
        proxy_pass http://backend;
    }
}

/etc/hosts 파일에 가상 호스트를 추가합니다:

ip-адрес-сервера-с-nginx vhost1

HTTP 서버 에뮬레이터

HTTP 서버 에뮬레이터로서 우리는 nodejs-스텁-서버 부터 막심 이그나텐코

Nodejs-stub-server에는 rpm이 없습니다. 여기 https://github.com/patsevanton/nodejs-stub-server rpm을 생성하십시오. rpm은 다음을 사용하여 컴파일됩니다. 페도라 코퍼

업스트림 nginx rpm에 nodejs-stub-server 패키지 설치

yum -y install yum-plugin-copr
yum copr enable antonpatsev/nodejs-stub-server
yum -y install stub_http_server
systemctl start stub_http_server

스트레스 테스트

Apache 벤치마크를 이용하여 테스트를 진행합니다.

설치하세요:

yum install -y httpd-tools

5개의 다른 서버에서 Apache 벤치마크를 사용하여 테스트를 시작합니다.

while true; do ab -H "User-Agent: 1server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 2server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 3server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 4server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 5server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done

그라파나 설정

공식 Grafana 웹사이트에서는 대시보드를 찾을 수 없습니다.

그러므로 우리는 그것을 손으로 할 것입니다.

내가 저장한 대시보드를 찾을 수 있습니다. 여기에.

또한 내용이 포함된 테이블 변수를 생성해야 합니다. nginx.access_log.
nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

Singlestat 총 요청 수:

SELECT
 1 as t,
 count(*) as c
 FROM $table
 WHERE $timeFilter GROUP BY t

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

Singlestat 실패한 요청:

SELECT
 1 as t,
 count(*) as c
 FROM $table
 WHERE $timeFilter AND status NOT IN (200, 201, 401) GROUP BY t

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

단일 상태 실패율:

SELECT
 1 as t, (sum(status = 500 or status = 499)/sum(status = 200 or status = 201 or status = 401))*100 FROM $table
 WHERE $timeFilter GROUP BY t

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

단일 상태 평균 응답 시간:

SELECT
 1, avg(request_time) FROM $table
 WHERE $timeFilter GROUP BY 1

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

단일 상태 최대 응답 시간:

SELECT
 1 as t, max(request_time) as c
 FROM $table
 WHERE $timeFilter GROUP BY t

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

집계 상태:

$columns(status, count(*) as c) from $table

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

파이처럼 데이터를 출력하려면 플러그인을 설치하고 grafana를 다시 시작해야 합니다.

grafana-cli plugins install grafana-piechart-panel
service grafana-server restart

파이 TOP 5 상태:

SELECT
    1, /* fake timestamp value */
    status,
    sum(status) AS Reqs
FROM $table
WHERE $timeFilter
GROUP BY status
ORDER BY Reqs desc
LIMIT 5

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

또한 스크린샷 없이 요청을 드리겠습니다.

http_user_agent 수:

$columns(http_user_agent, count(*) c) FROM $table

좋음비율/나쁨비율:

$rate(countIf(status = 200) AS good, countIf(status != 200) AS bad) FROM $table

응답 타이밍:

$rate(avg(request_time) as request_time) FROM $table

업스트림 응답 시간(첫 번째 업스트림 응답 시간):

$rate(avg(arrayElement(upstream_response_time,1)) as upstream_response_time) FROM $table

모든 가상 호스트의 테이블 수 상태:

$columns(status, count(*) as c) from $table

대시보드의 일반 보기

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

avg()와 Quantile()의 비교

avg ()
nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티
분위수()
nginx 로그를 Clickhouse로 보내기 위한 Avito의 Nginx-log-collector 유틸리티

결론 :

커뮤니티가 nginx-log-collector 개발/테스트 및 사용에 참여하기를 바랍니다.
그리고 누군가가 nginx-log-collector를 구현할 때 디스크, RAM 및 CPU에서 얼마나 절약했는지 알려줄 것입니다.

텔레그램 채널:

밀리초:

밀리초가 중요한 분은 여기에 글을 쓰거나 투표해 주세요. 발행물.

출처 : habr.com

코멘트를 추가