今年3月份在公司的內部k8s培訓會上,開發同事對應用整合進pod提出了幾個問題,主要圍繞在java應用的日誌統一收集、集中存放和java jvm內存監控數據收集相關的點上,本文將介紹使用filebeat實現pod日誌的統一收集,集中存放使用集羣外的elasticsearch,後續能夠加上kibana及模板文件實現更友好的數據展現。java
1、準備和測試tomcat基礎鏡像
該鏡像主要是配置jdk環境變量和tomcat軟件包部署,若是有特殊的需求,例如安裝其餘軟件包、配置tomcat https等也能夠在dockerfile裏面實現。以後若是有對jdk和tomcat進行統一升級,更新基礎鏡像便可。node
一、dockerfile編譯鏡像並推送到本地倉庫linux
# 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.0
二、其餘docker主機經過本地倉庫下載鏡像run測試web
# 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
2、在基礎鏡像上疊加app並推送到本地倉庫
這裏選擇使用shopxx java開源項目來作模擬java app,將部署的shopxx打包好放入指定位置便可運行。爲了簡單,這裏選擇Shopxx調用的數據庫放在k8s集羣以外,只要pod網絡能夠正常訪問便可。docker
# 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
3、準備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
4、準備yaml配置文件
一、tomcat.yaml文件
一個POD裏面運行兩個容器,兩個容器經過emptydir的方式共享/home/tomcat/logs目錄下的日誌文件。Filebeat的配置文件經過configmap方式掛載進容器apache
# 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-config
二、configamp.yaml
經過configmap的形式建立filebeat.yml配置文件,指定收集日誌的路徑、elasticsearch的配置信息及索引名稱centos
# 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"
三、service.yaml文件
經過service.yaml文件將shopxx發佈出去api
# 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
5、經過yaml配置文件建立pod、configmap、servicetomcat
# kubectl create -f . # kubectl get pod # kubectl get svc # kubectl get deployment
6、測試與驗證
Filebeat容器日誌:
Shopxx容器日誌:
使用elasticsearch-head插件查看對應的索引:
訪問web測試: