feat: 重构项目脚本结构和文档
docs: 添加API和部署文档 refactor: 重新组织脚本目录结构 fix: 修复Nomad配置中的region设置 chore: 移除过期节点相关配置 test: 更新MCP服务器测试脚本 build: 更新Makefile以适配新脚本路径
This commit is contained in:
@@ -25,6 +25,96 @@ config/{environment}/{provider}/{region_or_service}/{key}
|
||||
- **region_or_service**: 区域或服务名称,如 `kr`、`us`、`sgp` 等
|
||||
- **key**: 具体的配置键名,如 `token`、`tenancy_ocid`、`user_ocid` 等
|
||||
|
||||
### Consul集群配置变量
|
||||
|
||||
Consul集群自身配置也应遵循上述命名规范。以下是一些关键配置变量的示例:
|
||||
|
||||
#### 集群基本配置
|
||||
```
|
||||
config/dev/consul/cluster/data_dir
|
||||
config/dev/consul/cluster/raft_dir
|
||||
config/dev/consul/cluster/datacenter
|
||||
config/dev/consul/cluster/bootstrap_expect
|
||||
config/dev/consul/cluster/log_level
|
||||
config/dev/consul/cluster/encrypt_key
|
||||
```
|
||||
|
||||
#### 节点配置
|
||||
```
|
||||
config/dev/consul/nodes/master/ip
|
||||
config/dev/consul/nodes/ash3c/ip
|
||||
config/dev/consul/nodes/warden/ip
|
||||
```
|
||||
|
||||
#### 网络配置
|
||||
```
|
||||
config/dev/consul/network/client_addr
|
||||
config/dev/consul/network/bind_interface
|
||||
config/dev/consul/network/advertise_interface
|
||||
```
|
||||
|
||||
#### 端口配置
|
||||
```
|
||||
config/dev/consul/ports/dns
|
||||
config/dev/consul/ports/http
|
||||
config/dev/consul/ports/https
|
||||
config/dev/consul/ports/grpc
|
||||
config/dev/consul/ports/grpc_tls
|
||||
config/dev/consul/ports/serf_lan
|
||||
config/dev/consul/ports/serf_wan
|
||||
config/dev/consul/ports/server
|
||||
```
|
||||
|
||||
#### 服务发现配置
|
||||
```
|
||||
config/dev/consul/service/enable_script_checks
|
||||
config/dev/consul/service/enable_local_script_checks
|
||||
config/dev/consul/service/enable_service_script
|
||||
```
|
||||
|
||||
#### 性能配置
|
||||
```
|
||||
config/dev/consul/performance/raft_multiplier
|
||||
```
|
||||
|
||||
#### 日志配置
|
||||
```
|
||||
config/dev/consul/log/enable_syslog
|
||||
config/dev/consul/log/log_file
|
||||
```
|
||||
|
||||
#### 连接配置
|
||||
```
|
||||
config/dev/consul/connection/reconnect_timeout
|
||||
config/dev/consul/connection/reconnect_timeout_wan
|
||||
config/dev/consul/connection/session_ttl_min
|
||||
```
|
||||
|
||||
#### Autopilot配置
|
||||
```
|
||||
config/dev/consul/autopilot/cleanup_dead_servers
|
||||
config/dev/consul/autopilot/last_contact_threshold
|
||||
config/dev/consul/autopilot/max_trailing_logs
|
||||
config/dev/consul/autopilot/server_stabilization_time
|
||||
config/dev/consul/autopilot/disable_upgrade_migration
|
||||
```
|
||||
|
||||
#### 快照配置
|
||||
```
|
||||
config/dev/consul/snapshot/enabled
|
||||
config/dev/consul/snapshot/interval
|
||||
config/dev/consul/snapshot/retain
|
||||
config/dev/consul/snapshot/name
|
||||
```
|
||||
|
||||
#### 备份配置
|
||||
```
|
||||
config/dev/consul/backup/enabled
|
||||
config/dev/consul/backup/interval
|
||||
config/dev/consul/backup/retain
|
||||
config/dev/consul/backup/name
|
||||
```
|
||||
|
||||
### 示例配置
|
||||
|
||||
#### 应用配置
|
||||
@@ -119,6 +209,186 @@ pair, _, _ := kv.Get("config/dev/app/name", nil)
|
||||
appName := string(pair.Value)
|
||||
```
|
||||
|
||||
## 部署遵循最佳变量命名规范的Consul集群
|
||||
|
||||
为了确保Consul集群完全遵循最佳变量命名规范,我们提供了一套完整的部署方案。
|
||||
|
||||
### 部署流程
|
||||
|
||||
1. **设置Consul变量**: 使用脚本将所有Consul集群配置存储到Consul KV中
|
||||
2. **生成配置文件**: 使用Consul模板从KV存储动态生成配置文件
|
||||
3. **部署集群**: 使用Nomad部署使用动态配置的Consul集群
|
||||
|
||||
### 部署脚本
|
||||
|
||||
我们提供了以下脚本来简化部署过程:
|
||||
|
||||
#### setup_consul_cluster_variables.sh
|
||||
此脚本将Consul集群配置存储到Consul KV中,遵循 `config/{environment}/{provider}/{region_or_service}/{key}` 格式。
|
||||
|
||||
```bash
|
||||
# 设置Consul集群变量
|
||||
./deployment/scripts/setup_consul_cluster_variables.sh
|
||||
```
|
||||
|
||||
#### generate_consul_config.sh
|
||||
此脚本使用Consul模板从KV存储生成最终的Consul配置文件。
|
||||
|
||||
```bash
|
||||
# 生成Consul配置文件
|
||||
./deployment/scripts/generate_consul_config.sh
|
||||
```
|
||||
|
||||
#### deploy_consul_cluster_kv.sh
|
||||
此脚本是一个综合部署脚本,执行完整的部署流程。
|
||||
|
||||
```bash
|
||||
# 部署遵循最佳变量命名规范的Consul集群
|
||||
./deployment/scripts/deploy_consul_cluster_kv.sh
|
||||
```
|
||||
|
||||
### 配置模板
|
||||
|
||||
我们提供了Consul配置模板文件 `consul.hcl.tmpl`,使用Consul模板语法从KV存储中动态获取配置:
|
||||
|
||||
```hcl
|
||||
# 基础配置
|
||||
data_dir = "{{ keyOrDefault `config/dev/consul/cluster/data_dir` `/opt/consul/data` }}"
|
||||
raft_dir = "{{ keyOrDefault `config/dev/consul/cluster/raft_dir` `/opt/consul/raft` }}"
|
||||
|
||||
# 启用UI
|
||||
ui_config {
|
||||
enabled = {{ keyOrDefault `config/dev/consul/ui/enabled` `true` }}
|
||||
}
|
||||
|
||||
# 服务器配置
|
||||
server = true
|
||||
bootstrap_expect = {{ keyOrDefault `config/dev/consul/cluster/bootstrap_expect` `3` }}
|
||||
|
||||
# 网络配置
|
||||
client_addr = "{{ keyOrDefault `config/dev/consul/nodes/master/ip` `100.117.106.136` }}"
|
||||
bind_addr = "{{ keyOrDefault `config/dev/consul/nodes/master/ip` `100.117.106.136` }}"
|
||||
advertise_addr = "{{ keyOrDefault `config/dev/consul/nodes/master/ip` `100.117.106.136` }}"
|
||||
|
||||
# 集群连接 - 从KV获取其他节点IP
|
||||
retry_join = [
|
||||
"{{ keyOrDefault `config/dev/consul/nodes/ash3c/ip` `100.116.80.94` }}",
|
||||
"{{ keyOrDefault `config/dev/consul/nodes/warden/ip` `100.122.197.112` }}"
|
||||
]
|
||||
```
|
||||
|
||||
### Nomad作业配置
|
||||
|
||||
我们提供了完全遵循最佳变量命名规范的Nomad作业配置文件 `consul-cluster-kv.nomad`,该文件使用Consul模板从KV存储动态获取配置:
|
||||
|
||||
```hcl
|
||||
task "consul" {
|
||||
driver = "exec"
|
||||
|
||||
# 使用模板从Consul KV获取配置
|
||||
template {
|
||||
data = <<EOF
|
||||
# Consul配置文件 - 从KV存储动态获取
|
||||
# 遵循 config/{environment}/{provider}/{region_or_service}/{key} 格式
|
||||
|
||||
# 基础配置
|
||||
data_dir = "{{ keyOrDefault `config/dev/consul/cluster/data_dir` `/opt/consul/data` }}"
|
||||
raft_dir = "{{ keyOrDefault `config/dev/consul/cluster/raft_dir` `/opt/consul/raft` }}"
|
||||
|
||||
# 服务器配置
|
||||
server = true
|
||||
bootstrap_expect = {{ keyOrDefault `config/dev/consul/cluster/bootstrap_expect` `3` }}
|
||||
|
||||
# 网络配置
|
||||
client_addr = "{{ keyOrDefault `config/dev/consul/nodes/master/ip` `100.117.106.136` }}"
|
||||
bind_addr = "{{ keyOrDefault `config/dev/consul/nodes/master/ip` `100.117.106.136` }}"
|
||||
advertise_addr = "{{ keyOrDefault `config/dev/consul/nodes/master/ip` `100.117.106.136` }}"
|
||||
|
||||
# 集群连接 - 从KV获取其他节点IP
|
||||
retry_join = [
|
||||
"{{ keyOrDefault `config/dev/consul/nodes/ash3c/ip` `100.116.80.94` }}",
|
||||
"{{ keyOrDefault `config/dev/consul/nodes/warden/ip` `100.122.197.112` }}"
|
||||
]
|
||||
EOF
|
||||
destination = "local/consul.hcl"
|
||||
}
|
||||
|
||||
config {
|
||||
command = "consul"
|
||||
args = [
|
||||
"agent",
|
||||
"-config-dir=local"
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 验证部署
|
||||
|
||||
部署完成后,可以通过以下方式验证Consul集群是否正确遵循了最佳变量命名规范:
|
||||
|
||||
1. **检查Consul KV中的配置**:
|
||||
```bash
|
||||
# 检查Consul集群配置
|
||||
curl -s http://localhost:8500/v1/kv/config/dev/consul/?keys | jq '.'
|
||||
```
|
||||
|
||||
2. **验证Consul集群状态**:
|
||||
```bash
|
||||
# 检查集群leader
|
||||
curl -s http://localhost:8500/v1/status/leader
|
||||
|
||||
# 检查集群节点
|
||||
curl -s http://localhost:8500/v1/status/peers
|
||||
```
|
||||
|
||||
3. **验证配置文件**:
|
||||
```bash
|
||||
# 验证生成的配置文件语法
|
||||
consul validate /root/mgmt/components/consul/configs/consul.hcl
|
||||
```
|
||||
|
||||
### 动态更新配置
|
||||
|
||||
使用这种部署方式,您可以动态更新Consul集群配置,而无需重新部署整个集群:
|
||||
|
||||
1. **更新Consul KV中的配置**:
|
||||
```bash
|
||||
# 更新日志级别
|
||||
curl -X PUT http://localhost:8500/v1/kv/config/dev/consul/cluster/log_level -d "DEBUG"
|
||||
|
||||
# 更新快照间隔
|
||||
curl -X PUT http://localhost:8500/v1/kv/config/dev/consul/snapshot/interval -d "12h"
|
||||
```
|
||||
|
||||
2. **重新生成配置文件**:
|
||||
```bash
|
||||
# 重新生成配置文件
|
||||
./deployment/scripts/generate_consul_config.sh
|
||||
```
|
||||
|
||||
3. **重新加载Consul配置**:
|
||||
```bash
|
||||
# 重新加载Consul配置
|
||||
consul reload
|
||||
```
|
||||
|
||||
### 环境隔离
|
||||
|
||||
通过使用环境变量和不同的配置路径,您可以轻松实现不同环境的隔离:
|
||||
|
||||
```bash
|
||||
# 开发环境
|
||||
ENVIRONMENT=dev ./deployment/scripts/setup_consul_cluster_variables.sh
|
||||
|
||||
# 生产环境
|
||||
ENVIRONMENT=prod ./deployment/scripts/setup_consul_cluster_variables.sh
|
||||
```
|
||||
|
||||
这样,不同环境的配置将存储在不同的路径下:
|
||||
- 开发环境: `config/dev/consul/...`
|
||||
- 生产环境: `config/prod/consul/...`
|
||||
|
||||
## 存储(Storage)配置
|
||||
|
||||
### 持久化存储
|
||||
|
||||
Reference in New Issue
Block a user