【问题标题】:Custom docker container in Kubernetes cluster with log using StackdriverKubernetes 集群中的自定义 docker 容器,使用 Stackdriver 记录日志
【发布时间】:2018-05-09 07:34:35
【问题描述】:

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

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

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

有什么建议吗?

【问题讨论】:

    标签: apache docker logging kubernetes google-cloud-stackdriver


    【解决方案1】:

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

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

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

    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 -> 日志记录中,您将能够在 Stackdriver 中使用以下过滤器查看 apache 访问/错误日志:

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

    【讨论】:

    • 警告:我们应该在 GCP Console 下查看上述方法的应用程序日志 > “Logging” > “Logs Viewer” > “GKE Con​​tainer” 不在 GCP Console > “Logging” > “Logs Viewer” > “ Kubernetes 集群”我浪费了一天时间才知道这一点 :(
    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 2019-03-24
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2017-12-10
    相关资源
    最近更新 更多