gpt4 book ai didi

spring-boot - Kubernetes 部署 - 外部化日志文件

转载 作者:行者123 更新时间:2023-12-01 01:43:31 24 4
gpt4 key购买 nike

我有一个带有以下 docker 文件的 Spring Boot 应用程序。

FROM docker.com/base/jdk1.8:latest

MAINTAINER Application Engineering [ https://docker.com/ ]

RUN mkdir -p /opt/docker/svc

COPY application/weather-service.war /opt/docker/svc/

CMD java -jar /opt/docker/svc/weather-service.war --spring.config.location=file:/conf/application.properties -Dlogging.config=/conf/logback.xml

我可以将 kubernetes configMap 或 secrets 用于 application.properties,并使用如下所示的卷挂载选项。
"spec": {
"volumes": [
{
"name": "svc-prop",
"configMap": {
"name": "svc-app-config",
"items": [
{
"key": "application.properties",
"path": "application.properties"
}
]
}
}
],
"containers": [
"volumeMounts": [
{
"name": "svc-prop",
"mountPath": "/conf"
}
]

我如何才能为 logback.xml 实现相同的目标。在这种情况下,我是否需要将 secret 用作文件?

我不想将 logback.xml 文件与图像捆绑在一起,因为我们可能会在运行时更改日志级别。

还有其他更好的方法可以在 Kubernetes 中为 Spring Boot 应用程序保留 logback.xml 吗?

最佳答案

通常你不想提供整个logback.xml文件,而是 logger最需要在运行时更新的列表。
为了实现这一点,您可以使用 Logback's file inclusion特征:

  • 写下您的 logback.xml文件照常,除了 logger列表。使用 include元素代替:

  •     <configuration scan="true" scanPeriod="10 seconds" debug="true">
    <appender ...></appender>
    <root level="INFO">...</root>

    <!-- Import loggers configuration from external file -->
    <include file="config/mount/loggers-include.xml"/>
    </configuration>

    注意那些 scan*属性。它们对于在运行时重新加载日志配置至关重要。
  • 使用 loggers-include.xml 定义 Kubernetes ConfigMap 中的所有记录器数据部分:
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: microservice-loggers # the name to refer to from deployment (see below)
    namespace: upc
    data:
    loggers-include.xml: |+
    <included>
    <logger name="org.springframework.cloud.netflix.zuul" level="INFO"/>
    <logger name="com.netflix.zuul" level="INFO"/>
    <logger name="com.netflix.hystrix" level="INFO"/>
    <logger name="com.netflix.ribbon" level="DEBUG"/>
    <logger name="com.netflix.loadbalancer" level="INFO"/>
    </included>

    请注意,所有包含的内容必须包含在 included 中。标签以便 Logback 正确解析。
  • 将您的 ConfigMap 数据挂载到容器中作为 config/mount/loggers-include.xml文件:
    apiVersion: apps/v1
    kind: Deployment
    ...
    spec:
    ...
    template:
    ...
    spec:
    # declare the volume created from ConfigMap
    volumes:
    - name: config-volume # used with this name below
    configMap:
    name: microservice-loggers # declared in previous step
    containers:
    - name: microservice
    ...
    ports:
    ...
    # mount the volume declared above to container's file system
    volumeMounts:
    - mountPath: /microservice/config/mount
    name: config-volume # declared above

    请注意 mount目录不得由容器本身或其图像创建。此外,如果存在这样的目录,则其所有内容将被移除 在安装过程中。
  • Apply the ConfigMaprun the declared deployment .要检查 loggers 文件是否正确挂载,请执行以下命令:
    $ kubectl exec restorun-7d757b7c6-wcslx -- ls -l /microservice/config/mount
    total 0
    lrwxrwxrwx 1 root root 26 Aug 14 05:52 loggers-include.xml -> ..data/loggers-include.xml

    此外,如果您已设置 debug=true Logback 中的属性 configuration元素(参见步骤 1),那么您应该在应用程序启动期间在 STDOUT 中看到以下记录:
    05:52:17,031 |-INFO in ch.qos.logback.core.joran.util.ConfigurationWatchListUtil@6e06451e - Adding [file:/microservice/config/mount/loggers-include.xml] to configuration watch list.
  • 现在您可以编辑您的 ConfigMap(例如将 com.netflix.hystrix 设置为级别 WARN),保存其文件并告诉 Kubernetes 将更改应用于应用程序:
    $ kubectl apply -f microservice-log-configmap.yaml
    configmap "microservice-loggers" configured

    同样,Logback 应该通过将以下消息记录到标准输出来反射(reflect)更改:
        05:59:16,974 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.netflix.hystrix] to WARN

    您还可以通过直接从 Spring Boot Actuator 询问来检查有效的日志级别(如果您有权访问此端点):
    $ curl http://<k8s-external-ip>/actuator/loggers/com.netflix.hystrix
    {
    "configuredLevel" : "WARN",
    "effectiveLevel" : "WARN"
    }

  • 如果级别保持不变,请等待一分钟并再次检查:更改通过 Kubernetes 和 Logback 传播需要一些时间。有关此主题的更多信息:
  • Logback auto reloading
  • Kubernetes ConfigMap mounting
    (请参阅已安装的 ConfigMap 自动更新部分)
  • 关于spring-boot - Kubernetes 部署 - 外部化日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49566123/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com