What is the equivalent JSON for this Kubernetes service specification?

12/3/2015

Instead of this YAML file, I want to pass a JSON file. What is the equivalent JSON for it? I want to use it in the kubectl create -f ... command:

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
-- Gabriel Petrovay
json
kubernetes
yaml

3 Answers

12/3/2015

There are a lot of online YAML to JSON (and vice versa) converters covering 1.1 and 1.2 spec.

I haven't used Kubernetes before, but I can see that you can pass multiple documents. Basically the YAML structure that you use is a short version of two documents. JSON doesn't have an equivalent to this, so you have to break it into two separate documents (files).

The three dashes in YAML is a way of defining multiple documents. So basically the above is not one JSON oblect/file, but two.

The first

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

And the second

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

As a side note, since this is not useful for your purpose, in order to represent them as one JSON object, then you need an array. But this would mean that the YAML would have to change too. So in order to have this

[
  {
    "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
                }
              ]
            }
          ]
        }
      }
    }
  }
]

The YAML equivalent would be this

---
-
  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
-- Alkis Kalogeris
Source: StackOverflow

12/3/2015

You can use YAML to create Kubernetes object also:

$ kubectl create -f nginx.yaml

If you want to get JSON, You can do

$ kubectl get pods -o json
-- George
Source: StackOverflow

12/26/2015

Considering the initial specification in the spec.yaml file and the call:

kubectl create -f spec.yaml

The equivalent using the JSON format is:

  1. separate spec.yaml into multiple JSON files: srv.json and rc.json (one for each YAML fragment)
  2. provide all of them to kubectl:

    kubectl create -f srv.json -f rc.json
-- Gabriel Petrovay
Source: StackOverflow