这个Kubernetes服务规范的等效JSON是什么?

时间:2015-12-03 11:17:04

标签: json yaml kubernetes

我想传递一个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

3 个答案:

答案 0 :(得分:2)

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

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

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

第一个

{
  "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

答案 1 :(得分:1)

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

SELECT 
  CASE 
   WHEN (a.CNT = 0 AND b.cnt = 0) THEN 'N'
   ELSE  CASE  WHEN a.CNT = b.CNT THEN 'Y'
         ELSE 'N' END 
   END      AS Final_Status
FROM
  (SELECT NVL(COUNT(*),1) CNT
  FROM FPC_INFO
  WHERE BILL_MONTH_DT = '01-SEP-2015'
  AND CUST_TYPE_NM    = UPPER('UVERSE')
  AND FINALIZED_USER IS NOT NULL
  ) a,
  (SELECT COUNT(*) CNT
  FROM FPC_INFO
  WHERE BILL_MONTH_DT = '01-SEP-2015'
  AND CUST_TYPE_NM    = UPPER('UVERSE')
  ) b

如果你想获得JSON,你可以

$ kubectl create -f nginx.yaml

答案 2 :(得分: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