将 Fluentd-config 文件复制到 kubernetes pod

Copying a Fluentd-config file to a kubernetes pod

我有一个 fluentd.conf 文件,我正尝试在我的 minikube 集群中使用(复制)它(在 docker 容器中工作正常)。 步骤如下:

  1. 我将使用以下命令构建(在我的 minikube docker-env 中)docker 图像 Dockerfile:
FROM fluent/fluentd:v1.11-1

# Use root account to use apk
USER root

# below RUN includes plugin as examples elasticsearch is not required
# you may customize including plugins as you wish
RUN apk add --no-cache --update --virtual .build-deps \
    sudo build-base ruby-dev \
    && sudo gem install fluent-plugin-elasticsearch \
    && sudo gem sources --clear-all \
    && apk del .build-deps \
    && rm -rf /tmp/* /var/tmp/* /usr/lib/ruby/gems/*/cache/*.gem

COPY conf/fluent.conf /fluentd/etc/
USER fluent

  1. 这会成功执行(因此复制有效)并生成可运行的映像,接下来的步骤是启动 pods。 部分部署文件
      spec:
        containers:
        - image: fluentd
          imagePullPolicy: ""
          name: fluentd
          ports:
          - containerPort: 24224
          - containerPort: 24224
            protocol: UDP
          resources: {}
          volumeMounts:
          - mountPath: /fluentd/etc
            name: fluentd-claim0
        restartPolicy: Always
        serviceAccountName: ""
        volumes:
        - name: fluentd-claim0
          persistentVolumeClaim:
            claimName: fluentd-claim0

一切正常,但 fluentd-pod returns 出现以下错误: No such file or directory @ rb_sysopen - /fluentd/etc/fluent.conf

有人知道我遗漏了什么或做错了什么吗?

提前致谢,

发生这种情况是因为您正在同一文件夹中装载卷。

在您的映像中,配置文件位于 /fluentd/etc/fluent.conf

在您的 pod 中,您将 fluentd-claim0 卷装入 /fluentd/etc/:

volumeMounts:
- mountPath: /fluentd/etc
  name: fluentd-claim0

就像在 linux 中的非空目录中挂载卷一样,挂载点目录中的所有文件都将被挂载本身隐藏。

为了“解决”这个问题,您可以使用 volumeMounts 条目的 subPath 选项,如 the documentation 中所述。例如:

volumeMounts:
- mountPath: /fluentd/etc/myfileordirectory
  name: fluentd-claim0
  subPath: myfileordirectory

这样只有 myfileordirectory 会挂载到 /fluentd/etc/ 中,其余文件仍然可见。

这种方法的局限性在于您需要提前知道卷中的文件列表。如果这不可能,唯一的选择是为您的配置文件或您的 mountPath 使用不同的目录。

我发现此设置中有两个问题。

首先,您的 image: fluentd 是 运行 Docker Hub fluentd image 而不是您的自定义图像。您需要提供一个名称,包括注册地址或您的 Docker Hub 用户名才能使用其他名称。 (你说你正在使用 minikube,所以你可以在 minikube 上下文中使用 docker build -t fluentd 来获取此图像;尽量避免使用可能与 Docker Hub 图像名称冲突的名称,即使对于本地构建也是如此。 )

其次,您的卷设置正在创建一个新的空永久卷,然后用该卷覆盖映像中的配置文件。没有任何东西复制到持久卷中,所以你在容器中得到一个空目录。 (这不同于 Docker 命名卷;即使在普通的 Docker 中,我也不建议依赖首次使用时复制卷的行为。)您可以删除所有提及卷的内容。

这为您留下了一个更简单的 pod 规范,特别是如果您还删除了保留为默认值的字段:

spec:
  containers:
    - image: my/fluentd # not plain "fluentd"
      name: fluentd
      ports:
      - containerPort: 24224
      - containerPort: 24224
        protocol: UDP
    restartPolicy: Always

另一种选择是在 ConfigMap. It wouldn't necessarily be in your image in this case, and if you use a tool like Helm 中提供配置文件进行部署,有机会在部署时设置配置。在这里,您将拥有大部分卷机械,但 volumes: 块将引用 configMap: 而不是 PVC。