经过一些研究,我已经从我的自定义 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"