Compartir tecnología

Colección de registros ELK: recopile registros de aplicaciones que se ejecutan en modo pod en el clúster k8s

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina


Prefacio

Referencia de construcción e interpretación de Filebeat+ELK
Enlace:Aprendizaje de k8s: proceso detallado de recopilación de registros ELK basado en k8s
Este capítulo no repetirá la descripción.

ambiente

máquina virtual

IPnombre de la CPUUPCMemoriadisco duro
192.168.10.11maestro012 CPU de doble núcleo4G100 GRAMOS
192.168.10.12trabajador012 CPU de doble núcleo4G100 GRAMOS
192.168.10.13trabajador022 CPU de doble núcleo4G100 GRAMOS
192.168.10.17ALCE1 CPU de doble núcleo4G100 GRAMOS

Versión centos7.9
K8s-1.27 implementado
El servidor ELK ha implementado Filebeat+ELK

Se implementa ejecutando filebeat (sidecar) en la aplicación Pod. Esta vez, utilizaremos Tomcat como ejemplo para ilustrar.

1. Prepare el directorio de datos de Tomcat

De forma predeterminada, no hay ningún archivo de página de inicio del sitio web en el contenedor Tomcat. Si no lo agrega, el contenedor en el pod no se ejecutará normalmente.

operación del host work01

mkdir /opt/tomcatwebroot
echo "tomcat is running" > /opt/tomcatwebroot/index.html
  • 1
  • 2

2. Escriba el archivo de lista de recursos de la aplicación Tomcat

operación del host maestro

vim tomcat-logs.yaml
  • 1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-demo
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      project: www
      app: tomcat-demo
  template:
    metadata:
      labels:
        project: www
        app: tomcat-demo
    spec:
      nodeName: worker01
      containers:
      - name: tomcat
        image: tomcat:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        resources:
          requests:
            cpu: 0.5
            memory: 500Mi
          limits:
            cpu: 1
            memory: 1Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        volumeMounts:
        - name: tomcat-logs
          mountPath: /usr/local/tomcat/logs
        - name: tomcatwebroot
          mountPath: /usr/local/tomcat/webapps/ROOT

      - name: filebeat
        image: docker.io/elastic/filebeat:7.17.2
        imagePullPolicy: IfNotPresent
        args: [
          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        resources:
          limits:
            memory: 500Mi
          requests:
            cpu: 100m
            memory: 100Mi
        securityContext:
          runAsUser: 0
        volumeMounts:
        - name: filebeat-config
          mountPath: /etc/filebeat.yml
          subPath: filebeat.yml
        - name: tomcat-logs
          mountPath: /usr/local/tomcat/logs
      volumes:
      - name: tomcat-logs
        emptyDir: {}
      - name: tomcatwebroot
        hostPath:
          path: /opt/tomcatwebroot
          type: Directory
      - name: filebeat-config
        configMap:
          name: filebeat-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-config
  namespace: default

data:
  filebeat.yml: |-
    filebeat.inputs:
    - type: log
      paths:
        - /usr/local/tomcat/logs/catalina.*

      fields:
        app: www
        type: tomcat-catalina
      fields_under_root: true
      multiline:
        pattern: '^['
        negate: true
        match: after

    setup.ilm.enabled: false
    setup.template.name: "tomcat-catalina"
    setup.template.pattern: "tomcat-catalina-*"

    output.logstash:
      hosts: ['192.168.10.17:5056']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

Este yaml define archivos de configuración de Tomcat y Filebeat Deployment y Filebeat.
Sección de implementación

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-demo
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      project: www
      app: tomcat-demo
  template:
    metadata:
      labels:
        project: www
        app: tomcat-demo
    spec:
      nodeName: worker01
      containers:
      - name: tomcat
        image: tomcat:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        resources:
          requests:
            cpu: 0.5
            memory: 500Mi
          limits:
            cpu: 1
            memory: 1Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        volumeMounts:
        - name: tomcat-logs
          mountPath: /usr/local/tomcat/logs
        - name: tomcatwebroot
          mountPath: /usr/local/tomcat/webapps/ROOT

      - name: filebeat
        image: docker.io/elastic/filebeat:7.17.2
        imagePullPolicy: IfNotPresent
        args: [
          "-c", "/etc/filebeat.yml",
          "-e",
        ]
        resources:
          limits:
            memory: 500Mi
          requests:
            cpu: 100m
            memory: 100Mi
        securityContext:
          runAsUser: 0
        volumeMounts:
        - name: filebeat-config
          mountPath: /etc/filebeat.yml
          subPath: filebeat.yml
        - name: tomcat-logs
          mountPath: /usr/local/tomcat/logs
      volumes:
      - name: tomcat-logs
        emptyDir: {}
      - name: tomcatwebroot
        hostPath:
          path: /opt/tomcatwebroot
          type: Directory
      - name: filebeat-config
        configMap:
          name: filebeat-config
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

metadatos: define el nombre y el espacio de nombres del Deployment.
spec: Contiene la especificación detallada del Despliegue.
réplicas: especifica el número de réplicas, es decir, ejecutando dos instancias de Tomcat.
selector: define un selector para hacer coincidir las etiquetas de Pod.
plantilla: describe la plantilla del Pod, incluidos los metadatos y las especificaciones.
nodeName: especifica el nombre del nodo (trabajador01) donde se ejecuta el Pod.
contenedores: Se definen dos contenedores: Tomcat y Filebeat.

Contenedor Tomcat:
imagen: utilice tomcat: última imagen.
Puertos: exponer el puerto 8080.
recursos: Define las solicitudes y límites de recursos.
livenessProbe y readinessProbe: se utilizan para controles de estado.
volumeMounts: Se montan dos volúmenes.

Contenedor de archivos Beat:
imagen: Utilice filebeat:7.17.2 imagen.
args: especifica los parámetros de inicio.
recursos: Define las solicitudes y límites de recursos.
securityContext: ejecutar como usuario root.
volumeMounts: Se montan dos volúmenes.

volúmenes:
tomcat-logs: utilice volúmenes vacíos de Dir.
tomcatwebroot: utilice el volumen hostPath.
filebeat-config: utiliza volúmenes de ConfigMap.


Sección del archivo de configuración (ConfigMap)

apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-config
  • 1
  • 2
  • 3
  • 4

explicar
metadatos: define el nombre de ConfigMap.
datos: debe contener el contenido de configuración de Filebeat (omitido aquí).

3. Escriba el archivo de configuración logstash

configuración del host de alces
Escriba archivos de configuración de logstash sin afectar los archivos de configuración anteriores

vim /etc/logstash/conf.d/tomcat-logstash-to-elastic.conf
  • 1
input {
  beats {
    host => "0.0.0.0"
    port => "5056"
  }
}

filter {

}


output {
    elasticsearch {
      hosts => "192.168.10.17:9200"
      index => "tomcat-catalina-%{+yyyy.MM.dd}"
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

correr

/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tomcat-logstash-to-elastic.conf --path.data /usr/share/logstash/data3 &
  • 1

Verifique que el puerto esté activo

ss -anput | grep ":5056"
  • 1

Insertar descripción de la imagen aquí

4. Aplicar el archivo de lista de recursos de Tomcat

operación del host maestro

kubectl apply -f tomcat-logs.yaml
  • 1

Espera un momento porque necesitas descargar la imagen.
Luego revisa la vaina
Nota: se requiere VPN

kubectl get deployment.apps
kubectl get pods
  • 1
  • 2

Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí

5. Verifique si tomcat y filebeat en el Pod son normales

Ver registros generados por Tomcat (-c: contenedor)
Insertar descripción de la imagen aquí
Ver registros de recopilación de filebeat

 kubectl logs tomcat-demo-664584f857-k8whd -c filebeat
  • 1

Insertar descripción de la imagen aquí

6. Agregar índice a la página de kiana

Acceso al navegador del host

192.168.10.17:5601
  • 1

Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Insertar descripción de la imagen aquí
Puedes ver que has visto el registro.
Insertar descripción de la imagen aquí
Finalizar
Si te ayuda haz clic y sigue