惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
News | PayPal Newsroom
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
博客园 - Franky
SecWiki News
SecWiki News
Recent Announcements
Recent Announcements
T
Troy Hunt's Blog
The Register - Security
The Register - Security
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
S
Security Affairs
博客园 - 司徒正美
S
Schneier on Security
I
InfoQ
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Forbes - Security
Forbes - Security
腾讯CDC
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Cloudbric
Cloudbric
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
V
Vulnerabilities – Threatpost
C
Check Point Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
Schneier on Security
Schneier on Security
O
OpenAI News
K
Kaspersky official blog

Li Hui Blog

工作流调研与实践经验 如何搭建私有仓库 Nexus 并配置 SSL 证书 浅谈一个产品的用户引导 归档 GoCD 加入基础密码验证 搭建一套 gocd 的环境 如何使用 gitbook cli工具 React JSX语法再实践 React 组件示例 如何搭建第一个 React 程序 [SpringBoot 指南] 连接 mysql 数据库并进行增删改查测试 [SpringBoot 指南] 如何开始 Springboot 之旅 实现自己第一个接口 LeetCode刷题笔记 VPC笔记 读书的目的 [人文书籍] 把时间当作朋友 Docker基础命令 [技术书籍]JSON必知必会 Docker原理研究 使用Ubuntu 及宝塔搭建Ghost平台 [Linux]Ubuntu安装宝塔面板 2020年年终总结 CURL学习教程 2020年度计划记录 [安全系列]生成ssh证书 [CLI应用学习]时间使用GitHub CLI [linux系列] 修改主机用户名 轻松学CLI应用总章节 [Shell编程系列]基础教程2 [Shell编程系列]基础教程1 [安全相关]使用腾讯云子账号提高账号安全性 [Git教程系列]基础教程5 解决修改错分支的问题 [博客优化]为Wordpress加入伪静态 2020年的12本书籍 [Git系列教程]基本使用4 Git log的使用 [Git系列教程]Git基础操作3 [Git教程系列]基础操作2 [Git教程系列]Git初次配置 [周年总结]脉脉深情,深思志远,周年总结计划 [博客优化]为网站添加SSL证书 [博客优化]Wordpress加入redis服务器提高速度 开篇词 Posts Archive Li Hui Blog
从零开始搭建单节点 ELK
2024-08-27 · via Li Hui Blog

最近上了一个新项目,在这个项目中开发环境中暂时没有基础设施,需要通过原始的方式进行部署和查询日志,因此最近参与了搭建 ELK 的项目,其实这里有一个问题,为啥要使用 ELK ? 这个技术选型的主要原因有两点,第一点项目成员对于 ELK 都有一定的使用经验,选择 ELK 可以减少项目同学的上手成本,第二点,ELK 是一个成熟的框架,了解其搭建过程也可以帮助更好的了解 ELK 软件之间的交互。

首先有一个问题,ELK 指的是什么?指的是 elastic search(数据存储), logstash(数据过滤), kibana(数据展示)。这三个便是 ELK 的三部分,当然也会有其他厂商的适配的其他版本,比如 Amazon OpenSeach, 或者。

当然除此之外还有一个向 ELK 发送数据的服务,比如收集日志用的比较多的 filebeat (beats 中的一种,其他 beats 可以参照 beats 的官网 ),我们这边因为只涉及到日志的收集,因此此处采用对应的 filebeat 即可,当然使用其他家的也可以。 基本流程是如上所示

elk-log-logic

整理流程如上所示。

  1. 监听文件并发送
  2. 过滤转换数据
  3. 保存数据
  4. 查询 es 数据并展示

解析来我们一起来看下安装的教程 首先需要启动起来后三个部分, 即 logstash, es, 以及 kibana.

本文采用 docker compose 方式启动, 安装 docker compose 参照 docker compose 安装

对应的 docker-compose 文件如下:

docker-compose.yml

 1version: '3'
 2services:
 3  elasticsearch:
 4    image: elasticsearch:7.13.0
 5    container_name: elasticsearch
 6    ports:
 7      - "9200:9200"
 8      - "9300:9300"
 9    volumes:
10      - test_data:/usr/share/elasticsearch/data/
11      - ./elk-config/elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
12    environment:
13      - discovery.type=single-node
14      - http.host=0.0.0.0
15      - transport.host=0.0.0.0
16      - xpack.security.enabled=false
17      - xpack.monitoring.enabled=false
18      - cluster.name=elasticsearch
19      - bootstrap.memory_lock=true
20    networks:
21      - elk
22
23  logstash:
24    image: logstash:7.13.0
25    container_name: logstash
26    ports:
27      - "5044:5044"
28      - "9600:9600"
29    volumes:
30      - ./elk-config/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf
31      - ./elk-config/logstash/logstash.yml:/usr/share/logstash/config/logstash.yml
32      - ls_data:/usr/share/logstash/data
33
34    networks:
35      - elk
36    depends_on:
37      - elasticsearch
38
39  kibana:
40    image: kibana:7.13.0
41    container_name: kibana
42    ports:
43      - "5601:5601"
44    volumes:
45      - ./elk-config/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml
46      - kb_data:/usr/share/kibana/data
47    networks:
48      - elk
49    depends_on:
50      - elasticsearch
51
52networks:
53  elk:
54    driver: bridge
55
56volumes:
57  test_data:
58  ls_data:
59  kb_data:

从上述文件中我们可以看到我们有一些配置文件需要配置下

./elk-config/logstash/logstash.conf

 1input {
 2   beats{
 3   port => 5044
 4   }
 5}
 6
 7filter {
 8}
 9
10output {
11 if "hero-app" in [tags] {
12    elasticsearch {
13      hosts => ["http://elasticsearch:9200"]
14      index => "[hero-app]-%{+YYYY.MM.dd}"
15      user => "elastic"
16      password => "password"
17    }
18 }
19 if "another-hero-app" in [tags] {
20    elasticsearch {
21      hosts => ["http://elasticsearch:9200"]
22      index => "[another-hero-app]-%{+YYYY.MM.dd}"
23      user => "elastic"
24      password => "password"
25    }
26 }
27}

Logstash.yml

1http.host: 0.0.0.0
2xpack.monitoring.elasticsearch.hosts: ["http://elasticsearch:9200"]

elasticsearch.yml

1cluster.name: "elasticsearch"
2network.host: localhost

kibana.yml

1server.name: kibana
2server.host: "0"
3elasticsearch.hosts: [ "http://elasticsearch:9200" ]
4monitoring.ui.container.elasticsearch.enabled: true

然后 我们使用 docker-compose up -d 即可启动 ELK 框架,如果 es 无法启动成功,可以尝试增加 docker 内存,比如 colima 配置如下

1colima start --cpu 4 --memory 8

然后我们继续做后续的步骤, 即 filebeat 的内容,可以从网站中下载对应的文件(注意要下载对应的架构,系统以及和 ELK 版本)

download-elk-page

解压之后可以查看对应的配置文件 filebeat.yml

增加监听的文件的配置

 1# ============================== Filebeat inputs ===============================
 2
 3filebeat.inputs:
 4
 5# Each - is an input. Most options can be set at the input level, so
 6# you can use different inputs for various configurations.
 7# Below are the input specific configurations.
 8- type: log
 9  enabled: true
10  paths:
11    - /Users/lihui/Desktop/elk/logs/hero-app-logs/*.log
12  tags: ["hero-app"]
13  multiline:
14    type: pattern
15    pattern: '^[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}'
16    negate: true
17    match: after
18- type: log
19  enabled: true
20  paths:
21    - /Users/lihui/Desktop/elk/logs/another-hero-app-logs/*.log
22  tags: ["another-hero-app"]
23  multiline:
24    type: pattern
25    pattern: '^[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}'
26    negate: true
27    match: after

然后采用下面的命令进行启动监听

1./filebeat -e -c filebeat.yml

而后登入 kibana 页面进行配置对应的信息。 首先解决单节点 warning 问题 kibana -> stack-management -> index management 将所有的 index 中的配置调整为 0

kibana-stack-managment

kibanan-index-management

修改为 0 之后既可以表明是单节点,保存健康状态即为 health.

修改 Index Patterns 以显示对应的 es 数据

kibana-index-patterns

附录

如何减少对于远程服务器的对于远程服务的侵入呢? 可以参与 sshfs 挂在远程文件到本地目录

1sshfs root@10.10.10.100:/home/demouser/hero-app/logs/ /Users/local/Desktop/elk/logs/hero-app-logs/

其他情况

如果本地是 mac 不好解决如果做? 可以采用 brew 安装

1# 安装依赖工具 fuse
2brew install osxfuse 
3# 安装远程同步工具 sshfs
4brew install sshfs

brew 对于这两个软件可能存在不兼容的文件,因此可以从官网下载

官网地址:

  1. osxfuse Home - macFUSE 下载对应的 macFuse 软件
  2. sshfs Releases · osxfuse/sshfs

参考资料:

  1. 如何在mac上实现远程挂载方案 | osxfuse & sshfs - 掘金
  2. 2023最新ELK日志平台(elasticsearch+logstash+kibana)搭建 - 掘金
  3. ELK搭建(一):手把手教你搭建分布式微服务日志监控 - 掘金