item-atlas/deploy/admin-preview/README.md

57 lines
4.2 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.

# 管理后台本机预览
在 kind 中创建独立的 `item-atlas-admin-preview` 命名空间。管理员和普通账号由独立的 Keycloak realm 管理。网关和物品服务使用同一次后端候选发布的镜像。PostgreSQL 在一个独立的持久卷上,为登录和物品服务分别创建数据库和用户。
```mermaid
flowchart LR
browser[本机浏览器] -->|127.0.0.1:18482| web[Nginx 与管理后台]
web -->|OIDC 授权码与 PKCE| identity[Keycloak]
web -->|管理 API| gateway[Spring Cloud Gateway]
gateway --> inventory[物品服务]
inventory -->|内网 JWKS| identity
identity --> iddb[(身份数据库)]
inventory --> pg[(物品数据库)]
```
浏览器通过同源 `/auth` 登录,物品服务从集群内获取签名公钥。令牌的 issuer 仍须匹配浏览器地址。管理接口验证签名、有效期、audience、`atlas-admin` 客户端和 `atlas-space-admin` 角色。此配置使用回环 HTTP,公网部署请使用 [HTTPS 身份配置](../../docs/deployment/identity-and-media.md)。
## 启动
在能访问 kind 的终端执行,确认当前上下文、节点镜像架构和存储类位置。已有的候选后端镜像可以直接用。新节点需按 [候选交付流程](../../docs/deployment/backend-release.md) 拉取清单中的两个后端镜像。Docker 和 kubectl 如果同在虚拟机里,以下构建与部署命令也在虚拟机中执行。
```sh
kind_cluster=atlas-dev
context="kind-$kind_cluster"
namespace=item-atlas-admin-preview
secret_dir=/path/on/ssd/item-atlas-admin-preview
docker build --build-arg VITE_OIDC_URL=/auth \
--build-arg VITE_OIDC_REALM=item-atlas-admin-preview \
-t item-atlas-admin:admin-preview-0.1.1 apps/admin
kind load docker-image --name "$kind_cluster" item-atlas-admin:admin-preview-0.1.1
python3 deploy/admin-preview/prepare-secrets.py "$secret_dir"
kubectl --context "$context" create namespace "$namespace"
for component in postgres keycloak inventory; do
kubectl --context "$context" -n "$namespace" create secret generic "atlas-admin-$component" \
--from-env-file="$secret_dir/$component.env"
done
kubectl --context "$context" -n "$namespace" create secret generic atlas-admin-realm \
--from-file="$secret_dir/item-atlas-admin-preview-realm.json"
kubectl --context "$context" apply --dry-run=server -k deploy/admin-preview
kubectl --context "$context" apply -k deploy/admin-preview
kubectl --context "$context" -n "$namespace" get pods,pvc
kubectl --context "$context" -n "$namespace" port-forward --address 127.0.0.1 svc/item-atlas-admin 18482:8080
```
首次部署生成两位预览用户 `preview-admin`、`preview-viewer`,密码在私有目录的 `credentials.json`。保留该目录。已有预览更新镜像后重新执行 `apply -k`,无需重新建 Secret。Keycloak 仅在首次创建 realm 时从文件导入用户。
## 验收和排错
打开 [管理后台](http://127.0.0.1:18482/)。管理员可以分页查空间、按名称或完整 ID 定位、查看物品和照片数量。普通账号登录后会看到权限提示。删除请使用专用测试空间,提交后检查数据库中的 `space_admin_action`。照片记录被删除时,数据库触发器会在同一个事务登记 `media_cleanup`。本预览用于核对队列写入,真实对象清理由 [对象存储冒烟脚本](../../backend/tests/media_smoke.py) 在带存储的环境验收。
页面打不开先查 `/healthz` 和端口转发。浏览器返回登录页时看 Keycloak 回调地址和 Nginx `/auth` 路由。管理 API 返回 `401` 时核对 issuer、JWKS 和有效期,返回 `403` 时核对客户端和管理员角色。物品服务启动时 Flyway 会等待数据库,持续失败时查看 PVC、PostgreSQL 日志及 Flyway 日志。后台 Nginx 的访问日志保留路径、状态码与响应耗时,省略包含授权码的查询参数。
更新后台 Pod 后端口转发会断开,重新运行 `kubectl port-forward` 即可。停止转发后入口关闭,预览数据库保留在 PVC 中。 [端口转发](https://kubernetes.io/docs/reference/kubectl/generated/kubectl_port-forward/) · [Keycloak hostname](https://www.keycloak.org/server/hostname) · [Spring Boot Flyway 配置](https://docs.spring.io/spring-boot/4.0/appendix/application-properties/#application-properties.data-migration)