简体   繁体   English

Kubernetes 中的 Sidecar 容器日志文件路径问题

[英]Issue with Sidecar container log file path in Kubernetes

I have a deployment with 2 containers running in the same Pod in my Kubernetes cluster.我在 Kubernetes 集群的同一个 Pod 中部署了 2 个容器。 One is NGINX and the other is a sidecar (bash image).一个是 NGINX,另一个是边车(bash 图像)。 Following is the definition file:-以下是定义文件:-

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      volumes:
      - name: logs
        emptyDir: {}
      containers:
      - image: nginx
        name: nginx
        resources: {}
        volumeMounts:
          - name: logs
            mountPath: /var/log/nginx
      - image: bash
        name: sidecar
        command: ["/bin/sh","-c","tail -f /var/log/sidecar/access.log"]
        volumeMounts:
        - name: logs
          mountPath: /var/log/sidecar

I understand that the Docker image (NGINX) generates the standard logs to the location /var/log/nginx/access.log (ref-> https://docs.docker.com/config/containers/logging/ )我了解 Docker 图像 (NGINX) 生成标准日志到位置/var/log/nginx/access.log (ref-> https://docs.Z05B6053C41A2130AFD6FC3B158BDA4E )./comconfig6

I'm using emptyDir volume which means all the containers will share the same storage location within the pod on the host machine.我正在使用 emptyDir 卷,这意味着所有容器将在主机上的 pod 内共享相同的存储位置。

When I do curl on the service or Nginx pod, I get successful response from the NGINX server, and its logs can be seen using the kubectl logs <pod name> sidecar -f command on the console.当我在服务或 Nginx pod 上执行 curl 时,我得到了来自 NGINX 服务器的成功响应,并且可以使用控制台上的kubectl logs <pod name> sidecar -f其日志。

What I do not understand completely is that the file path in the command (inside bash/sidecar container) is /var/log/sidecar/access.log and not /var/log/nginx/access.log then how come I'm able to get those logs because the file access.log does not exists inside the directory /var/log/sidecar.我完全不明白的是命令中的文件路径(在 bash/sidecar 容器内)是/var/log/sidecar/access.log而不是 /var/log/nginx/access.log 那我怎么来能够获取这些日志,因为目录 /var/log/sidecar 中不存在文件 access.log。

Please advise, thanks请指教,谢谢

Before getting into your question, you will need to know how Kubernetes Pod works.在回答您的问题之前,您需要了解 Kubernetes Pod 的工作原理。

In Kubernetes, All the containers in the same Pod are scheduled in the same node.在 Kubernetes 中,同一个 Pod 中的所有容器都调度在同一个节点上。 Inside the node, they are orchestrated with the container runtime.在节点内部,它们与容器运行时一起编排。

Taking the docker runtime for simplicity, Kubernetes will spawn up two containers according to your configuration.为简单起见,使用 docker 运行时,Kubernetes 将根据您的配置生成两个容器。 One is the NGINX and one is the bash.一种是 NGINX,一种是 bash。 In container orchestration, you can define a volume and share them across different container runtime.在容器编排中,您可以定义一个卷并在不同的容器运行时共享它们。 But behind the scene, they are using the same folders/files from the host machine.但在幕后,他们使用来自主机的相同文件夹/文件。

So, circling back to your question, Kubernetes here created two containers in your hosting machine.因此,回到您的问题,Kubernetes 在您的主机中创建了两个容器。 And it also created a docker volume to share across these two containers.它还创建了一个 docker 卷以在这两个容器之间共享。 Inside the containers, the volume are mounted into different path but still share the same source of truth, which comes from the worker machine.在容器内部,卷被挂载到不同的路径,但仍然共享相同的事实来源,来自工作机器。

In case you want to know how it is going on your worker machine, you can ssh into your worker and run the following command.如果您想知道它在您的工作机器上的运行情况,您可以 ssh 进入您的工作机器并运行以下命令。

# Get your container ID
> docker ps
# Inspect your container
> docker inspect <container>

And try to spot the volume part in the output.并尝试找出 output 中的音量部分。 You should be able to see that they are sharing the volume.您应该能够看到他们正在共享该卷。

Maybe you are doing something wrong, when testing this.在测试这个时,也许你做错了什么。 I have installed your K8s manifest to my K8s cluster and found everything working as expected:我已将您的 K8s 清单安装到我的 K8s 集群中,并发现一切正常:

  1. You have a Pod with two containers (nginx and sidecar) that share a single volume.您有一个 Pod,其中有两个容器(nginx 和 sidecar),它们共享一个卷。

  2. The nginx container mounts the volume to /var/log/nginx and writes a couple of files to it. nginx 容器将卷挂载到/var/log/nginx并向其中写入几个文件。

  3. The sidecar containers mount the same volume to /var/log/sidecar and thus gains access to the logs written by nginx under /var/log/sidecar . sidecar 容器将相同的卷挂载到/var/log/sidecar ,从而可以访问/var/log/sidecar下的 nginx 写入的日志。

  4. The sidecar container does not have direct access to /var/log/nginx . sidecar 容器无法直接访问/var/log/nginx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM