【问题标题】:What is the equivalent JSON for this Kubernetes service specification?这个 Kubernetes 服务规范的等效 JSON 是什么?
【发布时间】:2016-03-07 23:35:25
【问题描述】:

我想传递一个 JSON 文件,而不是 this YAML 文件。它的等效 JSON 是什么?我想在kubectl create -f ... 命令中使用它:

apiVersion: v1
kind: Service
metadata:
  name: my-nginx-svc
  labels:
    app: nginx
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: nginx
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: my-nginx
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

【问题讨论】:

标签: json yaml kubernetes


【解决方案1】:

考虑到spec.yaml 文件中的初始规范和调用:

kubectl create -f spec.yaml

使用 JSON 格式的等价物是:

  1. spec.yaml 分隔成多个 JSON 文件:srv.jsonrc.json(每个 YAML 片段一个)
  2. 全部提供给kubectl:

    kubectl create -f srv.json -f rc.json
    

【讨论】:

    【解决方案2】:

    您也可以使用 YAML 创建 Kubernetes 对象:

    $ kubectl create -f nginx.yaml
    

    如果你想得到JSON,你可以这样做

    $ kubectl get pods -o json
    

    【讨论】:

      【解决方案3】:

      有很多在线 YAMLJSON(反之亦然)转换器涵盖 1.1 和 1.2 规范。

      我之前没有用过Kubernetes,但是我可以看到你可以传递多个文档。基本上,您使用的YAML 结构是两个文档的简短版本。 JSON 没有与此等效的,因此您必须将其分成两个单独的文档(文件)。

      YAML 中的三个破折号是定义多个文档的一种方式。 所以基本上上面不是一个JSON oblect/file,而是两个。

      第一个

      {
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {
          "name": "my-nginx-svc",
          "labels": {
            "app": "nginx"
          }
        },
        "spec": {
          "type": "LoadBalancer",
          "ports": [
            {
              "port": 80
            }
          ],
          "selector": {
            "app": "nginx"
          }
        }
      } 
      

      第二个

      {
        "apiVersion": "v1",
        "kind": "ReplicationController",
        "metadata": {
          "name": "my-nginx"
        },
        "spec": {
          "replicas": 2,
          "template": {
            "metadata": {
              "labels": {
                "app": "nginx"
              }
            },
            "spec": {
              "containers": [
                {
                  "name": "nginx",
                  "image": "nginx",
                  "ports": [
                    {
                      "containerPort": 80
                    }
                  ]
                }
              ]
            }
          }
        }
      }
      

      附带说明,由于这对您的目的没有用,为了将它们表示为一个 JSON 对象,那么您需要一个数组。但这意味着YAML 也必须改变。所以为了有这个

      [
        {
          "apiVersion": "v1",
          "kind": "Service",
          "metadata": {
            "name": "my-nginx-svc",
            "labels": {
              "app": "nginx"
            }
          },
          "spec": {
            "type": "LoadBalancer",
            "ports": [
              {
                "port": 80
              }
            ],
            "selector": {
              "app": "nginx"
            }
          }
        },
        {
          "apiVersion": "v1",
          "kind": "ReplicationController",
          "metadata": {
            "name": "my-nginx"
          },
          "spec": {
            "replicas": 2,
            "template": {
              "metadata": {
                "labels": {
                  "app": "nginx"
                }
              },
              "spec": {
                "containers": [
                  {
                    "name": "nginx",
                    "image": "nginx",
                    "ports": [
                      {
                        "containerPort": 80
                      }
                    ]
                  }
                ]
              }
            }
          }
        }
      ]
      

      YAML 等效项是这样的

      ---
      -
        apiVersion: v1
        kind: Service
        metadata:
          name: my-nginx-svc
          labels:
            app: nginx
        spec:
          type: LoadBalancer
          ports:
          - port: 80
          selector:
            app: nginx
      -
        apiVersion: v1
        kind: ReplicationController
        metadata:
          name: my-nginx
        spec:
          replicas: 2
          template:
            metadata:
              labels:
                app: nginx
            spec:
              containers:
              - name: nginx
                image: nginx
                ports:
                - containerPort: 80
      

      【讨论】:

        猜你喜欢
        • 2016-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-17
        • 2019-04-16
        • 1970-01-01
        相关资源
        最近更新 更多