How to create Pod from REST API

7/4/2018

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
-- E235
kubernetes
kubernetes-apiserver
kubernetes-pod

1 Answer

7/4/2018

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 -d@nginx-pod.json  

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.

-- E235
Source: StackOverflow