Mi informacion de contacto
Correo[email protected]
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
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.
máquina virtual
IP | nombre de la CPU | UPC | Memoria | disco duro |
---|---|---|---|---|
192.168.10.11 | maestro01 | 2 CPU de doble núcleo | 4G | 100 GRAMOS |
192.168.10.12 | trabajador01 | 2 CPU de doble núcleo | 4G | 100 GRAMOS |
192.168.10.13 | trabajador02 | 2 CPU de doble núcleo | 4G | 100 GRAMOS |
192.168.10.17 | ALCE | 1 CPU de doble núcleo | 4G | 100 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.
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
operación del host maestro
vim tomcat-logs.yaml
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']
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
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
explicar
metadatos: define el nombre de ConfigMap.
datos: debe contener el contenido de configuración de Filebeat (omitido aquí).
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
input {
beats {
host => "0.0.0.0"
port => "5056"
}
}
filter {
}
output {
elasticsearch {
hosts => "192.168.10.17:9200"
index => "tomcat-catalina-%{+yyyy.MM.dd}"
}
}
correr
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tomcat-logstash-to-elastic.conf --path.data /usr/share/logstash/data3 &
Verifique que el puerto esté activo
ss -anput | grep ":5056"
operación del host maestro
kubectl apply -f tomcat-logs.yaml
Espera un momento porque necesitas descargar la imagen.
Luego revisa la vaina
Nota: se requiere VPN
kubectl get deployment.apps
kubectl get pods
Ver registros generados por Tomcat (-c: contenedor)
Ver registros de recopilación de filebeat
kubectl logs tomcat-demo-664584f857-k8whd -c filebeat
Acceso al navegador del host
192.168.10.17:5601
Puedes ver que has visto el registro.
Finalizar
Si te ayuda haz clic y sigue