1

我想知道我必须遵循哪些步骤才能将在我的自定义 apache 容器(部署在 Kubernetes 的 pod 中)中创建的日志发送到 Stackdriver 收集器。

我注意到,如果我使用标准 apache(或 nginx)容器创建 pod,access.log 和 error.log 会自动发送到 Stackdriver。

事实上,我可以在 Kubernetes 仪表板和 Google Cloud Dashboard 上看到日志--->Logging--->Logs 相反,我没有看到任何与我的自定义 apache 相关的内容......

有什么建议么?

4

1 回答 1

1

经过一些研究,我已经从我的自定义 apache 容器中解决了日志转发器的问题。

我不知道为什么“标准重定向”(使用 /dev/stdout 或 /proc/self/fd/1)无论如何都不起作用我遵循的解决方案称为“带有日志代理的边车容器”

1)创建一个 configMag 文件,您将在其中设置流利的配置:

apiVersion: v1
data:
  fluentd.conf: |
    <source>
      type tail
      format none
      path /var/log/access.log
      pos_file /var/log/access.log.pos
      tag count.format1
    </source>

    <source>
      type tail
      format none
      path /var/log/error.log
      pos_file /var/log/error.log.pos
      tag count.format2
    </source>

    <match **>
      type google_cloud
    </match>
kind: ConfigMap
metadata:
  name: my-fluentd-config

2)创建一个带有 2 个容器的 pod:自定义 apache + 一个日志代理。两个容器都会挂载一个日志文件夹。只有日志代理会挂载 fluentd 配置:

apiVersion: v1
kind: Pod
metadata:
  name: my-sidecar
  labels:
    app: my-sidecar 
spec:
  volumes:
  - name: varlog
    emptyDir: {}
  - name: config-volume
    configMap:
      name: my-fluentd-config 

  containers:
  - name: my-apache
    image: <your_custom_image_repository>
    ports:
      - containerPort: 80
        name: http
        protocol: TCP 
    volumeMounts:
    - name: varlog
      mountPath: /var/log

  - name: log-agent
    image: gcr.io/google_containers/fluentd-gcp:1.30
    env:
    - name: FLUENTD_ARGS
      value: -c /etc/fluentd-config/fluentd.conf
    volumeMounts:
    - name: varlog
      mountPath: /var/log
    - name: config-volume
      mountPath: /etc/fluentd-config

3) 进入 my-apache 容器:

kubectl exec -it my-sidecar --container my-apache -- /bin/bash

并更改/检查 httpd.conf 是否使用以下文件:

ErrorLog /var/log/error.log

CustomLog /var/log/access.log common

(如果您更改某些内容,请记住重新启动 apache ..)

4) 现在在 Google Cloud Console -> Logging 中,您将能够在 Stackdriver 中使用如下过滤器查看 apache 访问/错误日志:

resource.type="container"
labels."compute.googleapis.com/resource_name"="my-sidecar"
于 2017-11-27T13:42:38.787 回答