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:

相关推荐
步、步、为营4 分钟前
.NET 事件模式举例介绍
java·开发语言·.net
cui_hao_nan7 分钟前
设计模式——模板方法
java·设计模式
小吕学编程7 分钟前
HttpServletRequest常用方法
java·http
在未来等你8 分钟前
Java并发编程实战 Day 11:并发设计模式
java·设计模式·多线程·并发编程·threadlocal·生产者消费者·读写锁
藥瓿锻14 分钟前
2024 CKA题库+详尽解析| 15、备份还原Etcd
linux·运维·数据库·docker·容器·kubernetes·cka
李少兄20 分钟前
解决 idea提示`SQL dialect is not configured` 问题
java·sql·intellij-idea
BreezeDove26 分钟前
IDEA安装&迁移IDEA配置数据位置
java·ide·intellij-idea
太阳之神aboluo30 分钟前
压测软件-Jmeter
java·运维·jmeter
编程乐学(Arfan开发工程师)43 分钟前
42、响应处理-【源码分析】-浏览器与PostMan内容协商完全适配
java·spring boot·后端·测试工具·lua·postman
珹洺1 小时前
数据库系统概论(十七)超详细讲解数据库规范化与五大范式(从函数依赖到多值依赖,再到五大范式,附带例题,表格,知识图谱对比带你一步步掌握)
java·数据库·sql·安全·oracle