centos安装openresty和logstash安装

红太狼 2023-10-14 19:39 122阅读 0赞

1.安装环境

linux: centos8.1
使用用户:root
openresty:1.21.4.1-alpine-fat
logstash:8.8.1
docker:20.10.24
docker-compose: 1.29.2

2.安装步骤

2.1 新建挂载文件目录和配置文件

2.1.1 新建文件目录

  1. mkdir logs logstash-pipeline -- 在你的安装目录新建挂载文件

2.1.2 调整logstash.yml

因为我只安装logstash,没有安装es,所以需要调整logstash.yml

  1. http.host: "0.0.0.0"
  2. xpack.monitoring.elasticsearch.hosts: [ "http://yourip:9200" ]
  3. xpack.monitoring.enabled: false
  4. log.level: debug

前面两个配置是我在容器中直接复制出来的,没删除,因为没安装es,我就没管他。

1.http.host: “0.0.0.0”:
指定 Elasticsearch 监听的网络地址。0.0.0.0 表示监听所有网络接口。表示 Elasticsearch 将会监听服务器上的所有可用 IP 地址,允许远程连接。
2.xpack.monitoring.elasticsearch.hosts: [ “http://yourip:9200” ]:
指定 Elasticsearch 集群的监控目标。你需要将 “http://yourip:9200” 替换为你实际的 Elasticsearch 集群的地址。用于将 Elasticsearch 集群的监控数据发送到指定的目标。
3.xpack.monitoring.enabled: false:
用于控制是否启用监控功能。设置为 false,Elasticsearch 将不会生成和发送监控数据。
4.log.level: debug:
用于设置 Elasticsearch 的日志级别。我在开发环境调试,所以将日志级别设置为 debug,有助于排查问题。如果是生产环境,需要将日志级别设置为更高的级别,比如 info 或 warn,这样可以减少日志量。

2.1.3 调整nginx.conf

  1. user root;
  2. worker_processes auto;
  3. worker_cpu_affinity auto;
  4. worker_rlimit_nofile 65535;
  5. error_log logs/error.log notice;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;
  12. # log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. # '$status $body_bytes_sent "$http_referer" '
  14. # '"$http_user_agent" "$http_x_forwarded_for"';
  15. #
  16. # log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  17. # '$status $body_bytes_sent "$http_referer" '
  18. # '"$http_user_agent" "$http_x_forwarded_for" '
  19. # '$request_time $upstream_response_time $host $request_id';
  20. # access_log logs/access.log main;
  21. log_format json_combined escape=json '{'
  22. '"request_start_time":"$time_local",'
  23. '"client":"$remote_addr",'
  24. '"http_referer":"$http_referer",'
  25. '"request_uri":"$request_uri",'
  26. '"uri":"$uri",'
  27. '"scheme":"$scheme",'
  28. '"status":"$status",'
  29. '"response_byte_size":"$body_bytes_sent",'
  30. '"request_total_time":"$request_time",'
  31. '"upstream_addr":"$upstream_addr",'
  32. '"upstream_response_time":"$upstream_response_time",'
  33. '"upstream_header_time":"$upstream_header_time",'
  34. '"upstream_bytes_received":"$upstream_bytes_received",'
  35. '"upstream_connect_time":"$upstream_connect_time",'
  36. '"upstream_status":"$upstream_status",'
  37. '"agent":"$http_user_agent",'
  38. '"request":"$request",'
  39. '"request_id":"$request_id",'
  40. '"http_x_forwarded_for":"$http_x_forwarded_for",'
  41. '"project_code":"$http_project_code"'
  42. '}';
  43. access_log logs/access.log json_combined;
  44. sendfile on;
  45. #tcp_nopush on;
  46. #keepalive_timeout 0;
  47. keepalive_timeout 65;
  48. server {
  49. # 修改成自己的端口,和docker-compose映射的端口保持一致即可
  50. listen 8000;
  51. location /demo {
  52. content_by_lua_block {
  53. ngx.say("配置成功了耶")
  54. }
  55. }
  56. }
  57. }

这里我的access.log日志格式设置成了json格式,logstash解析access.log就相对简单很多。我开始设置的是第一种,解析时贼费劲,我的发量已经不能支持我挣扎。

2.1.4 新增logstash.conf

  1. input {
  2. file {
  3. path => "/usr/share/logstash/openresty-logs/access.log" # logstash挂载的日志路径
  4. start_position => "beginning"
  5. # sincedb_path => "/dev/null" # 这是全量的,每次都会从头读
  6. }
  7. }
  8. output {
  9. stdout {
  10. codec => rubydebug
  11. }
  12. }

这里只是个简单的示例,按需配置

2.2 docker-compose配置

1.创建一个自定义网络

  1. docker network create mynetwork

2.docker-compose.yml配置

  1. version: '3.1'
  2. services:
  3. openresty:
  4. image: openresty/openresty:1.21.4.1-alpine-fat
  5. ports:
  6. - 8000:8000
  7. volumes:
  8. - /etc/localtime:/etc/localtime:ro
  9. - /etc/timezone:/etc/timezone:ro
  10. - ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf
  11. - ./logs:/usr/local/openresty/nginx/logs
  12. restart: always
  13. networks:
  14. - mynetwork
  15. logstash:
  16. image: docker.elastic.co/logstash/logstash:8.8.1
  17. volumes:
  18. - /etc/localtime:/etc/localtime:ro
  19. - /etc/timezone:/etc/timezone:ro
  20. - ./logstash-pipeline:/usr/share/logstash/pipeline
  21. - /www/docker-compose/openresty/logstash.yml:/usr/share/logstash/config/logstash.yml
  22. - ./logs:/usr/share/logstash/openresty-logs
  23. networks:
  24. - mynetwork
  25. networks:
  26. mynetwork:
  27. external: true

mynet网络在networks部分被标记为external,表示这个网络是在Docker Compose的管理范围之外创建的。配置这个是为了和其他容器通过服务名通信,如果你没有需要或者有其他实现方式,直接去掉即可。
这是最终的目录结构:
图片.png

2.3 启动

  1. # 在docker-compose.yml目录中执行
  2. docker-compose up -d
  3. # 查看是否启动成功
  4. docker ps
  5. # 启动日志排查
  6. docker-compose logs -f

图片.png


重要的人会越来越少,剩下的人也会越来越重要

发表评论

表情:
评论列表 (有 0 条评论,122人围观)

还没有评论,来说两句吧...

相关阅读

    相关 openresty安装

    一、简介 OpenResty是一个基于Nginx与Lua的高性能Web平台,其内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展

    相关 CentOS7 安装 Logstash

    0.介绍 Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的、多样化的日志搜集起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件。