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:

相关推荐
爱笑的眼睛1121 小时前
超越剪枝与量化:下一代AI模型压缩工具的技术演进与实践
java·人工智能·python·ai
阿里云云原生1 天前
Android App 崩溃排查指南:阿里云 RUM 如何让你快速从告警到定位根因?
android·java
历程里程碑1 天前
C++ 9 stack_queue:数据结构的核心奥秘
java·开发语言·数据结构·c++·windows·笔记·算法
醇氧1 天前
【Windows】从守护到终结:解析一个 Java 服务的优雅停止脚本
java·开发语言·windows
努力发光的程序员1 天前
互联网大厂Java求职面试实录
java·jvm·线程池·多线程·hashmap·juc·arraylist
小鹿学程序1 天前
FileZilla连接到虚拟机
java·服务器·开发语言
Haooog1 天前
Docker面试题(不定时更新)
java·docker·面试
feathered-feathered1 天前
Redis基础知识+RDB+AOF(面试)
java·数据库·redis·分布式·后端·中间件·面试
毕设源码-赖学姐1 天前
【开题答辩全过程】以 高校排课系统的优化设计与实现为例,包含答辩的问题和答案
java·eclipse
q_19132846951 天前
基于SpringBoot2+Vue2的行业知识答题考试系统
java·vue.js·spring boot·mysql·毕业设计·计算机毕业设计·演示文稿