ELK 是指 Elasticsearch、Logstash 和 Kibana 这三个开源软件的组合。 Elasticsearch 是一个分布式的搜索和分析引擎,用于日志的存储,搜索,分析,查询。 Logstash 是一个数据收集、转换和传输工具,用于收集过滤和转换数据,然后将其发送到 Elasticsearch 或其他目标存储中。 Kibana 是一个数据可视化平台,通过与 Elasticsearch 的集成,提供了强大的数据分析和仪表盘功能。 Filebeat 是 Elastic Stack(ELK)中的一个组件,用于轻量级的日志文件收集和转发。它能够实时监控指定的日志文件,并将其发送到 Elasticsearch 或 Logstash 进行处理和分析。
ELK的架构有多种,本篇分享使用的架构如图所示: Beats(Filebeat) -> -> Elasticsearch -> Kibana,目前生产环境一天几千万的日志,内存占用大概 10G 左右
本篇 ELK 的版本为 v7.8.1,本篇使用的容器网络为 devopsnetwork ,需创建 docker network create devopsnetwork
./data
及配置文件./config/elasticsearch.yml
version: '3.1'
services:
elk_elasticsearch:
image: elasticsearch:7.8.1
container_name: elk_elasticsearch
restart: always
environment:
- discovery.type=single-node
- ES_JAVA_OPTS=-Xms4096m -Xmx4096m
ports:
- 9200:9200
- 9300:9300
volumes:
# 授权 chmod 777 ./config/ && chmod 777 ./data/
- ./data:/usr/share/elasticsearch/data
- ./config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
networks:
- devopsnetwork
networks:
devopsnetwork:
external: true
network.host: 0.0.0.0
xpack:
ml.enabled: false
monitoring.enabled: false
security.enabled: false
watcher.enabled: false
将其拷贝到服务器执行启动即可
#cd /app/elasticsearch
mkdir ./config
mkdir ./data
chmod 777 ./config/ && chmod 777 ./data/
docker compose up -d
验证访问
./pipeline
和./config
目录可以运行容器复制出来version: '3.1'
services:
elk_logstash:
image: logstash:7.17.16
container_name: elk_logstash
restart: always
ports:
- 5044:5044
volumes:
# 授权 chmod 777 ./logs/ && chmod 777 ./data/ && chmod 777 ./pipeline/ && chmod 777 ./config/
- /etc/timezone:/etc/timezone
- /etc/localtime:/etc/localtime:ro
- ./logs:/usr/share/logstash/logs
- ./data:/usr/share/logstash/data
- ./pipeline:/usr/share/logstash/pipeline
- ./config:/usr/share/logstash/config
networks:
- devopsnetwork
networks:
devopsnetwork:
external: true
input {
beats {
port => 5044
codec => json {
charset => "UTF-8"
}
}
}
filter {
}
output {
elasticsearch {
hosts => ["http://192.168.123.102:9200"]
index => "%{[app]}-%{+YYYY.MM.dd}"
}
stdout {
codec => rubydebug
}
}
将其拷贝到服务器执行启动即可
mkdir ./data
mkdir ./logs
chmod 777 ./logs/ && chmod 777 ./data/ && chmod 777 ./pipeline/ && chmod 777 ./config/
docker compose up -d
./config/kibana.yml
version: '3.1'
services:
elk_kibana:
image: kibana:7.8.1
container_name: elk_kibana
restart: always
environment:
- discovery.type=single-node
ports:
- 5601:5601
volumes:
- ./config/kibana.yml:/usr/share/kibana/config/kibana.yml
networks:
- devopsnetwork
networks:
devopsnetwork:
external: true
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://192.168.123.102:9200/"]
i18n.locale: "zh-CN"
xpack:
apm.ui.enabled: false
graph.enabled: false
ml.enabled: false
monitoring.enabled: false
reporting.enabled: false
security.enabled: false
grokdebugger.enabled: false
searchprofiler.enabled: false
将其拷贝到服务器执行启动即可
docker compose up -d
version: '3.1'
services:
elk_filebeat:
image: elastic/filebeat:7.8.1
container_name: elk_filebeat
restart: always
volumes:
# 授权 chmod 777 ./config/ && chmod 777 ./data/ && chmod 777 ./logs/ && chmod 777 /app/logs
- ./config/filebeat.yml:/usr/share/filebeat/filebeat.yml:ro
- ./data:/usr/share/filebeat/data
- ./logs:/usr/share/filebeat/logs
- /app/logs:/app/logs
networks:
- devopsnetwork
networks:
devopsnetwork:
external: true
output.logstash:
#logstash hosts
hosts: ["192.168.123.102:5044"]
fields_under_root: true
filebeat.inputs:
- type: log
enabled: true
paths:
- /app/logs/*/*.log
close_older: 24h
ignore_older: 24h
json.keys_under_root: true
json.overwrite_keys: true
encoding: utf-8
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- drop_fields:
fields: ["log","@version","ecs","agent","beat","host","beat.hostname","beat.version","beat.name","prospector.type","input.type","host.id","host.name","host.os.build","host.os.family","host.os.name","host.os.platform","host.os.platform","log.file.path","tags","offset","host.architecture","host.os.version"]
server {
listen 80;
listen 443 ssl;
server_name kibana.devops.test.com; # 自行修改成你的域名
ssl_certificate /certs/kibana.devops.test.com/server.crt;
ssl_certificate_key /certs/kibana.devops.test.com/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://192.168.123.102:5601;
proxy_http_version 1.1;
proxy_buffering off;
proxy_request_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
配置完成,即可使用域名访问
在Nginx配置文件中添加以下内容
location / {
auth_basic "Restricted Content";
auth_basic_user_file /certs/kibana.devops.test.com/passwd;
...
}
添加对应的passwd文件,使用 htpasswd 生成,如账号密码是 root devops666 的配置文件
root:WvesKBTr22.wY
配置完成,重载配置后刷新页面就提示输入账号密码了
location / {
allow 192.168.123.201; # 允许的IP地址
deny all; # 拒绝所有其他IP地址
}
本篇只针对 ELK 的安装进行了介绍及整理对应的 Docker Compose 配置文件,后续即可快速安装配置 ELK 环境,如何与项目结合使用后续再分享