20-logging
Pod 日志收集
应用程序和系统日志可以帮助我们了解集群内部的运行情况,日志对于我们调试问题和监视集群情况也是非常有用的。而且大部分的应用都会有日志记录,对于传统的应用大部分都会写入到本地的日志文件之中。对于容器化应用程序来说则更简单,只需要将日志信息写入到 stdout 和 stderr 即可,容器默认情况下就会把这些日志输出到宿主机上的一个 JSON 文件之中,同样我们也可以通过 docker logs 或者 kubectl logs 来查看到对应的日志信息。
但是,通常来说容器引擎或运行时提供的功能不足以记录完整的日志信息,比如,如果容器崩溃了、Pod 被驱逐了或者节点挂掉了,我们仍然也希望访问应用程序的日志。所以,日志应该独立于节点、Pod 或容器的生命周期,这种设计方式被称为 cluster-level-logging,即完全独立于 Kubernetes 系统,需要自己提供单独的日志后端存储、分析和查询工具。
Kubernetes 中大多数的Pod日志被输出到控制台,在宿主机的文件系统每个Pod会创建一个存放日志的文件夹/var/log/pods/这里会存放所有这个节点运行的Pod的日志,但是这个文件夹下一般都是软连接,由于Kubernetes 底层的CRI容器运行时可以使用很多所以日志本身并不存放在这个文件夹,以下为容器运行时真正存放日志目录:
- container log: /var/log/containers/*.log
- Pod log:/var/log/pods
集群级别日志架构
- 使用在每个节点上运行的节点级日志记录代理。
- 在应用程序的 Pod 中,包含专门记录日志的边车(Sidecar)容器。
- 将日志直接从应用程序中推送到日志记录后端。
使用节点级日志代理
可以通过在每个节点上使用节点级的日志记录代理来实现集群级日志记录。 日志记录代理是一种用于暴露日志或将日志推送到后端的专用工具。 通常,日志记录代理程序是一个容器,它可以访问包含该节点上所有应用程序容器的日志文件的目录。
由于日志记录代理必须在每个节点上运行,推荐以 DaemonSet 的形式运行该代理。
节点级日志在每个节点上仅创建一个代理,不需要对节点上的应用做修改。
容器向标准输出和标准错误输出写出数据,但在格式上并不统一。 节点级代理收集这些日志并将其进行转发以完成汇总。
使用边车容器运行日志代理 sidecar
从应用中直接暴露日志目录
EFK on IAAS
Yum 仓库
|
Bash rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
vim /etc/yum.repos.d/elasic.repo [elasticsearch] name=Elasticsearch repository for 7.x packages baseurl=https://artifacts.elastic.co/packages/7.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
|
Elasticsearch
Elasticsearch是一个开源的分布式搜索和分析引擎,建立在Apache Lucene库之上。它提供了一个高性能、可伸缩和全文搜索能力强大的分布式系统,适用于处理大规模数据集的搜索、分析和实时数据处理。
|
Bash wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.16-x86_64.rpm rpm --install elasticsearch-7.17.16-x86_64.rpm
|
检查集群状态
|
Bash # 列出节点健康状态 curl -XGET 127.0.0.1:9200/_cat/health?v # 显示cluster状态 curl -XGET 127.0.0.1:9200/_cluster/health\?pretty # 列出 master节点 curl -XGET 127.0.0.1:9200/_cat/master?v # 列出节点及利用率 curl -XGET 127.0.0.1:9200/_cat/nodes?v # 显示索引 curl localhost:9200/_cat/indices?v
|
Kibana
Kibana是一个开源的数据可视化和分析平台,与Elasticsearch紧密集成。它提供了一个直观的Web界面,让用户能够轻松地探索、分析和可视化存储在Elasticsearch中的数据。
|
Bash wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.16-x86_64.rpm
|
Filebeat
Filebeat是一个轻量级的开源日志数据收集器,由Elasticsearch提供支持。它专门用于收集、解析和发送日志文件和其他结构化数据到Elasticsearch或Logstash等目标系统进行处理和分析。
|
YAML # ============================== Filebeat inputs =============================== filebeat.inputs:
# Each - is an input. Most options can be set at the input level, so # you can use different inputs for various configurations. # Below are the input specific configurations.
# filestream is an input for collecting log messages from files. - type: filestream
# Unique ID among all inputs, an ID is required. id: my-filestream-id
# Change to true to enable this input configuration. enabled: false
# Paths that should be crawled and fetched. Glob based paths. paths: - /var/log/*.log #- c:\programdata\elasticsearch\logs\*
# ================================== Outputs ===================================
# Configure what output to use when sending the data collected by the beat.
# ---------------------------- Elasticsearch Output ---------------------------- output.elasticsearch: # Array of hosts to connect to. hosts: ["localhost:9200"]
# Protocol - either `http` (default) or `https`. #protocol: "https"
|
EFK on Kubernetes
安装 ElasticSearch
|
Bash # Add the Elastic Helm charts repo helm repo add elastic https://helm.elastic.co
# 查询版本 我们使用 7.17.3 [root@master-01 ~]# helm search repo elastic/elasticsearch -l NAME CHART VERSION APP VERSION DESCRIPTION elastic/elasticsearch 8.5.1 8.5.1 Official Elastic helm chart for Elasticsearch elastic/elasticsearch 7.17.3 7.17.3 Official Elastic helm chart for Elasticsearch elastic/elasticsearch 7.17.1 7.17.1 Official Elastic helm chart for Elasticsearch elastic/elasticsearch 7.16.3 7.16.3 Official Elastic helm chart for Elasticsearch elastic/elasticsearch 7.16.2 7.16.2 Official Elastic helm chart for Elasticsearch
[root@master-01 ~]# helm pull elastic/elasticsearch --version=7.17.3
|
修改 values
安装
|
Bash [root@master-01 20-log]# helm install els -n logging -f elasticsearch/els-values.yaml ./elasticsearch NAME: els LAST DEPLOYED: Sat Nov 18 20:17:57 2023 NAMESPACE: logging STATUS: deployed REVISION: 1 NOTES: 1. Watch all cluster members come up. $ kubectl get pods --namespace=logging -l app=elasticsearch-master -w2. Test cluster health using Helm test. $ helm --namespace=logging test els
|
集群验证
|
Bash [root@master-01 20-log]# kubectl get pods --namespace=logging -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES elasticsearch-master-0 1/1 Running 0 99s 10.244.171.24 worker-01 <none> <none> elasticsearch-master-1 1/1 Running 0 74s 10.244.184.101 master-01 <none> <none>
[root@master-01 20-log]# curl 10.244.37.199:9200/_cluster/health {"cluster_name":"elasticsearch","status":"green","timed_out":false,"number_of_nodes":2,"number_of_data_nodes":2,"active_primary_shards":1,"active_shards":2,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":100.0}
|
安装 Kibana
|
Bash [root@master-01 20-log]# helm search repo elastic/kibana -l NAME CHART VERSION APP VERSION DESCRIPTION elastic/kibana 8.5.1 8.5.1 Official Elastic helm chart for Kibana elastic/kibana 7.17.3 7.17.3 Official Elastic helm chart for Kibana elastic/kibana 7.17.1 7.17.1 Official Elastic helm chart for Kibana
# 拉取 chart helm pull elastic/kibana --version=7.17.3
|
|
Bash [root@master-01 20-log]# helm -n logging install kibana -f kibana/kibana-values.yaml ./kibana NAME: kibana LAST DEPLOYED: Sat Nov 18 20:49:28 2023 NAMESPACE: logging STATUS: deployed REVISION: 1 TEST SUITE: None
# 更新 [root@master-01 20-log]# helm -n logging upgrade kibana -f kibana/kibana-values.yaml ./kibana Release "kibana" has been upgraded. Happy Helming! NAME: kibana LAST DEPLOYED: Sat Nov 18 21:14:34 2023 NAMESPACE: logging STATUS: deployed REVISION: 2 TEST SUITE: None
|
修改 Kibana SVC 使用 NodePort
安装 Filebeat
|
Bash [root@master-01 20-log]# helm search repo elastic/filebeat -l NAME CHART VERSION APP VERSION DESCRIPTION elastic/filebeat 8.5.1 8.5.1 Official Elastic helm chart for Filebeat elastic/filebeat 7.17.3 7.17.3 Official Elastic helm chart for Filebeat elastic/filebeat 7.17.1 7.17.1 Official Elastic helm chart for Filebeat elastic/filebeat 7.16.3 7.16.3 Official Elastic helm chart for Filebeat
[root@master-01 20-log]# helm pull elastic/filebeat --version=7.17.3
|
|
Bash [root@master-01 20-log]# helm -n logging install filebeat -f filebeat/filebeat-values-1.yaml ./filebeat
NAME: filebeat LAST DEPLOYED: Sat Nov 18 21:53:15 2023 NAMESPACE: logging STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: 1. Watch all containers come up. $ kubectl get pods --namespace=logging -l app=filebeat-filebeat -w
|
|
YAML https://artifacthub.io/packages/helm/elastic/elasticsearch
https://artifacthub.io/packages/helm/elastic/kibana
https://artifacthub.io/packages/helm/fluent/fluentd
helm repo add elastic https://helm.elastic.co
|

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)