You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Kubernetes扩展Pod仅运行在指定单个节点?

Kubernetes节点调度配置方案

完全可以配置Pod仅运行在单个指定节点,同时也能精准将你的应用部署到节点2、3上,核心是通过Kubernetes的节点标签、选择器或亲和性规则实现调度控制,以下是具体实现步骤:

第一步:给节点打标签

先给集群内的节点打上对应标签,方便后续Pod选择:

# 给节点1标记为系统专用节点
kubectl label nodes <节点1名称> node-role=system
# 给节点2、3标记为应用运行节点
kubectl label nodes <节点2名称> node-role=app
kubectl label nodes <节点3名称> node-role=app

场景1:让Pod仅运行在单个指定节点

如果需要某个应用的Pod只部署到特定节点(比如节点2),有两种常用方式:

方式1:使用nodeSelector(简单直接)

在Deployment或Pod的YAML配置中添加nodeSelector字段,指定目标节点的主机名:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: single-node-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: single-node
  template:
    metadata:
      labels:
        app: single-node
    spec:
      nodeSelector:
        kubernetes.io/hostname: <节点2的主机名> # 替换为节点2实际主机名
      containers:
      - name: my-app
        image: your-app-image:latest

方式2:使用节点亲和性(灵活支持复杂规则)

如果需要软约束(比如优先调度到某节点,不可行时再选其他)或更复杂的匹配逻辑,使用nodeAffinity:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: single-node-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: single-node
  template:
    metadata:
      labels:
        app: single-node
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution: # 硬约束,必须满足才能调度
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - <节点2的主机名>
      containers:
      - name: my-app
        image: your-app-image:latest

场景2:将应用部署到节点2和节点3

利用之前打的node-role=app标签,通过nodeSelector让Pod仅调度到这两个节点:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: <目标副本数>
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      nodeSelector:
        node-role: app
      containers:
      - name: my-app
        image: your-app-image:latest

额外优化:确保节点1仅运行系统Pod

如果要严格让节点1只跑Kubernetes系统组件(如kube-proxy、coredns),可以给节点1添加污点,拒绝普通应用Pod调度:

kubectl taint nodes <节点1名称> node-role=system:NoSchedule

系统组件默认带有对应容忍规则,不会被影响,而你的应用Pod因无容忍配置,将无法调度到节点1。

内容的提问来源于stack exchange,提问作者MrTux01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 23:05:19