Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Pod from REST API

How can I create a Pod using REST API ?

I checked the Kubernetes API documentation:
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#-strong-write-operations-strong--54

They are writing that need to use POST request:
POST /api/v1/namespaces/{namespace}/pods

I have this YAML of simple nginx pod:

cat > nginx-pod.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: nginx1
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
EOF
like image 273
E235 Avatar asked Jul 04 '18 14:07

E235


People also ask

What is an API pod?

Pod is a kubernetes abstraction that runs one or more containers. All pods in kubernetes run in a namespace. All the system related pods run in a namespace called kube-system. By default all the user pods run in default namespace. Pods API is the part of Kubernetes API which allows user to run CRUD operations on pods.

Does Kubernetes use REST API?

The REST API is the fundamental fabric of Kubernetes. All operations and communications between components, and external user commands are REST API calls that the API Server handles. Consequently, everything in the Kubernetes platform is treated as an API object and has a corresponding entry in the API.

Can we create pod without container?

Workloads use metadata resources, which are the objects used to configure the behavior of other resources within the cluster. Workloads will eventually run a container, but to run a container, you will need to run a Pod. It is possible to create a Pod as a standalone object.


2 Answers

Need to translate the YAML file to JSON file:

cat > nginx-pod.json <<EOF
{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "name": "nginx1"
    },
    "spec": {
        "containers": [
            {
                "name": "nginx",
                "image": "nginx:1.7.9",
                "ports": [
                    {
                        "containerPort": 80
                    }
                ]
            }
        ]
    }
}
EOF

Use the curl command like that:

curl -k -v -X POST -H "Authorization: Bearer <JWT_TOKEN>" -H "Content-Type: application/json" https://127.0.0.1:6443/api/v1/namespaces/default/pods [email protected]  

Of course, the token you are using should have permissions to create pod.

If someone has a way to do it without converting to JSON, please share.

like image 90
E235 Avatar answered Oct 18 '22 20:10

E235


Adding an answer as per the author's request.

We can use the Yaml file directly as follows.

curl -k -X POST -H 'Content-Type: application/yaml' \
-H "Authorization: Bearer <JWT_TOKEN>" --data '
apiVersion: v1
kind: Pod
metadata:
  name: nginx1
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
' "https://127.0.0.1:6443/api/v1/namespaces/default/pods"

One weird thing is that If I try to pass YAML file in curl's data(@file.yaml) It throws an error. Seems YAML content type doesn't accept binary hence I used cat to pass the contents. So the following will also work.

curl -k -X POST -H 'Content-Type: application/yaml' -H "Authorization: Bearer <JWT_TOKEN>"\
 --data "$(cat nginx-pod.yaml)" "https://127.0.0.1:6443/api/v1/namespaces/default/pods"

Reference:

  1. Kubernetes API reference
  2. Kubernetes Deployment curl example - Example 1 is created based on this.
like image 41
Mani Avatar answered Oct 18 '22 20:10

Mani