当前位置: 首页 > article >正文

K8S学习之基础六:k8s中pod亲和性

Pod节点亲和性和反亲和性

podaffinity:pod节点亲和性指的是pod会被调度到更趋近与哪个pod或哪类pod。

podunaffinity:pod节点反亲和性指的是pod会被调度到远离哪个pod或哪类pod

1.  Pod节点亲和性

requiredDuringSchedulingIgnoredDuringExecution: 硬亲和性

preferredDuringSchedulingIgnoredDuringExecution:软亲和性

vi pod-required-affinity-demo-1.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    tier: monitor
spec:
    containers:
    - name: nginx
      image: 172.16.80.140/nginx/nginx:1.26
      imagePullPolicy: IfNotPresent
kubectl apply -f pod-required-affinity-demo-1.yaml 
kubectl get pods -owide

vi pod-required-affinity-demo-2.yaml 
kind: Pod
metadata:
  name: busybox
  labels:
    app: backend
    tier: db
spec:
    containers:
    - name: busybox
      image: 172.16.80.140/busybox/busybox:latest
      imagePullPolicy: IfNotPresent
      command: ["sh","-c","sleep 3600"]
    affinity:
      podAffinity:
         requiredDuringSchedulingIgnoredDuringExecution:
         - labelSelector:
              matchExpressions:
              - {key: app, operator: In, values: ["nginx"]}
           topologyKey: kubernetes.io/hostname

以上可以看出,第二个pod(busybox)由于设置了硬亲和性,需要和具有app=nginx的标签的pod在一起,所以也被创 建到了node01上

Pod节点亲和性

vi pod-required-affinity-demo-3.yaml 
kind: Pod
metadata:
  name: busybox1
  labels:
    app: backend1
    tier: db
spec:
    containers:
    - name: busybox1
      image: 172.16.80.140/busybox/busybox:latest
      imagePullPolicy: IfNotPresent
      command: ["sh","-c","sleep 3600"]
    affinity:
      podAntiAffinity:
         requiredDuringSchedulingIgnoredDuringExecution:
         - labelSelector:
              matchExpressions:
              - {key: app, operator: In, values: ["nginx"]}
           topologyKey: kubernetes.io/hostname

以上可以看出,第三个pod(backend)设置了pod发亲和性,和匹配的pod创建在了不同的node上

vi pod-required-affinity-demo-4.yaml
apiVersion: v1
kind: Pod
metadata:
  name: busybox2
  labels:
    app: backend
    tier: db
spec:
    containers:
    - name: busybox2
      image: 172.16.80.140/busybox/busybox:latest
      imagePullPolicy: IfNotPresent
      command: ["sh","-c","sleep 3600"]
    affinity:
      podAntiAffinity:
         requiredDuringSchedulingIgnoredDuringExecution:
         - labelSelector:
              matchExpressions:
              - {key: app, operator: In, values: ["nginx"]}
           topologyKey: kubernetes.io/hostname
         - labelSelector:
              matchExpressions:
              - {key: tier, operator: In, values: ["db"]}
           topologyKey: kubernetes.io/hostname

以上可以看出,因为busybox2的反亲和性,导致没有pod符合条件,只能是pending状态


http://www.kler.cn/a/570607.html

相关文章:

  • 单例模式(线程案例)
  • 使用Java操作Excel
  • R语言基础| 基本图形绘制
  • 数据集/API 笔记:新加坡风速 API
  • Linux12-UDP\
  • 打造高清3D虚拟世界|零基础学习Unity HDRP高清渲染管线(第一天)
  • go语言逆向-符号恢复
  • iOS逆向工程概述与学习路线图
  • Mysql面试篇笔记:
  • 【Python】在Windows下配置Python最小环境并在React执行Python脚本
  • SpringCloud系列教程(十一):token验证
  • unidbg 实现 JNI 与 Java 交互
  • ffmpeg源码编译支持cuda
  • PyCharm 对接 DeepSeek 大模型的详细操作流程
  • ArcGIS操作:11 计算shp矢量面的质心坐标
  • 【JAVA】ThreadPoolTaskExecutor 线程池学习、后端异步、高并发处理
  • ArcGIS操作:08 计算shp面积并添加到属性表
  • 解释 CSS 盒模型的概念以及如何使用 box-sizing 属性
  • 厦大团队|报告:《读懂大模型概念、技术与应用实践》140 页 PPT(文末附链接下载)
  • 知识图谱的推荐实现方案(Vue)