Less than 1 minute
apiVersion: v1
kind: Pod
metadata:
name: go-http-podaffinity
spec:
containers:
- name: go-http
image: leigg/hellok8s:v1
# nodeSelector: 如果和亲和性同时配置,则必须都满足
affinity:
# podAffinity 可以和 podAntiAffinity 同时存在
podAffinity: # pod亲和性
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- { key: app, operator: In, values: [ "go" ] }
topologyKey: kubernetes.io/os # 必须指定,它是你希望调度Pod的目标节点群共有的标签,亲和性规则也仅会在此节点群生效
# namespaces: ["dev","test"] 允许指定命名空间,不指定则是当前Pod所在的空间
# namespaceSelector: # 或者 使用标签筛选命令空间
# matchExpressions:
# - key:
# operator:
# matchLabels:
# - environment: production
# preferredDuringSchedulingIgnoredDuringExecution:
# - podAffinityTerm:
# topologyKey:
# weight:
podAntiAffinity: # pod反亲和性
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- { key: highcpu, operator: In, values: [ "true" ] }
topologyKey: kubernetes.io/hostname # 反亲和性中,此key的值固定为 kubernetes.io/hostname