• 【K8S】集群中部署nginx应用 运行手写yaml文件报错排查过程


    ❌报错信息

    提取报错信息【 unknown field “spec.selector.replicas”】【 unknown field “spec.selector.template”

    [root@master ~]# kubectl apply -f nginx-deployment.yaml
    Error from server (BadRequest): error when creating "nginx-deployment.yaml": Deployment in version "v1" cannot be handled as a Deployment: strict decoding error: unknown field "spec.selector.replicas", unknown field "spec.selector.template"
    
    • 1
    • 2

    image-20231013140232174

    🔎排查过程

    根据报错信息,排查一下nginx-deployment YAML文件。

    • 原nginx-deployment.yaml文件(编写有误)
    # nginx-deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata: 
      name: nginx-deploy
      namespace: default
      labels:
        chapter: first-app
    spec: 
      selector: 
        matchLabels: 
          app: nginx
        replicas: 2
        template:
          metadata: 
            labels: 
              app: nginx
          spec: 
            containers: 
              - name: nginx
                image: nginx:1.7.9
                ports: 
                  - containerPort: 80
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    首先,使用YAML、YML在线编辑(校验)器校对一下此YAML文件格式是否正确。未发现异常。

    image-20231013142058090

    其次,根据报错信息,定位到【unknown field “spec.selector.replicas”】【unknown field “spec.selector.template”】这两处的字段中的replicastemplate这两个关键字。提示的大概意思是在spec.selector字段值里未找到这两个属性,属于未知属性。

    通过运行kubectl explain deployment 查看其中字段属性位置包含关系情况。

    [root@master ~]# kubectl explain deployment
    KIND:     Deployment
    VERSION:  apps/v1
    
    DESCRIPTION:
         Deployment enables declarative updates for Pods and ReplicaSets.
    
    FIELDS:
       apiVersion   <string>
         APIVersion defines the versioned schema of this representation of an
         object. Servers should convert recognized schemas to the latest internal
         value, and may reject unrecognized values. More info:
         https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
    
       kind <string>
         Kind is a string value representing the REST resource this object
         represents. Servers may infer this from the endpoint the client submits
         requests to. Cannot be updated. In CamelCase. More info:
         https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
    
       metadata     <Object>
         Standard object's metadata. More info:
         https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
    
       spec <Object>
         Specification of the desired behavior of the Deployment.
    
       status       <Object>
         Most recently observed status of the Deployment.
    
    • 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
    • 29

    查看replicas层级关系kubectl explain deployment.spec

    replicas这一字段属于spec下一级。不应该在selector这一字段的下级。

    同理,template这一字段也属于spec下一级,不应该在selector这一字段的下级。

    [root@master ~]# kubectl explain deployment.spec
    KIND:     Deployment
    VERSION:  apps/v1
    
    RESOURCE: spec <Object>
    
    DESCRIPTION:
         Specification of the desired behavior of the Deployment.
    
         DeploymentSpec is the specification of the desired behavior of the
         Deployment.
    
    FIELDS:
       minReadySeconds      <integer>
         Minimum number of seconds for which a newly created pod should be ready
         without any of its container crashing, for it to be considered available.
         Defaults to 0 (pod will be considered available as soon as it is ready)
    
       paused       <boolean>
         Indicates that the deployment is paused.
    
       progressDeadlineSeconds      <integer>
         The maximum time in seconds for a deployment to make progress before it is
         considered to be failed. The deployment controller will continue to process
         failed deployments and a condition with a ProgressDeadlineExceeded reason
         will be surfaced in the deployment status. Note that progress will not be
         estimated during the time a deployment is paused. Defaults to 600s.
    
       replicas     <integer>
         Number of desired pods. This is a pointer to distinguish between explicit
         zero and not specified. Defaults to 1.
    
       revisionHistoryLimit <integer>
         The number of old ReplicaSets to retain to allow rollback. This is a
         pointer to distinguish between explicit zero and not specified. Defaults to
         10.
    
       selector     <Object> -required-
         Label selector for pods. Existing ReplicaSets whose pods are selected by
         this will be the ones affected by this deployment. It must match the pod
         template's labels.
    
       strategy     <Object>
         The deployment strategy to use to replace existing pods with new ones.
    
       template     <Object> -required-
         Template describes the pods that will be created.
    
    • 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
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    🕹️修改完成后的nginx-deployment.yaml,如下:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deploy
      namespace: default
      labels:
        chapter: first-app
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
            - name: nginx
              image: nginx:1.7.9
              ports:
                - containerPort: 80
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    ✅问题解决

    重新运行该YAML文件,运行成功🎇。

    [root@master ~]# kubectl apply -f nginx-deployment.yaml
    deployment.apps/nginx-deploy created
    [root@master ~]# kubectl get deployment
    NAME           READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-deploy   0/2     2            0           26s
    [root@master ~]# kubectl get pods
    NAME                            READY   STATUS    RESTARTS   AGE
    nginx-deploy-7759cfdc55-q4622   1/1     Running   0          33s
    nginx-deploy-7759cfdc55-skcgp   1/1     Running   0          33s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    🎇🎇🎇完结🎉🎉🎉
  • 相关阅读:
    netty系列之:HashedWheelTimer一种定时器的高效实现
    数据结构与算法【堆】的Java实现
    C++练习题。。。
    【多线程案例】单例模式
    【CFD小工坊】模型网格(三角形网格)
    Android Native 内存泄漏系统化解决方案
    Torch 数据集放到网络训练(六)
    怎样更改linux的用户名
    人工智能第2版学习——博弈中的搜索1
    上手Python之set(集合)
  • 原文地址:https://blog.csdn.net/qq_45392321/article/details/133813813