efk跟elk差不多,只是elk中的logstash 是java写的,过于重量级,于是改用filebeat替代logstash,efk是一个日志采集、存储、可视化运维集一体的日志系统,适合于微服务或多服务之间调用的日志管理
elasticsearch 是一个 分布式、高扩展、高实时的搜索引擎,也可以存储数据,如果做搜索相关的服务可以使用elasticsearch作为关键字搜索引擎
es安装方式有两种,
一是物理机安装,需要非root用户账号才能启动,可以创建一个es的账号安装启动es
elasticsearch.yml 配置记录
主配置
cluster.name: node-es # 集群名称
node.name: node1 # 当前节点名称
node.master: true # 是否为主机
node.data: true # 是否为数据节点 存储数据
path.data: /es/data # 数据存储地址
path.logs: /es/log # 日志地址
network.host: 0.0.0.0 # 绑定ip,0.0.0.0代表谁都可以连,不安全,类似redis配置的0.0.0.0,
http.port: 9200 # 本节点开放端口
transport.tcp.port: 9300 # 各节点交互端口
discovery.seed_hosts: ["10.10.6.189"] # 各个主节点ip
cluster.initial_master_nodes: ["node1"] # 指定哪个节点为主节点
http.cors.enabled: true # http cors相关
http.cors.allow-origin: "*" # http cors相关
从配置:
cluster.name: node-es # 集群名称
node.name: node2 # 集群中本机的节点名称,节点名称不能相同
path.data: /es/data
path.logs: /es/log
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node1"] # 从列表中选举主节点
discovery.zen.ping.unicast.hosts: ["10.10.6.189:9300"] # 集群配置相关
http.cors.enabled: true # http cors相关
http.cors.allow-origin: "*" # http cors相关
二是用docker 安装,这种安装方式就非常简单了,下载es镜像,挂载一下目录就可以了,本文使用docker安装es
docker pull elasticsearch:7.17.7
安装es 7.17.7 版本,也可以不指定版本用最新的
docker 启动单机es:
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m"
-e "discovery.type=single-node" -p 9200:9200 -p 9300:9300
-v /es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
-v /es/data:/usr/share/elasticsearch/data
-v /es/logs:/usr/share/elasticsearch/logs elasticsearch:7.17.7
elasticearch.yml
cluster.name: cook-es
node.name: cook_node1
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
docker 安装kibana
docker pull kibana:7.17.7
启动kibana
docker run --name kibana -v
/kib/config/kibana.yml:/usr/share/kibana/config/kibana.yml -p 5601:5601 -d kibana:7.17.7
kibana.yml
server.port: 5601 # 端口绑定
server.host: "0.0.0.0" # 绑定ip 0.0.0.0 代表谁都可以连
elasticsearch.hosts: ["http://192.168.1.1:9200"] # es 地址
i18n.locale: "zh-CN" # kibana管理后台语言设置为中文
filebeat作为go写的日志采集器,比logstash更轻量级,可以直接物理机部署
filebeat下载
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.7-linux-x86_64.tar.gz
解压
tar -zxvf filebeat-7.17.7-linux-x86_64.tar.gz
修改filebeat.yml
filebeat.inputs:
- type: log
enable: true
paths:
- /home/caipu_api/caipu_cook_web/logs/*.log
- /home/caipu_api/caipu_user_of_cook_web/logs/*.log
- /home/caipu_api/caipu_user_web/logs/*.log
json.keys_under_root: true # 收集json日志
json.overwrite_keys: true # 收集json日志
output.elasticsearch:
hosts: ["192.168.1.1:9200"] # es存储地址
index: "caipu_srv"
setup.template.enabled: false
setup.template.name: "caipu_srv"
setup.template.pattern: "caipu_srv-*"
日志格式为json格式
如:
{"@timestamp": "2022-12-13 22:22:27", "level": "info", "msg": "get banner cook 获取banner数据列表: [{'cook_id': 6, 'cook__name': '米面饼', 'cook__title': '米面饼', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/26/2020032615851855440221999385577.JPG?x-oss-process=style/p800', 'cook__viewcount': 11, 'cook__collect': 0}, {'cook_id': 2, 'cook__name': '香椿芝麻酱拌面', 'cook__title': '香椿芝麻酱拌面', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/24/20200324158501120212310274398.jpg?x-oss-process=style/p800', 'cook__viewcount': 15, 'cook__collect': 0}, {'cook_id': 3, 'cook__name': '玉米鸡蛋饼', 'cook__title': '玉米鸡蛋饼', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/26/2020032615851913534767231499680.jpg?x-oss-process=style/p800', 'cook__viewcount': 172, 'cook__collect': 0}, {'cook_id': 4, 'cook__name': '菠萝炒饭', 'cook__title': '菠萝炒饭', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/25/20200325158514127518138310906267.jpg?x-oss-process=style/p800', 'cook__viewcount': 10, 'cook__collect': 0}, {'cook_id': 5, 'cook__name': '自制米粉', 'cook__title': '自制米粉', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/17/2020031715844075140086228425239.jpg?x-oss-process=style/p800', 'cook__viewcount': 11, 'cook__collect': 0}]", "track_id": "be2d47c2-e3b4-42b8-805d-b2f876a52bb4"}
{"@timestamp": "2022-12-13 22:22:27", "level": "info", "msg": "get banner cook 响应数据: [{'cook_id': 6, 'cook__name': '米面饼', 'cook__title': '米面饼', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/26/2020032615851855440221999385577.JPG?x-oss-process=style/p800', 'cook__viewcount': 11, 'cook__collect': 0}, {'cook_id': 2, 'cook__name': '香椿芝麻酱拌面', 'cook__title': '香椿芝麻酱拌面', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/24/20200324158501120212310274398.jpg?x-oss-process=style/p800', 'cook__viewcount': 15, 'cook__collect': 0}, {'cook_id': 3, 'cook__name': '玉米鸡蛋饼', 'cook__title': '玉米鸡蛋饼', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/26/2020032615851913534767231499680.jpg?x-oss-process=style/p800', 'cook__viewcount': 172, 'cook__collect': 0}, {'cook_id': 4, 'cook__name': '菠萝炒饭', 'cook__title': '菠萝炒饭', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/25/20200325158514127518138310906267.jpg?x-oss-process=style/p800', 'cook__viewcount': 10, 'cook__collect': 0}, {'cook_id': 5, 'cook__name': '自制米粉', 'cook__title': '自制米粉', 'cook__default_img': 'https://i8.meishichina.com/attachment/recipe/2020/03/17/2020031715844075140086228425239.jpg?x-oss-process=style/p800', 'cook__viewcount': 11, 'cook__collect': 0}]", "track_id": "be2d47c2-e3b4-42b8-805d-b2f876a52bb4"}
filebeat启动:
./filebeat -e -c filebeat.yml
注意,不能使用nohup 启动,如果使用nohup启动的话,ssh窗口关闭后filebeat也会自动退出,需要将filebeat服务注册为系统服务
1 在 /etc/systemd/system 下新增 filebeat.service 配置文件
[Unit]
Description=Filebeat is a lightweight shipper for metrics.
Documentation=https://www.elastic.co/products/beats/filebeat
Wants=network-online.target
After=network-online.target
[Service]
Environment="LOG_OPTS=-e"
Environment="CONFIG_OPTS=-c /usr/filebeat/filebeat-7.17.7-linux-x86_64/filebeat.yml" # filebeat.yml 路径地址
Environment="PATH_OPTS=-path.home /usr/filebeat/filebeat-7.17.7-linux-x86_64/filebeat -path.config /usr/filebeat/filebeat-7.17.7-linux-x86_64 -path.data /usr/filebeat/filebeat-7.17.7-linux-x86_64/data -path.logs //usr/filebeat/filebeat-7.17.7-linux-x86_64/logs" # filebeat目录
ExecStart=/usr/filebeat/filebeat-7.17.7-linux-x86_64/filebeat $LOG_OPTS $CONFIG_OPTS $PATH_OPTS
Restart=always
[Install]
2、给该文件给予可执行的权限:
chmod +x /etc/systemd/system/filebeat.service
3、系统后台服务方式启动:
# 重新加载服务配置文件
systemctl daemon-reload
# 设置开机自启动
systemctl enable filebeat
# 启动服务
systemctl start filebeat
# 停止服务, 如果filebeat.yml 配置文件有变动可以先停止再重启filebeat服务
systemctl stop filebeat
# 查看服务状态
systemctl status filebeat.service
logstash 本机部署记录
1 下载logstash
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.7-linux-x86_64.tar.gz
2 解压
tar -zxvf filebeat-7.17.7-linux-x86_64.tar.gz
3 修改 logstash/config 文件中的 logstash-simple.yml
input {
tcp {
port => 4560 # logstash可作为web服务器接收日志上传
codec => json_lines # 日志格式为json
}
}
output {
elasticsearch {
hosts => ["http://192.168.1.1:9200"]
index => "caipu_srv_log"
}
}
4 启动logstash
nohup ./logstash -f ../config/logstash-sample.conf &
注意,logstash启动需要非root环境,所以启动的时候要切换为其他账号启动logstash
EL/FK 是一系列组件组成的日志管理系统,各个组件的版本都需要一致