Copied from : kubernetes/kubernetes#17404
The API docs don't have a great on-ramp for explaining how to use the API directly. We have some example docs from the DCOS docs, that don't really belong there. I'd like to donate them to the k8s API docs, but I'm not sure where to put them. Any ideas where these would go?
Unfortunately, it somewhat overlaps with the walkthrough 101 page, but the rest API bits don't really belong there...
Create a Kubernetes Pod Definition
A pod is one or more containers that are co-located on the same host. In this example we’re creating a pod which runs nginx.
Create the example nginx pod definition:
$ cat > nginx-pod.json <<EOF { "kind": "Pod", "apiVersion": "v1", "metadata":{ "name": "nginx", "namespace": "default", "labels": { "name": "nginx" } }, "spec": { "containers": [{ "name": "nginx", "image": "nginx", "ports": [{"containerPort": 80}], "resources": { "limits": { "memory": "128Mi", "cpu": "500m" } } }] } } EOF This pod consists of one container named nginx which is allocated 128 MB of memory and half a CPU share.
Create a Kubernetes Service Definition
A service inside kubernetes is an abstraction that gives a pod (or an external service) a named endpoint. In order to expose the nginx server to the web, you must create a Kubernetes service that refers to it.
Create an example nginx service definition:
$ cat > nginx-service.json <<EOF { "kind": "Service", "apiVersion": "v1", "metadata": { "name": "nginx-service", "namespace": "default", "labels": {"name": "nginx"} }, "spec": { "ports": [{"port": 80}], "selector": {"name": "nginx"} } } EOF Launch a Kubernetes Pod and Service by using REST API
You can use pure REST with a simple HTTP command-line tool like curl to access the apiserver API directly.
Create the nginx pod:
$ curl $KUBERNETES_MASTER/v1/namespaces/default/pods
-XPOST -H'Content-Type: application/json' [email protected]
List all created Kubernetes pods:
$ curl
-XPOST -H'Content-Type: application/json' [email protected]
List all created Kubernetes services:
$ curl