kwok

KWOK 模拟 Kubernetes 集群

补充说明

KWOK(Kubernetes WithOut Kubelet)用模拟节点和 Pod 快速搭建轻量 Kubernetes 集群,不需要真实容器运行时。它适合测试调度器、控制器、Operator 和大规模集群行为,能秒级创建成百上千个节点。

核心资源:Cluster、Node、Pod、Stage 和 Metric。KWOK 控制器维护模拟对象状态,可以和真实 API Server 配合使用。

语法

安装 CLI:

go install sigs.k8s.io/kwok/cmd/kwok@latest
go install sigs.k8s.io/kwok/cmd/kwokctl@latest
kwokctl create cluster --name demo
kubectl get nodes

在已有集群安装 KWOK 控制器:

kubectl apply -f https://github.com/kubernetes-sigs/kwok/releases/latest/download/kwok.yaml
kubectl -n kube-system get pods -l app=kwok-controller

创建模拟节点:

kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Node
metadata:
  name: fake-node-1
  annotations:
    kwok.x-k8s.io/node: fake
  labels:
    kubernetes.io/hostname: fake-node-1
    kubernetes.io/arch: amd64
    kubernetes.io/os: linux
status:
  allocatable:
    cpu: "32"
    memory: 128Gi
    pods: "110"
  capacity:
    cpu: "32"
    memory: 128Gi
    pods: "110"
  conditions:
  - type: Ready
    status: "True"
EOF
kubectl get nodes

创建 Pod 观察模拟调度:

kubectl run demo --image=nginx --overrides='{"spec":{"nodeName":"fake-node-1"}}'
kubectl get pods -o wide

使用 Stage 控制 Pod 状态:

kubectl apply -f - <<'EOF'
apiVersion: kwok.x-k8s.io/v1alpha1
kind: Stage
metadata:
  name: pod-running
spec:
  resourceRef:
    apiGroup: v1
    kind: Pod
  selector:
    matchLabels:
      app: demo
  delay:
    durationMilliseconds: 500
  next:
    statusTemplate: |
      phase: Running
EOF

注意

相关命令

参考链接