将文件复制到 Kubernetes Pod

Copy file to Kubernetes Pod

我正在尝试使用以下方式将二进制文件复制到 Kubernetes pod。如果文件是文本格式但不是二进制文件,则脚本有效。

from kubernetes import client, config

try:
    config.load_kube_config()
except TypeError:
    config.load_incluster_config()

api = client.CoreV1Api()

exec_command = ['tar', 'xvf', '-', '-C', '/']
resp = stream(
    api.connect_get_namespaced_pod_exec,
    'pod_name', 'namespace', command=exec_command,
    stderr=True, stdin=True, stdout=True, tty=False, _preload_content=False
)

source_file = './test_data/simulated/some.nc'
destination_path = '/tmp/some.nc'

with TemporaryFile() as tar_buffer:
    with tarfile.open(fileobj=tar_buffer, mode='w') as tar:
        tar.add(name=source_file, arcname=destination_path)
    tar_buffer.seek(0)
    commands = []
    commands.append(tar_buffer.read())

    while resp.is_open():
        resp.update(timeout=1)
        if resp.peek_stdout():
            print("STDOUT: %s" % resp.read_stdout())
        if resp.peek_stderr():
            print("STDERR: %s" % resp.read_stderr())
        if commands:
            c = commands.pop(0)
            resp.write_stdin(str(c))
            # works if source and destination files are txt format
            # and the above line is resp.write_stdin(c.decode())
        else:
            break
    resp.close()

在 STDERR 上,我得到以下日志 "tar: This does not look like a tar archive"

我找到了一个正在执行此操作的程序包。我的问题是复制二进制文件。原来这个包运行一个 Kubernetes 作业来完成复制。

https://github.com/nuvo/skbn