文章目录
- ❌报错信息
- 🔎排查过程
- ✅问题解决
❌报错信息
提取报错信息【 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"
🔎排查过程
根据报错信息,排查一下nginx-deployment
YAML文件。
- 原nginx-deployment.yaml文件(编写有误)
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata: name: nginx-deploynamespace: defaultlabels:chapter: first-app
spec: selector: matchLabels: app: nginxreplicas: 2template:metadata: labels: app: nginxspec: containers: - name: nginximage: nginx:1.7.9ports: - containerPort: 80
首先,使用YAML、YML在线编辑(校验)器校对一下此YAML文件格式是否正确。未发现异常。
其次,根据报错信息,定位到【unknown field “spec.selector.replicas”】【unknown field “spec.selector.template”】这两处的字段中的replicas
和template
这两个关键字。提示的大概意思是在spec.selector字段值里未找到这两个属性,属于未知属性。
通过运行kubectl explain deployment
查看其中字段属性位置包含关系情况。
[root@master ~]# kubectl explain deployment
KIND: Deployment
VERSION: apps/v1DESCRIPTION:Deployment enables declarative updates for Pods and ReplicaSets.FIELDS:apiVersion <string>APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadata <Object>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataspec <Object>Specification of the desired behavior of the Deployment.status <Object>Most recently observed status of the Deployment.
查看replicas层级关系kubectl explain deployment.spec
replicas
这一字段属于spec下一级。不应该在selector
这一字段的下级。
同理,template
这一字段也属于spec下一级,不应该在selector
这一字段的下级。
[root@master ~]# kubectl explain deployment.spec
KIND: Deployment
VERSION: apps/v1RESOURCE: spec <Object>DESCRIPTION:Specification of the desired behavior of the Deployment.DeploymentSpec is the specification of the desired behavior of theDeployment.FIELDS:minReadySeconds <integer>Minimum number of seconds for which a newly created pod should be readywithout 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 isconsidered to be failed. The deployment controller will continue to processfailed deployments and a condition with a ProgressDeadlineExceeded reasonwill be surfaced in the deployment status. Note that progress will not beestimated during the time a deployment is paused. Defaults to 600s.replicas <integer>Number of desired pods. This is a pointer to distinguish between explicitzero and not specified. Defaults to 1.revisionHistoryLimit <integer>The number of old ReplicaSets to retain to allow rollback. This is apointer to distinguish between explicit zero and not specified. Defaults to10.selector <Object> -required-Label selector for pods. Existing ReplicaSets whose pods are selected bythis will be the ones affected by this deployment. It must match the podtemplate'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.
🕹️修改完成后的nginx-deployment.yaml,如下:
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploynamespace: defaultlabels:chapter: first-app
spec:replicas: 2selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.7.9ports:- containerPort: 80
✅问题解决
重新运行该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