4bef241544
- server/api: 注册/登录/登出/会话恢复(JWT + httpOnly Cookie, bcrypt 密码哈希) - server/api: 联系表单与资讯订阅入库,支持 SMTP 邮件通知(可选) - 应用启动自动建表(幂等),docker-compose 新增 PostgreSQL 16 服务 - 前端移除全部 mock:user store、登录注册弹窗、联系表单、页脚订阅接入真实接口 - i18n 补充服务端错误码中英文案,移除 demoHint - deploy: Deployment 环境变量(Secret 引用)、可选集群内 PostgreSQL(postgres.yaml) - 新增 .env.example 与 README/deploy 文档更新
119 lines
5.2 KiB
Markdown
119 lines
5.2 KiB
Markdown
# Arcticnewone 生产部署配置
|
||
|
||
本目录存放 Arcticnewone 个人网站的 Kubernetes 生产部署配置(华为云 SWR 镜像 + Traefik Ingress)。所有资源统一部署在 `arcticnewone` 命名空间中。
|
||
|
||
## 文件说明
|
||
|
||
| 文件 | 说明 |
|
||
| --- | --- |
|
||
| `namespace.yaml` | Namespace:项目专属命名空间 `arcticnewone` |
|
||
| `deployment.yaml` | Deployment:SWR 镜像、副本数 1、私有镜像拉取 secrets、数据库/SMTP 环境变量 |
|
||
| `service.yaml` | Service:ClusterIP,80 端口转发到容器 3000 端口 |
|
||
| `ingress.yaml` | Ingress:域名 `personal.virtheart.com`,Traefik + cert-manager 自动签发证书 |
|
||
| `postgres.yaml` | (可选)集群内 PostgreSQL StatefulSet + PVC,仅在没有外部数据库时使用 |
|
||
|
||
`namespace.yaml` 负责创建 `arcticnewone` 命名空间;deployment/service/ingress 三个 YAML 的 `metadata` 均已指定 `namespace: arcticnewone`。
|
||
|
||
## 镜像构建与推送
|
||
|
||
镜像由项目根目录的 `build-and-push.sh` 构建并推送到华为云 SWR(多架构 `linux/amd64,linux/arm64`,tag 统一为 `latest`):
|
||
|
||
```bash
|
||
# 1. 设置环境变量(凭据绝不写入仓库)
|
||
export SWR_REGISTRY="swr.cn-north-4.myhuaweicloud.com"
|
||
export SWR_NAMESPACE="virtheart"
|
||
export SWR_USERNAME="<用户名>"
|
||
export SWR_PASSWORD="<密码>"
|
||
|
||
# 2. 执行构建与推送
|
||
./build-and-push.sh
|
||
```
|
||
|
||
推送完成后镜像地址:`swr.cn-north-4.myhuaweicloud.com/virtheart/arcticnewone:latest`。
|
||
|
||
说明:SWR 不支持 buildx 多架构一步推送产生的 OCI manifest 格式。脚本改为分架构推送(`latest-amd64` / `latest-arm64`),再用 `docker manifest create` + `docker manifest push` 合并为标准的 Docker manifest list(`latest`),并禁用 provenance(`--provenance=false`),保证 SWR 可正常解析。
|
||
|
||
## 数据库(PostgreSQL)
|
||
|
||
应用依赖 PostgreSQL 16+ 存储用户、联系留言与订阅数据。两种方式二选一:
|
||
|
||
### 方式 A:使用云托管数据库(推荐)
|
||
|
||
使用华为云 RDS for PostgreSQL 等托管服务(自带备份与高可用),创建数据库 `arcticnewone`,连接串格式:
|
||
`postgres://<用户>:<密码>@<主机>:<端口>/arcticnewone`
|
||
|
||
### 方式 B:集群内部署(deploy/postgres.yaml)
|
||
|
||
```bash
|
||
kubectl apply -f deploy/postgres.yaml
|
||
```
|
||
|
||
会创建一个 StatefulSet + PVC(8Gi),数据库地址为 `arcticnewone-postgres.arcticnewone.svc.cluster.local:5432`。
|
||
仅建议在测试/低负载场景使用;数据持久化依赖集群存储类。
|
||
|
||
## 部署前置条件
|
||
|
||
1. 镜像已构建并推送到华为云 SWR(通过项目根目录 `./build-and-push.sh` 执行,见上方「镜像构建与推送」)
|
||
2. 集群已安装 Traefik Ingress 与 cert-manager(ClusterIssuer 名称为 `lets-encrypt`)
|
||
3. 域名 `personal.virtheart.com` 的 DNS 已解析到集群入口
|
||
4. 创建私有镜像拉取 Secret(凭据从环境变量读取,绝不写入仓库;命名空间由 `namespace.yaml` 创建):
|
||
|
||
```bash
|
||
kubectl create secret docker-registry huawei-swr-secret -n arcticnewone \
|
||
--docker-server="$SWR_REGISTRY" \
|
||
--docker-username="$SWR_USERNAME" \
|
||
--docker-password="$SWR_PASSWORD" \
|
||
--dry-run=client -o yaml | kubectl apply -f -
|
||
```
|
||
|
||
5. 创建应用配置 Secret `arcticnewone-secrets`(`database-url` 与 `jwt-secret` 必填;SMTP 项可选,不配置时联系表单/订阅仅入库不发邮件):
|
||
|
||
```bash
|
||
kubectl create secret generic arcticnewone-secrets -n arcticnewone \
|
||
--from-literal=database-url='postgres://arcticnewone:<密码>@<数据库地址>:5432/arcticnewone' \
|
||
--from-literal=jwt-secret='<随机长字符串,建议 openssl rand -hex 32>' \
|
||
--from-literal=postgres-password='<集群内数据库密码,仅方式 B 需要>' \
|
||
--from-literal=smtp-host='smtp.example.com' \
|
||
--from-literal=smtp-port='465' \
|
||
--from-literal=smtp-user='<SMTP 用户名>' \
|
||
--from-literal=smtp-pass='<SMTP 密码>' \
|
||
--from-literal=smtp-from='北极新一 <noreply@virtheart.com>' \
|
||
--from-literal=smtp-contact-to='chenchun@virtheart.com' \
|
||
--dry-run=client -o yaml | kubectl apply -f -
|
||
```
|
||
|
||
## 部署步骤
|
||
|
||
```bash
|
||
kubectl apply -f deploy/namespace.yaml
|
||
kubectl apply -f deploy/deployment.yaml
|
||
kubectl apply -f deploy/service.yaml
|
||
kubectl apply -f deploy/ingress.yaml
|
||
```
|
||
|
||
`namespace.yaml` 需最先应用;其余三个 YAML 自带 `namespace: arcticnewone`,`kubectl apply` 时无需额外指定。
|
||
若使用集群内数据库,还需先应用 `deploy/postgres.yaml`。
|
||
|
||
部署后验证:
|
||
|
||
```bash
|
||
kubectl get deploy,svc,ingress -n arcticnewone
|
||
kubectl rollout status deployment/arcticnewone -n arcticnewone
|
||
kubectl logs deploy/arcticnewone -n arcticnewone | grep '\[db\]' # 确认数据库连接成功
|
||
curl -I https://personal.virtheart.com
|
||
```
|
||
|
||
## 回滚方案
|
||
|
||
镜像 tag 固定为 `latest`,回滚前先确认 SWR 上 `latest` 对应可回滚的版本(或先推送目标版本镜像),再执行:
|
||
|
||
```bash
|
||
# 回滚到上一版本镜像
|
||
kubectl rollout undo deployment/arcticnewone -n arcticnewone
|
||
|
||
# 或整体移除本次部署的资源
|
||
kubectl delete -f deploy/ingress.yaml -n arcticnewone
|
||
kubectl delete -f deploy/service.yaml -n arcticnewone
|
||
kubectl delete -f deploy/deployment.yaml -n arcticnewone
|
||
```
|