Start a Pod in Kubernetes using curl
Recently I came across the issue that a customer needed to start a Pod without the kubectl
or the oc
command line tooling. Since the Kubernetes API is a relatively straight-forward REST API, we can come up with a nice curl
command for that, which basically does the following:
- To create the Pod, send a POST request
- The Pod definition is sent as JSON
- The endpoint we’re using is
/api/v1/namespaces/mynamespace/pods
Sending this looks like this:
curl -v -XPOST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" 'https://api.mycluster.example.com:6443/api/v1/namespaces/mynamespace/pods' --data '{"apiVersion":"v1","kind":"Pod","metadata":{"name":"date-pod"},"spec":{"containers":[{"name":"run-pod","image":"registry.fedoraproject.org/fedora-minimal:42","command":["date"]}]}}'
[..]
< HTTP/2 201
< audit-id: 4ea87648-f665-4761-a5da-8223cad9796d
< cache-control: no-cache, private
< content-type: application/json
< strict-transport-security: max-age=31536000; includeSubDomains; preload
< x-kubernetes-pf-flowschema-uid: 8073422e-0684-4c2c-890f-e82d671d7c74
< x-kubernetes-pf-prioritylevel-uid: fbbb90b2-003c-4e05-b379-aede086844b2
< date: Thu, 08 May 2025 13:40:46 GMT
<
{
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "date-pod",
"namespace": "skrenger",
"uid": "5d7a4918-fe9a-4b53-9d1a-ad04581fe15c",
[..]
The above Pod runs the date
command and then exits. The Kubernetes API documentation has more details about the HTTP response codes and other possible methods.