k8s创建pod-affinity亲和性时报错解决办法

1.如下报错

Error from server (BadRequest): error when creating
“pod-required-affinity-demo-2.yaml”: Pod in version “v1” cannot be
handled as a Pod: json: cannot unmarshal string into Go struct field
LabelSelectorRequirement.spec.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution.labelSelector.matchExpressions.values
of type []string

查看yaml文件

apiVersion: v1
kind: Pod
metadata:
  name: pod-second
  labels:
    app: backend
    tier: db
spec:
  containers:
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["sh","-c","sleep 3600"]
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app2
            operator: In
            values: myapp2
        topologyKey: kubernetes.io/hostname

问题报错说明spec.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution.labelSelector.matchExpressions.values这里格式错误,查看该字段用法

[root@master1 ~]# kubectl explain pod.spec.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution.labelSelector.matchExpressions
KIND:     Pod
VERSION:  v1

RESOURCE: matchExpressions <[]Object>

DESCRIPTION:
     matchExpressions is a list of label selector requirements. The requirements
     are ANDed.

     A label selector requirement is a selector that contains values, a key, and
     an operator that relates the key and values.

FIELDS:
   key	<string> -required-
     key is the label key that the selector applies to.

   operator	<string> -required-
     operator represents a key's relationship to a set of values. Valid
     operators are In, NotIn, Exists and DoesNotExist.

   values	<[]string>
     values is an array of string values. If the operator is In or NotIn, the
     values array must be non-empty. If the operator is Exists or DoesNotExist,
     the values array must be empty. This array is replaced during a strategic
     merge patch.

您在 /var/spool/mail/root 中有新邮件

values is an array of string values.
values是字符串值的数组

可以将values字段值改为下面两个形式都可以,测试都能创建pod成功running

  - key: app2
    operator: In
    values: 
    - myapp2
topologyKey: kubernetes.io/hostname
  - key: app2
    operator: In
    values: ["myapp2"]
topologyKey: kubernetes.io/hostname

你可能感兴趣的:(kubernetes,容器,云原生)