flnxi-Wu/docs/guides/REGISTRY_DEPLOYMENT_GUIDE.md
WuYongJie d1c6efc79d
Some checks failed
CI Check / check (push) Has been cancelled
feat: NAS 部署与 Harbor 推送流程
2025-12-03 14:39:06 +08:00

4.7 KiB
Raw Blame History

Registry 部署快速指南

快速开始(阿里云 ACR)

1. 准备工作(5 分钟)

1.1 创建阿里云账号

  1. 访问 https://www.aliyun.com
  2. 注册/登录账号

1.2 创建容器镜像服务实例

  1. 进入「容器镜像服务 ACR」:https://cr.console.aliyun.com
  2. 选择「个人版」(免费)
  3. 选择地域(建议选择与服务器相同的地域,如:华东1-杭州)
  4. 创建实例

1.3 创建命名空间和仓库

  1. 创建命名空间(如:youyidian)
  2. 创建镜像仓库(如:3d-material)
  3. 选择仓库类型:私有

1.4 获取登录凭证

  1. 在仓库页面,点击「访问凭证」
  2. 设置固定密码(或使用临时密码)
  3. 记录以下信息:
    • 登录地址:registry.cn-hangzhou.aliyuncs.com(根据地域不同)
    • 用户名:通常是阿里云账号
    • 密码:刚才设置的密码

2. 配置部署脚本(2 分钟)

2.1 设置环境变量(推荐)

创建 .env.registry 文件(不提交到 Git):

# 阿里云容器镜像服务配置
ALIYUN_REGISTRY_HOST=registry.cn-hangzhou.aliyuncs.com
ALIYUN_REGISTRY_NAMESPACE=youyidian
ALIYUN_REGISTRY_IMAGE=3d-material
ALIYUN_REGISTRY_USER=your-username
ALIYUN_REGISTRY_PASSWORD=your-password

2.2 使用部署脚本

# 加载环境变量
Get-Content .env.registry | ForEach-Object {
    if ($_ -match '^([^=]+)=(.*)$') {
        $env:($matches[1]) = $matches[2]
    }
}

# 执行部署
.\一键镜像部署.ps1 `
    -Server 45.249.208.118 `
    -Username root `
    -Password "your-ssh-password" `
    -RegistryHost $env:ALIYUN_REGISTRY_HOST `
    -RegistryNamespace $env:ALIYUN_REGISTRY_NAMESPACE `
    -RegistryImage $env:ALIYUN_REGISTRY_IMAGE `
    -RegistryUser $env:ALIYUN_REGISTRY_USER `
    -RegistryPassword $env:ALIYUN_REGISTRY_PASSWORD `
    -Tag "v1.0.0"

3. 首次部署(3 分钟)

# 第一次部署需要构建和推送,耗时稍长
.\一键镜像部署.ps1 `
    -Server 45.249.208.118 `
    -Username root `
    -Password "your-ssh-password" `
    -RegistryHost "registry.cn-hangzhou.aliyuncs.com" `
    -RegistryNamespace "youyidian" `
    -RegistryImage "3d-material" `
    -RegistryUser "your-username" `
    -RegistryPassword "your-password" `
    -Tag "latest"

4. 后续部署(1-2 分钟)

后续部署会更快,因为:

  • 构建时使用缓存(如果依赖未变化)
  • 只推送变更的层
  • 服务器端只拉取变更的层

使用 Docker Hub(备选方案)

如果不想使用阿里云,可以使用 Docker Hub:

1. 注册 Docker Hub 账号

访问 https://hub.docker.com 注册

2. 创建仓库

在 Docker Hub 创建仓库:your-username/flnxi-material

3. 部署

.\一键镜像部署.ps1 `
    -Server 45.249.208.118 `
    -Username root `
    -Password "your-ssh-password" `
    -RegistryHost "docker.io" `
    -RegistryNamespace "your-username" `
    -RegistryImage "flnxi-material" `
    -RegistryUser "your-username" `
    -RegistryPassword "your-dockerhub-password" `
    -Tag "latest"

性能对比

当前方案(tar 上传)

  • 构建:3-5 分钟(本地)
  • 打包:30 秒
  • 上传:1-2 分钟(120MB)
  • 加载:30 秒
  • 总计:5-8 分钟

Registry 方案

  • 构建:3-5 分钟(本地,使用缓存)
  • 推送:30 秒-1 分钟(只推送变更层)
  • 拉取:30 秒-1 分钟(服务器端,只拉取变更层)
  • 总计:4-7 分钟(首次),2-3 分钟(后续)

优势

  1. ✅ 上传更快:只推送变更的层,不是整个镜像
  2. ✅ 部署更快:服务器端只拉取变更的层
  3. ✅ 版本管理:可以保留多个版本,方便回滚
  4. ✅ 安全性:私有仓库,代码不会泄露
  5. ✅ 可扩展:可以轻松部署到多台服务器

注意事项

  1. 首次部署:需要完整构建和推送,耗时稍长
  2. 网络要求:需要能访问 Registry(阿里云国内访问快)
  3. 认证安全:不要将密码提交到 Git,使用环境变量
  4. 版本管理:建议使用有意义的 tag(如:v1.0.0、latest)

故障排查

问题 1:登录失败

Error: unauthorized: authentication required

解决:检查用户名和密码是否正确

问题 2:推送失败

Error: denied: requested access to the resource is denied

解决:检查命名空间和仓库名称是否正确,是否有推送权限

问题 3:服务器拉取失败

Error: pull access denied

解决:确保服务器已登录 Registry,使用正确的用户名和密码

下一步

部署成功后,可以:

  1. 设置自动部署(CI/CD)
  2. 配置多环境部署(开发/测试/生产)
  3. 使用版本标签管理不同版本