我正在尝试为我的部署副本创建一个 podAffinity,以便部署在我的 GCP 集群内的不同节点中,我已经按照本站点 https://thenewstack.io/implement-node-and-pod-affinity-anti-affinity-in-kubernetes-a-practical-example/ 的文档进行操作,但是在执行 apply 时出现此错误:< /p>
error: error validating ".\\K8s\\6flask-deployment.yaml": error validating data: ValidationError(Deployment.spec.template.spec.affinity.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution[0].labelSelector): unknown field "MatchExpressions" in io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector;
这是我的flask-deployment.yaml。我试图将它也设置在第一个规范之下,但它也不起作用
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-deployment
namespace: flask-app
spec:
replicas: 2
selector:
matchLabels:
app: flask-app
deploy: separate
run: together
template:
metadata:
labels:
app: flask-app
deploy: separate
run: together
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
MatchExpressions:
- key: deploy
operator: In
values:
- separate
topologyKey: kubernetes.io/hostname
containers:
是识别错误吗? kubernetes linter 没有标记任何东西,我一直在与 SO 中和网络上的其他示例进行比较,但我找不到错误
答案 0 :(得分:1)
您在 matchExpressions
中指定的任何键值还需要位于 pod labels
的 matchLabels
和 spec
而不是 metadata
部分。它应该如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
namespace: flask-app
spec:
replicas: 2
selector:
matchLabels:
deploy: separate
template:
metadata:
labels:
deploy: separate
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: deploy
operator: In
values:
- separate
topologyKey: "kubernetes.io/hostname"