百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

容器日志采集的三种方式实战操作讲解(Filebeat)

wptr33 2025-01-27 00:37 37 浏览

一、概述

Filebeat 是一个轻量级的开源数据采集器,通常用于从不同来源收集和发送日志和事件数据。在 KubernetesDocker 环境中,Filebeat 常用于采集容器的日志。以下是通过 Filebeat 采集容器日志的三种常见方式的实战讲解:

【第一种方式】:Filebeat 与 应用运行在同一容器

  • 部署Filebeat:首先,在Kubernetes中或Docker环境中,部署 Filebeat 作为一个Sidecar容器,它将与应用容器一起运行(在同一个容器中运行)
  • 配置Filebeat:在Filebeat的配置文件(filebeat.yml)中,定义日志路径,以告诉 Filebeat 从哪里采集容器日志。
  • 输出设置:配置Filebeat将采集到的日志数据发送到所需的目的地,如Elasticsearch、Logstash或Kafka。
  • 启动Filebeat:启动Filebeat容器,它将开始采集容器的日志,并将其发送到配置的目的地。

【第二种方式】:Filebeat 与 应用运行不在同一容器

  • 部署Filebeat:同样,在Kubernetes中或Docker环境中,部署 Filebeat作为一个独立的 Sidecar 容器。
  • 配置Filebeat:在Filebeat的配置文件中,使用Docker日志驱动来采集容器日志。
  • 输出设置:配置Filebeat的输出,以将采集到的日志数据发送到目标。
  • 启动Filebeat:启动Filebeat容器,它将开始采集容器的日志。

【第三种方式】通过 Kubernetes Filebeat DaemonSet

  • 部署 Filebeat:在Kubernetes中,可以使用Filebeat DaemonSet部署Filebeat作为一个集群级别的日志采集器。
  • Filebeat配置:在Filebeat的配置中,设置输入插件以采集容器的日志。
  • Filebeat输出:配置Filebeat的输出插件,以将采集到的日志数据发送到目标,如Elasticsearch或其他存储。
  • 启动Filebeat:启动Filebeat DaemonSet,它将在Kubernetes集群中采集容器的日志。



Filebeat 官方文档:https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-overview.html

以前也写过关于 filebeat 更详细的介绍和实战操作的文章,只不过 filebeat 不是部署在 k8s 上,感兴趣的小伙伴可以先查阅我之前的文章:

二、K8s 集群部署

k8s 环境安装之前写过很多文档,可以参考我以下几篇文章:

三、ElasticSearch 和 kibana 环境部署

这里可以选择以下部署方式:

这里我选择 docker-compose 部署方式。

1)部署 docker

# 安装yum-config-manager配置工具
yum -y install yum-utils

# 建议使用阿里云yum源:(推荐)
#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装docker-ce版本
yum install -y docker-ce
# 启动并开机启动
systemctl enable --now docker
docker --version

2)部署 docker-compose

curl -SL https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose
docker-compose --version

3)创建网络

# 创建
docker network create bigdata

# 查看
docker network ls

4)修改 Linux 句柄数和最大线程数

#查看当前最大句柄数
sysctl -a | grep vm.max_map_count
#修改句柄数
vi /etc/sysctl.conf
vm.max_map_count=262144

#临时生效,修改后需要重启才能生效,不想重启可以设置临时生效
sysctl -w vm.max_map_count=262144

#修改后需要重新登录生效
vi /etc/security/limits.conf

# 添加以下内容
* soft nofile 65535
* hard nofile 65535
* soft nproc 4096
* hard nproc 4096

# 重启服务,-h 立刻重启,默认间隔一段时间才会开始重启
reboot -h now

5)下载部署包开始部署

# 这里选择 docker-compose 部署方式
git clone https://gitee.com/hadoop-bigdata/docker-compose-es-kibana.git

cd docker-compose-es-kibana

chmod -R 777 es kibana

docker-compose -f docker-compose.yaml up -d

docker-compose ps

四、容器日志采集的三种方式实战操作

1)【第一种方式】:Filebeat 与 应用运行在同一容器(不推荐)

这种方式应用服务与采集服务是没有隔离性的。一般不推荐使用这种方式。这里使用 Trino on k8s 来测试

1、下载 Trino on k8s 包

helm repo add trino https://trinodb.github.io/charts

helm pull trino/trino

tar -xf trino-0.14.0.tgz

2、重新构建镜像(将filebeat打包到Trino镜像中)

【1】下载 trino 镜像

docker pull trinodb/trino:432

【2】下载 Filebeat 部署包
官网地址:https://www.elastic.co/cn/downloads/past-releases#filebeat

curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.6.2-linux-x86_64.tar.gz

【3】修改启动脚本

# trio的启动服务脚本:/usr/lib/trino/bin/run-trino
# 注意配置文件,可以打包在容器里,也可以通过configmap管理,配置文件怎么配置,可以看我上面filebeat的文章。
#!/bin/bash

set -xeuo pipefail

launcher_opts=(--etc-dir /etc/trino)
if ! grep -s -q 'node.id' /etc/trino/node.properties; then
    launcher_opts+=("-Dnode.id=${HOSTNAME}")
fi

# 启动filebeat服务
nohup /opt/filebeat-7.6.2-linux-x86_64/filebeat -e -c /opt/filebeat-7.6.2-linux-x86_64/filebeat.yml >/dev/null 2>&1 &

exec /usr/lib/trino/bin/launcher run "${launcher_opts[@]}" "$@"

【4】开始重构镜像 Dockerfile

FROM trinodb/trino:432

ADD filebeat-7.6.2-linux-x86_64.tar.gz /opt/

COPY run-trino /usr/lib/trino/bin/run-trino

CMD ["/usr/lib/trino/bin/run-trino"]

【5】开始部署

helm install mytrino  ./trino -n trino --create-namespace

2)【第二种方式】:Filebeat 与 应用运行不在同一容器

1、Pod 下容器之间目录共享实现

默认情况下pod中的容器目录是不共享的,kubernets 可以通过emptyDir实现在同一Pod的不同容器间共享文件系统。示例如下:

apiVersion: v1
kind: Pod
metadata: 
  name: volume-emptydir
spec:
  containers:
  - name: write
    image: centos:centos7
    imagePullPolicy: IfNotPresent
    command: ["bash", "-c", "echo haha > /data/1.txt ;sleep 600000" ]
    volumeMounts:
    - name: data
      mountPath: /data
  - name: read
    image: centos:centos7
    imagePullPolicy: IfNotPresent
    command: ["bash", "-c", "cat  /data/1.txt ; sleep 600000"]
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    emptyDir: {}

2、开始编排部署

【1】添加 coordinator/worker 日志采集配置,trino/templates/deployment-coordinator.yamltrino/templates/deployment-worker.yaml

        containers:
        - args:
          - -e
          - -E
          - http.enabled=true
          env:
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                apiVersion: v1
                fieldPath: spec.nodeName
          image: docker.elastic.co/beats/filebeat:7.17.3
          imagePullPolicy: IfNotPresent
          livenessProbe:
            exec:
              command:
              - sh
              - -c
              - |
                #!/usr/bin/env bash -e
                curl --fail 127.0.0.1:5066
            failureThreshold: 3
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          name: filebeat
          readinessProbe:
            exec:
              command:
              - sh
              - -c
              - |
                #!/usr/bin/env bash -e
                filebeat test output
            failureThreshold: 3
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          resources:
            limits:
              cpu: "1"
              memory: 200Mi
            requests:
              cpu: 100m
              memory: 100Mi
          securityContext:
            privileged: false
            runAsUser: 0
          volumeMounts:
          - mountPath: /usr/share/filebeat/filebeat.yml
            name: filebeat-config
            readOnly: true
            subPath: filebeat.yml
          - name: data-share-dir
            mountPath: /data/trino/var
            
        volumes:
        - configMap:
            defaultMode: 384
            name: filebeat-config
          name: filebeat-config
        - name: data-share-dir
          emptyDir: {}
    
     # trino 镜像配置也得加上这个共享目录的挂载
          volumeMounts:
          - name: data-share-dir
            mountPath: /data/trino/var

filebeat configmap 配置

apiVersion: v1
data:
  filebeat.yml: |
    filebeat.inputs:
    - type: container
      paths:
        - /data/trino/var/log/*.log
      fields:
        index: k8s-pod-log
      processors:
      - add_kubernetes_metadata:
          host: ${NODE_NAME}
          matchers:
          - logs_path:
              logs_path: "/data/trino/var/log/"

    output.elasticsearch:
      host: '${NODE_NAME}'
      hosts: '192.168.182.110:9200'
      index: "filebeat-%{[fields][index]}-%{+yyyy.MM.dd}"

    setup.template.name: "default@template"
    setup.template.pattern: "filebeat-k8s-*"
    setup.ilm.enabled: false
kind: ConfigMap
metadata:
  annotations:
    meta.helm.sh/release-name: filebeat
    meta.helm.sh/release-namespace: logging
  labels:
    app: filebeat-filebeat
    app.kubernetes.io/managed-by: Helm
  name: filebeat-config

3)【第三种方式】通过 Kubernetes Filebeat DaemonSet

这种采集方式可以参考我这篇文章:Filebeat on k8s 日志采集实战操作


容器日志采集的三种方式实战操作讲解就先到这里了,有任何疑问也可关注我公众号:大数据与云原生技术分享,进行技术交流,如本篇文章对您有所帮助,麻烦帮忙一键三连(点赞、转发、收藏)~


相关推荐

redis的八种使用场景

前言:redis是我们工作开发中,经常要打交道的,下面对redis的使用场景做总结介绍也是对redis举报的功能做梳理。缓存Redis最常见的用途是作为缓存,用于加速应用程序的响应速度。...

基于Redis的3种分布式ID生成策略

在分布式系统设计中,全局唯一ID是一个基础而关键的组件。随着业务规模扩大和系统架构向微服务演进,传统的单机自增ID已无法满足需求。高并发、高可用的分布式ID生成方案成为构建可靠分布式系统的必要条件。R...

基于OpenWrt系统路由器的模式切换与网页设计

摘要:目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户、提升服务的目标。传统路由器自带的Luci界面提供了工厂模式的Web界面,用户可通过该界面配置路...

这篇文章教你看明白 nginx-ingress 控制器

主机nginx一般nginx做主机反向代理(网关)有以下配置...

如何用redis实现注册中心

一句话总结使用Redis实现注册中心:服务注册...

爱可可老师24小时热门分享(2020.5.10)

No1.看自己以前写的代码是种什么体验?No2.DooM-chip!国外网友SylvainLefebvre自制的无CPU、无操作码、无指令计数器...No3.我认为CS学位可以更好,如...

Apportable:拯救程序员,IOS一秒变安卓

摘要:还在为了跨平台使用cocos2d-x吗,拯救objc程序员的奇葩来了,ApportableSDK:FreeAndroidsupportforcocos2d-iPhone。App...

JAVA实现超买超卖方案汇总,那个最适合你,一篇文章彻底讲透

以下是几种Java实现超买超卖问题的核心解决方案及代码示例,针对高并发场景下的库存扣减问题:方案一:Redis原子操作+Lua脚本(推荐)//使用Redis+Lua保证原子性publicbo...

3月26日更新 快速施法自动施法可独立设置

2016年3月26日DOTA2有一个79.6MB的更新主要是针对自动施法和快速施法的调整本来内容不多不少朋友都有自动施法和快速施法的困扰英文更新日志一些视觉BUG修复就不翻译了主要翻译自动施...

Redis 是如何提供服务的

在刚刚接触Redis的时候,最想要知道的是一个’setnameJhon’命令到达Redis服务器的时候,它是如何返回’OK’的?里面命令处理的流程如何,具体细节怎么样?你一定有问过自己...

lua _G、_VERSION使用

到这里我们已经把lua基础库中的函数介绍完了,除了函数外基础库中还有两个常量,一个是_G,另一个是_VERSION。_G是基础库本身,指向自己,这个变量很有意思,可以无限引用自己,最后得到的还是自己,...

China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting

BEIJING,May21(Xinhua)--ChineseForeignMinisterWangYi,alsoamemberofthePoliticalBureau...

移动工作交流工具Lua推出Insights数据分析产品

Lua是一个适用于各种职业人士的移动交流平台,它在今天推出了一项叫做Insights的全新功能。Insights是一个数据平台,客户可以在上面实时看到员工之间的交流情况,并分析这些情况对公司发展的影响...

Redis 7新武器:用Redis Stack实现向量搜索的极限压测

当传统关系型数据库还在为向量相似度搜索的性能挣扎时,Redis7的RedisStack...

Nginx/OpenResty详解,Nginx Lua编程,重定向与内部子请求

重定向与内部子请求Nginx的rewrite指令不仅可以在Nginx内部的server、location之间进行跳转,还可以进行外部链接的重定向。通过ngx_lua模块的Lua函数除了能实现Nginx...