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

Skip to content

Commit 15d835c

Browse files
committed
Add notebook on how to create a ConfigMap and use its data in Pods
1 parent 023fcf5 commit 15d835c

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"How to create a ConfigMap and use its data in Pods\n",
8+
"===========================\n",
9+
"\n",
10+
"[ConfigMaps](https://kubernetes.io/docs/tasks/configure-pod-container/configmap/) allow you to decouple configuration artifacts from image content to keep containerized applications portable. In this notebook we would learn how to create a ConfigMap and also how to use its data in Pods as seen in https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"metadata": {
17+
"collapsed": false,
18+
"deletable": true,
19+
"editable": true
20+
},
21+
"outputs": [],
22+
"source": [
23+
"from kubernetes import client, config, watch\n",
24+
"from kubernetes.client.rest import ApiException"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {
31+
"collapsed": true
32+
},
33+
"outputs": [],
34+
"source": [
35+
"config.load_kube_config()"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {
42+
"collapsed": true
43+
},
44+
"outputs": [],
45+
"source": [
46+
"api_instance = client.CoreV1Api()\n",
47+
"cmap = client.V1ConfigMap()"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {
54+
"collapsed": true
55+
},
56+
"outputs": [],
57+
"source": [
58+
"cmap.metadata = client.V1ObjectMeta(name=\"special-config\")\n",
59+
"cmap.data = {}\n",
60+
"cmap.data[\"special.how\"] = \"very\"\n",
61+
"cmap.data[\"special.type\"] = \"charm\""
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {
68+
"collapsed": false
69+
},
70+
"outputs": [],
71+
"source": [
72+
"api_instance.create_namespaced_config_map(namespace=\"default\", body=cmap)"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": true
80+
},
81+
"outputs": [],
82+
"source": [
83+
"pod = client.V1Pod()\n",
84+
"spec = client.V1PodSpec()\n",
85+
"pod.metadata = client.V1ObjectMeta(name=\"dapi-test-pod\")"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {
92+
"collapsed": true
93+
},
94+
"outputs": [],
95+
"source": [
96+
"container = client.V1Container()\n",
97+
"container.name = \"test-container\"\n",
98+
"container.image = \"gcr.io/google_containers/busybox\"\n",
99+
"container.command = [\"/bin/sh\", \"-c\", \"env\"]"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"metadata": {
106+
"collapsed": true
107+
},
108+
"outputs": [],
109+
"source": [
110+
"container.env = [client.V1EnvVar(name=\"SPECIAL_LEVEL_KEY\"), client.V1EnvVar(name=\"SPECIAL_TYPE_KEY\")]\n",
111+
"container.env[0].value_from = client.V1EnvVarSource()\n",
112+
"container.env[0].value_from.config_map_key_ref = client.V1ConfigMapKeySelector(name=\"special-config\", key=\"special.how\")\n",
113+
"\n",
114+
"container.env[1].value_from = client.V1EnvVarSource()\n",
115+
"container.env[1].value_from.config_map_key_ref = client.V1ConfigMapKeySelector(name=\"special-config\", key=\"special.type\")"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"metadata": {
122+
"collapsed": true
123+
},
124+
"outputs": [],
125+
"source": [
126+
"spec.restart_policy = \"Never\"\n",
127+
"spec.containers = [container]\n",
128+
"pod.spec = spec"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": null,
134+
"metadata": {
135+
"collapsed": false
136+
},
137+
"outputs": [],
138+
"source": [
139+
"api_instance.create_namespaced_pod(namespace=\"default\",body=pod)"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": null,
145+
"metadata": {
146+
"collapsed": false,
147+
"scrolled": true
148+
},
149+
"outputs": [],
150+
"source": [
151+
"log = \"\"\n",
152+
"try: \n",
153+
" log = api_instance.read_namespaced_pod_log(name=\"dapi-test-pod\", namespace=\"default\")\n",
154+
"except ApiException as e:\n",
155+
" if str(e).find(\"ContainerCreating\") != -1:\n",
156+
" print(\"Creating Pod container.\\nRe-run current cell.\")\n",
157+
" else:\n",
158+
" print(\"Exception when calling CoreV1Api->read_namespaced_pod_log: %s\\n\" % e)\n",
159+
"\n",
160+
"for line in log.split(\"\\n\"):\n",
161+
" if line.startswith(\"SPECIAL\"):\n",
162+
" print(line)"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": null,
168+
"metadata": {
169+
"collapsed": false
170+
},
171+
"outputs": [],
172+
"source": [
173+
"api_instance.delete_namespaced_config_map(name=\"special-config\", namespace=\"default\", body=cmap)"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {
180+
"collapsed": false,
181+
"scrolled": true
182+
},
183+
"outputs": [],
184+
"source": [
185+
"api_instance.delete_namespaced_pod(name=\"dapi-test-pod\", namespace=\"default\", body=client.V1DeleteOptions())"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": null,
191+
"metadata": {
192+
"collapsed": true
193+
},
194+
"outputs": [],
195+
"source": []
196+
}
197+
],
198+
"metadata": {
199+
"kernelspec": {
200+
"display_name": "Python 2",
201+
"language": "python",
202+
"name": "python2"
203+
},
204+
"language_info": {
205+
"codemirror_mode": {
206+
"name": "ipython",
207+
"version": 2
208+
},
209+
"file_extension": ".py",
210+
"mimetype": "text/x-python",
211+
"name": "python",
212+
"nbconvert_exporter": "python",
213+
"pygments_lexer": "ipython2",
214+
"version": "2.7.13"
215+
}
216+
},
217+
"nbformat": 4,
218+
"nbformat_minor": 2
219+
}

0 commit comments

Comments
 (0)