What data formats does the Kubernetes API server support? What data formats does the Kubernetes API server support? kubernetes kubernetes

What data formats does the Kubernetes API server support?


Managing Kubernetes, Chapter 4 (section "Alternate encodings") says that the API server supports three data formats for resource specifications:

  • JSON
  • YAML
  • Protocol Buffers (protobuf)

I tested creating resources in these formats using curl and it works, as shown in the following.

Preparation

For easily talking to the API server, start a proxy to the API server with kubectl:

kubectl proxy

Now the API server is accessible on http://127.0.0.1:8001.

The Kubernetes API specification is accessible on http://127.0.0.1:8001/openapi/v2.

Request body formats

You have to specify the format of the HTTP POST request body (i.e. the resource specification) in the Content-Type header.

The following data formats are supported:

  • application/json
  • application/yaml
  • application/vnd.kubernetes.protobuf

Below are concrete examples of requests.

Create a resource with JSON

Define a resource specification in JSON and save it in a file.

For example, pod.json:

{   "apiVersion":"v1",   "kind":"Pod",   "metadata":{      "name":"test-pod"   },   "spec":{      "containers":[         {            "image":"nginx",            "name":"nginx-container"         }      ]   }}

Call API server to create the resource:

curl -H "Content-Type: application/json" -d "$(cat pod.json)" -X POST http://127.0.0.1:8001/api/v1/namespaces/default/pods

Create a resource with YAML

Define a resource specification in YAML and save it in a file.

For example, pod.yaml:

apiVersion: v1kind: Podmetadata:  name: test-podspec:  containers:  - image: nginx    name: nginx-container

Call API server to create the resource:

curl -H "Content-Type: application/yaml" -d "$(cat pod.yaml)" -X POST http://127.0.0.1:8001/api/v1/namespaces/default/pods

Create a resource with protobuf

I didn't test this, because the Kubernetes protobuf wire format uses a custom wrapper around the protobuf serialisation of the resource (see here and here). But, in principle, it should work.

Response body formats

When creating a resource as shown above, the API server returns the complete specification of the same resource in the HTTP response (that is, the specification that you submitted, initialised with all the default values, a status field, etc.).

You can choose the format for this response data with the Accept header in the request.

The accepted formats for the Accept header are the same as for the Content-Type header:

  • application/json (default)
  • application/yaml
  • application/vnd.kubernetes.protobuf

For example:

curl -H "Content-Type: application/json" -H "Accept: application/yaml" -d "$(cat pod.json)" -X POST http://127.0.0.1:8001/api/v1/namespaces/default/pods

All combinations of formats in the Content-Type and Accept headers are possible.


In Kubernetes both json and YAML formats are supported. am assuming you create and update the resources using kubectl. kubectl accepts both these formats.

if you pass YAML format, Internally the kubectl converts it to json and post it to API server.