Skip to content

Instantly share code, notes, and snippets.

@junaid18183
Created January 12, 2017 14:57
Show Gist options
  • Save junaid18183/4feee7b1d239af9767384037d74a9982 to your computer and use it in GitHub Desktop.
Save junaid18183/4feee7b1d239af9767384037d74a9982 to your computer and use it in GitHub Desktop.
Kubernetes API Example Usage

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 $KUBERNETES_MASTER/v1/pods Create the nginx service: $ curl $KUBERNETES_MASTER/v1/namespaces/default/services
-XPOST -H'Content-Type: application/json' [email protected] List all created Kubernetes services: $ curl $KUBERNETES_MASTER/v1/services Use the service through the API server proxy: $ curl $KUBERNETES_MASTER/v1/proxy/namespaces/default/services/nginx-service/ Delete the nginx service: $ curl $KUBERNETES_MASTER/v1/namespaces/default/services/nginx-service -XDELETE Delete the nginx pod: $ curl $KUBERNETES_MASTER/v1/namespaces/default/pods/nginx -XDELETE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment