Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to collect application logs uniformly using sidecar pattern in K8S (applicable to all technical language systems)

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to use sidecar mode in K8S to collect application logs (applicable to all technical language systems). The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Filebeat can collect container logs in sidecar mode, that is, filebeat and a specific service container are deployed in the same pod, specify the path or file to collect logs, and send logs to search engines such as specified location or Elasticsearch.

The mode of deploying filebeat within each pod has the advantage of low coupling with specific application services and strong scalability, but requires additional configuration in the yaml.

The theory can support all technical language systems, as long as you can output files and logs.

App service

-apiVersion: v1kind: Servicemetadata: name: test-app labels: app: test-appspec: selector: app: test-app ports:-protocol: TCP port: 8080 targetPort: 8080 name: test-port# defines a configmap---apiVersion: v1kind: ConfigMapmetadata: name: k8s-app: filebeatdata: filebeat.yml:-filebeat.prospectors:-type: log paths :-/ logdata/*.log tail_files: true fields: pod_name:'${pod_name} 'POD_IP:' ${POD_IP} 'setup.template.name: "app-logs" setup.template.pattern: "app-logs-*" output.elasticsearch: # Log output to ES hosts: ["192.168.1.xx:9200" "192.168.1.xxx:9200"] index: "app-logs-% {+ yyyy.MM}" # deployment, or via daemonset-apiVersion: apps/v1kind: Deploymentmetadata: name: test-appspec: 1 minReadySeconds: 1 minReadySeconds: 15 # Mark pod ready after 15 seconds of rolling upgrade strategy: rollingUpdate: # replicas is 2 The number of pod in the upgrade process is between 1 and 3 maxSurge: 1 # when rolling upgrade starts 1 pod maxUnavailable: 1 # maximum number of pod allowed to be in Unavailable during rolling upgrade selector: matchLabels: app: test-app template: metadata: labels: app: test-app spec: terminationGracePeriodSeconds: 30 # 30 seconds elegant shutdown program containers :-image: hub.exmaple.com/publib/filebeat:6.1.3 # download the image that comes to the private image library in advance (the official image may be blocked) name: filebeat args: ["- c" "/ opt/filebeat/filebeat.yml", "- e" ] env:-name: POD_IP valueFrom: fieldRef: apiVersion: v1 fieldPath: status.podIP-name: pod_name valueFrom: fieldRef: apiVersion: V1 fieldPath: metadata.name securityContext: runAsUser: 0 resources: Limits: memory: 200Mi requests: cpu: 200m memory: 200Mi volumeMounts:-name: config # put the contents of configmap in the local directory of the container mountPath: / opt/filebeat/-name: data mountPath: / usr/share/filebeat/data-name: logdata # Two applications in the same pod share the directory logdata Write an app image that reads mountPath: / logdata-name: test-app image: hub.example.com/service/test-service:latest # to provide specific services ports:-containerPort: 8080 volumeMounts:-name: logdata # specify to hang to logdata mountPath: / usr/local/tomcat/logs volumes:-name: Data emptyDir: {}-name: logdata # defines logdata to mount a directory of type EmptyDir emptyDir: {}-name: config configMap: name: test-filebeat-config # using the previously defined configmap items:-key: filebeat.yml path: filebeat.yml

View the running logs of filebeat and app

Kubectl logs-f filebeatkubectl logs-f test-app

At this point, you can see the detailed running log, and a new index of app-logs-xxxx.xx will be created on elasticsearch.

On how to use sidecar mode in K8S to collect application logs (applicable to all technical language systems) is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report