• 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    问题报错说明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 中有新邮件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

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

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

      - key: app2
        operator: In
        values: 
        - myapp2
    topologyKey: kubernetes.io/hostname
    
    • 1
    • 2
    • 3
    • 4
    • 5
      - key: app2
        operator: In
        values: ["myapp2"]
    topologyKey: kubernetes.io/hostname
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    小目标检测QueryDet
    论文速览 MobiCom 2023 | NeRF2 : Neural Radio-Frequency Radiance Fields
    自动驾驶技术详解
    .m3u8.sqlite文件转mp4,m3u8.sqlite文件转视频工具(开源免费)
    Qtcreator中文显示乱码问题终于解决
    Nginx服务、Vite项目如何设置ws(websocket)代理?
    Linux dup和dup2
    10:00面试,10:06就出来了,问的问题有点变态。。。
    Ubuntu18.04切换Python版本
    【python】python进行debug操作
  • 原文地址:https://blog.csdn.net/qq_17576885/article/details/133988552