Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b988ba

Browse files
authored
Merge pull request kubernetes-client#272 from djkonro/intro_notebook
Add introductory notebook
2 parents a73407a + e87c3bb commit 8b988ba

File tree

1 file changed

+309
-0
lines changed

1 file changed

+309
-0
lines changed
Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"deletable": true,
7+
"editable": true
8+
},
9+
"source": [
10+
"Managing kubernetes objects using common resource operations with the python client\n",
11+
"-----------------------------------------------------------------------------------------------\n",
12+
"\n",
13+
"Some of these operations include;\n",
14+
"\n",
15+
"- **`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",
16+
"\n",
17+
"\n",
18+
"- **`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",
19+
"\n",
20+
"\n",
21+
"- **`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",
22+
"\n",
23+
"\n",
24+
"- **`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",
25+
"\n",
26+
"\n",
27+
"- **`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",
28+
"\n",
29+
"\n",
30+
"- **`delete_xxxx`** : delete a resource. This performs operations similar to **`kubectl delete`**.\n",
31+
"\n",
32+
"\n",
33+
"For Futher information see the Documentation for API Endpoints section in https://github.com/kubernetes-incubator/client-python/blob/master/kubernetes/README.md"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {
40+
"collapsed": true,
41+
"deletable": true,
42+
"editable": true
43+
},
44+
"outputs": [],
45+
"source": [
46+
"from kubernetes import client, config"
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {
52+
"deletable": true,
53+
"editable": true
54+
},
55+
"source": [
56+
"### Load config from default location."
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {
63+
"collapsed": true,
64+
"deletable": true,
65+
"editable": true
66+
},
67+
"outputs": [],
68+
"source": [
69+
"config.load_kube_config()"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {
75+
"deletable": true,
76+
"editable": true
77+
},
78+
"source": [
79+
"### Create API endpoint instance as well as API resource instances (body and specification)."
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"metadata": {
86+
"collapsed": true,
87+
"deletable": true,
88+
"editable": true
89+
},
90+
"outputs": [],
91+
"source": [
92+
"api_instance = client.ExtensionsV1beta1Api()\n",
93+
"dep = client.ExtensionsV1beta1Deployment()\n",
94+
"spec = client.ExtensionsV1beta1DeploymentSpec()"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {
100+
"deletable": true,
101+
"editable": true
102+
},
103+
"source": [
104+
"### Fill required object fields (apiVersion, kind, metadata and spec)."
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {
111+
"collapsed": true,
112+
"deletable": true,
113+
"editable": true
114+
},
115+
"outputs": [],
116+
"source": [
117+
"name = \"my-busybox\"\n",
118+
"dep.metadata = client.V1ObjectMeta(name=name)\n",
119+
"\n",
120+
"spec.template = client.V1PodTemplateSpec()\n",
121+
"spec.template.metadata = client.V1ObjectMeta(name=\"busybox\")\n",
122+
"spec.template.metadata.labels = {\"app\":\"busybox\"}\n",
123+
"spec.template.spec = client.V1PodSpec()\n",
124+
"dep.spec = spec\n",
125+
"\n",
126+
"container = client.V1Container()\n",
127+
"container.image = \"busybox:1.26.1\"\n",
128+
"container.args = [\"sleep\", \"3600\"]\n",
129+
"container.name = name\n",
130+
"spec.template.spec.containers = [container]"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {
136+
"deletable": true,
137+
"editable": true
138+
},
139+
"source": [
140+
"### Create Deployment using create_xxxx command for Deployments."
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": null,
146+
"metadata": {
147+
"collapsed": false,
148+
"deletable": true,
149+
"editable": true
150+
},
151+
"outputs": [],
152+
"source": [
153+
"api_instance.create_namespaced_deployment(namespace=\"default\",body=dep)"
154+
]
155+
},
156+
{
157+
"cell_type": "markdown",
158+
"metadata": {
159+
"deletable": true,
160+
"editable": true
161+
},
162+
"source": [
163+
"### Use list_xxxx command for Deployment, to list Deployments."
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": null,
169+
"metadata": {
170+
"collapsed": false,
171+
"deletable": true,
172+
"editable": true
173+
},
174+
"outputs": [],
175+
"source": [
176+
"deps = api_instance.list_namespaced_deployment(namespace=\"default\")\n",
177+
"for item in deps.items:\n",
178+
" print(\"%s %s\" % (item.metadata.namespace, item.metadata.name))"
179+
]
180+
},
181+
{
182+
"cell_type": "markdown",
183+
"metadata": {
184+
"deletable": true,
185+
"editable": true
186+
},
187+
"source": [
188+
"### Use read_xxxx command for Deployment, to display the detailed state of the created Deployment resource."
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": null,
194+
"metadata": {
195+
"collapsed": false,
196+
"deletable": true,
197+
"editable": true
198+
},
199+
"outputs": [],
200+
"source": [
201+
"api_instance.read_namespaced_deployment(namespace=\"default\",name=name)"
202+
]
203+
},
204+
{
205+
"cell_type": "markdown",
206+
"metadata": {
207+
"deletable": true,
208+
"editable": true
209+
},
210+
"source": [
211+
"### Use patch_xxxx command for Deployment, to make specific update to the Deployment."
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": null,
217+
"metadata": {
218+
"collapsed": false,
219+
"deletable": true,
220+
"editable": true
221+
},
222+
"outputs": [],
223+
"source": [
224+
"dep.metadata.labels = {\"key\": \"value\"}\n",
225+
"api_instance.patch_namespaced_deployment(name=name, namespace=\"default\", body=dep)"
226+
]
227+
},
228+
{
229+
"cell_type": "markdown",
230+
"metadata": {
231+
"deletable": true,
232+
"editable": true
233+
},
234+
"source": [
235+
"### Use replace_xxxx command for Deployment, to update Deployment with a completely new version of the object."
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": null,
241+
"metadata": {
242+
"collapsed": false,
243+
"deletable": true,
244+
"editable": true
245+
},
246+
"outputs": [],
247+
"source": [
248+
"dep.spec.template.spec.containers[0].image = \"busybox:1.26.2\"\n",
249+
"api_instance.replace_namespaced_deployment(name=name, namespace=\"default\", body=dep)"
250+
]
251+
},
252+
{
253+
"cell_type": "markdown",
254+
"metadata": {
255+
"deletable": true,
256+
"editable": true
257+
},
258+
"source": [
259+
"### Use delete_xxxx command for Deployment, to delete created Deployment."
260+
]
261+
},
262+
{
263+
"cell_type": "code",
264+
"execution_count": null,
265+
"metadata": {
266+
"collapsed": false,
267+
"deletable": true,
268+
"editable": true,
269+
"scrolled": true
270+
},
271+
"outputs": [],
272+
"source": [
273+
"api_instance.delete_namespaced_deployment(name=name, namespace=\"default\", body=client.V1DeleteOptions(propagation_policy=\"Foreground\", grace_period_seconds=5))"
274+
]
275+
},
276+
{
277+
"cell_type": "code",
278+
"execution_count": null,
279+
"metadata": {
280+
"collapsed": true,
281+
"deletable": true,
282+
"editable": true
283+
},
284+
"outputs": [],
285+
"source": []
286+
}
287+
],
288+
"metadata": {
289+
"kernelspec": {
290+
"display_name": "Python 2",
291+
"language": "python",
292+
"name": "python2"
293+
},
294+
"language_info": {
295+
"codemirror_mode": {
296+
"name": "ipython",
297+
"version": 2
298+
},
299+
"file_extension": ".py",
300+
"mimetype": "text/x-python",
301+
"name": "python",
302+
"nbconvert_exporter": "python",
303+
"pygments_lexer": "ipython2",
304+
"version": "2.7.13"
305+
}
306+
},
307+
"nbformat": 4,
308+
"nbformat_minor": 2
309+
}

0 commit comments

Comments
 (0)