112 lines
3.7 KiB
Markdown
112 lines
3.7 KiB
Markdown
# config — Consul 配置 GitOps
|
||
|
||
Gitea 仓保存 `idwms/*.json` → CI 用 K8s Secret 渲染密码 → `PUT` 到 Consul KV → 可选重启 API。
|
||
|
||
应用仍只读 Consul(`AddConsul("idwms/...")`),不读本仓库。
|
||
|
||
## 分支 → 环境
|
||
|
||
| 分支 | 集群 | Namespace | 前端 CORS |
|
||
|------|------|-----------|-----------|
|
||
| `master` | prod(134) | `prod` | `https://ideship.com` |
|
||
| `test` | test(13) | `test` | `https://test.ideship.com` |
|
||
|
||
`test` 分支里中间件地址应为 `*.test.svc.cluster.local`;`master` 为 `*.prod.svc.cluster.local`。
|
||
|
||
## 目录
|
||
|
||
| 文件 | 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}`。同步时从对应 namespace 的 `prod-credentials` / `jwt-secret` 注入,**明文密码不进 git**。
|
||
|
||
## 一次性准备
|
||
|
||
### 1. JWT(业务 API 用)
|
||
|
||
```bash
|
||
# prod
|
||
kubectl -n prod create secret generic jwt-secret \
|
||
--from-literal=JWT_SECRET_KEY='请换成足够长的随机串' \
|
||
--dry-run=client -o yaml | kubectl apply -f -
|
||
|
||
# test(也可用 secrets.yaml 里 prod-credentials 的 JWT_SECRET_KEY)
|
||
kubectl --kubeconfig=/etc/rancher/k3s/k3s-test.yaml -n test \
|
||
create secret generic jwt-secret \
|
||
--from-literal=JWT_SECRET_KEY='ideship-jwt-secret-test-change-me' \
|
||
--dry-run=client -o yaml | kubectl apply -f -
|
||
```
|
||
|
||
### 2. (可选)Consul ACL Token
|
||
|
||
```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 push -u origin master # → prod Consul
|
||
git push -u origin test # → test Consul
|
||
```
|
||
|
||
Runner 标签需含 `k3s-host`;134 上需有 `/etc/rancher/k3s/k3s-test.yaml`。
|
||
|
||
### 4. 与线上对账(推荐先做)
|
||
|
||
```bash
|
||
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
|
||
export NAMESPACE=prod
|
||
./scripts/export-from-consul.sh
|
||
# 把导出文件里的密码改回 ${MYSQL_ROOT_PASSWORD} 等占位符后再提交到 master
|
||
```
|
||
|
||
## 日常
|
||
|
||
改 JSON → push `master` / `test` → workflow 按分支写入对应 Consul 并 `rollout restart`。
|
||
|
||
手动(在 runner 机上):
|
||
|
||
```bash
|
||
# prod(ClusterIP,本机可达)
|
||
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml NAMESPACE=prod
|
||
./scripts/sync-to-consul.sh
|
||
|
||
# test(内网 NodePort consul-ci;安全组仅 runner→13:30850)
|
||
export KUBECONFIG=/etc/rancher/k3s/k3s-test.yaml NAMESPACE=test
|
||
export CONSUL_HTTP_ADDR=http://172.16.16.13:30850
|
||
./scripts/sync-to-consul.sh
|
||
```
|
||
|
||
Consul **不要**走公网 Ingress;集群内业务仍用 `consul.test.svc:8500`。
|
||
|
||
## 密钥来源
|
||
|
||
| 占位符 | Secret(在目标 NAMESPACE 内 `prod-credentials`) |
|
||
|--------|--------------------------------------------------|
|
||
| `${MYSQL_ROOT_PASSWORD}` | MYSQL_ROOT_PASSWORD |
|
||
| `${REDIS_PASSWORD}` | REDIS_PASSWORD |
|
||
| `${RABBITMQ_DEFAULT_PASS}` | RABBITMQ_DEFAULT_PASS |
|
||
| `${ELASTIC_PASSWORD}` | ELASTIC_PASSWORD |
|
||
| `${JWT_SECRET_KEY}` | JWT_SECRET_KEY(或单独 `jwt-secret`) |
|
||
| `${COS_SECRET_ID}` / `${COS_SECRET_KEY}` | COS_* |
|
||
| `${FEISHU_WEBHOOK}` | FEISHU_WEBHOOK |
|
||
| `${WECHAT_MP_*}` | WECHAT_MP_APP_ID / SECRET / TOKEN |
|
||
| `${OMS_TRACK_*}` | OMS_TRACK_APP_KEY / SECRET |
|
||
| `${LAZADA_*}` `${SHOPEE_*}` `${TIKTOK_*}` `${TEMU_*}` | 对应平台密钥 |
|
||
|
||
同步后 **默认不再** `rollout restart`(已暂时关闭);需要时手动 restart 或恢复脚本里注释块。
|
||
|
||
## 注意
|
||
|
||
- `App:CorsOrigins` 改完必须重启 API(CI 默认会 restart)。
|
||
- COS 等共享公网地址可两端相同;库名/中间件 DNS 必须按环境分开。
|
||
- Consul 保持 ClusterIP,CI 经节点访问 Service ClusterIP。
|