feat: add apisix/etcd/grafana/prometheus

This commit is contained in:
Sun-ZhenXing
2025-09-26 16:40:04 +08:00
parent 8e096fb9a7
commit 30014852ca
17 changed files with 1473 additions and 0 deletions

37
src/etcd/.env.example Normal file
View File

@@ -0,0 +1,37 @@
# etcd Environment Variables
# etcd image version
ETCD_VERSION=v3.6.0
# Host port mapping for client connections (2379)
ETCD_CLIENT_PORT_OVERRIDE=2379
# Host port mapping for peer connections (2380)
ETCD_PEER_PORT_OVERRIDE=2380
# Human-readable name for this etcd member
ETCD_NAME=etcd-node
# Initial cluster configuration
ETCD_INITIAL_CLUSTER=etcd-node=http://localhost:2380
# Initial cluster state ('new' or 'existing')
ETCD_INITIAL_CLUSTER_STATE=new
# Initial cluster token for bootstrap
ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
# Auto compaction retention in hours
ETCD_AUTO_COMPACTION_RETENTION=1
# Storage size limit in bytes (2GB = 2147483648)
ETCD_QUOTA_BACKEND_BYTES=2147483648
# Heartbeat interval in milliseconds
ETCD_HEARTBEAT_INTERVAL=100
# Election timeout in milliseconds
ETCD_ELECTION_TIMEOUT=1000
# Enable etcd v2 API
ETCD_ENABLE_V2=false

135
src/etcd/README.md Normal file
View File

@@ -0,0 +1,135 @@
# etcd
[English](./README.md) | [中文](./README.zh.md)
This service deploys etcd, a distributed, reliable key-value store for the most critical data of a distributed system.
## Services
- `etcd`: The etcd key-value store service.
## Environment Variables
| Variable Name | Description | Default Value |
| ------------------------------ | ----------------------------------------------- | --------------------------------- |
| ETCD_VERSION | etcd image version | `v3.6.0` |
| ETCD_CLIENT_PORT_OVERRIDE | Host port mapping for client connections (2379) | `2379` |
| ETCD_PEER_PORT_OVERRIDE | Host port mapping for peer connections (2380) | `2380` |
| ETCD_NAME | Human-readable name for this etcd member | `etcd-node` |
| ETCD_INITIAL_CLUSTER | Initial cluster configuration | `etcd-node=http://localhost:2380` |
| ETCD_INITIAL_CLUSTER_STATE | Initial cluster state ('new' or 'existing') | `new` |
| ETCD_INITIAL_CLUSTER_TOKEN | Initial cluster token for bootstrap | `etcd-cluster` |
| ETCD_AUTO_COMPACTION_RETENTION | Auto compaction retention in hours | `1` |
| ETCD_QUOTA_BACKEND_BYTES | Storage size limit in bytes | `2147483648` (2GB) |
| ETCD_HEARTBEAT_INTERVAL | Heartbeat interval in milliseconds | `100` |
| ETCD_ELECTION_TIMEOUT | Election timeout in milliseconds | `1000` |
| ETCD_ENABLE_V2 | Enable etcd v2 API | `false` |
Please modify the `.env` file as needed for your use case.
## Volumes
- `etcd_data`: A volume for storing etcd data persistently.
## Network Ports
- `2379`: Client communication port
- `2380`: Peer communication port (for clustering)
## Single Node Setup
The default configuration runs etcd as a single node, suitable for development and testing.
## Cluster Setup
To set up a multi-node etcd cluster, you need to:
1. Define multiple etcd services in your compose file
2. Configure the `ETCD_INITIAL_CLUSTER` variable properly
3. Set unique names for each node
Example for a 3-node cluster:
```yaml
services:
etcd1:
# ... base config
environment:
- ETCD_NAME=etcd1
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd1:2379
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd1:2380
etcd2:
# ... base config
environment:
- ETCD_NAME=etcd2
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd2:2379
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd2:2380
etcd3:
# ... base config
environment:
- ETCD_NAME=etcd3
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd3:2379
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd3:2380
```
## Client Access
### Using etcdctl
Connect to etcd using the etcdctl client:
```bash
# Set endpoint
export ETCDCTL_ENDPOINTS=http://localhost:2379
# Put a key-value pair
etcdctl put mykey myvalue
# Get a value
etcdctl get mykey
# List all keys
etcdctl get --prefix ""
```
### Using HTTP API
etcd provides a RESTful HTTP API:
```bash
# Put a key-value pair
curl -X PUT http://localhost:2379/v3/kv/put \
-H 'Content-Type: application/json' \
-d '{"key":"bXlrZXk=","value":"bXl2YWx1ZQ=="}'
# Get a value
curl -X POST http://localhost:2379/v3/kv/range \
-H 'Content-Type: application/json' \
-d '{"key":"bXlrZXk="}'
```
## Performance Tuning
- Adjust `ETCD_QUOTA_BACKEND_BYTES` based on your storage needs
- Tune `ETCD_HEARTBEAT_INTERVAL` and `ETCD_ELECTION_TIMEOUT` for your network latency
- Configure `ETCD_AUTO_COMPACTION_RETENTION` to manage data size
## Security Notes
- The default configuration is for development/testing only
- For production, enable TLS encryption and authentication
- Consider network security and firewall rules
- Regular backups are recommended
## Monitoring
etcd exposes metrics at `http://localhost:2379/metrics` in Prometheus format.
## License
etcd is licensed under the Apache 2.0 license.

135
src/etcd/README.zh.md Normal file
View File

@@ -0,0 +1,135 @@
# etcd
[English](./README.md) | [中文](./README.zh.md)
本服务部署 etcd这是一个分布式、可靠的键值存储用于分布式系统的最关键数据。
## 服务
- `etcd`: etcd 键值存储服务。
## 环境变量
| 变量名 | 描述 | 默认值 |
| ------------------------------ | ----------------------------------- | --------------------------------- |
| ETCD_VERSION | etcd 镜像版本 | `v3.6.0` |
| ETCD_CLIENT_PORT_OVERRIDE | 客户端连接的主机端口映射2379 | `2379` |
| ETCD_PEER_PORT_OVERRIDE | 对等连接的主机端口映射2380 | `2380` |
| ETCD_NAME | 此 etcd 成员的人类可读名称 | `etcd-node` |
| ETCD_INITIAL_CLUSTER | 初始集群配置 | `etcd-node=http://localhost:2380` |
| ETCD_INITIAL_CLUSTER_STATE | 初始集群状态('new' 或 'existing' | `new` |
| ETCD_INITIAL_CLUSTER_TOKEN | 用于引导的初始集群令牌 | `etcd-cluster` |
| ETCD_AUTO_COMPACTION_RETENTION | 自动压缩保留时间(小时) | `1` |
| ETCD_QUOTA_BACKEND_BYTES | 存储大小限制(字节) | `2147483648` (2GB) |
| ETCD_HEARTBEAT_INTERVAL | 心跳间隔(毫秒) | `100` |
| ETCD_ELECTION_TIMEOUT | 选举超时(毫秒) | `1000` |
| ETCD_ENABLE_V2 | 启用 etcd v2 API | `false` |
请根据您的使用情况修改 `.env` 文件。
## 数据卷
- `etcd_data`: 用于持久存储 etcd 数据的数据卷。
## 网络端口
- `2379`: 客户端通信端口
- `2380`: 对等通信端口(用于集群)
## 单节点设置
默认配置将 etcd 作为单节点运行,适用于开发和测试。
## 集群设置
要设置多节点 etcd 集群,您需要:
1. 在您的 compose 文件中定义多个 etcd 服务
2. 正确配置 `ETCD_INITIAL_CLUSTER` 变量
3. 为每个节点设置唯一名称
3 节点集群示例:
```yaml
services:
etcd1:
# ... 基础配置
environment:
- ETCD_NAME=etcd1
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd1:2379
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd1:2380
etcd2:
# ... 基础配置
environment:
- ETCD_NAME=etcd2
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd2:2379
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd2:2380
etcd3:
# ... 基础配置
environment:
- ETCD_NAME=etcd3
- ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd3:2379
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd3:2380
```
## 客户端访问
### 使用 etcdctl
使用 etcdctl 客户端连接到 etcd
```bash
# 设置端点
export ETCDCTL_ENDPOINTS=http://localhost:2379
# 放置键值对
etcdctl put mykey myvalue
# 获取值
etcdctl get mykey
# 列出所有键
etcdctl get --prefix ""
```
### 使用 HTTP API
etcd 提供 RESTful HTTP API
```bash
# 放置键值对
curl -X PUT http://localhost:2379/v3/kv/put \
-H 'Content-Type: application/json' \
-d '{"key":"bXlrZXk=","value":"bXl2YWx1ZQ=="}'
# 获取值
curl -X POST http://localhost:2379/v3/kv/range \
-H 'Content-Type: application/json' \
-d '{"key":"bXlrZXk="}'
```
## 性能调优
- 根据您的存储需求调整 `ETCD_QUOTA_BACKEND_BYTES`
- 根据您的网络延迟调整 `ETCD_HEARTBEAT_INTERVAL``ETCD_ELECTION_TIMEOUT`
- 配置 `ETCD_AUTO_COMPACTION_RETENTION` 来管理数据大小
## 安全注意事项
- 默认配置仅适用于开发/测试
- 对于生产环境,启用 TLS 加密和身份验证
- 考虑网络安全和防火墙规则
- 建议定期备份
## 监控
etcd 在 `http://localhost:2379/metrics` 以 Prometheus 格式公开指标。
## 许可证
etcd 采用 Apache 2.0 许可证。

View File

@@ -0,0 +1,64 @@
x-default: &default
restart: unless-stopped
volumes:
- &localtime /etc/localtime:/etc/localtime:ro
- &timezone /etc/timezone:/etc/timezone:ro
logging:
driver: json-file
options:
max-size: 100m
services:
etcd:
<<: *default
image: quay.io/coreos/etcd:${ETCD_VERSION:-v3.6.0}
container_name: etcd
ports:
- "${ETCD_CLIENT_PORT_OVERRIDE:-2379}:2379"
- "${ETCD_PEER_PORT_OVERRIDE:-2380}:2380"
volumes:
- *localtime
- *timezone
- etcd_data:/etcd-data
environment:
- ETCD_NAME=${ETCD_NAME:-etcd-node}
- ETCD_DATA_DIR=/etcd-data
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_ADVERTISE_CLIENT_URLS=http://localhost:2379
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://localhost:2380
- ETCD_INITIAL_CLUSTER=${ETCD_INITIAL_CLUSTER:-etcd-node=http://localhost:2380}
- ETCD_INITIAL_CLUSTER_STATE=${ETCD_INITIAL_CLUSTER_STATE:-new}
- ETCD_INITIAL_CLUSTER_TOKEN=${ETCD_INITIAL_CLUSTER_TOKEN:-etcd-cluster}
- ETCD_AUTO_COMPACTION_RETENTION=${ETCD_AUTO_COMPACTION_RETENTION:-1}
- ETCD_QUOTA_BACKEND_BYTES=${ETCD_QUOTA_BACKEND_BYTES:-2147483648}
- ETCD_HEARTBEAT_INTERVAL=${ETCD_HEARTBEAT_INTERVAL:-100}
- ETCD_ELECTION_TIMEOUT=${ETCD_ELECTION_TIMEOUT:-1000}
- ETCD_ENABLE_V2=${ETCD_ENABLE_V2:-false}
command:
- etcd
- --name=${ETCD_NAME:-etcd-node}
- --data-dir=/etcd-data
- --listen-client-urls=http://0.0.0.0:2379
- --advertise-client-urls=http://localhost:2379
- --listen-peer-urls=http://0.0.0.0:2380
- --initial-advertise-peer-urls=http://localhost:2380
- --initial-cluster=${ETCD_INITIAL_CLUSTER:-etcd-node=http://localhost:2380}
- --initial-cluster-state=${ETCD_INITIAL_CLUSTER_STATE:-new}
- --initial-cluster-token=${ETCD_INITIAL_CLUSTER_TOKEN:-etcd-cluster}
- --auto-compaction-retention=${ETCD_AUTO_COMPACTION_RETENTION:-1}
- --quota-backend-bytes=${ETCD_QUOTA_BACKEND_BYTES:-2147483648}
- --heartbeat-interval=${ETCD_HEARTBEAT_INTERVAL:-100}
- --election-timeout=${ETCD_ELECTION_TIMEOUT:-1000}
- --enable-v2=${ETCD_ENABLE_V2:-false}
deploy:
resources:
limits:
cpus: '1.0'
memory: 1G
reservations:
cpus: '0.25'
memory: 256M
volumes:
etcd_data: