Skip to main content

僭越了,有人在用 Rust 写 Kubernetes

Published 3 min readMarkdown

一个新语言问世,最爱做的事情之一,就是重写存量软件了。 云原生喝酒 SIG 重点扶持项目——rk8s(https://github.com/rk8s-dev/rk8s) 也可以归在这个范畴里,只不过这个项目重写的东西比较大,是 Kubernetes。

从 2025 年 1 月第一个 Commit 开始,到现在有了 200 多次 Commit,十几万行代码。当然距离 Kubernetes 的几百万行代码还差得远——老马就是喜欢整这种大无畏项目。

另外该项目也是国内第一个脱离 Cargo 转向使用 Buck2 做构建的项目。

那么这些代码都做了什么呢?看 project 目录中,我们可以看到除了网络、存储相关内容之外,三个醒目成员:

  • rkb:容器镜像构建工具

  • rkl:容器运行时

  • rks:说自己是个 RKS 守护进程 CLI

构建

README.md 中推荐的是直接使用 Cargo 进行构建:

$ cd rk8s/project/
$ cargo build -p rkl
$ cargo build -p rks
...

这中间需要一些环境支持,当然也可以用 ./.devcontainer 中的 Dockerfile 生成一个工作镜像完成构建工作。

RKL

看看他的帮助输出:

$  project/target/debug/rkl --help
A simple container runtime

Usage: rkl <workload> <command> [OPTIONS]

Commands:
  pod        Operations related to pods
  container  Manage standalone containers
  compose    Manage multi-container apps using compose
  help       Print this message or the help of the given subcommand(s)

Options:
  -h, --help  Print help

可以看到,它能支持 容器、Pod 和 Compose 三种工作负载的运行。

容器

编写下面的 single.yaml 用于运行容器:

name: single-container-test
# 注意,image 字段的路径
image: ./rk8s/project/test/bundles/busybox
ports:
  - containerPort: 80
    protocol: ""
    hostPort: 0
    hostIP: ""
args:
  - sleep
  - "100"
resources:
  limits:
    cpu: 500m
    memory: 233Mi

运行一下看看:

$ sudo project/target/debug/rkl container run single.yaml
Container: single-container-test runs successfully!
$  sudo project/target/debug/rkl container list
ID                                     PID     STATUS   BUNDLE                                           CREATED                    CREATOR
single-container-test                  156245  Running  /rk8s/project/test/bundles/busybox  2025-09-27T20:28:34+08:00  root
$  sudo project/target/debug/rkl container exec single-container-test /bin/ls
bin    dev    etc    lib    lib64  proc   sys    usr

Pod

RKL 也支持类似 Podman 或者 Kubelet 单机运行 Pod 的能力,例如如下的 pod.yaml:

# 同样也要注意 Bundle 和 Image 两个字段的路径
apiVersion: v1
kind: Pod
metadata:
  name: simple-container-task  
  labels:
    app: my-app 
    bundle: ./rk8s/project/test/bundles/pause
spec:
  containers:
    - name: main-container1    
      image: ./rk8s/project/test/bundles/busybox
      args:             
        - "dd"                   
        - "if=/dev/zero"  
        - "of=/dev/null"          
      ports:
        - containerPort: 80
      resources:
        limits:
          cpu: "500m"
          memory: "512Mi"
status:

上面代码中包含了对 Pause 容器的引用,也有 main-container1 这个“业务”容器,运行过程跟单容器类似:

$ sudo project/target/debug/rkl pod run pod.yaml
$  sudo project/target/debug/rkl pod state simple-container-task
Pod: simple-container-task
PodSandbox ID: simple-container-task
{
  "ociVersion": "v1.0.2",
  "id": "simple-container-task",
  "status": "running",
  "pid": 165045,
  "bundle": "/home/dustise/rk8s/project/test/bundles/pause",
  "annotations": {},
  "created": "2025-09-27T12:52:58.258522466Z",
  "creator": 0,
  "useSystemd": false,
  "cleanUpIntelRdtSubdirectory": false
}
Containers:
{
  "ociVersion": "v1.0.2",
  "id": "simple-container-task-main-container1",
  "status": "running",
  "pid": 165061,
  "bundle": "/home/dustise/rk8s/project/test/bundles/busybox",
  "annotations": {},
  "created": "2025-09-27T12:52:58.400974102Z",
  "creator": 0,
  "useSystemd": false,
  "cleanUpIntelRdtSubdirectory": false
}
$  sudo project/target/debug/rkl pod  exec simple-container-task simple-container-task-main-container1 /bin/sh
/bin/sh: can't access tty; job control turned off
/ # ls
bin    dev    etc    lib    lib64  proc   sys    usr

Compose

写这个功能好像劳动力过剩的样子。

RKS

  • RKS 提供了支持调度和状态管理的控制面,类似 K8s 的 Master 三大件。

  • RKL 则提供了容器的运行时支持,类似 Node Worker。

  • 使用兼容 ETCD 的 Xline 提供数据库支持。

  • 组件之间的通信使用的是基于 QUIC 的协议。

首先启动 Xline:

$ git clone https://github.com/xline-kv/Xline.git
$ cd Xline
$ docker pull ghcr.io/xline-kv/xline:latest
$ cp fixtures/{private,public}.pem scripts/
$ ./scripts/quick_start.sh
...
7b41380e4c4e60d5531321789987a35b4e0368b1aa3948efe8e3f2a0671301ed
Prometheus starts on http://172.20.0.6:9090/graph and http://127.0.0.1:9090/graph (if you are using Docker Desktop).

Xline 启动之后,编写如下 config.yaml 用于启动服务:

addr: "127.0.0.1:50051"

xline_config:

  endpoints:

    - "http://172.20.0.3:2379"

    - "http://172.20.0.4:2379"

    - "http://172.20.0.5:2379"

  prefix: "/coreos.com/network"

  subnet_lease_renew_margin: 60

network_config:

  Network: "10.1.0.0/16"

  SubnetMin: "10.1.1.0"

  SubnetMax: "10.1.254.0"

  SubnetLen: 24

启动服务:

$ sudo project/target/debug/rks start --config config.yaml
[rks] listening on 127.0.0.1:50051

在集群上创建一个 Pod:

$ sudo project/target/debug/rkl pod create pod.yaml --cluster 127.0.0.1:50051
RKL connected to RKS at 127.0.0.1:50051
pod simple-container-task created
$ sudo project/target/debug/rkl pod list --cluster 127.0.0.1:50051
RKL connected to RKS at 127.0.0.1:50051
NAME  READY  STATUS  RESTARTS  AGE
simple-container-task

类似前面的本地 Pod,只不过这次是在 RKS 集群中运行了。

后记

项目 README 有些滞后,并没有展示跨节点调度和 Service 通信的能力,期待后续的更新。

另外,RKL 的用法也略微有些迷惑,跟 kubectl 和 podman 都不同,也只能期待文档的进一步细化了。

No comments yet. Be the first to comment.

More from this blog