首页 > 解决方案 > Kubernetes emptyDir 卷中不同容器中同一文件的不完整尾部输出

问题描述

为了测试 emptyDir Volume 中的文件是否在容器之间同步,我使用tail观察了两个容器中的同一个文件,我偶然发现了以下行为:

吊舱定义:

apiVersion: v1
kind: Pod
metadata:
  name: fortune
spec:
  containers:
    - image: luksa/fortune
      name: html-generator
      volumeMounts:
        - name: html
          mountPath: /var/htdocs
    - image: nginx:alpine
      name: web-server
      volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
          readOnly: true
      ports:
        - containerPort: 80
          protocol: TCP
  volumes:
    - name: html
      emptyDir: {}

示例取自Marko Luksa的Kubernetes in Action一书。luksa/fortune图像只是将财富文本写入容器/var/htdocs/index.html内的文件。html-generator每 10 秒写入一个新文件,其中的内容是fortune.

在两个容器中拖尾相同的文件有时会输出容器的不完整响应web-server

部分html-generator容器输出:

kubectl exec -c html-generator -it fortune -- tail -f /var/htdocs/index.html

The very ink with which all history is written is merely fluid prejudice.
                -- Mark Twain

部分web-server容器输出

kubectl exec -c web-server -it fortune -- tail -f /usr/share/nginx/html/index.html

h all history is written is merely fluid prejudice.
                -- Mark Twain

问题:这是由

  1. 尾巴
  2. 节点磁盘IO速度慢
  3. Kubernetes 卷同步逻辑
  4. 别的东西?

PS.:我还注意到,在写入 index.html 时卷曲 Web 服务 pod 端口会导致 nginx 返回一个空的响应正文。

标签: kubernetestailkubernetes-podkubernetes-container

解决方案


容器输出不完整的问题是由nginx alpine在 pod 定义中使用引起的。当您将图像从 更改为 时nginx:alpinenginx由于这些图像中使用了不同的尾部二进制文件,问题就会消失。

Kubernetes 卷同步似乎不太可能导致问题 - 如emptyDir 文档中所写

默认情况下,emptyDir卷存储在支持节点的任何介质上——可能是磁盘、SSD 或网络存储,具体取决于您的环境。

emptyDir 创建的分区是短暂的,应用程序不能期望该分区有任何性能 SLA(例如磁盘 IOPS),因此“2. 节点磁盘的 IO 速度慢”也可能导致此类问题,但基于再现和更改图像(看起来解决问题)可能会被排除在外。


推荐阅读