k8s中日志的收集方式。一般有两种方式:
一:sidecar模式,就是一个pod中部署两个容器,一个跑应用,一个采集日志,用emptdir的共享目录形式。
缺点:一个应用一个收集日志的容器,后期的话资源消耗是个问题。
二:节点模式,一个节点跑一个agent来采集标准输出和标准错误输出的日志,然后发送给后端。(标准日志:容器内输出到/dev/std...,或tail -f ,默认会存储在节点的/var/lib/docker/containers/...路径下,kubectl logs 能看到的日志就是标准日志。集群系统的日志一般在/var/log/containers/下。)详情网上查阅资料,这个只是简单的一个说法,并不严谨。
先尝试第一个模式: sidecar模式
一:创建filebeat的dockerfile,这里我用了centos,做好后镜像大约300+M,先尝试看看问题,后期换个基础镜像。
- FROM centos:latest
- WORKDIR /usr/local
- ADD filebeat-7.5.0-linux-x86_64.tar.gz .
- RUN ln -s filebeat-7.5.0-linux-x86_64 filebeat \
- && cd filebeat \
- && mkdir config \
- && chmod +x filebeat \
- && cp filebeat.yml config/
-
- ENTRYPOINT ["/usr/local/filebeat/filebeat","-c","/usr/local/filebeat/config/filebeat.yml"]
所需要的 filebeat 配置文件如下:
- filebeat.idle_timeout: 2s
- filebeat.name: filebeat-shiper
- filebeat.spool_zie: 50000
-
- filebeat.inputs: # 从这里开始定义每个日志的路径、类型、收集方式等信息
- - type: log # 指定收集的类型为 log
- paths:
- - /var/log/nginx/access.log # 设置 access.log 的路径
- fields: # 设置一个 fields,用于标记这个日志
- type: access-log # 为 fields 设置一个关键字 type,值为 access-log
- enabled: true
- backoff: 1s
- backoff_factor: 2
- close_inactive: 1h
- encoding: plain
- harvester_buffer_size: 262144
- max_backoff: 10s
- max_bytes: 10485760
- scan_frequency: 10s
- tail_lines: true
- - type: log
- paths:
- - /var/log/nginx/error.log # 设置 error.log 的路径
- fields: # 设置一个 fields,用于标记这个日志
- type: error-log # 为 fields 设置一个关键字 type,值为 error-log
- enabled: true
- backoff: 1s
- backoff_factor: 2
- close_inactive: 1h
- encoding: plain
- harvester_buffer_size: 262144
- max_backoff: 10s
- max_bytes: 10485760
- scan_frequency: 10s
- tail_lines: true
- - type: log
- paths:
- - /var/log/nginx/blacklist.log # 设置 blacklist.log 的路径
- fields: # 设置一个 fields,用于标记这个日志
- type: blacklist-log # 为 fields 设置一个关键字 type,值为 blacklist-log
- enabled: true
- backoff: 1s
- backoff_factor: 2
- close_inactive: 1h
- encoding: plain
- harvester_buffer_size: 262144
- max_backoff: 10s
- max_bytes: 10485760
- scan_frequency: 10s
- tail_lines: true
-
- output.elasticsearch:
- workers: 4
- bulk_max_size: 8192
- hosts: # 设置 elastic 的地址
- - 10.16.12.206:30187
- - 10.16.12.207:30187
- - 10.16.12.208:30187
- - 10.16.13.214:30187
- - 10.16.13.215:30187
- index: web-nginx-%{[fields.type]}-%{+yyyy.MM.dd} # 设置索引名称,后面引用的 fields.type 变量。此处的配置应该可以省略(不符合下面创建索引条件的日志,会使用该索引,后续会测试是否是这样)
- indices: # 使用 indices 代表要创建多个索引
- - index: web-nginx-access-log-%{+yyyy.MM.dd} # 设置 access.log 日志的索引,注意索引前面的 web-nginx 要与setup.template.pattern 的配置相匹配
- when.equals: # 设置创建索引的条件:当 fields.type 的值等于 access-log 时才生效
- fields.type: access-log
- - index: web-nginx-error-log-%{+yyyy.MM.dd}
- when.equals:
- fields.type: error-log
- - index: web-nginx-blacklist-log-%{+yyyy.MM.dd}
- when.equals:
- fields.type: blacklist-log
-
- processors:
- - drop_fields:
- fields:
- - agent.ephemeral_id
- - agent.hostname
- - agent.id
- - agent.type
- - agent.version
- - ecs.version
- - input.type
- - log.offset
- - version
- - decode_json_fields:
- fields:
- - message
- max_depth: 1
- overwrite_keys: true
-
- setup.ilm.enabled: false # 如果要创建多个索引,需要将此项设置为 false
- setup.template.name: web-nginx-log # 设置模板的名称
- setup.template.pattern: web-nginx-* # 设置模板的匹配方式,上面索引的前缀要和这里保持一致
- setup.template.overwrite: true
- setup.template.enabled: true
直接构建镜像,并推送到私有仓库
docker build -t 10.1.1.1:6000/centos-filebeat:0.6 .
docker push 10.1.1.1:6000/centos-filebeat:0.6
可以docker run运行看一下是否正常
二:创建一个deploymen资源,包含两个容器
- [root@k8s-master ~/software]# cat nginx-filebeat-sidecar.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: nginx-fb
- namespace: sidecar
- spec:
- replicas: 1
- selector:
- matchLabels:
- app: nginx-fb
- template:
- metadata:
- namespace: sidecar
- labels:
- app: nginx-fb
- spec:
- containers:
- - image: nginx:alpine
- name: nginx
- ports:
- - containerPort: 80
- volumeMounts:
- - name: applog
- mountPath: /var/log/nginx
- - image: 10.1.1.1:6000/centos-filebeat:0.6
- name: filebeat
- volumeMounts:
- - name: applog
- mountPath: /log
- - name: filebeat-config
- mountPath: /usr/local/filebeat/config/
- volumes:
- - name: applog
- emptyDir: {}
- - name: filebeat-config
- configMap:
- name: filebeat-config
三:创建好yaml总的namespace和configmap
namespace,metadata里只填写name就可以
- [root@k8s-master ~/software]# kubectl get namespaces sidecar -o yaml
- apiVersion: v1
- kind: Namespace
- metadata:
- creationTimestamp: "2022-06-10T09:38:02Z"
- name: sidecar
- resourceVersion: "669711"
- selfLink: /api/v1/namespaces/sidecar
- uid: 5b4d0ecc-ff6a-4b62-a41f-a9924efa4e0a
- spec:
- finalizers:
- - kubernetes
- status:
- phase: Active
configmap:以命令行的方式创建的,看一下filebeat的配置文件,配置文件有些问题,并不标准,下期更新一下
kubectl create configmap filebeat-config --from-file=filebeat.yml --namespace=sidecar #configmap名字要和上面deployment资源中引用的configmap名字相同,均标了红色。
- [root@k8s-master ~/software]# cat filebeat.yml
- filebeat.inputs:
- - type: log
- enabled: true
- paths:
- - /log/access.log
- json.keys_under_root: true
- json.overwrite_keys: true
- tags: ["access"]
-
- - type: log
- enabled: true
- paths:
- - /log/error.log
- tags: ["error"]
-
- output.elasticsearch:
- hosts: ["10.1.1.1:9200"]
- indices:
- - index: "nginx_access-%{[beat.version]}-%{+yyyy.MM}"
- when.contains:
- tags: "access"
- - index: "nginx_error-%{[beat.version]}-%{+yyyy.MM}"
- when.contains:
- tags: "error"
-
- setup.template.name: "nginx"
- setup.template.pattern: "nginx_*"
- setup.template.enabled: false
- setup.template.overwrite: true
四:创建service资源,让nginx可以被外部访问
- [root@k8s-master ~/software]# cat service-nginx.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: service-nginx
- namespace: sidecar
- spec:
- type: NodePort
- ports:
- - port: 80
- protocol: TCP
- targetPort: 80
- nodePort: 30080
- selector:
- app: nginx-fb
五:3和4 直接启动 kubectl creat -f
nginx 在浏览器中可以正常访问 http://10.1.1.1:30080

ES还并没有部署在k8s中,在虚拟机中,从filebeat的配置文件可以看出来,可以正常看到两类日志,没有问题。
