Call Kubernetes API by Kubectl proxy

Create a Pod using Kubernetes APIs by kubectl proxy

Basic Information:

kubectl proxy --port=8001
Starting to serve on 127.0.0.1:8001

POST /api/v1/namespaces/{namespace}/pods

POST http://127.0.0.1:8001/api/v1/namespaces/default/pods

JSON Body

{
  "apiVersion": "v1",
  "kind": "Pod",
  "metadata": {
    "name": "nginx-pod",
    "labels": {
      "app": "nginx"
    }
  },
  "spec": {
    "containers": [
      {
        "name": "nginx-container",
        "image": "nginx",
        "ports": [
          {
            "containerPort": 80,
            "protocol": "TCP"
          }
        ]
      }
    ]
  }
}
  • Yaml to JSON

Return JSON

status 201

{
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata": {
        "name": "nginx-pod",
        "namespace": "default",
        "selfLink": "/api/v1/namespaces/default/pods/nginx-pod",
        "uid": "2111389e-9e21-4d89-a3e6-ca3aceb9c8bc",
        "resourceVersion": "7562",
        "creationTimestamp": "2021-01-12T04:56:19Z",
        "labels": {
            "app": "nginx"
        },
        "managedFields": [
            {
                "manager": "PostmanRuntime",
                "operation": "Update",
                "apiVersion": "v1",
                "time": "2021-01-12T04:56:19Z",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:labels": {
                            ".": {},
                            "f:app": {}
                        }
                    },
                    "f:spec": {
                        "f:containers": {
                            "k:{\"name\":\"nginx-container\"}": {
                                ".": {},
                                "f:image": {},
                                "f:imagePullPolicy": {},
                                "f:name": {},
                                "f:ports": {
                                    ".": {},
                                    "k:{\"containerPort\":80,\"protocol\":\"TCP\"}": {
                                        ".": {},
                                        "f:containerPort": {},
                                        "f:protocol": {}
                                    }
                                },
                                "f:resources": {},
                                "f:terminationMessagePath": {},
                                "f:terminationMessagePolicy": {}
                            }
                        },
                        "f:dnsPolicy": {},
                        "f:enableServiceLinks": {},
                        "f:restartPolicy": {},
                        "f:schedulerName": {},
                        "f:securityContext": {},
                        "f:terminationGracePeriodSeconds": {}
                    }
                }
            }
        ]
    },
    "spec": {
        "volumes": [
            {
                "name": "default-token-vmv4d",
                "secret": {
                    "secretName": "default-token-vmv4d",
                    "defaultMode": 420
                }
            }
        ],
        "containers": [
            {
                "name": "nginx-container",
                "image": "nginx",
                "ports": [
                    {
                        "containerPort": 80,
                        "protocol": "TCP"
                    }
                ],
                "resources": {},
                "volumeMounts": [
                    {
                        "name": "default-token-vmv4d",
                        "readOnly": true,
                        "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
                    }
                ],
                "terminationMessagePath": "/dev/termination-log",
                "terminationMessagePolicy": "File",
                "imagePullPolicy": "Always"
            }
        ],
        "restartPolicy": "Always",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "ClusterFirst",
        "serviceAccountName": "default",
        "serviceAccount": "default",
        "securityContext": {},
        "schedulerName": "default-scheduler",
        "tolerations": [
            {
                "key": "node.kubernetes.io/not-ready",
                "operator": "Exists",
                "effect": "NoExecute",
                "tolerationSeconds": 300
            },
            {
                "key": "node.kubernetes.io/unreachable",
                "operator": "Exists",
                "effect": "NoExecute",
                "tolerationSeconds": 300
            }
        ],
        "priority": 0,
        "enableServiceLinks": true,
        "preemptionPolicy": "PreemptLowerPriority"
    },
    "status": {
        "phase": "Pending",
        "qosClass": "BestEffort"
    }
}

Check the the pod is created

kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
nginx-pod   1/1     Running   0          48s

Other Kubernetes APIs Please Check the official documentation

https://v1-18.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#-strong-api-overview-strong-

Leave a Reply

Your email address will not be published.

ANOTE.DEV