K8s 中如需管理存储卷(Persistent Volumes)中的内容,使用 FTP 是个不错的选择。推荐使用 atmoz/sftp
搭建 FTP,使用 SFTP 协议 22 端口。
版本
atmoz/sftp 有两个版本,atmoz/sftp:debian
版本体积稍大,为 57.83 MB,OpenSSH 版本较老; atmoz/sftp:alpine
版本体积为前者的 1/10,为 5.9 MB,OpenSSH 版本较新。
Debain 的更新周期为 2 年,发布后只进行 bug 修复,通常被认为更稳定。Alpine 更新频率更高(更新周期大约为 6 个月),倾向于使用更新的软件。
配置
配置环境变量 SFTP_USERS
为 foo:pass:::upload
。参数含义分别为 用户名:密码:UID(可以不设置):GID(可以不设置):允许访问的目录(不设置则为用户根目录)
,FTP 的根目录为 /home/foo/upload
。
默认不允许用 root 登录,如果需管理的文件只有 root 用户才有权限,会出现权限不足问题。
另外,将需要管理的存储卷挂载到容器 FTP 可管理的目录下。
获取 root 权限
可以将 UID 和 GID 设置为 0,即代表 root 用户。如:ujcms:pass:0:0
。
但由于默认不允许拥有 root 权限的用户登录,此时登录会被拒绝。需修改配置 PermitRootLogin yes
,允许 root 登录。将以下文件通过 ConfigMap 挂载到容器的 /etc/ssh/sshd_config
:
# Secure defaults
# See: https://stribika.github.io/2015/01/04/secure-secure-shell.html
Protocol 2
HostKey /etc/ssh/ssh_host_ed25519_key
HostKey /etc/ssh/ssh_host_rsa_key
# Faster connection
# See: https://github.com/atmoz/sftp/issues/11
UseDNS no
# Limited access
PermitRootLogin yes
X11Forwarding no
AllowTcpForwarding no
# Force sftp and chroot jail
Subsystem sftp internal-sftp
ForceCommand internal-sftp
ChrootDirectory %h
# Enable this for more logs
#LogLevel VERBOSE