今年3月份在公司的内部k8s培训会上,开发同事对应用整合进pod提出了几个问题,主要围绕在java应用的日志统一收集、集中存放和java jvm内存监控数据收集相关的点上,本文将介绍使用filebeat实现pod日志的统一收集,集中存放使用集群外的elasticsearch,后续可以加上kibana及模板文件实现更友好的数据展示。
一、准备和测试tomcat基础镜像该镜像主要是配置jdk环境变量和tomcat软件包部署,如果有特殊的需求,例如安装其他软件包、配置tomcat https等也可以在dockerfile里面实现。之后如果有对jdk和tomcat进行统一升级,更新基础镜像即可。
1、dockerfile编译镜像并推送到本地仓库
# docker pull centos # cd tomcat/ # ls # cat dockerfile FROM centos MAINTAINER yangliangwei "ylw@fjhb.cn" WORKDIR /home COPY java1.8.tgz /home/ RUN tar zxf /home/java1.8.tgz -C /usr/local/ && rm -rf /home/java1.8.tgz ENV JAVA_HOME /usr/local/java/ ENV CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV PATH $PATH:$JAVA_HOME/bin COPY apache-tomcat-7.0.86.tar.gz /home/ RUN tar zxf apache-tomcat-7.0.86.tar.gz -C /home && rm -rf /home/apache-tomcat-7.0.86.zip RUN mv /home/apache-tomcat-7.0.86 /home/tomcat EXPOSE 8080 ENTRYPOINT /home/tomcat/bin/startup.sh && tail -f /home/tomcat/logs/catalina.out # docker build -t tomcat_base:v1.0 . # docker tag tomcat_base:v1.0 registry.59iedu.com # docker push registry.59iedu.com/tomcat_base:v1.02、其他docker主机通过本地仓库下载镜像run测试
# docker pull registry.59iedu.com/tomcat_base:v1.0 # docker run -idt --name tomcat_test -p 8889:8080 registry.59iedu.com/tomcat_base:v1.0 # curl http://localhost:8889二、在基础镜像上叠加app并推送到本地仓库这里选择使用shopxx java开源项目来做模拟java app,将部署的shopxx打包好放入指定位置即可运行。为了简单,这里选择Shopxx调用的数据库放在k8s集群之外,只要pod网络可以正常访问即可。
# cd shopxx # cat dockerfile FROM registry.59iedu.com/tomcat_base:v1.0 MAINTAINER yangliangwei "ylw@fjhb.cn" COPY ROOT.tgz /home/ RUN tar zxf /home/ROOT.tgz -C /home/tomcat/webapps/ && rm -rf /home/ROOT.tgz # docker build -t tomcat_shopxx:v1 . # docker tag tomcat_shopxx:v1 registry.59iedu.com/tomcat_shopxx:v1 # docker push registry.59iedu.com/tomcat_shopxx:v1三、准备filebeat镜像Filebeat容器主要负责将app容器里面的日志推送到elasticsearch, 为了降低耦合度及后续的维护难度,创建filebeat容器的时候将filebeat.yml配置文件以configmap的方式实现。所以在镜像编译部分不考虑filebeat.yml配置文件
# cd filebeat-v5.4.0/ # ls -lh # cat dockerfile FROM docker.io/centos MAINTAINER YangLiangWei <ylw@fjhb.cn> # Install Filebeat WORKDIR /usr/local COPY filebeat-5.4.0-linux-x86_64.tar.gz /usr/local RUN cd /usr/local && \ tar xvf filebeat-5.4.0-linux-x86_64.tar.gz && \ rm -f filebeat-5.4.0-linux-x86_64.tar.gz && \ ln -s /usr/local/filebeat-5.4.0-linux-x86_64 /usr/local/filebeat && \ chmod +x /usr/local/filebeat/filebeat && \ mkdir -p /etc/filebeat ADD ./docker-entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/docker-entrypoint.sh ENTRYPOINT ["docker-entrypoint.sh"] CMD ["/usr/local/filebeat/filebeat","-e","-c","/etc/filebeat/filebeat.yml"] # cat docker-entrypoint.sh #!/bin/bash config=/etc/filebeat/filebeat.yml env echo 'Filebeat init process done. Ready for start up.' echo "Using the following configuration:" cat /etc/filebeat/filebeat.yml exec "$@" # docker build -t filebeat-v5.4.0 . # docker tag filebeat-v5.4.0 registry.59iedu.com/filebeat:v5.4.0 # docker push registry.59iedu.com/filebeat:v5.4.0四、准备yaml配置文件1、tomcat.yaml文件一个POD里面运行两个容器,两个容器通过emptydir的方式共享/home/tomcat/logs目录下的日志文件。Filebeat的配置文件通过configmap方式挂载进容器
# cat tomcat.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: tomcat-shopxx namespace: default spec: replicas: 1 template: metadata: labels: k8s-app: tomcat-shopxx spec: containers: - image: registry.59iedu.com/filebeat:v5.4.0 imagePullPolicy: Always name: filebeat volumeMounts: - name: app-logs mountPath: /log - name: filebeat-config mountPath: /etc/filebeat/ - image: registry.59iedu.com/tomcat_shopxx:v1 name : tomcat-shopxx imagePullPolicy: Always ports: - containerPort: 8080 volumeMounts: - name: app-logs mountPath: /home/tomcat/logs volumes: - name: app-logs emptyDir: {} - name: filebeat-config configMap: name: filebeat-config2、configamp.yaml通过configmap的形式创建filebeat.yml配置文件,指定收集日志的路径、elasticsearch的配置信息及索引名称
# cat configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: filebeat-config data: filebeat.yml: | filebeat.prospectors: - input_type: log paths: - "/var/log/yum.log" - "/log/*" output.elasticsearch: hosts: ["192.168.1.19:9600"] index: "filebeat-tomcat-shopxx"3、service.yaml文件通过service.yaml文件将shopxx发布出去
# cat service.yaml apiVersion: v1 kind: Service metadata: name: tomcat-shopxx labels: k8s-app: tomcat-shopxx spec: type: NodePort ports: - port: 8080 protocol: TCP targetPort: 8080 name: http nodePort: 8480 selector: k8s-app: tomcat-shopxx五、通过yaml配置文件创建pod、configmap、service
# kubectl create -f . # kubectl get pod # kubectl get svc # kubectl get deployment六、测试与验证Filebeat容器日志:Shopxx容器日志:使用elasticsearch-head插件查看对应的索引:访问web测试: