calfonso/rusternetes

GitHub: calfonso/rusternetes

使用 Rust 从零重新实现的 Kubernetes 容器编排平台,提供与原生 K8s 高度一致的 API 和行为,同时支持单进程轻量化部署。

Stars: 612 | Forks: 42

# Rūsternetes **使用 Rust 从头开始重新实现的 Kubernetes。** [文档站点](https://calfonso.github.io/rusternetes/) 超过 216,000 行 Rust 代码,分布在 10 个 crate 中。包含 31 个 controller。超过 3,100 个测试。正在积极针对官方 Kubernetes e2e 测试套件进行一致性测试 —— 目前在 160 轮测试中通过了 94% 的一致性测试(415/441)。 这不是对 Go 代码库的简单封装,也不是部分模拟。每一个组件 —— API server、scheduler、controller manager、kubelet、kube-proxy —— 都是使用 Rust 从头编写的,实现了真正的 Kubernetes API 接口、网络格式和行为语义。 ## Web Console Rūsternetes 包含一个内置的 Web console,提供实时的集群拓扑可视化、实时指标、pod 日志流以及完整的资源管理功能。它会自动部署 —— 内嵌于 API server 中,无需单独安装。 [![带有实时日志的集群拓扑](https://static.pigsec.cn/wp-content/uploads/repos/cas/8d/8d3c8511ea848d2260ecfb8628f3b98cdedd997cf395af73f61af63816f0aab3.png)](docs/screenshots/console-topology-logs.png) | | | | |---|---|---| | [![概览](https://static.pigsec.cn/wp-content/uploads/repos/cas/2e/2eb0f2438fd584f9cdb5ea6dc5899f3f86d06f4e6e9143e3411afaa2e8486cc0.png)](docs/screenshots/console-overview.png) | [![拓扑](https://static.pigsec.cn/wp-content/uploads/repos/cas/42/42ef1426d4ec95c5c5af5ec46236411479416f3944731b9e6837df49fa0ba965.png)](docs/screenshots/console-topology.png) | [![工作负载](https://static.pigsec.cn/wp-content/uploads/repos/cas/b7/b70ccad819b13839978fcbacc507fc8f51f63b47515a00ad0438f264fbccb805.png)](docs/screenshots/console-workloads.png) | | **概览** — 健康环、迷你图表、deployment 发布进度、事件流 | **拓扑** — 带有流量粒子、CPU 热力图、协议徽章的动态 node/pod/service 地图 | **工作负载** — Pod 阶段图表、带有伸缩/重启功能的 deployment 卡片、重启热力图 | | [![网络](https://static.pigsec.cn/wp-content/uploads/repos/cas/d4/d41a97f50102764098e059c8e40dd50b4625d94a5a6e1f08d065819e949d2b2a.png)](docs/screenshots/console-networking.png) | [![存储](https://static.pigsec.cn/wp-content/uploads/repos/cas/87/87f1ebb2988cae3f1f896af165d0748931dabb61428d2d134f03710441fc85e7.png)](docs/screenshots/console-storage.png) | [![节点](https://static.pigsec.cn/wp-content/uploads/repos/cas/96/9651656188afb1e50e50e286fff4510c281fe7c898fc4ba6173e79a001f24a27.png)](docs/screenshots/console-nodes.png) | | **网络** — Service CIDR、DNS、kube-proxy 配置、service 路由图 | **存储** — 容量、StorageClass 供应、PVC/PV 管理 | **节点** — 来自真实 Docker 统计数据的 CPU/内存仪表,cordon/uncordon | | [![配置](https://static.pigsec.cn/wp-content/uploads/repos/cas/93/9382feeba0464304ed55d74a4d41e222cdf76d9b9d18fa8bde02bc8d9ce4221b.png)](docs/screenshots/console-config.png) | [![事件](https://static.pigsec.cn/wp-content/uploads/repos/cas/17/17fd58892e0859825e609c513e094540118a3a721ea3f5a3f273ba1af503616c.png)](docs/screenshots/console-events.png) | [![RBAC](https://static.pigsec.cn/wp-content/uploads/repos/cas/81/8142f129b5cd2214356a0fac1f70cf2a3fd4c9f289b05491fcc1a5bafb691161.png)](docs/screenshots/console-rbac.png) | | **配置** — 带有键徽章的 ConfigMaps、Secrets、Service Accounts | **事件** — 频率直方图、类型/原因过滤、自动刷新 | **RBAC** — Subject-role 映射、binding 可视化、规则徽章 | 请查阅 [Console 用户指南](docs/CONSOLE_USER_GUIDE.md) 获取完整文档。 ## 架构 ``` ┌───────────────────────────────────────────────────────────────┐ │ Control Plane │ │ │ │ ┌──────────────────┐ ┌──────────────┐ ┌────────────────┐ │ │ │ API Server │ │ Scheduler │ │ Controller │ │ │ │ Axum + TLS │ │ Affinity │ │ Manager │ │ │ │ REST + Watch │ │ Taints │ │ 31 control │ │ │ │ RBAC + Webhooks │ │ Preemption │ │ loops │ │ │ │ Web Console │ │ │ │ │ │ │ └────────┬─────────┘ └──────────────┘ └────────────────┘ │ │ │ │ │ ┌────────▼─────────┐ │ │ │ Storage │ │ │ │ etcd|SQLite|Redis│ │ │ └──────────────────┘ │ ├───────────────────────────────────────────────────────────────┤ │ Node Components │ │ │ │ ┌──────────────────┐ ┌──────────────────────────────────┐ │ │ │ Kubelet │ │ Kube-Proxy │ │ │ │ bollard (Docker)│ │ iptables routing │ │ │ │ Probes+Volumes │ │ ClusterIP/NodePort/LB │ │ │ └──────────────────┘ └──────────────────────────────────┘ │ └───────────────────────────────────────────────────────────────┘ ``` ## 选择你的部署方式 Rusternetes 支持在同一代码库下使用多种部署模式: **带有 etcd 的完整集群** —— 标准的生产环境部署,每个组件在单独的容器中运行,由具有 Raft 共识和 leader 选举的 etcd 集群作为后端支持。 **替换数据库** —— 用 [Rhino](https://github.com/calfonso/rhino) 替换 etcd,这是一个用 Rust 编写的、与 etcd 兼容的 gRPC server,可将所有内容存储在 SQLite、Redis、PostgreSQL 或 MySQL 中。相同的 Kubernetes API,相同的二进制文件,无需任何 etcd 基础设施。只需修改 compose 文件即可。 **单一二进制文件,单一进程** —— 所有五个组件作为一个进程中的并发 tokio 任务运行,并带有内嵌的 SQLite 或 Redis 后端。无需 etcd,无需外部基础设施。你的整个集群状态都存放在一个单一的 SQLite 文件或一个 Redis 实例中。 All-in-one 模式专为完整 K8s 集群显得过于庞大的环境而构建:边缘设备、CI/CD pipeline、本地开发、IoT 网关、嵌入式系统以及物理隔离环境。 ## 前置条件 - **Rust**(最新稳定版)—— 通过 [rustup.rs](https://rustup.rs/) 安装 - **Protocol Buffers 编译器** —— 构建 API server 所需 - Fedora/RHEL: `sudo dnf install -y protobuf-compiler protobuf-devel` - Debian/Ubuntu: `sudo apt install -y protobuf-compiler` - macOS: `brew install protobuf` - **容器运行时** —— Docker 或 Podman ## 快速开始 ### 完整集群 (Podman + etcd) ``` git clone https://github.com/calfonso/rusternetes.git cd rusternetes export KUBELET_VOLUMES_PATH=$(pwd)/.rusternetes/volumes podman compose build podman compose up -d bash scripts/bootstrap-cluster.sh export KUBECONFIG=~/.kube/rusternetes-config kubectl get nodes kubectl create deployment nginx --image=nginx ``` ### 完整集群 (Docker Compose + etcd) ``` git clone https://github.com/calfonso/rusternetes.git cd rusternetes export KUBELET_VOLUMES_PATH=$(pwd)/.rusternetes/volumes docker compose build docker compose up -d bash scripts/bootstrap-cluster.sh export KUBECONFIG=~/.kube/rusternetes-config kubectl get nodes ``` ### 带有 SQLite 的完整集群 (无 etcd) 相同的集群,但由 [Rhino](https://github.com/calfonso/rhino) 替换 etcd。无需重新编译 —— 使用相同的二进制文件。 ``` # Podman podman compose -f compose.sqlite.yml build podman compose -f compose.sqlite.yml up -d # Docker docker compose -f docker-compose.sqlite.yml build docker compose -f docker-compose.sqlite.yml up -d bash scripts/bootstrap-cluster.sh ``` ### 带有 Redis 的完整集群 (无 etcd) 相同的集群,但 Rhino 使用 Redis 作为内存存储: ``` podman compose -f compose.redis.yml build podman compose -f compose.redis.yml up -d bash scripts/bootstrap-cluster.sh ``` ### All-in-one 二进制文件 在单一进程中运行完整的 Kubernetes,并内嵌 SQLite: ``` cargo build -p rusternetes ./target/release/rusternetes --data-dir ./cluster.db ``` 或者使用 Redis: ``` cargo build -p rusternetes --features redis ./target/release/rusternetes --storage-backend redis --redis-url redis://localhost:6379 ``` **前提条件:** 需要 Podman 或 Docker 供 kubelet 管理容器。在带有 Podman 的 Linux 上,kube-proxy 进行 iptables 访问时需要 rootful 模式。有关详细的设置,请参阅 [DEVELOPMENT.md](docs/DEVELOPMENT.md)。 ## 已实现的功能 ### API Server 基于 Axum 的 HTTPS server,实现了 Kubernetes REST API。包含 76 个 handler 模块,涵盖 core/v1、apps/v1、batch/v1、rbac.authorization.k8s.io/v1、storage.k8s.io/v1、networking.k8s.io/v1 等。 - 为所有主要资源类型提供完整的 CRUD - 带有 Server-Sent Events 的 Watch API - Server-Side Apply、Strategic Merge Patch、JSON Patch - Field selectors 和 label selectors - 带有 watch、status/scale 子资源、schema 校验的 Custom Resource Definitions - Validating 和 Mutating Admission Webhooks - 带有 CEL 表达式的 ValidatingAdmissionPolicy - 带有 Roles、ClusterRoles 和 Bindings 的 RBAC 授权 - ServiceAccount JWT token 签名 (RS256) - TLS/mTLS、审计日志、Pod Security Standards - OpenAPI v3 发现、聚合发现 ### Scheduler 采用 Filter/score 插件架构,具有: - Node/Pod 亲和性与反亲和性 - Taints 和 tolerations - 资源请求和限制评分 - Priority classes 和抢占 - 拓扑分布约束 ### Controller Manager 31 个运行并发循环的调谐 controller: | Controller | 功能说明 | |---|---| | Deployment | 滚动更新、回滚、历史版本 | | ReplicaSet | 期望副本数强制执行 | | ReplicationController | 传统 RC 支持 | | StatefulSet | 有序 pod 管理、稳定的网络 ID | | DaemonSet | 每节点 pod 调度 | | Job | 运行至完成的工作负载、索引完成 | | CronJob | 定时任务创建 | | Endpoints | 根据 pod selectors 维护 service endpoint | | EndpointSlice | 可扩展的 endpoint 切片 | | Service | ClusterIP 分配、service 生命周期 | | ServiceAccount | 默认 SA 创建、token 管理 | | Namespace | 终结处理、资源清理 | | Node | Node 状态、心跳监控 | | PV Binder | PersistentVolume 与 PVC 绑定 | | Dynamic Provisioner | 根据 StorageClasses 自动创建 PV | | Volume Snapshot | 快照生命周期管理 | | Volume Expansion | 在线 PVC 调整大小 | | ResourceQuota | Namespace 资源使用情况追踪 | | ResourceClaim | 动态资源分配 | | HPA | Horizontal Pod Autoscaler | | VPA | Vertical Pod Autoscaler | | PDB | Pod Disruption Budget 强制执行 | | LoadBalancer | 外部 LB 供应 (cloud + MetalLB) | | Ingress | Ingress 资源管理 | | NetworkPolicy | 网络策略生命周期 | | CRD | 自定义资源 schema 校验 | | CSR | 证书签名请求 | | Garbage Collector | Owner reference 级联删除 | | TTL Controller | 已完成资源清理 | | Taint Eviction | 驱逐受污染节点上的 pod | | Events | 事件记录和 TTL 清理 | ### Kubelet 通过 [bollard](https://github.com/fussybeaver/bollard) (Docker API) 进行容器运行时集成: - Pod 生命周期:创建、启动、停止、带有 grace period 的重启 - Pause 容器网络 namespace 共享 - Liveness、readiness 和 startup 探针 (HTTP、TCP、exec) - Volume 挂载:emptyDir、hostPath、projected、configMap、secret、downwardAPI - 容器资源限制 (CPU、内存) - Init 容器和 sidecar 容器 - 生命周期钩子 (preStop、postStart) — exec 和 httpGet - 容器日志检索 - 通过 WebSocket 实现 pod exec 和 attach - Sysctls、fsGroup、IPC namespace 共享 ### Kube-Proxy 在 host 网络模式下基于 iptables 的 service 路由: - ClusterIP、NodePort、LoadBalancer service 类型 - 会话亲和性 (ClientIP) - Endpoints 和 EndpointSlice 消费 - Service CIDR 路由 ### 存储 带有 `Storage` trait 的可插拔存储后端: - **etcd 后端** —— 用于生产环境,支持乐观并发(通过 mod_revision 进行 CAS) - **通过 rhino 实现的 SQLite** —— 轻量级替代方案,无需 etcd 集群。可作为 gRPC server (`docker-compose.sqlite.yml`) 使用,也可内嵌于进程内(all-in-one 二进制文件) - **内存后端** —— 用于单元测试 - Key schema:`/registry/{resource_type}/{namespace}/{name}` 有关部署模式的完整详细信息,请参阅[存储后端](docs/storage/STORAGE_BACKENDS.md)。 ## 一致性 Rusternetes 正在使用 [Sonobuoy](https://sonobuoy.io/) 积极针对官方 Kubernetes v1.35 一致性测试套件进行测试。 | 轮次 | 通过 | 总数 | 通过率 | 备注 | |-------|------|-------|------|-------| | 97 | ~40 | 441 | ~9% | 基准线 | | 101 | 245 | 441 | 56% | 部署了 76 个修复 | | 141 | 368 | 441 | 83% | Watch + 存储修复 | | 146 | 379 | 441 | 86% | CRD + scheduler 修复 | | 155 | 403 | 441 | 91.4% | 此前最高分 | | 159 | 410 | 441 | 93.0% | 此前最高分 | | 160 | 415 | 441 | 94.1% | 最新完整运行结果 | ``` # 运行 conformance tests bash scripts/run-conformance.sh # 监控进度 bash scripts/conformance-progress.sh ``` ## 项目结构 ``` crates/ api-server/ Axum HTTPS API (76 handler modules, 2500-line router) controller-manager/ 31 reconciliation controllers scheduler/ Filter/score plugin scheduling kubelet/ Container runtime, probes, volumes kube-proxy/ iptables service routing storage/ Pluggable storage: etcd, SQLite (rhino), memory common/ Shared types (36 resource modules), errors, utilities kubectl/ CLI tool cloud-providers/ AWS, GCP, Azure integrations rusternetes/ All-in-one binary (all components as tokio tasks) scripts/ bootstrap-cluster.sh Bootstrap CoreDNS, services, SA tokens run-conformance.sh Full conformance test lifecycle conformance-progress.sh Monitor pass/fail progress generate-certs.sh TLS certificate generation docs/ Architecture, guides, conformance tracking ``` ## 开发 ``` cargo build # Debug build cargo test # All workspace tests cargo test -p rusternetes-api-server # Single crate cargo clippy --all-targets --all-features -- -D warnings make pre-commit # Format + clippy + test ``` 请参阅 [DEVELOPMENT.md](docs/DEVELOPMENT.md) 获取完整指南,并参阅 [CONTRIBUTING.md](docs/CONTRIBUTING.md) 了解贡献准则。 ## 文档 **[完整文档站点](docs/guide/index.html)** —— 包含 30 页,涵盖所有功能、配置选项和用例。 | 主题 | 链接 | |-------|------| | 快速开始 | [快速开始](docs/guide/quickstart.html) | | 部署模式 | [部署概览](docs/guide/deployment.html) | | All-in-One 二进制文件 | [All-in-One](docs/guide/all-in-one.html) | | 配置 | [API Server](docs/guide/api-server-config.html) / [Kubelet](docs/guide/kubelet-config.html) / [存储](docs/guide/storage-config.html) | | 功能 | [工作负载](docs/guide/workloads.html) / [网络](docs/guide/networking.html) / [安全](docs/guide/security.html) / [CRD](docs/guide/crds.html) | | Web Console | [Console](docs/guide/console.html) / [CONSOLE.md](docs/CONSOLE.md) | | 身份验证 | [身份验证](docs/guide/authentication.html) / [AUTHENTICATION.md](docs/AUTHENTICATION.md) | | kubectl | [kubectl 参考](docs/guide/kubectl.html) | | API 参考 | [API 参考](docs/guide/api-reference.html) | | 一致性 | [一致性状态](docs/guide/conformance.html) | | 开发 | [DEVELOPMENT.md](docs/DEVELOPMENT.md) | ## License Apache-2.0
标签:Rust, 可视化界面, 容器编排, 提示注入, 搜索引擎查询, 系统底层, 网络流量审计, 请求拦截, 通知系统, 集群管理