在 Kubernetes 集群中限制 DaemonSet 测试部署的策略
·
background:需要在大规模集群中测试 DaemonSet 部署的监控服务,但只想在单个节点上运行测试 Pod
1. 使用节点选择器 (Node Selector)
原理:通过标签选择特定节点进行部署
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: monitoring-test
spec:
selector:
matchLabels:
app: monitoring
template:
metadata:
labels:
app: monitoring
spec:
nodeSelector:
testing-node: "true" # 只部署到带有此标签的节点
containers:
- name: monitoring-agent
image: your-monitoring-image:test
操作步骤:
-
为测试节点打标签:
kubectl label nodes <target-node-name> testing-node=true -
部署修改后的 DaemonSet
-
测试完成后删除标签即可停止 Pod:
kubectl label nodes <target-node-name> testing-node-
2. 使用节点亲和性 (Node Affinity)
优势:比 nodeSelector 更灵活的表达式
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: monitoring-test
spec:
template:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: testing-monitoring
operator: In
values: ["true"]
3. 使用污点和容忍度 (Taint and Toleration)
原理:只允许 Pod 部署到特定污点节点
-
给测试节点添加污点:
kubectl taint nodes <node-name> testing-monitoring=true:NoSchedule -
在 DaemonSet 中配置容忍度:
tolerations: - key: "testing-monitoring" operator: "Equal" value: "true" effect: "NoSchedule"
4. 临时修改 DaemonSet 为 Deployment
快速测试方案:
# 获取 DaemonSet 配置
kubectl get daemonset <daemonset-name> -o yaml > monitoring-test.yaml
# 修改为 Deployment
sed -i 's/DaemonSet/Deployment/g' monitoring-test.yaml
# 添加节点选择器
yq eval '.spec.template.spec.nodeSelector = {"testing-node": "true"}' -i monitoring-test.yaml
# 部署测试
kubectl apply -f monitoring-test.yaml
5. 使用暂停调度策略
安全方案:防止 DaemonSet 全局部署
-
先暂停 DaemonSet 控制器:
kubectl patch daemonset <name> -p '{"spec": {"template": {"spec": {"nodeSelector": {"non-existent": "true"}}}}}' -
创建测试 Pod 手动调度到目标节点:
kubectl run monitoring-test --image=your-image --overrides='{"spec": {"nodeSelector": {"testing-node": "true"}}}'
6. 使用 Canary 发布策略
渐进式方案:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: monitoring-canary
spec:
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1 # 每次只更新1个节点
然后通过节点选择控制首个部署节点。
建议方案
-
测试流程:
-
多环境策略:
- 开发环境:使用 Deployment 在单个节点测试
- 预发环境:使用带节点选择的 DaemonSet 在部分节点测试
- 生产环境:全量部署 DaemonSet
-
验证命令:
# 检查Pod部署情况 kubectl get pods -l app=monitoring -o wide # 检查节点标签 kubectl get nodes --show-labels # 查看调度事件 kubectl describe pod <pod-name> | grep -A10 Events -
清理方案:
# 删除测试DaemonSet kubectl delete daemonset monitoring-test # 移除节点标签 kubectl label nodes <node-name> testing-node-
选择哪种方案取决于您的具体需求:
- 最快测试:方案4(临时改为Deployment)
- 最接近生产:方案1或2(带节点选择的DaemonSet)
- 最安全:方案5(暂停调度+手动部署)
上述方法可以在不影响整个集群的情况下验证监控服务的功能和性能。
更多推荐



所有评论(0)