要求每个pod挂相同cm模板,得到属于自己的配置

这里写图片描述

具体实现

以filebeat镜像为例:

docker-entrypoint.sh

#!/bin/bash

set -ex

echo "pod-ip is $POD_IP"
sed -i "s#\#pod-ip#pod-ip: $POD_IP#g" /etc/filebeat/filebeat.yml  #注意这句
filebeat -e -c /etc/filebeat/filebeat.yml
Dockerfile

FROM docker.elastic.co/beats/filebeat:5.4.1

COPY docker-entrypoint.sh /usr/local/bin/
#RUN chmod +x /docker-entrypoint.sh

ENTRYPOINT ["docker-entrypoint.sh"]
apiVersion: v1
kind: ConfigMap
metadata:
  name: tomcat-spring
data:
  filebeat.yml: |
    filebeat.prospectors:
    - input_type: log
      fields:
        service-name: tomcat-spring
        #pod-ip                       # docker-entrypoint.sh会替换这行,具体,先sed获取pod变量值,然后替换这个#pod-ip.
      paths:
        - "/log/*.txt"
      multiline:
        pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3}'
        negate: true
        match: after
    output.elasticsearch:
      hosts: ["elasticsearch.logging.svc.cluster.local:9200"]
      username: "elastic"
      password: "changeme"
      template.name: "filebeat"
      template.path: "filebeat.template.json"
      template.overwrite: true
      index: "filebeat-docker-test"
    # Available log levels are: critical, error, warning, info, debug
    logging.level: info
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐