Debian Codegraph @

doc/articles/kubernetes.html

raw
1 ---
2 layout: default
3 title: Kubernetes
4 ---
5
6 <div class="hgroup">
7 <div class="hgroup-inline">
8 <div class="panel">
9 <h1 id="kubernetes">Kubernetes</h1>
10 </div>
11 <div style="clear: both"></div>
12 </div>
13 </div>
14
15 <div class="hgroup">
16 <div class="hgroup-inline">
17 <div class="panel">
18 <p>
19 <a href="https://kubernetes.io/">Kubernetes</a> is a cluster orchestration system for
20 containerized workloads. Out of the box, it is configured with YAML files, making it a
21 natural fit for Jsonnet.
22 </p>
23 </div>
24 <div style="clear: both"></div>
25 </div>
26 </div>
27
28 <div class="hgroup">
29 <div class="hgroup-inline">
30 <div class="panel">
31 <h2 id="integrating">Using Jsonnet With Kubernetes</h2>
32 </div>
33 <div style="clear: both"></div>
34 </div>
35 </div>
36
37 <div class="hgroup">
38 <div class="hgroup-inline">
39 <div class="panel">
40 <p>
41 Jsonnet's output JSON can be consumed by <tt>kubecfg</tt> which
42 treats it as if it were YAML. This approach also works for other configuration formats that
43 extend JSON, such as Javascript or HashiCorp's Configuration Language (<a
44 href="https://github.com/hashicorp/hcl">HCL</a>).
45 </p>
46 <p>
47 If we configure only a single Kubernetes object in each run of Jsonnet, we miss out on the
48 ability to remove duplication not just within an object, but across sets of objects with
49 commonalities. Fortunately, there are three ways to configure sets of Kubernetes objects:
50 Use YAML <a href="/learning/getting_started.html#stream">stream</a> output, <a
51 href="/learning/getting_started.html#multi">multi-file</a> output, or a single kubectl
52 list object. This latter option is provided by Kubernetes without requiring any special
53 support from Jsonnet. It allows us to group several Kubernetes objects into a single
54 object. This is the most popular option because it does not require intermediate files, and
55 other tools don't always reliably support YAML streams.
56 </p>
57 The Kubernetes community has been very active with Jsonnet, and the following resources may
58 be helpful:
59 </p>
60 <ul>
61 <li>
62 A <a href="https://github.com/google/jsonnet/tree/master/case_studies/kubernetes">simple
63 example</a> from the Jsonnet repo.
64 </li>
65 <li>
66 <a href="https://github.com/kubecfg/kubecfg">Kubecfg</a> (whose name may sound familiar to
67 current or ex-Googlers) is an unopiniated tool for evaluating Jsonnet and
68 pushing the results to Kubernetes. It comes with a <a
69 href="https://github.com/bitnami-labs/kube-libsonnet">useful template library</a>. See
70 this <a href="https://engineering.bitnami.com/articles/an-example-of-real-kubernetes-bitnami.html">blog post</a>.
71 </li>
72 <li>
73 <a href="https://tanka.dev">Tanka</a>, by <a href="https://grafana.com">Grafana Labs</a>
74 is the spiritual successor of Ksonnet. It extends jsonnet with native functions
75 to import Kustomize manifests and Helm charts, making it an allround tool to
76 work with Kubernetes.
77 </li>
78 <li>
79 <a href="https://github.com/jsonnet-libs/k8s">jsonnet-libs/k8s</a>
80 generator produces over 30 Jsonnet Kubernetes libraries and counting, with most
81 notably the <a href="https://jsonnet-libs.github.io/k8s-libsonnet/">k8s-libsonnet</a> library
82 as the succesor of ksonnet-lib.
83 </li>
84 <li>
85 <a href="https://github.com/deepmind/kapitan">Kapitan</a> by <a
86 href="https://deepmind.com">Deepmind</a> is another tool for driving Kubernetes with
87 Jsonnet and textual templating.
88 </li>
89 <li>
90 <a href="https://www.box.com">Box</a> have <a
91 href="https://blog.box.com/blog/kubernetes-box-microservices-maximum-velocity/">blogged</a>
92 and <a href="https://youtu.be/QIDrdZlEQdw?t=10m35s">spoken about</a> how their internal
93 Kubernetes-based infrastructure uses Jsonnet.
94 </li>
95 <li>
96 <a href="https://databricks.com">Databricks</a> have <a
97 href="https://databricks.com/blog/2017/06/26/declarative-infrastructure-jsonnet-templating-language.html">written
98 about</a> how they manage infrastructure on Kubernetes using Jsonnet. They also published
99 a <a href="https://github.com/databricks/jsonnet-style-guide">style guide</a>, which may
100 be informative.
101 </li>
102 </ul>
103 </div>
104 <div style="clear: both"></div>
105 </div>
106 </div>
107
108 <div class="hgroup">
109 <div class="hgroup-inline">
110 <div class="panel">
111 <h2 id="syntax">Conversion Tool</h2>
112 </div>
113 <div style="clear: both"></div>
114 </div>
115 </div>
116
117 <div class="hgroup">
118 <div class="hgroup-inline">
119 <div class="panel">
120 <p>
121 As a convenience, the following tool allows you to convert YAML Kubernetes examples into
122 basic Jsonnet files. It simply converts the YAML to JSON and then runs the Jsonnet
123 formatter on that JSON to make it look a bit cleaner. Sorry, the original file's comments
124 are lost during the YAML parsing. This tool just helps you get started. You will want to
125 further clean up the code to remove duplication.
126 </p>
127 </div>
128 <div style="clear: both"></div>
129 </div>
130 </div>
131
132 <div class="inverse hgroup">
133 <div class=hgroup-inline>
134 <div class="tab-window-input" id="kube-convert-input">
135 <div class="tab-header">
136 </div>
137 <textarea id=kube-yaml>
138 kind: ReplicationController
139 apiVersion: v1
140 metadata:
141 name: spark-master-controller
142 spec:
143 replicas: 1
144 selector:
145 component: spark-master
146 template:
147 metadata:
148 labels:
149 component: spark-master
150 spec:
151 containers:
152 - name: spark-master
153 image: k8s.gcr.io/spark:1.5.2_v1
154 command: ["/start-master"]
155 ports:
156 - containerPort: 7077
157 - containerPort: 8080
158 resources:
159 requests:
160 cpu: 100m
161 </textarea>
162 </div>
163 <div class="bigarrow">➡</div>
164 <div class="tab-window-output" id="kube-convert-output">
165 <div class="tab-header">
166 <div class=selected onclick="tab_click(this, 'kube-convert-output')">output.jsonnet</div>
167 </div>
168 <textarea readonly class="selected code-json" id="kube-convert-output">
169 {
170 kind: 'ReplicationController',
171 apiVersion: 'v1',
172 metadata: {
173 name: 'spark-master-controller',
174 },
175 spec: {
176 replicas: 1,
177 selector: {
178 component: 'spark-master',
179 },
180 template: {
181 metadata: {
182 labels: {
183 component: 'spark-master',
184 },
185 },
186 spec: {
187 containers: [
188 {
189 name: 'spark-master',
190 image: 'k8s.gcr.io/spark:1.5.2_v1',
191 command: [
192 '/start-master',
193 ],
194 ports: [
195 {
196 containerPort: 7077,
197 },
198 {
199 containerPort: 8080,
200 },
201 ],
202 resources: {
203 requests: {
204 cpu: '100m',
205 },
206 },
207 },
208 ],
209 },
210 },
211 },
212 }
213 </textarea>
214 </div>
215 <script>
216 yaml_conv_demo(
217 'kube-convert-input',
218 'kube-yaml',
219 'kube.yaml',
220 'kube-convert-output',
221 );
222 </script>
223 <div style="clear: both"></div>
224 </div>
225 </div>
226
227 <div class="hgroup">
228 <div class="hgroup-inline">
229 <div class="panel">
230 <h2 id=collections>Kubernetes Collections</h2>
231 </div>
232 <div style="clear: both"></div>
233 </div>
234 </div>
235
236 <div class="hgroup">
237 <div class="hgroup-inline">
238 <div class="panel">
239 <p>
240 In some places, the Kubernetes API objects use lists of named objects or (name, value) pairs
241 rather than using objects to map names to values. This makes those objects hard to reference
242 and extend in Jsonnet. For example, the containers part of the Pod spec looks like this:
243 </p>
244 <pre>local PodSpec = {
245 containers: [
246 {
247 name: 'foo',
248 env: [
249 { name: 'var1', value: 'somevalue' },
250 { name: 'var2', value: 'somevalue' },
251 ],
252 },
253 {
254 name: 'bar',
255 env: [
256 { name: 'var2', value: 'somevalue' },
257 { name: 'var3', value: 'somevalue' },
258 ],
259 },
260 ],
261 };</pre>
262 <p>
263 If we wanted to override this to modify the <code>var3</code> environment variable of
264 <code>bar</code>, we could use array indexes to reference the particular container and
265 environment variable, but the resulting code is brittle and ugly:
266 </p>
267 <pre>PodSpec {
268 containers: [
269 super.containers[0],
270 super.containers[1] {
271 env: [
272 super.env[0],
273 super.env[1] { value: 'othervalue' },
274 ],
275 },
276 ],
277 }</pre>
278 <p>
279 A better solution is to use an object to represent the mapping, converting to the Kubernetes
280 representation post-hoc. We can now create a new Pod spec with the same override, specified
281 much more easily and safely:
282 </p>
283 </div>
284 <div style="clear: both"></div>
285 </div>
286 </div>
287
288 <div class="inverse hgroup">
289 <div class=hgroup-inline>
290 <div class="tab-window-input" id="collections-input">
291 <div class="tab-header">
292 </div>
293 <textarea id=pod-spec-jsonnet>
294 local utils = import 'utils.libsonnet';
295
296 local PodSpec = {
297 containersObj:: {
298 foo: {
299 envObj:: {
300 var1: 'somevalue',
301 var2: 'somevalue',
302 },
303 env: utils.pairList(self.envObj),
304 },
305 bar: {
306 envObj:: {
307 var2: 'somevalue',
308 var3: 'somevalue',
309 },
310 env: utils.pairList(self.envObj),
311 },
312 },
313 containers:
314 utils.namedObjectList(self.containersObj),
315 };
316
317 PodSpec {
318 containersObj+: {
319 bar+: { envObj+: { var3: 'othervalue' } }
320 }
321 }
322 </textarea>
323 <textarea id=utils-libsonnet>
324 // utils.libsonnet
325 {
326 pairList(tab, kfield='name',
327 vfield='value'):: [
328 { [kfield]: k, [vfield]: tab[k] }
329 for k in std.objectFields(tab)
330 ],
331
332 namedObjectList(tab, name_field='name'):: [
333 tab[name] + { [name_field]: name }
334 for name in std.objectFields(tab)
335 ],
336 }
337 </textarea>
338 </div>
339 <div class="bigarrow">➡</div>
340 <div class="tab-window-output" id="collections-output">
341 <div class="tab-header">
342 <div class=selected onclick="tab_output_click(this, 'output-json')">
343 output.json
344 </div>
345 </div>
346 <textarea readonly class="selected code-json" id="output-json">
347 {
348 "containers": [
349 {
350 "env": [
351 {
352 "name": "var2",
353 "value": "somevalue"
354 },
355 {
356 "name": "var3",
357 "value": "othervalue"
358 }
359 ],
360 "name": "bar"
361 },
362 {
363 "env": [
364 {
365 "name": "var1",
366 "value": "somevalue"
367 },
368 {
369 "name": "var2",
370 "value": "somevalue"
371 }
372 ],
373 "name": "foo"
374 }
375 ]
376 }
377 </textarea>
378 </div>
379 <script>
380 demo(
381 'collections-input',
382 {
383 'pod-spec-jsonnet': 'pod-spec.jsonnet',
384 'utils-libsonnet': 'utils.libsonnet',
385 },
386 'pod-spec.jsonnet',
387 'collections-output',
388 false,
389 false
390 );
391 </script>
392 <div style="clear: both"></div>
393 </div>
394 </div>
395
396 <div class="hgroup">
397 <div class="hgroup-inline">
398 <div class="panel">
399 <p>
400 Such a pattern also makes referencing easier. Now, you can use the following code to access
401 the same environment variable:
402 </p>
403 <pre>PodSpec.containersObj.bar.envObj.var3</pre>
404 </div>
405 <div style="clear: both"></div>
406 </div>
407 </div>