【问题标题】:Kubernetes emptyDir and symlinksKubernetes emptyDir 和符号链接
【发布时间】:2021-10-27 09:50:54
【问题描述】:

上下文

我有一个带有两个容器的 pod:

  • main,其简单的工作就是显示目录的内容
  • sidecar 负责将 blob 存储的内容同步到预定义目录中

为了使同步是原子的,sidecar 将 blob 存储内容下载到新的临时目录中,然后在目标目录中切换符号链接。

目标目录使用emptyDir 卷在两个容器之间共享。

问题

main 有符号链接,但不能列出后面的内容。

问题

如何获取最新的同步数据?

附加信息

原因

我尝试使用Git-Sync 实现Apache Airflow 所做的工作,但是,我需要从 Azure Blob 存储同步文件,而不是使用 Git。这是必要的,因为 (1) 我的内容大部分是动态的,并且 (2) azureFile 卷类型有一些严重的 performance issues

同步例程

declare -r container='https://mystorageaccount.dfs.core.windows.net/mycontainer'
declare -r destination='/shared/container'

declare -r temp_dir="$(mktemp -d)"
azcopy copy --recursive "$container/*" "$temp_dir"

declare -r temp_file="$(mktemp)"
ln -sf "$temp_dir" "$temp_file"
mv -Tf "$temp_file" "$destination"

我们最终得到了什么:

$ ls /shared
container -> /tmp/tmp.doGz2U0QNy
$ ls /shared/container
file1.txt file2.txt

解决方案

我最初的尝试有两个错误:

  1. 卷中不存在符号链接目标
  2. 符号链接目标指向 sidecar 容器中的绝对路径,因此,从主容器的角度来看,该文件夹不存在

这是修改后的例程:

declare -r container='https://mystorageaccount.dfs.core.windows.net/mycontainer'
declare -r destination='/shared/container'
declare -r cache_dir="$(dirname $destination)"

declare -r temp_dir="$(mktemp -d -p $cache_dir)"
azcopy copy --recursive "$container/*" "$temp_dir"

ln -sf "$(basename $temp_dir)" "$cache_dir/symlink"
mv -Tf "$cache_dir/symlink" "$destination"

【问题讨论】:

  • 你能提供一个更完整的例子吗?符号链接实际上只是目录条目中的文件路径,仅此而已,因此如果实际内容不在卷内,则符号链接不一定指向它。

标签: linux kubernetes airflow symlink persistent-volumes


【解决方案1】:

符号链接只是一种包含文件名的特殊文件;它实际上并不以任何有意义的方式包含文件内容,也不必指向存在的文件。 mktemp(1) 默认在/tmp 中创建目录,这可能不在共享卷中。

想象一下,将一个实体文件夹放在实体文件柜中,在便利贴上写上the third drawer at the very front,然后开车到另一栋大楼,将便条交给同事。便利贴(符号链接)仍然存在,但在其他建筑物(容器文件系统)的上下文中,它命名的位置并不是特别有意义。

解决此问题的最简单方法是让mktemp 直接在目标卷中创建文件,然后创建相对路径符号链接。

# extract the volume location (you may already have this)
volume_dir=$(dirname "$destination")

# force the download location to be inside the volume
# (mktemp --tmpdir option)
temp_dir=$(mktemp -d --tmpdir "$volume_dir")

# actually do the download
azcopy copy --recursive "$container/*" "$temp_dir"

# set the symlink to a relative-path symlink, since the directory
# and the link are in the same place; avoids problems if the volume
# is mounted in different places in the two containers
ln -sf $(basename "$temp_dir") "$destination"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2018-12-15
    • 1970-01-01
    相关资源
    最近更新 更多