k8s的Pod常见的几种调度形式

1 常见的Pod调度方式:

1 自由调度: 默认的kube-scheduler调度

2 定向调度:nodeName, nodeSelector实现

3 亲和性调度:nodeAffinity, PodAffinity, PodAntiAffinity实现

4 污点和容忍调度:taint,tolerations实现

2 实践

2.1 定向调度

nodeName, NodeSelector:

1 首先node结点打标签,disk=ssd

结点打标签

kubectl label nodes k8s-node1 disk=ssd

查看结点标签

kubectl get nodes --show-labels

2 pod模板文件设定NodeSelector的值: disk: ssd

apiVersion: apps/v1

kind: Deployment

metadata:

name: testnginx-deployment

spec:

selector:

matchLabels:

app: testnginx

replicas: 3

template:

metadata:

labels:

app: testnginx

spec:

containers:

  • name: testnginx

image: nginx:latest

ports:

  • containerPort: 80

nodeSelector:

disk: ssd

3 预定义标签

kubernetes.io/hostname

2.2 亲和性调度

2.2.1 nodeAffinity

pod与node关系

RequiredDuringSchedulingIgnoredDuringExecution: 必须满足指定规则才能调度pod到Node上

PreferredDuringSchedulingIgnoredDuringExecution:强调优先满足指定规则,软限制

apiVersion: apps/v1

kind: Deployment

metadata:

name: testnginx-deployment

spec:

selector:

matchLabels:

app: testnginx

replicas: 3

template:

metadata:

labels:

app: testnginx

spec:

必须满足指定规则 硬限制

复制代码
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: disk
            operator: In
            values:
            - ssd

尽量优先满足指定规则 软限制

复制代码
    affinity:
      nodeAffinity:
        preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 1
          preference:
            matchExpressions:
            - key: disk
              operator: In
              values:
              - ssd
  containers:
  - name: testnginx
    image: nginx:latest
    ports:
    - containerPort: 80

2.2.2 PodAffinity

pod与pod的关系

硬,软亲和性

requiredDuringSchedulingIgnoredDuringExecution 硬性要求,必须要满足条件,保证部署在一起

spec:

affinity:

podAffinity:

requiredDuringSchedulingIgnoredDuringExecution:

  • labelSelector:

matchExpressions:

-key: app

operator: In

values:

  • testnginx

topologyKey: "kubernetes.io/hostname"

preferredDuringSchedulingIgnoredDuringExecution 软性要求,尽量满足条件,保证部署在一起

affinity:

podAffinity:

preferredDuringSchedulingIgnoredDuringExecution:

  • weight: 60

podAffinityTerm:

labelSelector:

matchExpressions:

  • {key: app, operator: In, values: ["testnginx"]}

topologyKey: kubernetes.io/hostname

  • weight: 30

podAffinityTerm:

labelSelector:

matchExpressions:

  • {key: app, operator: In, values: ["backend"]}

topologyKey: kubernetes.io/hostname

2.2.2 PodAntiAffinity

pod与pod的关系

硬,软反亲和性

requiredDuringSchedulingIgnoredDuringExecution 硬性要求,必须满足条件,保证分散部署的最佳实践

#如果节点上的pod标签存在满足app=nginx,则不能部署到节点上

spec:

affinity:

podAntiAffinity:

requiredDuringSchedulingIgnoredDuringExecution:

  • labelSelector:

matchExpressions:

  • key: app

operator: In

values:

  • nginx

topologyKey: "kubernetes.io/hostname"

preferredDuringSchedulingIgnoredDuringExecution 软性要求,可以不完全满足,可同一node跑多个副本

软性要求

如果节点上的pod标签存在满足app=nginx,也可以部署到节点上,尽可能先部署到其它节点,如果没有满足也可以部署到此节点(大概是这么理解吧)

复制代码
spec:
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
        - labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - nginx
          topologyKey: "kubernetes.io/hostname"

2.3 污点容忍调度

污点(Taint) 是应用在节点之上的,为了排斥pod 所存在的

容忍度(Toleration)是应用于 Pod 上的,允许(但并不要求)Pod 调度到带有与之匹配的污点的节点上。

结点亲和性,是pod的一种属性(偏好或硬件要求),它使pod被吸引到一类特点的结点

Taint则相反,它使结点能够排斥一类特点的Pod,Taint和Toleration相互配合使用

2.3.1 污点 taint

设置污点

kubectl taint nodes node1 xtz=value1:NoSchedule

去除污点

kubectl taint nodes node1 xtz:NoSchedule-

#节点说明中,查找 Taints 字段

kubectl describe node node-name

三种调度策略:

PreferNoSchedule(软策略,尽量不调度到污点结点)

NoExecute(不会将 Pod 调度到具有该污点的 Node 上,同时会将 Node 上已经存在的 Pod驱逐出去)

NoSchedule(不会将Pod调度到具有该污点的Node上)

2.3.2 容忍度tolerations

spec:

containers:

相关推荐
云上小朱几秒前
问题处理-k8s环境中,hadoop端口9000无法被访问
kubernetes
?ccc?6 分钟前
容器技术技术入门与 Docker 环境部署
运维·docker·容器
找不到、了10 分钟前
分布式理论:CAP、Base理论
java·分布式
天天摸鱼的java工程师13 分钟前
2025已过半,Java就业大环境究竟咋样了?
java·后端
时时刻刻看着自己的心15 分钟前
docker启动报错
运维·docker·容器
人生在勤,不索何获-白大侠18 分钟前
day16——Java集合进阶(Collection、List、Set)
java·开发语言
Zedthm24 分钟前
LeetCode1004. 最大连续1的个数 III
java·算法·leetcode
艺杯羹35 分钟前
MyBatis之核心对象与工作流程及SqlSession操作
java·mybatis
神的孩子都在歌唱43 分钟前
3423. 循环数组中相邻元素的最大差值 — day97
java·数据结构·算法
容器魔方1 小时前
开源之夏2025 | Karmada 社区中选学生名单公布!
云原生·容器·云计算