在JSON中允许多个K8s资源

jgwigjjp  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(105)

我有一个YAML文件,其中包含多个资源部署和服务。

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: petclinic
  name: petclinic
spec:
  replicas: 1
  selector:
    matchLabels:
      app: petclinic
  strategy: {}
  template:
    metadata:
      labels:
        app: petclinic
    spec:
      containers:
        - image: arey/springboot-petclinic
          name: springboot-petclinic
          resources: {}
          ports:
            - containerPort: 8080
status: {}
---
apiVersion: v1
kind: Service
metadata:
  name: petclinic
spec:
  type: LoadBalancer
  selector:
    app: petclinic
  ports:
    - port: 8080
      targetPort: 8080
      protocol: TCP
      name: http

我在JSON中创建了一个类似的文件。我能够使用JSON创建部署资源并成功部署,但当我尝试包含服务资源时,它失败了。如果能帮助我在哪里出错,我将不胜感激。下面是JSON的代码片段。

{
   "apiVersion": "apps/v1",
   "kind": "Deployment",
   "metadata": {
      "creationTimestamp": null,
      "labels": {
         "app": "petclinic"
      },
      "name": "petclinic"
   },
   "spec": {
      "replicas": 2,
      "selector": {
         "matchLabels": {
            "app": "petclinic"
         }
      },
      "strategy": {},
      "template": {
         "metadata": {
            "labels": {
               "app": "petclinic"
            }
         },
         "spec": {
            "containers": [
               {
                  "image": "arey/springboot-petclinic",
                  "name": "springboot-petclinic",
                  "resources": {},
                  "ports": [
                     {
                        "containerPort": 8080
                     }
                  ]
               }
            ]
         }
      }
   },
   "status": {}
}

{
   "apiVersion": "v1",
   "kind": "Service",
   "metadata": {
        "name": "petclinic"
   },
   "spec": {
      "type": "LoadBalancer",
      "selector": {
       
            "app": "petclinic"
         
      },
        
            "ports": [
               {
                  "port": 8080,
                  "targetPort": 8080,
                  "protocol": "TCP",
                  "name": "http"                   
                  ]
             }     
   }    
}
oprakyz7

oprakyz71#

服务资源中存在语法错误(ports中的数组右括号)。它应该是:

{
  "apiVersion": "v1",
  "kind": "Service",
  "metadata": {
    "name": "petclinic"
  },
  "spec": {
    "type": "LoadBalancer",
    "selector": {
      "app": "petclinic"
    },
    "ports": [
      {
        "port": 8080,
        "targetPort": 8080,
        "protocol": "TCP",
        "name": "http"
      }
    ]
  }
}

要使文件成为有效的JSON文件,可以将定义 Package 在List资源中:

{
  "apiVersion": "v1",
  "kind": "List",
  "items": [
    {
      "apiVersion": "v1",
      "kind": "Deployment",
      ...
    },
    {
      "apiVersion": "v1",
      "kind": "Service",
      ...
    }
  ]
}
j7dteeu8

j7dteeu82#

您可以首先使用kubectl应用yaml,然后使用kubectl get deploy,services -o json > example.json获取json
JSON的正确语法是在两个资源之间设置,,而不是在yaml中使用的---

...
    // deployment
},
{
    // service
...

相关问题