refactor: 更新项目结构和文档,移除不再使用的配置文件

- 修改README.md,更新项目特性和目录结构说明
- 重命名基础设施代码目录为tofu,并添加Docker Swarm配置目录
- 移除不再使用的Docker Compose和Traefik配置文件
- 更新Terraform配置,专注于Oracle Cloud支持,移除华为云相关配置
- 清理开发环境变量和示例文件
This commit is contained in:
2025-09-20 16:49:32 +00:00
parent 7eb4a33523
commit 377f176501
45 changed files with 3278 additions and 69 deletions

View File

@@ -0,0 +1,198 @@
# Consul + Terraform 集成指南
本指南介绍如何使用 Consul 安全地管理 Terraform 中的敏感配置信息,特别是 Oracle Cloud 的凭据。
## 概述
我们使用 Consul 作为安全的密钥存储,避免在 Terraform 配置文件中直接暴露敏感信息。
## 架构
```
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Terraform │───▶│ Consul │───▶│ Oracle Cloud │
│ │ │ (密钥存储) │ │ │
│ consul provider │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
```
## 前提条件
1. Consul 集群正在运行
2. 可以访问 Consul API (默认: http://localhost:8500)
3. 已安装 curl 和 Terraform
## 快速开始
### 1. 启动 Consul 集群
```bash
# 进入 swarm 目录
cd swarm
# 启动 Consul 集群
docker-compose -f configs/traefik-consul-setup.yml up -d
```
### 2. 设置 Oracle Cloud 配置
```bash
# 使用密钥管理脚本设置配置
./scripts/utilities/consul-secrets-manager.sh set-oracle
```
脚本会提示你输入:
- 租户 OCID
- 用户 OCID
- API 密钥指纹
- 私钥文件路径
- 区间 OCID
### 3. 配置 Terraform
```bash
# 设置 Terraform Consul Provider
./scripts/utilities/terraform-consul-provider.sh setup
```
### 4. 验证配置
```bash
# 查看存储在 Consul 中的配置
./scripts/utilities/consul-secrets-manager.sh get-oracle
```
### 5. 运行 Terraform
```bash
cd infrastructure/environments/dev
# 初始化 Terraform
terraform init
# 规划部署
terraform plan
# 应用配置
terraform apply
```
## 详细说明
### Consul 密钥存储结构
```
config/
└── dev/
└── oracle/
├── tenancy_ocid
├── user_ocid
├── fingerprint
├── private_key
└── compartment_ocid
```
### 脚本功能
#### consul-secrets-manager.sh
- `set-oracle`: 设置 Oracle Cloud 配置到 Consul
- `get-oracle`: 从 Consul 获取配置信息
- `delete-oracle`: 删除 Consul 中的配置
- `generate-vars`: 生成临时 Terraform 变量文件
- `cleanup`: 清理临时文件
#### terraform-consul-provider.sh
- `setup`: 创建 Terraform Consul Provider 配置文件
### 安全特性
1. **敏感信息隔离**: 私钥等敏感信息只存储在 Consul 中
2. **临时文件**: 私钥文件只在 Terraform 运行时临时创建
3. **权限控制**: 临时私钥文件设置为 600 权限
4. **自动清理**: 提供清理脚本删除临时文件
## 环境变量
```bash
# Consul 地址
export CONSUL_ADDR="http://localhost:8500"
# Consul ACL Token (如果启用了 ACL)
export CONSUL_TOKEN="your-token"
# 环境名称
export ENVIRONMENT="dev"
```
## 故障排除
### Consul 连接问题
```bash
# 检查 Consul 状态
curl http://localhost:8500/v1/status/leader
# 检查 Consul 服务
docker ps | grep consul
```
### 配置验证
```bash
# 验证 Consul 中的配置
curl http://localhost:8500/v1/kv/config/dev/oracle?recurse
# 检查 Terraform 配置
terraform validate
```
### 清理和重置
```bash
# 清理临时文件
./scripts/utilities/consul-secrets-manager.sh cleanup
# 删除 Consul 中的配置
./scripts/utilities/consul-secrets-manager.sh delete-oracle
```
## 最佳实践
1. **定期轮换密钥**: 定期更新 Oracle Cloud API 密钥
2. **备份配置**: 定期备份 Consul 数据
3. **监控访问**: 监控 Consul 密钥访问日志
4. **环境隔离**: 不同环境使用不同的 Consul 路径
## 扩展其他云服务商
可以类似地为其他云服务商添加 Consul 集成:
```bash
# 华为云配置路径
config/dev/huawei/access_key
config/dev/huawei/secret_key
# AWS 配置路径
config/dev/aws/access_key
config/dev/aws/secret_key
# Google Cloud 配置路径
config/dev/gcp/service_account_key
```
## 相关文件
- `infrastructure/environments/dev/terraform.tfvars` - Terraform 变量配置
- `scripts/utilities/consul-secrets-manager.sh` - Consul 密钥管理脚本
- `scripts/utilities/terraform-consul-provider.sh` - Terraform Consul Provider 配置脚本
- `swarm/configs/traefik-consul-setup.yml` - Consul 集群配置
## 支持
如有问题,请检查:
1. Consul 集群是否正常运行
2. 网络连接是否正常
3. 权限设置是否正确
4. 环境变量是否正确设置

View File

@@ -0,0 +1,86 @@
# Oracle Cloud 凭据配置指南
## 凭据文件位置
### 1. OpenTofu 配置文件
**文件位置**: `infrastructure/environments/dev/terraform.tfvars`
这是主要的配置文件,需要填入你的 OCI 凭据:
```hcl
# Oracle Cloud 配置
oci_config = {
tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaa_你的租户ID"
user_ocid = "ocid1.user.oc1..aaaaaaaa_你的用户ID"
fingerprint = "aa:bb:cc:dd:ee:ff:gg:hh:ii:jj:kk:ll:mm:nn:oo:pp"
private_key_path = "~/.oci/oci_api_key.pem"
region = "ap-seoul-1"
compartment_ocid = "ocid1.compartment.oc1..aaaaaaaa_你的区间ID"
}
```
### 2. OCI 私钥文件
**文件位置**: `~/.oci/oci_api_key.pem`
这是你的 API 私钥文件,内容类似:
```
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC...
你的私钥内容
-----END PRIVATE KEY-----
```
### 3. OCI 配置文件 (可选)
**文件位置**: `~/.oci/config`
这是 OCI CLI 的配置文件,可以作为备用:
```ini
[DEFAULT]
user=ocid1.user.oc1..aaaaaaaa_你的用户ID
fingerprint=aa:bb:cc:dd:ee:ff:gg:hh:ii:jj:kk:ll:mm:nn:oo:pp
tenancy=ocid1.tenancy.oc1..aaaaaaaa_你的租户ID
region=ap-seoul-1
key_file=~/.oci/oci_api_key.pem
```
## 设置步骤
### 步骤 1: 创建 .oci 目录
```bash
mkdir -p ~/.oci
chmod 700 ~/.oci
```
### 步骤 2: 保存私钥文件
```bash
# 将你的私钥内容保存到文件
nano ~/.oci/oci_api_key.pem
# 设置正确的权限
chmod 400 ~/.oci/oci_api_key.pem
```
### 步骤 3: 编辑 terraform.tfvars
```bash
# 编辑配置文件
nano infrastructure/environments/dev/terraform.tfvars
```
## 安全注意事项
1. **私钥文件权限**: 确保私钥文件权限为 400 (只有所有者可读)
2. **不要提交到 Git**: `.gitignore` 已经配置忽略 `*.tfvars` 文件
3. **备份凭据**: 建议安全备份你的私钥和配置信息
## 验证配置
配置完成后,可以运行以下命令验证:
```bash
# 检查配置
./scripts/setup/setup-opentofu.sh check
# 初始化 OpenTofu
./scripts/setup/setup-opentofu.sh init

View File

@@ -0,0 +1,153 @@
# Oracle Cloud 配置指南
## 概述
本指南将帮助你配置 Oracle Cloud Infrastructure (OCI) 以便与 OpenTofu 一起使用。
## 前提条件
1. Oracle Cloud 账户(可以使用免费层)
2. 已安装 OpenTofu
3. 已安装 OCI CLI可选但推荐
## 步骤 1: 创建 Oracle Cloud 账户
1. 访问 [Oracle Cloud](https://cloud.oracle.com/)
2. 点击 "Start for free" 创建免费账户
3. 完成注册流程
## 步骤 2: 获取必要的 OCID
### 获取 Tenancy OCID
1. 登录 Oracle Cloud Console
2. 点击右上角的用户图标
3. 选择 "Tenancy: <your-tenancy-name>"
4. 复制 OCID 值
### 获取 User OCID
1. 在 Oracle Cloud Console 中
2. 点击右上角的用户图标
3. 选择 "User Settings"
4. 复制 OCID 值
### 获取 Compartment OCID
1. 在导航菜单中选择 "Identity & Security" > "Compartments"
2. 选择你要使用的 compartment通常是 root compartment
3. 复制 OCID 值
## 步骤 3: 创建 API 密钥
### 生成密钥对
```bash
# 创建 .oci 目录
mkdir -p ~/.oci
# 生成私钥
openssl genrsa -out ~/.oci/oci_api_key.pem 2048
# 生成公钥
openssl rsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem
# 设置权限
chmod 400 ~/.oci/oci_api_key.pem
chmod 400 ~/.oci/oci_api_key_public.pem
```
### 添加公钥到 Oracle Cloud
1. 在 Oracle Cloud Console 中,进入 "User Settings"
2. 在左侧菜单中选择 "API Keys"
3. 点击 "Add API Key"
4. 选择 "Paste Public Key"
5. 复制 `~/.oci/oci_api_key_public.pem` 的内容并粘贴
6. 点击 "Add"
7. 复制显示的 fingerprint
## 步骤 4: 配置 terraform.tfvars
编辑 `infrastructure/environments/dev/terraform.tfvars` 文件:
```hcl
# Oracle Cloud 配置
oci_config = {
tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaa_your_actual_tenancy_id"
user_ocid = "ocid1.user.oc1..aaaaaaaa_your_actual_user_id"
fingerprint = "aa:bb:cc:dd:ee:ff:gg:hh:ii:jj:kk:ll:mm:nn:oo:pp"
private_key_path = "~/.oci/oci_api_key.pem"
region = "ap-seoul-1" # 或你选择的区域
compartment_ocid = "ocid1.compartment.oc1..aaaaaaaa_your_compartment_id"
}
```
## 步骤 5: 验证配置
```bash
# 检查配置
./scripts/setup/setup-opentofu.sh check
# 初始化 OpenTofu
./scripts/setup/setup-opentofu.sh init
# 生成计划
./scripts/setup/setup-opentofu.sh plan
```
## 可用区域
常用的 Oracle Cloud 区域:
- `ap-seoul-1` - 韩国首尔
- `ap-tokyo-1` - 日本东京
- `us-ashburn-1` - 美国弗吉尼亚州
- `us-phoenix-1` - 美国亚利桑那州
- `eu-frankfurt-1` - 德国法兰克福
## 免费层资源
Oracle Cloud 免费层包括:
- 2 个 AMD 计算实例VM.Standard.E2.1.Micro
- 4 个 Arm 计算实例VM.Standard.A1.Flex
- 200 GB 块存储
- 10 GB 对象存储
- 负载均衡器
- 数据库等
## 故障排除
### 常见错误
1. **401 Unauthorized**: 检查 API 密钥配置
2. **404 Not Found**: 检查 OCID 是否正确
3. **权限错误**: 确保用户有足够的权限
### 验证连接
```bash
# 安装 OCI CLI可选
pip install oci-cli
# 配置 OCI CLI
oci setup config
# 测试连接
oci iam compartment list
```
## 安全最佳实践
1. 定期轮换 API 密钥
2. 使用最小权限原则
3. 不要在代码中硬编码凭据
4. 使用 compartment 隔离资源
5. 启用审计日志
## 参考资料
- [Oracle Cloud Infrastructure 文档](https://docs.oracle.com/en-us/iaas/)
- [OCI Terraform Provider](https://registry.terraform.io/providers/oracle/oci/latest/docs)
- [Oracle Cloud 免费层](https://www.oracle.com/cloud/free/)