k8s的Pod亲和性

针对对象为Pod,目的是实现,新建Pod和目标Pod调度到一起,在同一个Node上。

示例:

bash 复制代码
root@k8s-master:/home/vagrant# cat pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: testpod01
  labels:
    app: myapp01
    env: test1
spec:
  containers:
  - name: testpod01
    image: nginx:1.23.2
    imagePullPolicy: IfNotPresent

root@k8s-master:/home/vagrant# cat pod2.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: testpod02
  labels:
    app: myapp02
    env: test2
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:  ##必须满足下面匹配规则
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In  
            values:
            - myapp01  ## app=myapp01, 上面的Pod是符合要求的
        topologyKey: "kubernetes.io/hostname"
  containers:
  - name: testpod02
    image: redis:6.2
    imagePullPolicy: IfNotPresent
root@k8s-master:/home/vagrant# kubectl create -f pod.yaml
root@k8s-master:/home/vagrant# kubectl create -f pod2.yaml 

root@k8s-master:/home/vagrant# kubectl get pods --show-labels -o wide
NAME            READY   STATUS    RESTARTS   AGE     IP              NODE          NOMINATED NODE   READINESS GATES   LABELS
node-affinity   1/1     Running   0          26m     172.16.126.2    k8s-worker2   <none>           <none>            <none>
testpod01       1/1     Running   0          8m15s   172.16.194.65   k8s-worker1   <none>           <none>            app=myapp01,env=test1
testpod02       1/1     Running   0          2m20s   172.16.194.66   k8s-worker1   <none>           <none>            app=myapp02,env=test2
相关推荐
C_心欲无痕1 天前
ts - tsconfig.json配置讲解
linux·前端·ubuntu·typescript·json
冰西瓜6001 天前
国科大2025操作系统高级教程期末回忆版
linux
HIT_Weston1 天前
93、【Ubuntu】【Hugo】搭建私人博客:面包屑(一)
linux·运维·ubuntu
唯情于酒1 天前
Docker学习
学习·docker·容器
cuijiecheng20181 天前
Linux下Beyond Compare过期
linux·运维·服务器
喵叔哟1 天前
20.部署与运维
运维·docker·容器·.net
HIT_Weston1 天前
92、【Ubuntu】【Hugo】搭建私人博客:侧边导航栏(六)
linux·运维·ubuntu
CodeAllen嵌入式1 天前
Windows 11 本地安装 WSL 支持 Ubuntu 24.04 完整指南
linux·运维·ubuntu
码农小韩1 天前
基于Linux的C++学习——指针
linux·开发语言·c++·学习·算法
wdfk_prog1 天前
[Linux]学习笔记系列 -- [fs]seq_file
linux·笔记·学习