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
status.allocatable 和 conditions,否则调度器不会调度。kwokctl create cluster — 创建模拟集群kwokctl delete cluster — 删除模拟集群kubectl get nodes — 查看模拟节点kubectl get pods -o wide — 查看调度结果kubectl get stages — 查看状态规则