前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【K8S】集群中部署nginx应用 运行手写yaml文件报错排查过程

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

作者头像
宝耶需努力
发布2023-10-19 18:56:07
6420
发布2023-10-19 18:56:07
举报
文章被收录于专栏:Cloud-DIYCloud-DIY

❌报错信息

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

代码语言:javascript
复制
[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"
image-20231013140232174
image-20231013140232174

🔎排查过程

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

  • 原nginx-deployment.yaml文件(编写有误)
代码语言:javascript
复制
# 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

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

image-20231013142058090
image-20231013142058090

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

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

代码语言:javascript
复制
[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.

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

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

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

代码语言:javascript
复制
[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.

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

代码语言:javascript
复制
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

✅问题解决

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

代码语言:javascript
复制
[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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ❌报错信息
  • 🔎排查过程
  • ✅问题解决
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档