item-atlas/deploy/public/README.md

121 lines
10 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 见物公网预览
`api-atlas.tutudog.com` 为原生客户端提供 HTTPS。完整路径为公网 Nginx → 两条 FRP STCP → 工作 Mac 的 SSD VM → Kubernetes Service。`/api/` 到网关,`/auth/realms/item-atlas/` 与 `/auth/resources/` 到 Keycloak。身份服务用 Keycloak 的生产启动模式和固定 issuer。账号由内部后台创建,开放注册关闭。
## 应用和数据
在 `item-atlas-public` 命名空间部署,另有 PostgreSQL 持久卷和私有图片桶。物品、录入和身份服务各用独立数据库。先准备环境私密文件,再应用底座及应用 chart。
```sh
kubectl get namespace item-atlas-public >/dev/null 2>&1 || kubectl create namespace item-atlas-public
python3 deploy/public/prepare-secrets.py "$ATLAS_SECRET_DIR" \
--owner "$ATLAS_INITIAL_ACCOUNT" --ollama-url "$ATLAS_INTERNAL_OLLAMA_URL"
```
把生成的各 `.env` 按文件名创建同名 Kubernetes Secret。用 `item-atlas-realm.json` 创建 `atlas-public-realm`,文件键用 `item-atlas.json`。在 Garage 建立 `jianwu-public-photos`,导入 `atlas-public-media.env` 里的应用密钥,只授予该桶的读取和写入权限。镜像仓库通过 `item-atlas-registry` 提供拉取权限。生成文件、用户口令和服务密钥保存在 SSD 上的受控目录。
这些文件用于初始化。已有环境的 Keycloak realm、账号和数据库密码由运行中的服务维护。备份与恢复时,以当前数据库、图片桶和现用 Secret 为准。
```sh
theme=deploy/public/keycloak/jianwu/login
kubectl -n item-atlas-public create configmap atlas-public-login-theme \
--from-file="$theme/theme.properties" --from-file="$theme/messages/messages_zh_CN.properties" \
--from-file="$theme/messages/messages_en.properties" --from-file="$theme/resources/css/jianwu.css" \
--from-file=brand.png=apps/ios/ItemAtlas/Assets.xcassets/BrandMark.imageset/BrandMark.png \
--dry-run=client -o yaml | kubectl -n item-atlas-public apply --server-side \
--field-manager=item-atlas-login-theme -f -
kubectl -n item-atlas-public apply -f deploy/public/base.yaml
kubectl -n item-atlas-public rollout status deployment/atlas-keycloak --timeout=5m
helm upgrade --install item-atlas deploy/helm/item-atlas -n item-atlas-public \
-f deploy/public/values.yaml --set-string image.tag="$ATLAS_PUBLISHED_SHA" \
--atomic --wait --timeout 12m --history-max 10
```
`ATLAS_PUBLISHED_SHA` 取已成功发布镜像的 Drone 构建标识。公网预览使用同一 Helm chart。初次建立该环境时,运行上述 Helm 命令并验收,再配置公网域名。切换版本前先记录当前修订和三个镜像摘要。
`jianwu` 主题继承 Keycloak 26.8.2 的 `keycloak.v2` 登录表单。首次导入会给 `atlas-ios` 设置 Login theme。已有 realm 在管理端打开 Clients → atlas-ios → Settings → Login theme,选择 `jianwu`。更新主题文件后重启 Keycloak Deployment 以刷新缓存。单副本重启期间登录入口会短暂中断。先检查主题文件和回调,再验收登录。遇到显示问题可把该客户端的 Login theme 恢复为默认。 [Keycloak 主题](https://www.keycloak.org/ui-customization/themes)
### 原生会话配置
启用公网 values 前准备两份 Secret。生成独立的 RSA 密钥对,至少 2048 位,原文件和备份保存到 SSD 的受控目录。
| Secret | 内容 | 注入对象 |
| --- | --- | --- |
| `atlas-native-verification` | `ATLAS_NATIVE_ISSUER=https://api-atlas.tutudog.com/session` 与 `ATLAS_NATIVE_PUBLIC_KEY`(Base64 X.509 DER) | 物品、录入服务,并供只读候选验签 |
| `atlas-native-signing` | `ATLAS_NATIVE_PRIVATE_KEY`(Base64 PKCS#8 DER) | 稳定版物品服务 |
首次使用 Flyway V5 建表。运维核对用户原有的 Keycloak 用户编号后,将规范化用户名、原编号和 BCrypt 哈希插入 `native_account`。可用 `htpasswd -niB -C 12 "$ATLAS_USERNAME"` 从标准输入生成哈希,再只保存冒号后的哈希字段。更新密码时按用户事务更新哈希并清理该用户的 `native_session`,已有访问令牌最长 5 分钟到期。暂停账号时设置 `enabled=false`。
渲染并加载本目录的 Nginx 模板,使 `/api/session/` 使用独立限流。在真机安装新版前先通过 [原生会话冒烟](../../docs/deployment/identity-and-media.md#ios-公网连通性验收)。若回退到早于 V5 的镜像,原生登录入口也随之回退,安排回退前先确认手机端的可用版本与登录路径。
## 转发和验收
在集群里用 FRP 客户端直连 `svc/item-atlas-gateway` 和 `svc/atlas-keycloak`。将 `frpc.toml.template` 渲染为受控文件,其中两条 STCP 各用独立密钥。云端 visitor 使用匹配的名称和密钥,只监听 Nginx 容器可达的私有网桥,由 systemd 管理。
```sh
kubectl -n item-atlas-public create secret generic atlas-public-frpc \
--from-file=frpc.toml="$ATLAS_SECRET_DIR/frpc.toml" --dry-run=client -o yaml |
kubectl -n item-atlas-public apply --server-side -f -
kubectl -n item-atlas-public apply -f deploy/public/frpc.yaml
kubectl -n item-atlas-public rollout status deployment/atlas-public-frpc --timeout=2m
kubectl -n item-atlas-public logs deployment/atlas-public-frpc --tail=20
```
日志应显示两个代理注册成功。渲染 `nginx.conf.template`,将两个 `ATLAS_VISITOR_*` 填为实际私有监听地址和端口。用 `nginx -t` 通过后重载。先用 `curl --resolve` 指定目标 IP,核对 TLS、OIDC discovery 和业务 API,再加入公网 A 记录。通过公网跑原生会话冒烟,带令牌查询 `GET /api/households` 应返回 200。另查未登录的 API 及管理路径,预期分别为 401 和 404。仍有旧版 iOS 使用时再验一次 OIDC 授权码回调。 [iOS 验收步骤](../../docs/deployment/identity-and-media.md#ios-公网连通性验收)
工作 Mac 需要挂载 SSD 并运行 Linux VM,集群会启动 FRP 客户端并重连。通常检查顺序是 DNS、TLS、Nginx、STCP、Service 与应用。检查应用版本用 `helm history item-atlas -n item-atlas-public`,回退前先核对数据库迁移兼容性。需要关闭公网预览时停用该域名的独立 Nginx 站点。数据库和私有桶有独立备份流程。
### API 证书
公网 API 使用独立的 Certbot HTTP-01 证书。首次接入先配置 HTTP 站点,让挑战路径使用 Nginx 配置挂载目录的 `.acme-api`。已有 HTTPS 站点时可先保留原证书。申请成功后将 `install-api-certificate.sh` 安装为 `/opt/item-atlas-api-cert/install-certificate.sh` 并运行,再切换 `nginx.conf.template` 所示的专用证书路径。检查配置、重载并启用 `item-atlas-api-cert-renew.timer`。
```sh
certbot renew --cert-name api-atlas.tutudog.com --dry-run
systemctl list-timers item-atlas-api-cert-renew.timer
```
## PC 工作台
`pc-atlas.tutudog.com` 的页面及静态资源经独立的 `jianwu-public-pc` STCP 到 PC 容器。同源 `/api/` 复用网关的 STCP 通道。会话请求使用现有的限流区,管理 API 路径在 PC 入口返回 404。PC 前端和 FRP 客户端使用独立 Deployment,前端更新时可以保留已注册的通道。
先按 [PC 构建说明](../../apps/pc/README.md#构建与部署) 生成 ARM64 镜像。用不可变发布标识作为标签,在 kind 环境载入该镜像,或令集群从私有仓库拉取。将 `pc-frpc.toml.template` 渲染到受控文件,再部署独立 Helm release。
```sh
kubectl -n item-atlas-public create secret generic atlas-pc-frpc \
--from-file=frpc.toml="$ATLAS_SECRET_DIR/pc-frpc.toml"
helm upgrade --install item-atlas-pc deploy/helm/item-atlas-pc -n item-atlas-public \
--set-string image="$ATLAS_PC_IMAGE" --set frp.enabled=true \
--atomic --wait --timeout 3m
kubectl -n item-atlas-public rollout status deployment/item-atlas-pc --timeout=60s
```
云机用同一条 STCP 密钥渲染 `pc-visitor.toml.template`,由 `item-atlas-pc-frpc.service` 监听私有地址的 18792 端口。渲染 `pc-nginx.conf.template` 后先检查 Nginx 配置,再重载。首次接入可以用已有覆盖该域名的证书验证 HTTPS,之后为 PC 域名建立独立的 A 记录。
独立证书用 Certbot 的 HTTP-01 webroot。Nginx 将 `/.well-known/acme-challenge/` 指到配置挂载目录下的 `.acme-pc`,其他 HTTP 请求跳转 HTTPS。签发后将 `install-pc-certificate.sh` 安装在云机 `/opt/item-atlas-pc/`,运行一次,并把站点证书目录指到 `/etc/nginx/ssl/pc-atlas.tutudog.com/current`。检查配置并重载后,启用 `item-atlas-pc-cert-renew.timer`。核对外部证书、续期模拟和定时器的下次执行时间。
```sh
certbot renew --cert-name pc-atlas.tutudog.com --dry-run
systemctl list-timers item-atlas-pc-cert-renew.timer
curl -fsS https://pc-atlas.tutudog.com/floor-plan
```
验收包括深层页面刷新、原生账号登录、物品搜索和平面图读取。PC 版本用 `helm history item-atlas-pc -n item-atlas-public` 查看。后续镜像更新可回退到上一份包含 FRP 的 PC 修订,并复验页面和 API。
## H5 移动网页
`m-atlas.tutudog.com` 通过 `jianwu-public-h5` STCP 服务移动网页,复用原网关 STCP 提供同源 `/api/`。H5 与 PC 使用相同的静态站点 chart,镜像、Secret、FRP 客户端和 Helm release 各自独立。
1. 按 [H5 构建说明](../../apps/h5/README.md#构建镜像) 生成并加载 ARM64 镜像。渲染 `h5-frpc.toml.template`,用独立 STCP 密钥创建 `atlas-h5-frpc`。
2. 在入口云机渲染 `h5-visitor.toml.template`,放在 `/opt/item-atlas-h5/frpc.toml`。安装 `item-atlas-h5-frpc.service`,visitor 仅监听 Nginx 私有网桥的 18793 端口。
3. 渲染 `h5-nginx.conf.template`。首次签发时,先加载带挑战路径的 HTTP 站点和 `.acme-h5` webroot,再设置 A 记录和签发独立证书。将 `install-h5-certificate.sh` 安装为 `/opt/item-atlas-h5/install-certificate.sh`,运行一次后启用 HTTPS 站点和 `item-atlas-h5-cert-renew.timer`。每次改 Nginx 后先运行 `nginx -t`,通过再重载。
```sh
helm upgrade --install item-atlas-h5 deploy/helm/item-atlas-pc -n item-atlas-public \
-f deploy/public/h5-values.yaml --set-string image="$ATLAS_H5_IMAGE" \
--atomic --wait --timeout 3m
certbot renew --cert-name m-atlas.tutudog.com --dry-run
curl -fsS https://m-atlas.tutudog.com/healthz
```
验收时从公网手机视口走登录、手动录入和找物,照片要查缩略图与删除。匿名业务 API 应返回 401,管理路径应返回 404。后续 H5 更新可按 `helm history item-atlas-h5 -n item-atlas-public` 查历史并回滚到先前成功的修订,复验页面和 API。