Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add introductory notebook #272

Merged
merged 1 commit into from
Jul 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
309 changes: 309 additions & 0 deletions examples/notebooks/intro_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"Managing kubernetes objects using common resource operations with the python client\n",
"-----------------------------------------------------------------------------------------------\n",
"\n",
"Some of these operations include;\n",
"\n",
"- **`create_xxxx`** : create a resource object. Ex **`create_namespaced_pod`** and **`create_namespaced_deployment`**, for creation of pods and deployments respectively. This performs operations similar to **`kubectl create`**.\n",
"\n",
"\n",
"- **`read_xxxx`** : read the specified resource object. Ex **`read_namespaced_pod`** and **`read_namespaced_deployment`**, to read pods and deployments respectively. This performs operations similar to **`kubectl describe`**.\n",
"\n",
"\n",
"- **`list_xxxx`** : retrieve all resource objects of a specific type. Ex **`list_namespaced_pod`** and **`list_namespaced_deployment`**, to list pods and deployments respectively. This performs operations similar to **`kubectl get`**.\n",
"\n",
"\n",
"- **`patch_xxxx`** : apply a change to a specific field. Ex **`patch_namespaced_pod`** and **`patch_namespaced_deployment`**, to update pods and deployments respectively. This performs operations similar to **`kubectl patch`**, **`kubectl label`**, **`kubectl annotate`** etc.\n",
"\n",
"\n",
"- **`replace_xxxx`** : replacing a resource object will update the resource by replacing the existing spec with the provided one. Ex **`replace_namespaced_pod`** and **`replace_namespaced_deployment`**, to update pods and deployments respectively, by creating new replacements of the entire object. This performs operations similar to **`kubectl rolling-update`**, **`kubectl apply`** and **`kubectl replace`**.\n",
"\n",
"\n",
"- **`delete_xxxx`** : delete a resource. This performs operations similar to **`kubectl delete`**.\n",
"\n",
"\n",
"For Futher information see the Documentation for API Endpoints section in https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/README.md"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"from kubernetes import client, config"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Load config from default location."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"config.load_kube_config()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Create API endpoint instance as well as API resource instances (body and specification)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"api_instance = client.ExtensionsV1beta1Api()\n",
"dep = client.ExtensionsV1beta1Deployment()\n",
"spec = client.ExtensionsV1beta1DeploymentSpec()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Fill required object fields (apiVersion, kind, metadata and spec)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"name = \"my-busybox\"\n",
"dep.metadata = client.V1ObjectMeta(name=name)\n",
"\n",
"spec.template = client.V1PodTemplateSpec()\n",
"spec.template.metadata = client.V1ObjectMeta(name=\"busybox\")\n",
"spec.template.metadata.labels = {\"app\":\"busybox\"}\n",
"spec.template.spec = client.V1PodSpec()\n",
"dep.spec = spec\n",
"\n",
"container = client.V1Container()\n",
"container.image = \"busybox:1.26.1\"\n",
"container.args = [\"sleep\", \"3600\"]\n",
"container.name = name\n",
"spec.template.spec.containers = [container]"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Create Deployment using create_xxxx command for Deployments."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"api_instance.create_namespaced_deployment(namespace=\"default\",body=dep)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Use list_xxxx command for Deployment, to list Deployments."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"deps = api_instance.list_namespaced_deployment(namespace=\"default\")\n",
"for item in deps.items:\n",
" print(\"%s %s\" % (item.metadata.namespace, item.metadata.name))"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Use read_xxxx command for Deployment, to display the detailed state of the created Deployment resource."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"api_instance.read_namespaced_deployment(namespace=\"default\",name=name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Use patch_xxxx command for Deployment, to make specific update to the Deployment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"dep.metadata.labels = {\"key\": \"value\"}\n",
"api_instance.patch_namespaced_deployment(name=name, namespace=\"default\", body=dep)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Use replace_xxxx command for Deployment, to update Deployment with a completely new version of the object."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"dep.spec.template.spec.containers[0].image = \"busybox:1.26.2\"\n",
"api_instance.replace_namespaced_deployment(name=name, namespace=\"default\", body=dep)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Use delete_xxxx command for Deployment, to delete created Deployment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"scrolled": true
},
"outputs": [],
"source": [
"api_instance.delete_namespaced_deployment(name=name, namespace=\"default\", body=client.V1DeleteOptions(propagation_policy=\"Foreground\", grace_period_seconds=5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}