#如何将日志归档到第三方存储
目前,平台生成的日志将存储在日志存储组件中;但是,这些日志的保留期限相对较短。对于合规要求较高的企业,日志通常需要更长的保留时间,以满足审计需求。此外,存储的经济性也是企业重点关注的问题之一。
基于上述场景,平台提供了日志归档解决方案,允许用户将日志传输到外部 NFS 或对象存储。
本文档介绍了将日志导出或归档到平台外部存储的解决方案。
如果希望 ClickHouse 使用 S3 作为其原生日志数据存储或冷数据存储,请参阅如何将 S3 存储与 ClickHouse 结合使用和安装。
下面的归档工作流会启动一个独立的 razor consumer --database-type=file Deployment,从平台的 Kafka topic 读取数据,并将数据写入 NFS 或 S3。示例清单基于 ACP 4.3 获取,仍然描述了预期模式,但其中有几处细节在 ACP 4.4 中已发生变化:
- 示例中的 Razor 镜像标签(
v3.16.0-beta.3.g3df8e987)已过时。在复制示例之前,请从存储集群上正在运行的deployment/razor中读取当前镜像标签。 - 示例使用了
serviceAccount: lanaya。在 ACP 4.4 的 ClickHouse 或 OpenSearch 集群中不存在lanayaServiceAccount;请使用随存储 operator 提供的当前归档 ServiceAccount,或使用pods/exec、secrets/get以及对归档hostPath的访问权限创建自己的 SA。 - 示例从 ACP 4.3 的 chart 布局中挂载了
kafka-basic-auth。在 ACP 4.4 中,Kafka 凭据位于platform-default-mq-conn中(请参阅安装中的 Kafka 连接 Secret 部分)。可以挂载platform-default-mq-conn,并调整 razor consumer 参数以从这些密钥读取 SASL;也可以将凭据复制到具有示例所需布局的 Secret 中。
如需经过 v4.4 验证的归档工作流,请联系支持团队。如下所述,该模式仍适用于 Elasticsearch(旧版)后端。
在 Alauda OS 节点上,日志导出 Deployment 挂载的主机目录必须位于 /var/cpaas 下,例如 /var/cpaas/data/logarchive。这些节点是不可变的,传统操作系统的布局 /cpaas 在此处不可写。请仅修改下面示例中的 spec.template.spec.volumes[].hostPath.path;mountPath 值以及 log-exporter-config ConfigMap 中的 output.path 值是容器内的路径,应保持不变。
#传输到外部 NFS
#前提条件
| 资源 | 描述 |
|---|---|
| NFS | 提前设置 NFS 服务,并确定要挂载的 NFS 路径。 |
| Kafka | 提前获取 Kafka 服务地址。 |
| 镜像地址 | 必须使用 global 集群中的 CLI 工具执行以下命令,以获取镜像地址:- 获取 alpine 镜像地址: kubectl get daemonset nevermore -n cpaas-system -o jsonpath='{.spec.template.spec.initContainers[0].image}' - 获取 razor 镜像地址: kubectl get deployment razor -n cpaas-system -o jsonpath='{.spec.template.spec.containers[0].image}' |
#创建日志同步资源
-
在左侧导航栏中,点击集群管理 > 集群。
-
点击要传输日志的集群右侧的操作按钮 > CLI 工具。
-
根据以下参数说明修改 YAML;修改完成后,将代码粘贴到打开的 CLI 工具命令行中,然后按 Enter 键执行。
资源类型 字段路径 描述 ConfigMap data.export.yml.output.compression压缩日志文本;支持的选项包括 none(不压缩)、zlib、gzip。 ConfigMap data.export.yml.output.file_type导出日志文件的类型;支持 txt、csv、json。 ConfigMap data.export.yml.output.max_size单个归档文件的大小,单位为 MB。如果超过此值,系统将根据 compression 字段的配置自动压缩并归档日志。 ConfigMap data.export.yml.scopes日志传输范围;当前支持的日志包括:系统日志、原生应用日志、Kubernetes 日志、产品日志。 Deployment spec.template.spec.containers[0].command[7]Kafka 服务地址。 Deployment spec.template.spec.volumes[3].hostPath.path要挂载的 NFS 路径。 Deployment spec.template.spec.initContainers[0].imageAlpine 镜像地址。 Deployment spec.template.spec.containers[0].imageRazor 镜像地址。 cat << "EOF" |kubectl apply -f - apiVersion: v1 data: export.yml: | scopes: # The scope of log transfer; by default, only application logs are collected system: false # System logs workload: true # Application logs kubernetes: false # Kubernetes logs platform: false # Product logs output: type: local path: /cpaas/data/logarchive layout: TimePrefixed # Size of a single archived file; unit is MB. If it exceeds this value, logs will be automatically compressed and archived based on the compression field's configuration. max_size: 200 compression: zlib # Optional: none (no compression) / zlib / gzip file_type: txt # Optional: txt csv json kind: ConfigMap metadata: name: log-exporter-config namespace: cpaas-system --- apiVersion: apps/v1 kind: Deployment metadata: labels: service_name: log-exporter name: log-exporter namespace: cpaas-system spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 5 selector: matchLabels: service_name: log-exporter strategy: rollingUpdate: maxSurge: 0 maxUnavailable: 1 type: RollingUpdate template: metadata: creationTimestamp: null labels: app: lanaya cpaas.io/product: Platform-Center service_name: log-exporter version: v1 namespace: cpaas-system spec: automountServiceAccountToken: true affinity: podAffinity: {} podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - podAffinityTerm: labelSelector: matchExpressions: - key: service_name operator: In values: - log-exporter topologyKey: kubernetes.io/hostname weight: 50 initContainers: - args: - -ecx - | chown -R 697:697 /cpaas/data/logarchive command: - /bin/sh image: registry.example.cn:60080/ops/alpine:3.16 # Alpine image address imagePullPolicy: IfNotPresent name: chown resources: limits: cpu: 100m memory: 200Mi requests: cpu: 10m memory: 50Mi securityContext: runAsUser: 0 terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /cpaas/data/logarchive name: data containers: - command: - /razor - consumer - --v=1 - --kafka-group-log=log-nfs - --kafka-auth-enabled=true - --kafka-tls-enabled=true - --kafka-endpoint=192.168.143.120:9092 # Fill in based on actual environment - --database-type=file - --export-config=/etc/log-export/export.yml image: registry.example.cn:60080/ait/razor:v3.16.0-beta.3.g3df8e987 # Razor image imagePullPolicy: Always livenessProbe: failureThreshold: 5 httpGet: path: /metrics port: 8080 scheme: HTTP initialDelaySeconds: 20 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 3 name: log-export ports: - containerPort: 80 protocol: TCP readinessProbe: failureThreshold: 5 httpGet: path: /metrics port: 8080 scheme: HTTP initialDelaySeconds: 20 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 3 resources: limits: cpu: "2" memory: 4Gi requests: cpu: 440m memory: 1280Mi securityContext: runAsGroup: 697 runAsUser: 697 terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /etc/secrets/kafka name: kafka-basic-auth readOnly: true - mountPath: /etc/log-export name: config readOnly: true - mountPath: /cpaas/data/logarchive name: data dnsPolicy: ClusterFirst nodeSelector: kubernetes.io/os: linux restartPolicy: Always schedulerName: default-scheduler securityContext: fsGroup: 697 serviceAccount: lanaya serviceAccountName: lanaya terminationGracePeriodSeconds: 10 tolerations: - effect: NoSchedule key: node-role.kubernetes.io/master operator: Exists - effect: NoSchedule key: node-role.kubernetes.io/control-plane operator: Exists - effect: NoSchedule key: node-role.kubernetes.io/cpaas-system operator: Exists volumes: - name: kafka-basic-auth secret: defaultMode: 420 secretName: kafka-basic-auth - name: elasticsearch-basic-auth secret: defaultMode: 420 secretName: elasticsearch-basic-auth - configMap: defaultMode: 420 name: log-exporter-config name: config - hostPath: path: /cpaas/data/logarchive # NFS path to be mounted type: DirectoryOrCreate name: data EOF -
容器状态变为 Running 后,即可在 NFS 路径中查看持续归档的日志;日志文件目录结构如下:
/cpaas/data/logarchive/$date/$project/$namespace-$cluster/logfile
#传输到外部 S3 存储
#前提条件
| 资源 | 描述 |
|---|---|
| S3 存储 | 提前准备 S3 存储服务地址,并获取 access_key_id 和 secret_access_key 的值;创建用于存储日志的 bucket。 |
| Kafka | 提前获取 Kafka 服务地址。 |
| 镜像地址 | 必须使用 global 集群中的 CLI 工具执行以下命令,以获取镜像地址:- 获取 alpine 镜像地址: kubectl get daemonset nevermore -n cpaas-system -o jsonpath='{.spec.template.spec.initContainers[0].image}' - 获取 razor 镜像地址: kubectl get deployment razor -n cpaas-system -o jsonpath='{.spec.template.spec.containers[0].image}' |
#创建日志同步资源
-
在左侧导航栏中,点击 集群管理 > 集群。
-
点击要传输日志的集群右侧的操作按钮 > CLI 工具。
-
根据以下参数说明修改 YAML;修改完成后,将代码粘贴到打开的 CLI 工具命令行中,然后按 Enter 键执行。
资源类型 字段路径 描述 Secret data.access_key_id对获取的 access_key_id 进行 Base64 编码。 Secret data.secret_access_key对获取的 secret_access_key 进行 Base64 编码。 ConfigMap data.export.yml.output.compression压缩日志文本;支持的选项包括 none(不压缩)、zlib、gzip。 ConfigMap data.export.yml.output.file_type导出日志文件的类型;支持 txt、csv、json。 ConfigMap data.export.yml.output.max_size单个归档文件的大小,单位为 MB。如果超过此值,系统将根据 compression 字段的配置自动压缩并归档日志。 ConfigMap data.export.yml.scopes日志传输范围;当前支持的日志包括:系统日志、原生应用日志、Kubernetes 日志、产品日志。 ConfigMap data.export.yml.output.s3.bucket_nameBucket 名称。 ConfigMap data.export.yml.output.s3.endpointS3 存储服务地址。 ConfigMap data.export.yml.output.s3.regionS3 存储服务的地域信息。 Deployment spec.template.spec.containers[0].command[7]Kafka 服务地址。 Deployment spec.template.spec.volumes[3].hostPath.path要挂载的本地路径,用于临时存储日志信息。日志同步到 S3 存储后将自动删除。 Deployment spec.template.spec.initContainers[0].imageAlpine 镜像地址。 Deployment spec.template.spec.containers[0].imageRazor 镜像地址。 cat << "EOF" |kubectl apply -f - apiVersion: v1 type: Opaque data: # Must include the following two keys access_key_id: bWluaW9hZG1pbg== # Base64 encode the obtained access_key_id secret_access_key: bWluaW9hZG1pbg== # Base64 encode the obtained secret_access_key kind: Secret metadata: name: log-export-s3-secret namespace: cpaas-system --- apiVersion: v1 data: export.yml: | scopes: # The scope of log transfer; by default, only application logs are collected system: false # System logs workload: true # Application logs kubernetes: false # Kubernetes logs platform: false # Product logs output: type: s3 path: /cpaas/data/logarchive s3: s3forcepathstyle: true bucket_name: baucket_name_s3 # Fill in the prepared bucket name endpoint: http://192.168.179.86:9000 # Fill in the prepared S3 storage service address region: "dummy" # Region information access_secret: log-export-s3-secret insecure: true layout: TimePrefixed # Size of a single archived file; unit is MB. If it exceeds this value, logs will be automatically compressed and archived based on the compression field's configuration. max_size: 200 compression: zlib # Optional: none (no compression) / zlib / gzip file_type: txt # Optional: txt, csv, json kind: ConfigMap metadata: name: log-exporter-config namespace: cpaas-system --- apiVersion: apps/v1 kind: Deployment metadata: labels: service_name: log-exporter name: log-exporter namespace: cpaas-system spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 5 selector: matchLabels: service_name: log-exporter strategy: rollingUpdate: maxSurge: 0 maxUnavailable: 1 type: RollingUpdate template: metadata: creationTimestamp: null labels: app: lanaya cpaas.io/product: Platform-Center service_name: log-exporter version: v1 namespace: cpaas-system spec: affinity: podAffinity: {} podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - podAffinityTerm: labelSelector: matchExpressions: - key: service_name operator: In values: - log-exporter topologyKey: kubernetes.io/hostname weight: 50 initContainers: - args: - -ecx - | chown -R 697:697 /cpaas/data/logarchive command: - /bin/sh image: registry.example.cn:60080/ops/alpine:3.16 # Alpine image address imagePullPolicy: IfNotPresent name: chown resources: limits: cpu: 100m memory: 200Mi requests: cpu: 10m memory: 50Mi securityContext: runAsUser: 0 terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /cpaas/data/logarchive name: data containers: - command: - /razor - consumer - --v=1 - --kafka-group-log=log-s3 - --kafka-auth-enabled=true - --kafka-tls-enabled=true - --kafka-endpoint=192.168.179.86:9092 # Fill in the Kafka service address based on actual environment - --database-type=file - --export-config=/etc/log-export/export.yml image: registry.example.cn:60080/ait/razor:v3.16.0-beta.3.g3df8e987 # Razor image imagePullPolicy: Always livenessProbe: failureThreshold: 5 httpGet: path: /metrics port: 8080 scheme: HTTP initialDelaySeconds: 20 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 3 name: log-export ports: - containerPort: 80 protocol: TCP readinessProbe: failureThreshold: 5 httpGet: path: /metrics port: 8080 scheme: HTTP initialDelaySeconds: 20 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 3 resources: limits: cpu: "2" memory: 4Gi requests: cpu: 440m memory: 1280Mi securityContext: runAsGroup: 697 runAsUser: 697 terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /etc/secrets/kafka name: kafka-basic-auth readOnly: true - mountPath: /etc/log-export name: config readOnly: true - mountPath: /cpaas/data/logarchive name: data dnsPolicy: ClusterFirst nodeSelector: kubernetes.io/os: linux restartPolicy: Always schedulerName: default-scheduler securityContext: fsGroup: 697 serviceAccount: lanaya serviceAccountName: lanaya terminationGracePeriodSeconds: 10 tolerations: - effect: NoSchedule key: node-role.kubernetes.io/master operator: Exists - effect: NoSchedule key: node-role.kubernetes.io/control-plane operator: Exists - effect: NoSchedule key: node-role.kubernetes.io/cpaas-system operator: Exists volumes: - name: kafka-basic-auth secret: defaultMode: 420 secretName: kafka-basic-auth - name: elasticsearch-basic-auth secret: defaultMode: 420 secretName: elasticsearch-basic-auth - configMap: defaultMode: 420 name: log-exporter-config name: config - hostPath: path: /cpaas/data/logarchive # Local temporary storage address for logs type: DirectoryOrCreate name: data EOF -
容器状态变为 Running 后,即可在 bucket 中查看持续归档的日志。