Domain Cloud Native

作者 actionbook5c40d3ad7851無授權條款1.5K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫6 週前更新

Use when building cloud-native apps. Keywords: kubernetes, k8s, docker, container, grpc, tonic, microservice, service mesh, observability, tracing, metrics, health check, cloud, deployment, 云原生, 微服务, 容器

AI 產生的概覽

為 Rust 雲原生服務設計提供 12 要素、無狀態、可觀測性與優雅關閉等約束指引。

功能
此技能為建構雲原生應用程式(主要是 Rust)提供領域約束。它將 12 要素設定、無狀態設計、健康檢查、優雅關閉與可觀測性等規則對應到設計影響與 crate 選擇。文件包含優雅關閉與健康/就緒端點的程式碼模式,以及常見錯誤與相關技能對照表。
適用情境
適用於設計或審查執行於容器或 Kubernetes 中的雲原生服務。適合需要無狀態設計、分散式追蹤、指標、健康檢查或優雅關閉的工作。它僅提供指引,不產出檔案或指令碼。
執行需求
無需指令碼或特殊工具,僅為純說明文件。程式碼模式涉及 tonic、kube、tracing、opentelemetry、prometheus、config、axum 等 Rust crate,但閱讀這些指引無需安裝任何項目。

Cloud-Native Domain

Layer 3: Domain Constraints

Domain Constraints → Design Implications

Domain RuleDesign ConstraintRust Implication
12-FactorConfig from envEnvironment-based config
ObservabilityMetrics + tracestracing + opentelemetry
Health checksLiveness/readinessDedicated endpoints
Graceful shutdownClean terminationSignal handling
Horizontal scaleStateless designNo local state
Container-friendlySmall binariesRelease optimization

Critical Constraints

Stateless Design

RULE: No local persistent stateWHY: Pods can be killed/rescheduled anytimeRUST: External state (Redis, DB), no static mut

Graceful Shutdown

RULE: Handle SIGTERM, drain connectionsWHY: Zero-downtime deploymentsRUST: tokio::signal + graceful shutdown

Observability

RULE: Every request must be traceableWHY: Debugging distributed systemsRUST: tracing spans, opentelemetry export

Trace Down ↓

From constraints to design (Layer 2):

"Need distributed tracing"    ↓ m12-lifecycle: Span lifecycle    ↓ tracing + opentelemetry
"Need graceful shutdown"    ↓ m07-concurrency: Signal handling    ↓ m12-lifecycle: Connection draining
"Need health checks"    ↓ domain-web: HTTP endpoints    ↓ m06-error-handling: Health status

Key Crates

PurposeCrate
gRPCtonic
Kuberneteskube, kube-runtime
Dockerbollard
Tracingtracing, opentelemetry
Metricsprometheus, metrics
Configconfig, figment
HealthHTTP endpoints

Design Patterns

PatternPurposeImplementation
gRPC servicesService meshtonic + tower
K8s operatorsCustom resourceskube-runtime Controller
ObservabilityDebuggingtracing + OTEL
Health checksOrchestration/health, /ready
Config12-factorEnv vars + secrets

Code Pattern: Graceful Shutdown

rust
use tokio::signal;
async fn run_server() -> anyhow::Result<()> {    let app = Router::new()        .route("/health", get(health))        .route("/ready", get(ready));
    let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
    axum::Server::bind(&addr)        .serve(app.into_make_service())        .with_graceful_shutdown(shutdown_signal())        .await?;
    Ok(())}
async fn shutdown_signal() {    signal::ctrl_c().await.expect("failed to listen for ctrl+c");    tracing::info!("shutdown signal received");}

Health Check Pattern

rust
async fn health() -> StatusCode {    StatusCode::OK}
async fn ready(State(db): State<Arc<DbPool>>) -> StatusCode {    match db.ping().await {        Ok(_) => StatusCode::OK,        Err(_) => StatusCode::SERVICE_UNAVAILABLE,    }}

Common Mistakes

MistakeDomain ViolationFix
Local file stateNot statelessExternal storage
No SIGTERM handlingHard killsGraceful shutdown
No tracingCan't debugtracing spans
Static configNot 12-factorEnv vars

Trace to Layer 1

ConstraintLayer 2 PatternLayer 1 Implementation
StatelessExternal stateArc<Client> for external
Graceful shutdownSignal handlingtokio::signal
TracingSpan lifecycletracing + OTEL
Health checksHTTP endpointsDedicated routes

Related Skills

WhenSee
Async patternsm07-concurrency
HTTP endpointsdomain-web
Error handlingm13-domain-error
Resource lifecyclem12-lifecycle

來源與署名

來源:actionbook/rust-skills位於skills/domain-cloud-native提交5c40d3a

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架