在Kubernetes集群中的自定义docker容器与使用Stackdriver的日志

我想知道为了发送在我的自定义Apache容器中创build的日志(部署在Kubernetes的一个容器中)到Stackdriver收集器必须遵循哪些步骤。

我注意到,如果我使用标准的apache(或nginx)容器创build一个窗格,access.log和error.log会自动发送到Stackdriver。

实际上,我可以在Kubernetes仪表板和Google Cloud Dashboard上看到日志 – >日志logging – >日志,但是我没有看到任何与我的自定义Apache相关的东西。

有什么build议么?

让我们从Nginx容器的默认行为开始,它将日志发送到access.log和error.log。 这两个文件都有一个符号链接 (软链接),如下所示

access.log -> /dev/stdout error.log -> /dev/stderr 

因此,您可以访问Kubernetes Dashboard和Google Cloud Dashboard上的日志。

解决您的问题

正如你有自定义的Apache或Nginx服务器,我build议你检查access.log和error.log是否分别具有到/ dev / stdout,/ dev / stderr文件的符号链接。

如果没有符号链接,则需要为两个文件创build它才能访问日志。 这是执行它的命令。

 ln -s source_file myfile 

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

我不知道为什么“标准redirect”(使用/ dev / stdout或/ proc / self / fd / 1)无法正常工作,我跟踪的解决scheme称为“带有日志代理的sidecar容器”

1)创build一个configMag文件,你将设置一个stream畅的configuration:

 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)创build一个容器与2个容器:自定义apache +日志代理。 这两个容器将安装一个日志文件夹。 只有日志代理将挂载fluentdconfiguration:

 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容器中input:

 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云端控制台 – >日志logging中,您将能够看到Stackdriver中的apache访问/错误日志,并使用如下filter:

 resource.type="container" labels."compute.googleapis.com/resource_name"="my-sidecar"