创建于2025-03-20更新于 2025-03-22
开发容器 Tailscale 版
I. 准备工作
- Tailscale 账号: 确保你已经注册了 Tailscale 账号。
- Tailscale 授权密钥: 在 Tailscale 管理后台(https://login.tailscale.com/admin/settings/keys)创建一个预授权密钥。
- 勾选 "Reusable" (可重用)。
- 取消勾选 "Ephemeral" (短暂)!
- (可选) 添加一个描述,例如 "dev-container"。
- 复制生成的密钥。
- Kubernetes 集群: 确保你的 Kubernetes 集群已经部署并正常运行。
- kubectl 客户端: 确保你的
kubectl客户端已经配置好,可以访问你的 Kubernetes 集群。 - Docker: 确保你已经安装了 Docker。
- PersistentVolumeClaim 支持: 确保你的 Kubernetes 集群支持 PersistentVolumeClaim,并且已经配置了 StorageClass。
- SSH 客户端: 确保你的本地机器安装了 SSH 客户端。
II. 创建 SSH 密钥对
- 生成 SSH 密钥对(在本地机器上):
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa- 这将生成两个文件:
~/.ssh/id_rsa(私钥) 和~/.ssh/id_rsa.pub(公钥)。 - 重要: 保护好你的私钥!
III. 创建 Kubernetes Secret (SSH Key 和 Tailscale Auth Key)
- 创建 Kubernetes Secret (SSH Key):
kubectl create secret generic ssh-private-key \
--from-file=id_rsa=~/.ssh/id_rsa \
--from-file=id_rsa.pub=~/.ssh/id_rsa.pub- 这将创建一个名为
ssh-private-key的 Secret,其中包含私钥和公钥. - 重要: 确保你所在的命名空间是正确的,否则需要在命令中指定命名空间 (
-n <命名空间>).
- 创建 Kubernetes Secret (Tailscale Auth Key):
kubectl create secret generic tailscale-auth-key \
--from-literal=auth-key=YOUR_PREAUTH_KEY- 将
YOUR_PREAUTH_KEY替换为你从 Tailscale 管理后台复制的预授权密钥. - 重要: 确保你所在的命名空间是正确的,否则需要在命令中指定命名空间 (
-n <命名空间>).
IV. 创建开发容器镜像
- 创建 Dockerfile:
FROM ubuntu:latest
# 安装必要的工具
RUN apt-get update && apt-get install -y openssh-server curl iptables rsync
# 安装 Tailscale
RUN curl -fsSL https://tailscale.com/install.sh | sh && apt-get update && apt-get install -y --only-upgrade tailscale
# 创建开发用户
RUN useradd -m developer
RUN echo "developer:password" | chpasswd
RUN usermod -aG sudo developer
# 创建 tailscale 用户组
RUN groupadd tailscale
# 将 developer 用户添加到 tailscale 组
RUN usermod -a -G tailscale developer
# 创建 idea-tools 目录
RUN mkdir -p /opt/idea-tools
RUN chown -R developer:developer /opt/idea-tools
# 暴露 SSH 端口
EXPOSE 22
# 复制 SSH 配置文件
COPY sshd_config /etc/ssh/sshd_config
# 复制入口脚本
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# 设置工作目录 (可选)
WORKDIR /home/developer
# 启动 SSH 服务和 Tailscale
CMD ["/entrypoint.sh"]- 创建
sshd_config文件:
Port 22
PermitRootLogin no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
X11Forwarding no
PrintMotd no
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server- 创建
entrypoint.sh文件:
#!/bin/bash
# 从 Secret 中读取私钥,并写入到开发用户的 .ssh 目录
mkdir -p /home/developer/.ssh
cp /mnt/ssh-key/id_rsa /home/developer/.ssh/id_rsa
cp /mnt/ssh-key/id_rsa.pub /home/developer/.ssh/authorized_keys
chown -R developer:developer /home/developer/.ssh
chmod 700 /home/developer/.ssh
chmod 600 /home/developer/.ssh/id_rsa
chmod 644 /home/developer/.ssh/authorized_keys
# 创建 /run/sshd 目录
mkdir -p /run/sshd
# 启动 SSH 服务
/usr/sbin/sshd -D &
# 启动 tailscaled 进程
/usr/sbin/tailscaled --tun=userspace-networking & # 显式启动 tailscaled
# 启动 Tailscale,使用预授权密钥,固定 hostname
tailscale up --accept-routes --hostname=dev-container --auth-key=$TAILSCALE_AUTH_KEY --socket=/local/tailscaled.sock
# 将 /opt/idea-tools 目录复制到工作目录
if [ -d "/opt/idea-tools" ]; then
rsync -avzh /opt/idea-tools/ /home/developer/
chown -R developer:developer /home/developer/
fi
# 保持脚本运行
tail -f /dev/nullV. 创建 Kubernetes 部署、PVC 和 PV (如果需要持久化)
- 创建
pvc.yaml文件 (如果需要持久化 idea-tools):
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: idea-tools-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: YOUR_STORAGE_CLASS_NAME # 替换为你的 StorageClass 名称
resources:
requests:
storage: 1Gi- 重要: 将
YOUR_STORAGE_CLASS_NAME替换为你的 Kubernetes 集群中可用的 StorageClass 名称。 你可以使用kubectl get storageclass命令查看可用的 StorageClass。 如果你的集群没有 StorageClass,你可能需要手动创建 PersistentVolume。
- 创建
deployment.yaml文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dev-container
spec:
replicas: 1
selector:
matchLabels:
app: dev-container
template:
metadata:
labels:
app: dev-container
spec:
# hostNetwork: true # 不建议使用 hostNetwork,除非你完全了解其安全风险
containers:
- name: dev-container
image: <你的镜像仓库地址>/<你的用户名>/my-dev-container:latest
ports:
- containerPort: 22
# hostPort: 22 # 如果使用了 hostNetwork,需要配置 hostPort
securityContext:
capabilities:
add:
- NET_ADMIN # Tailscale 需要 NET_ADMIN
# - NET_RAW # 除非应用程序需要原始套接字,否则不建议添加 NET_RAW
drop:
- ALL # 删除不必要的 capabilities
env:
- name: TAILSCALE_AUTH_KEY
valueFrom:
secretKeyRef:
name: tailscale-auth-key
key: auth-key
volumeMounts:
- name: ssh-key-volume
mountPath: /mnt/ssh-key
readOnly: true
- name: idea-tools-volume
mountPath: /opt/idea-tools # 将 /opt/idea-tools 挂载为 Volume
volumes:
- name: ssh-key-volume
secret:
secretName: ssh-private-key
items:
- key: id_rsa
path: id_rsa
- key: id_rsa.pub
path: id_rsa
- name: idea-tools-volume
persistentVolumeClaim:
claimName: idea-tools-pvc # 使用 PVC 进行持久化存储- 重要:
hostNetwork: true被注释掉,除非你真的需要并且理解其安全影响。hostPort: 22只有在hostNetwork: true时才需要配置。- 确保
claimName: idea-tools-pvc与你创建的 PersistentVolumeClaim 的名称一致.
- 应用配置:
kubectl apply -f pvc.yaml # 如果需要持久化 idea-tools
kubectl apply -f deployment.yamlVI. 配置 Tailscale 路由(重要!)
- 获取 Pod CIDR: 使用
kubectl get nodes -o yaml获取你的 Kubernetes 集群的 Pod CIDR。 - 在 Tailscale 管理后台配置路由: 登录 Tailscale 管理后台 (https://login.tailscale.com/admin/routes),找到你的 Kubernetes 节点,并启用 " 允许子网路由 ",填入你的 Pod CIDR。
VII. 获取 Tailscale IP 地址
- 等待 Pod 启动完成.
- 在 Tailscale 管理后台查看设备列表(https://login.tailscale.com/admin/machines)。 你应该能看到一个名为
dev-container的设备。 - 记下
dev-container设备的 Tailscale IP 地址。
VIII. 配置 SSH 连接
- 在本地机器上,使用 Tailscale IP 地址连接到开发容器:
ssh developer@<Tailscale IP 地址>- 将
<Tailscale IP 地址>替换为你从 Tailscale 管理后台获取的dev-container设备的 Tailscale IP 地址.
IX. 配置 IDEA 远程开发
-
在 IDEA 中配置 SSH 连接:
- 打开 IDEA,进入
File->Settings->Connections->SSH Configurations。 - 点击
+号,创建一个新的 SSH Configuration。 - Host:
<Tailscale IP 地址>(例如 100.100.100.100) - Port:
22 - User name:
developer - Authentication type:
Key pair (OpenSSH or PuTTY) - Private key file: 选择你本地的 SSH 私钥文件 (
~/.ssh/id_rsa)。 - 点击
Test Connection按钮,确保连接成功。
- 打开 IDEA,进入
-
配置 IDEA 远程开发:
- 进入
File->Settings->Build, Execution, Deployment->Deployment。 - 点击
+号,创建一个新的 Deployment Configuration。 - Type:
SFTP - Connection: 选择你刚刚创建的 SSH Configuration。
- Root path:
/home/developer(或者你的开发容器中的工作目录) - 配置本地路径和远程路径的映射关系。
- 重要: 确保将 IDEA 上传工具的目录映射到
/home/developer目录。
- 进入
-
配置 IDEA 自动上传/下载:
- 在
Deployment配置中,配置自动上传和下载选项,以便在本地修改代码后自动上传到开发容器,或者在开发容器中修改代码后自动下载到本地。
- 在
X. 开始远程开发
-
连接到远程服务器:
- 在 IDEA 中,打开
Tools->Deployment->Browse Remote Host,连接到你的远程服务器。
- 在 IDEA 中,打开
-
开始开发:
- 现在你可以在 IDEA 中像在本地一样进行开发了。 你的代码会自动上传到开发容器,并且你可以在开发容器中运行和调试你的应用程序。
安全注意事项:
- 最小权限原则: 只授予 Pod 必需的 capabilities,并删除所有不必要的 capabilities。
- Secret 管理: 妥善保管 Kubernetes Secrets,避免泄露。
hostNetwork: true: 除非你真的需要并且理解其安全影响,否则不建议使用hostNetwork: true。- 网络策略: 使用 Kubernetes 网络策略来限制 Pod 之间的网络通信,增强安全性。
- 定期更新: 定期更新你的 Docker 镜像和 Kubernetes 资源,以修复安全漏洞。
优化建议:
- 调整 Tailscale MTU: 根据你的网络环境,调整 Tailscale MTU,以提高网络性能。
- 选择更近的 DERP 服务器: 如果可能,手动选择距离客户端和开发容器更近的 DERP 服务器。
- 使用 UDP 加速: 在客户端和开发容器上启用 UDP 加速,以提高 UDP 协议的传输速度和可靠性。
- 检查网络拥塞: 使用网络监控工具,检查客户端和开发容器之间的网络连接是否存在拥塞。
总结:
这个方案提供了一个完整的、安全的、可靠的远程开发环境,使用了 Tailscale VPN、Kubernetes Secret 存储密钥、持久化存储,并考虑了安全和性能优化。 请按照以上步骤配置,并仔细检查配置的正确性。 特别注意 Tailscale 路由的配置,确保客户端可以访问 Kubernetes Pod 的 IP 地址。