Kubernetes-日志收集案例三【容器内置日志收集】

日志收集案例三:容器内置日志手机服务器进程

结合 日志收集之前Kafka - zookeeper - logstash - ES - Kibana

一、镜像制作

#可以直接使用已经制做好的镜像 registry.cn-hangzhou.aliyuncs.com/birkhoff/tomcat-app1:v4-filebeat
==============================================================================
#基础镜像
docker pull centos:7.9.2009
#一层一层 一个个打镜像
=> centos:7.9.2009(include filebeat-7.12.1)
=> jdk-base:v8.212
=> tomcat-base:v8.5.43
=> tomcat-app1:v4-filebeat

[root@k8s-master01 1.webapp-filebeat-image-Dockerfile]# ll
total 31896
-rw-r--r-- 1 root root      308 Dec 27 22:52 build-command.sh
-rwxr-xr-x 1 root root    23611 Jun 22  2021 catalina.sh
-rw-r--r-- 1 root root      398 Dec 27 22:51 Dockerfile
-rw-r--r-- 1 root root 32600353 May 18  2021 filebeat-7.12.1-x86_64.rpm
-rw-r--r-- 1 root root      618 Dec 27 22:54 filebeat.yml
-rw-r--r-- 1 root root       41 Dec 27 22:54 index.html
drwxr-xr-x 2 root root       24 Dec 27 22:54 myapp
-rw-r--r-- 1 root root      149 Aug 16  2022 myapp.tar.gz
-rwxr-xr-x 1 root root      372 May 24  2022 run_tomcat.sh
-rw-r--r-- 1 root root     6462 Oct 10  2021 server.xml

#1.build-command.sh
#!/bin/bash
TAG=$1
nerdctl build -t  registry.cn-hangzhou.aliyuncs.com/birkhoff/tomcat-app1:${TAG}  .
nerdctl push registry.cn-hangzhou.aliyuncs.com/birkhoff/tomcat-app1:${TAG}

#2.catalina.sh 需要有执行权限 chmod a+x catalina.sh

#3.Dockerfile
FROM registry.cn-hangzhou.aliyuncs.com/birkhoff/tomcat-base:v8.5.43
ADD catalina.sh /apps/tomcat/bin/catalina.sh
ADD server.xml /apps/tomcat/conf/server.xml
ADD myapp.tar.gz /data/tomcat/webapps/myapp/
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD filebeat.yml /etc/filebeat/filebeat.yml
RUN chown  -R tomcat.tomcat /data/ /apps/
EXPOSE 8080 8443
CMD ["/apps/tomcat/bin/run_tomcat.sh"]

#4.filebeat.yml 配置实际Kafaka集群地址
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /apps/tomcat/logs/catalina.out
  fields:
    type: filebeat-tomcat-catalina
- type: log
  enabled: true
  paths:
    - /apps/tomcat/logs/localhost_access_log.*.txt
  fields:
    type: filebeat-tomcat-accesslog
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 1
setup.kibana:

output.kafka:
  hosts: ["192.168.40.101:9092","192.168.40.111:9092","192.168.40.112:9092"]
  required_acks: 1
  topic: "filebeat-magedu-app1"
  compression: gzip
  max_message_bytes: 1000000

#5.run_tomcat.sh 需要有执行权限run_tomcat.sh
#!/bin/bash
/usr/share/filebeat/bin/filebeat -e -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat -path.data /var/lib/filebeat -path.logs /var/log/filebeat &
su - tomcat -c "/apps/tomcat/bin/catalina.sh start"
tail -f /etc/hosts

#6.server.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="/data/tomcat/webapps"  unpackWARs="false" autoDeploy="false">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

#7.catalina.sh 省略太长了

二、部署tomcat-app1应用 使用之前的镜像 tomcat-app1.yaml

[root@xianchaomaster1 3.container-filebeat-process]# cat 3.tomcat-app1.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-tomcat-app1-filebeat-deployment-label
  name: magedu-tomcat-app1-filebeat-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-tomcat-app1-filebeat-selector
  template:
    metadata:
      labels:
        app: magedu-tomcat-app1-filebeat-selector
    spec:
      #nodeName: xianchaonode1
      containers:
      - name: magedu-tomcat-app1-filebeat-container
        image: harbor.magedu.net/magedu/tomcat-app1:v4-filebeat
        imagePullPolicy: IfNotPresent
        #imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 1
            memory: "512Mi"
          requests:
            cpu: 500m
            memory: "512Mi"

[root@xianchaomaster1 3.container-filebeat-process]# kubectl get pods
NAME                                                      READY   STATUS     RESTARTS   AGE
magedu-tomcat-app1-filebeat-deployment-5f9979485f-7mc6k   2/2     Running    0          8h

 三、部署 Service tomcat-service.yaml

[root@xianchaomaster1 3.container-filebeat-process]# cat 4.tomcat-service.yaml
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: magedu-tomcat-app1-filebeat-service-label
  name: magedu-tomcat-app1-filebeat-service
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30094
  selector:
    app: magedu-tomcat-app1-filebeat-selector


[root@xianchaomaster1 3.container-filebeat-process]# kubectl get svc
magedu-tomcat-app1-filebeat-service   NodePort    10.108.226.180   <none>        80:30094/TCP   7h36m

#访问应用
http://IP:30094/myapp/

四、确保ES、Kibana、Kafka、Zookeeper 应用都正常(参照之前的部署链接 https://www.cnblogs.com/birkhoffxia/articles/17283817.html) 

五、配置Logstash 读取Kafka中内容

[root@xianchaomaster1 3.container-filebeat-process]# cat 5.logstash-filebeat-process-kafka-to-es.conf
input {
  kafka {
    bootstrap_servers => "192.168.40.180:9092,192.168.40.181:9092,192.168.40.182:9092"
    topics => ["filebeat-magedu-app1"]
    codec => "json"
  }
}

output {
  if [fields][type] == "filebeat-tomcat-catalina" {
    elasticsearch {
      hosts => ["192.168.40.180:9200","192.168.40.181:9200"]
      index => "filebeat-tomcat-catalina-%{+YYYY.MM.dd}"
    }}

  if [fields][type] == "filebeat-tomcat-accesslog" {
    elasticsearch {
      hosts => ["192.168.40.180:9200","192.168.40.181:9200"]
      index => "filebeat-tomcat-accesslog-%{+YYYY.MM.dd}"
    }}

}

[root@xianchaomaster1 conf.d]# mv 5.logstash-filebeat-process-kafka-to-es.conf filebeat-kafka-to-es.conf
[root@xianchaomaster1 conf.d]# ll
total 12
-rw-r--r-- 1 root root 583 Apr 10 08:28 filebeat-kafka-to-es.conf
-rw-r--r-- 1 root root 691 Apr  6 14:51 logstash.conf
-rw-r--r-- 1 root root 698 Apr  7 07:13 logstash-sidercar-kafka-to-es.conf

[root@xianchaomaster1 conf.d]# systemctl restart logstash
:
[root@xianchaomaster1 conf.d]# tail -f /var/log/logstash/logstash-plain.log
[2023-04-10T08:28:58,974][INFO ][org.apache.kafka.clients.consumer.internals.AbstractCoordinator][main][6a1cc6f2a02041c7b02f94f3dfeeb2e5a7097ab2be13527f82509a94ed253a9a] [Consumer clientId=logstash-0, groupId=logstash] Successfully joined group with generation 27
[2023-04-10T08:28:58,974][INFO ][org.apache.kafka.clients.consumer.internals.AbstractCoordinator][main][3750b51f32ab834abd0911620c4d250b09235297cfb03edee277f1c8165f046c] [Consumer clientId=logstash-0, groupId=logstash] Successfully joined group with generation 27
[2023-04-10T08:28:58,975][INFO ][org.apache.kafka.clients.consumer.internals.AbstractCoordinator][main][12843b2191cc6b633a21838985e69fb534d868a01900b956952ad9ff5c3625c8] [Consumer clientId=logstash-0, groupId=logstash] Successfully joined group with generation 27
[2023-04-10T08:28:58,975][INFO ][org.apache.kafka.clients.consumer.internals.ConsumerCoordinator][main][6a1cc6f2a02041c7b02f94f3dfeeb2e5a7097ab2be13527f82509a94ed253a9a] [Consumer clientId=logstash-0, groupId=logstash] Adding newly assigned partitions: jsonfile-log-topic-0
[2023-04-10T08:28:58,975][INFO ][org.apache.kafka.clients.consumer.internals.ConsumerCoordinator][main][3750b51f32ab834abd0911620c4d250b09235297cfb03edee277f1c8165f046c] [Consumer clientId=logstash-0, groupId=logstash] Adding newly assigned partitions: tomcat-app1-topic-0
[2023-04-10T08:28:58,976][INFO ][org.apache.kafka.clients.consumer.internals.ConsumerCoordinator][main][3750b51f32ab834abd0911620c4d250b09235297cfb03edee277f1c8165f046c] [Consumer clientId=logstash-0, groupId=logstash] Setting offset for partition tomcat-app1-topic-0 to the committed offset FetchPosition{offset=47634526, offsetEpoch=Optional[20], currentLeader=LeaderAndEpoch{leader=Optional[192.168.40.181:9092 (id: 102 rack: null)], epoch=20}}
#重启日志 
[2023-04-10T08:28:58,977][INFO ][org.apache.kafka.clients.consumer.internals.ConsumerCoordinator][main][12843b2191cc6b633a21838985e69fb534d868a01900b956952ad9ff5c3625c8] [Consumer clientId=logstash-0, groupId=logstash] Adding newly assigned partitions: filebeat-magedu-app1-0
[2023-04-10T08:28:58,978][INFO ][org.apache.kafka.clients.consumer.internals.ConsumerCoordinator][main][12843b2191cc6b633a21838985e69fb534d868a01900b956952ad9ff5c3625c8] [Consumer clientId=logstash-0, groupId=logstash] Found no committed offset for partition filebeat-magedu-app1-0
[2023-04-10T08:28:58,978][INFO ][org.apache.kafka.clients.consumer.internals.ConsumerCoordinator][main][6a1cc6f2a02041c7b02f94f3dfeeb2e5a7097ab2be13527f82509a94ed253a9a] [Consumer clientId=logstash-0, groupId=logstash] Setting offset for partition jsonfile-log-topic-0 to the committed offset FetchPosition{offset=2184170, offsetEpoch=Optional[29], currentLeader=LeaderAndEpoch{leader=Optional[192.168.40.182:9092 (id: 103 rack: null)], epoch=35}}
[2023-04-10T08:28:58,985][INFO ][org.apache.kafka.clients.consumer.internals.SubscriptionState][main][12843b2191cc6b633a21838985e69fb534d868a01900b956952ad9ff5c3625c8] [Consumer clientId=logstash-0, groupId=logstash] Resetting offset for partition filebeat-magedu-app1-0 to offset 0.

六、测试日志写入情况 Kibana中添加索引

#进入容器查看进程是否开启 tomcat和filebeat
kubectl exec -it magedu-tomcat-app1-filebeat-deployment-5f9979485f-p6pr2 bash
[root@magedu-tomcat-app1-filebeat-deployment-75fc8c9f68-4cp2p /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 23:12 ?        00:00:00 /bin/bash /apps/tomcat/bin/run_tomcat.sh
root          7      1  1 23:12 ?        00:00:00 /usr/share/filebeat/bin/filebeat -e -c /etc/filebeat/filebeat.yml -path.home /usr/share/filebeat -path.config /etc/filebeat
tomcat       36      1  7 23:12 ?        00:00:01 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/apps/tomcat/conf/logging.properties -Djava.util.logging.manager=or
root         37      1  0 23:12 ?        00:00:00 tail -f /etc/hosts
root         85      0  0 23:12 pts/0    00:00:00 bash
root        101     85  0 23:12 pts/0    00:00:00 ps -ef

[root@magedu-tomcat-app1-filebeat-deployment-5f9979485f-p6pr2 /]# cd /apps/tomcat/logs/
[root@magedu-tomcat-app1-filebeat-deployment-5f9979485f-p6pr2 logs]# echo "Winnie is here in father legs" >> catalina.out
[root@magedu-tomcat-app1-filebeat-deployment-5f9979485f-p6pr2 logs]# echo "Winnie is here in father legs access log" >> localhost_access_log.20230410.txt
[root@magedu-tomcat-app1-filebeat-deployment-5f9979485f-p6pr2 logs]# echo "Winnie is here in father legs 1 catalina.out " >> catalina.out

filebeat-tomcat-accesslog* 测试

 

filebeat-tomcat-catalina*测试

posted @ 2023-04-09 20:28  しみずよしだ  阅读(42)  评论(0)    收藏  举报