资讯中心

ELK+FileBeat 7.14.0版本安装、部署及使用

📅 2026/7/24 22:35:50
ELK+FileBeat 7.14.0版本安装、部署及使用
框架 简介 作用Elasticsearch 开源分布式搜索引擎提供存储、分析、搜索功能。特点分布式、基于reasful风格、支持海量高并发的准实时搜索场景、稳定、可靠、快速、使用方便等。 接收搜集的海量结构化日志数据并提供给Kibana查询分析。Logstash 开源日志搜集、分析、过滤框架支持多种数据输入输出方式。 用于收集日志对日志进行过滤形成结构化数据并转发到Elasticsearch中。Kibana 开源日志报表系统对Elasticsearch以及Logstash有良好的web页面支持。 对Elasticsearch提供的数据进行分析展示。一 、下载采用国内镜像云官网下载地址https://www.elastic.co/cn/downloads/elasticsearch国内镜像源华为https://mirrors.huaweicloud.com/elasticsearch/选择合适的版本下载推荐下载自带 JDK 的版本否者自己配置的 JDK 可能不符合版本要求。注意Elasticsearch 会优先使用系统配置的 JDK 。可将 Elasticsearch 自带的 JDK 配置到系统的环境变量中如果不这样做的话在安装 Logstash 时启动会报没有配置 JDK 环境变量的错误。二、elasticsearch安装部署sudotar-zxvfelasticsearch-7.14.0-linux-x86_64.tar.gz-C/usr/local/1. 编辑elasticsearch.yml文件进入elasticsearch-7.14.0/config/文件夹编辑elasticsearch.yml文件2.创建es用户赋值权限es不能使用root用户启动必须用其他用户并且给这个用户赋予操作elasticsearch-7.14.0文件夹的权限useradd -m espasswd eschown -R es:es /usr/local/elasticsearch-7.14.0/3.启动前需要先配置一些环境sudovi/etc/security/limits.conf 添加 * soft nofile65536* hard nofile655362、配置文件包含限制一个进程可以拥有的VMA(虚拟内存区域)的数量vi/etc/sysctl.conf 在文件末尾添加vm.max_map_count655360然后执行sudosysctl-p4.启动在/usr/local/elasticsearch-7.14.0/文件夹下执行./bin/elasticsearch浏览器打开http://192.168.2.129:9200/如出现下图则说明启动成功5.设置密码./bin/elasticsearch-setup-passwords interactive三、logstash安装部署sudo tar -zxvf logstash-7.14.0-linux-x86_64.tar.gz -C /usr/local/1. 编辑logstash-sample.conf进入logstash-7.14.0/config目录下编辑logstash-sample.conf文件。当然也可以新建一个配置文件这里以logstash-sample.conf为例在input-beats-port默认是5044后面部署的filebeat的日志都会发送到这里所以filebeat里面的配置要和这里的保持一致输出到es里的配置增加了action, 配置了es的hosts另外配置一个index这里的索引不能使用大写字母input{beats{port5044}}output{elasticsearch{hosts[http://localhost:9200]indexelkloguserelasticpasswordelastic}}2. 启动sudo ./bin/logstash -f ./config/logstash-sample.conf四、kibana安装部署将kibana安装包解压到/usr/local/目录sudo tar -zxvf kibana-7.14.0-linux-x86_64.tar.gz -C /usr/local/1.编辑kibana.yml文件进入kibana-7.14.0-linux-x86_64/config文件夹编辑kibana.yml文件sudo chown -R es:es /usr/local/kibana-7.14.0-linux-x86_64/ 启动kibana 在kibana-7.14.0-linux-x86_64文件夹下执行 ./bin/kibana 防火墙开放5601端口网页访问http://192.168.2.129:5601/五、filebeat安装部署filebeat是一个轻量级的日志采集服务可以将采集的日志发送到es或者logstash这里测试时将filebeat部署在另外一台服务器上ip:192.168.2.173解压filebeat到指定位置sudotar-zxvffilebeat-7.14.0-linux-x86_64.tar.gz-C/usr/local/1.编辑filebeat.yml文件进入filebeat-7.14.0-linux-x86_64文件夹编辑filebeat.yml文件这里只设置了输入格式为log的路径同时把enable设置为true# Filebeat inputs filebeat.inputs: - type: log enabled:truepaths:# 修改为你要采集的日志路径示例# - /opt/*.log# - /var/log/secure- /var/log/*.log exclude_files:[\.gz$]# Elasticsearch template setting setup.template.name:k8s-czgx-prod1setup.template.pattern:k8s-czgx-prod1-*setup.template.enabled:truesetup.ilm.enabled:false# Processors processors: - add_cloud_metadata: ~# Outputs output.elasticsearch: hosts:[127.0.0.1:9200]username:elasticpassword:elasticindex:k8s-czgx-prod1-%{YYYY.MM.dd}# Logging logging.level: info logging.to_files:truelogging.files: path: ./logs name: filebeat keepfiles:7在filebeat-7.14.0-linux-x86_64文件夹下执行./filebeat -e -c ./filebeat.yml六、收集java日志格式[2022-07-1410:03:30.310][http-nio-8080-exec-2][ERROR](com.zz.framework.exception.handler.GlobalExceptionHandler:83)- 数据校验异常,异常字段logstash配置cat/etc/logstash/conf.d/app-java.confEOF input { beats { port 5044 } } filter { grok { match [ message, \[%{DATA:logtime}\] ] break_on_match false } date { match [logtime,yyyy-MM-dd HH:mm:ss.SSS] targettimestamp } } output { if APPname1 in [tags] { elasticsearch { hosts [elasticsearch集群ip:9200] index java-APPname-%{YYYY-MM-dd} user elastic password 123456 } }else if APPname2 in [tags] { elasticsearch { hosts [elasticsearch集群ip:9200] index java-APPname-%{YYYY-MM-dd} user elastic password 123456 } }else { elasticsearch { hosts [elasticsearch集群ip:9200] index java-%{YYYY-MM-dd} user elastic password 123456 } } } EOFsystemctl start logstashfilebeat配置rpm-ivhfilebeat-7.17.4-x86_64.rpmcp/etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bakcat/etc/filebeat/filebeat.ymlEOF filebeat.inputs: - type: log enabled: true paths: - /tmp/APPname1.log tags: [APPname1] fields: env: test app: APPname multiline.pattern: ^\[ multiline.negate: true multiline.match: after - type: log enabled: true paths: - /tmp/APPname2.log tags: [APPname2] fields: env: stg app: APPname multiline.pattern: ^\[ multiline.negate: true multiline.match: after filebeat.config.modules: path:${path.config}/modules.d/*.yml reload.enabled: true output.logstash: hosts: [logstashIP1:5044,logstashIP2:5044,logstashIP3:5044] EOFsystemctl start filebeat7、Elasticsearch 快照备份与完整恢复流程适配 ELK 7.14.0基于你现有的 NFS 快照仓库back配套 Filebeat 生成的按天索引k8s-czgx-prod1-YYYY.MM.dd7.1 前置准备一次性操作1快照存储目录权限chown-Res:es /data/nfs/elk2注册快照仓库已执行记录备查curl-uelastic:elastic-XPUThttp://localhost:9200/_snapshot/back-HContent-Type:application/json-d{ type:fs, settings:{ location:/data/nfs/elk, compress:true } }校验仓库curl-uelastic:elastic http://localhost:9200/_snapshot/back7.2 自动每日备份脚本只备份当天索引新建/opt/es-daily-back.sh#!/bin/bashES_USERelasticES_PASSelasticES_URLhttp://localhost:9200REPObackTODAY$(date%Y.%m.%d)SNAP_NAMEdaily-snap-${TODAY}LOG_FILE/var/log/es_daily_backup.logecho${LOG_FILE}echo$(date)开始创建快照:${SNAP_NAME}${LOG_FILE}# 如果当天快照已存在先删除resp$(curl-s-u${ES_USER}:${ES_PASS}${ES_URL}/_snapshot/${REPO}/${SNAP_NAME})ifecho$resp|jq-r.snapshot|grep-q${SNAP_NAME};thenecho$(date)旧快照存在执行删除${LOG_FILE}curl-s-u${ES_USER}:${ES_PASS}-XDELETE${ES_URL}/_snapshot/${REPO}/${SNAP_NAME}${LOG_FILE}21fi# 创建快照仅备份当日索引关闭全局状态避免恢复冲突curl-s-u${ES_USER}:${ES_PASS}-XPUT${ES_URL}/_snapshot/${REPO}/${SNAP_NAME}?wait_for_completiontrue\-HContent-Type:application/json-d{ indices:k8s-czgx-prod1-${TODAY}, ignore_unavailable:true, include_global_state:false }${LOG_FILE}21# 自动清理保留最近30天快照OLD_SNAPSHOTS$(curl-s-u${ES_USER}:${ES_PASS}${ES_URL}/_snapshot/${REPO}/_all|\jq-r.snapshots[] | .snapshot|grep^daily-snap-|sort|head-n-30)forsnapin$OLD_SNAPSHOTS;doecho$(date)删除过期快照${snap}${LOG_FILE}curl-s-u${ES_USER}:${ES_PASS}-XDELETE${ES_URL}/_snapshot/${REPO}/${snap}${LOG_FILE}21doneecho$(date)备份流程结束${LOG_FILE}授权 定时任务chmodx /opt/es-daily-back.shcrontab-e# 每日凌晨01:00执行备份01* * * /opt/es-daily-back.sh7.3 常用快照管理命令# 查看所有快照curl-uelastic:elastic http://localhost:9200/_snapshot/back/_all# 删除指定快照禁止手动rm NFS目录文件curl-uelastic:elastic-XDELETEhttp://localhost:9200/_snapshot/back/daily-snap-2026.07.237.4 快照恢复操作重点线上环境推荐重命名恢复⚠️ 禁止直接同名恢复集群已有打开索引会报错。方式1Kibana Web页面恢复Stack Management → Snapshot and Restore找到目标快照daily-snap-2026.07.23→ RestoreLogistics页面开启Rename data streams and indicesCapture pattern(.*)Replacement patternrestore_$1Index settings页面所有选项保持关闭点击NextReview确认无报错执行 Restore snapshot恢复完成后索引名称restore_k8s-czgx-prod1-2026.07.23方式2API命令行恢复curl-uelastic:elastic-XPOSThttp://localhost:9200/_snapshot/back/daily-snap-2026.07.23/_restore-HContent-Type:application/json-d{ rename_pattern:(.*), rename_replacement:restore_$1 }