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:

相关推荐
海鸥-w2 小时前
什么是AOP
java
Cc.Y3 小时前
Java零基础入门:可变字符串与包装类:StringBuilder、StringBuffer 与 通讯录管理系统实战
java·开发语言·python
paopaokaka_luck3 小时前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis
sm_926787053 小时前
RFID 标签打印的技术实现要点与二次开发实践
java·大数据·前端·c++·编辑器
阿森_dev3 小时前
2027 届毕设选题怎么选:30 个 Java 方向选题的实现难度排序
java·开发语言·spring boot·毕业设计·课程设计
m0_646429973 小时前
Docker 完整核心知识点总结(超通俗易懂版)
运维·docker·容器
Sarvartha3 小时前
Lambda 表达式和函数式接口
java·开发语言
计算机毕业设计杰瑞3 小时前
【计算机毕设精品】基于SpringBoot的一体化教务与学业质量管理系统的设计与实现,附源码_增删改查_java项目_可完美运行部署_网站项目
java·spring boot·课程设计
colinu3 小时前
C++ 异步任务与线程生命周期:`std::async`、`future`、`thread`、`jthread` 与协作式退出
java·jvm·c++
蜗牛互联网4 小时前
Python + JSON Schema 实现工单结构化输出与本地复核
java·人工智能·后端