config/README.md

93 lines
2.8 KiB
Markdown
Raw Permalink 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.

# config — Consul 配置 GitOps
Gitea 仓保存 `idwms/*.json` → CI 用 K8s Secret 渲染密码 → `PUT` 到 Consul KV → 可选重启 API。
应用仍只读 Consul(`AddConsul("idwms/...")`),不读本仓库。
## 目录
| 文件 | Consul KV |
|------|-----------|
| `idwms/common.json` | `idwms/common` |
| `idwms/admin.json` | `idwms/admin` |
| `idwms/itfxapi.json` | `idwms/itfxapi` |
| `idwms/trackapi.json` | `idwms/trackapi` |
JSON 里敏感字段用占位符,例如 `${MYSQL_ROOT_PASSWORD}`。同步时从 `prod-credentials` / `jwt-secret` 注入,**明文密码不进 git**。
## 一次性准备
### 1. JWT(业务 API 用,中间件 Secret 里没有)
在 k3s 节点:
```bash
kubectl -n prod create secret generic jwt-secret \
--from-literal=JWT_SECRET_KEY='请换成足够长的随机串' \
--dry-run=client -o yaml | kubectl apply -f -
```
也可把 `JWT_SECRET_KEY` 加进现有 `manifests/secrets.yaml` 的 `prod-credentials`。
### 2. (可选)Consul ACL Token
若已开 ACL:
```bash
kubectl -n prod create secret generic consul-acl \
--from-literal=CONSUL_HTTP_TOKEN='...' \
--dry-run=client -o yaml | kubectl apply -f -
```
### 3. 推到 Gitea
```bash
cd d:/my/itfx/config
git remote add origin https://git.ideship.com/<org>/config.git
git add -A && git commit -m "chore: initial consul config repo"
git push -u origin master
```
在 Gitea 启用 Actions,确保 runner 标签含 `k3s-host`(与 API 部署相同)。
### 4. 与线上对账(推荐先做)
在能 `kubectl` 的机器上:
```bash
./scripts/export-from-consul.sh
# 把导出文件里的密码改回 ${MYSQL_ROOT_PASSWORD} 等占位符后再提交
```
避免模板库名/字段和现网不一致,第一次同步把线上冲掉。
## 日常
改 JSON → push `main`/`master` → workflow `Sync Consul Config` 自动同步并 `rollout restart` 对应 API。
手动:
```bash
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
./scripts/sync-to-consul.sh # 全部
RESTART_APIS=1 ./scripts/sync-to-consul.sh admin
```
## 密钥来源
| 占位符 | Secret |
|--------|--------|
| `${MYSQL_ROOT_PASSWORD}` | `prod-credentials` |
| `${REDIS_PASSWORD}` | `prod-credentials` |
| `${RABBITMQ_DEFAULT_PASS}` | `prod-credentials` |
| `${ELASTIC_PASSWORD}` | `prod-credentials` |
| `${JWT_SECRET_KEY}` | `jwt-secret` 或 `prod-credentials` |
与 `k3s/manifests/secrets.yaml` 一致:中间件用 Secret;本仓只存带占位符的业务配置模板。
## 注意
- `App:CorsOrigins` 在启动时打进 CORS 策略,改完必须重启 API(CI 默认会 restart)。
- 首次用仓库模板前务必 `export-from-consul` 对过现网,或确认 MySQL 库名(`oms` / `itfx` / `track`)正确。
- Consul 保持 ClusterIP,CI 经节点访问 Service ClusterIP,不暴露公网。