簡體   English   中英

Kubernetes emptyDir 和符號鏈接

[英]Kubernetes emptyDir and symlinks

語境

我有一個帶有兩個容器的吊艙:

  • main的簡單工作是顯示目錄的內容
  • sidecar負責將 blob 存儲的內容同步到預定義的目錄中

為了使同步具有原子性, sidecar將 blob 存儲內容下載到新的臨時目錄中,然后在目標目錄中切換符號鏈接。

使用emptyDir卷在兩個容器之間共享目標目錄。

問題

main有符號鏈接,但不能列出后面的內容。

如何訪問最新的同步數據?

附加信息

原因

我嘗試使用Git-Sync來實現Apache Airflow所做的事情,但我需要從 Azure Blob 存儲同步文件,而不是使用 Git。 這是必要的,因為 (1) 我的內容主要是動態的,並且 (2) azureFile卷類型有一些嚴重的性能問題

同步例程

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"

符號鏈接只是一種包含文件名的特殊文件; 它實際上並不以任何有意義的方式包含文件內容,也不必指向存在的文件。 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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM