作者:cuitcc
链接:https://zhuanlan.zhihu.com/p/1946952058902143914
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

本地ecs挂载火山云tos + 公网S3 作为 http 服务器

优点:成本底,tos有现成监控.

一、总体设计目标

使用 aptly 管理 Debian/Ubuntu 包,通过 火山云 TOS 存储仓库数据,并配置公网可访问的 APT 源,实现:

  • 包的集中管理(本地或 CI/CD 环境)
  • 支持多发行版(如 jammy, bookworm)、多架构(amd64, arm64
  • GPG 签名发布,确保安全性
  • 挂载火山云tos 的bucket 目录到本地服务器
  • 火山云配置域名客户端通过 HTTP(S) 访问仓库

二、aptly服务部署流程

1. 基础环境配置

# 安装依赖
sudo apt update && sudo apt install aptly
然后执行 sudo apt install s3fs 安装 S3FS 工具。
执行以下命令,将火山云 Access Key ID 和 Secret Access Key 信息存放到 .passwd-tos 文件。
echo AK:SK > /root/.passwd-tos

2. aptly初始化配置

# 创建配置文件 ~/.aptly.conf
{
“rootDir”: “/opt/aptly”,
“downloadConcurrency”: 4,
“architectures”: [“amd64”, “arm64”],
“S3PublishEndpoints”: {
“tos-beijing”: {
“region”: “cn-beijing”,
“bucket”: “yourcompany-apt-pool”,
“endpoint”: “tos-s3.cn-beijing.volces.com“,
“acl”: “public-read”
}
}
}

# 初始化目录结构
sudo mkdir -p /opt/aptly/{db,public}
将tos 的bucket 挂载到该目录
s3fs devops-dev /opt/aptly/ -o passwd_file=$HOME/.passwd-tos -o url=tos-s3-cn-beijing.volces.com -d -o f2

3. 创建GPG签名密钥

gpg –batch –generate-key <<EOF
Key-Type: RSA
Key-Length: 4096
Subkey-Type: RSA
Subkey-Length: 4096
Name-Real: “YourCompany APT”
Name-Email: [email protected]
Expire-Date: 0
Passphrase: “${GPG_PASSPHRASE}”
%commit
EOF

# 导出公钥备用
gpg –armor –export [email protected] > /opt/aptly/public/apt-repo.pub

三、仓库管理核心操作

1. 创建并发布仓库

# 创建主仓库
aptly repo create -distribution=”focal” -component=”main” yourcompany-main

# 添加本地软件包
aptly repo add yourcompany-main /path/to/debs/*.deb

2. 代理上游仓库(可选)

# 创建Ubuntu官方源镜像
aptly mirror create \
-architectures=amd64,arm64 \
ubuntu-focal-main archive.ubuntu.com/ubun focal main

# 同步镜像
aptly mirror update ubuntu-focal-main

# 转为本地仓库
aptly repo import ubuntu-focal-main yourcompany-main ubuntu

3. 仓库更新与清理

# 增量更新流程
aptly repo add yourcompany-main new-packages/*.deb
# 自动清理旧版本(保留最近3个)
aptly db cleanup -dry-run=false -keep-latest=3

四、客户端配置指南

1. 添加APT源

# 安装公钥
curl -s apt.yourcompany.com/apt | sudo gpg –dearmor -o /usr/share/keyrings/yourcompany.gpg

# 创建源文件
cat <<EOF | sudo tee /etc/apt/sources.list.d/yourcompany.list
deb [arch=amd64 signed-by=/usr/share/keyrings/yourcompany.gpg] \
apt.yourcompany.com/ focal main
EOF

# 更新并安装
sudo apt update && sudo apt install your-package