-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurations
454 lines (331 loc) · 8.97 KB
/
Configurations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
Configurations
==============
Edit a POD
Remember, you CANNOT edit specifications of an existing POD other than the below.
spec.containers[*].image
spec.initContainers[*].image
spec.activeDeadlineSeconds
spec.tolerations
Creating configmaps
---------------
Imperative wat
> kubectl create <config-name> \
app-config --from-literal=<key>=<value>
kubectl create configmap app-config \
--from-literal=APP_COLOR=blue \
--from-literal=APP_MOD=prod
> kubectl create configmap <config-name> --from-file=<path to file>
kubectl create configmap app-config --from-file=app_config.properties
Declarative way
> kubectl create -f config-map.yaml
---config-map.yaml -----
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_COLOR: blue
APP_MODE: prod
ConfigMap in prod
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
- envFrom:
- configMapRef:
name: app-config
ConfigMaps can also be added as environment variables
env:
- name: APP_COLOR
valueFrom:
configMapRef:
name: app-config
key: APP_COLOR
volumes:
- name: app-config-volume
configMap:
name: app-config
SECRETS
-------
Secrets can be created the imperative way or the declarative way
> kubeclt create secret generic \
<secret-name> --from-literal=<key>=<value>
kubectl create secret generic \
my-secret --from-literal=DB_Host=mysql \
my-secret --from-literal=DB_User=root \
my-secret --from-literal=DB_Password=paswrd
Secrets can be inputted from a fila
> kubectl create secret generic \
<secret-name> --from-file=<path-to-file>
kubectl create secret generic \
secret-from-file --from-tile=app_secret.properties
The Declarative way
> kubectl create -f secret-data.yaml
secret-data.yaml
---
apiVersion: v1
kind: Secret
metadata:
name: app-secret
data:
DB_Host: mysql
DB_User: root
DB_Password: paswrd
You need to ecnore the data sent into the secret-data
echo -n 'mysql' | bas64
echo -n 'root' | bas64
echo -n 'paswrd' | bas64
New file becomes
secret-data.yaml
---
apiVersion: v1
kind: Secret
metadata:
name: app-secret
data:
DB_Host: =OONE!ejkx
DB_User: cm9vdA==
DB_Password: MLAEuce
> Then run the > kubectl create -f secret-data.yaml
> kubectl describe secret app-secret
Will give you details on the secret without the content of the secret secret-data
> kubectl get secret app-secret -o yaml
Will give you the ouput of the secrets and the base64 encoded data
To decode the values
> echo -n '=OONE!ejkx' | base64 --decode
Injecting the secret into a pod definition path-to-file
pod-definition.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 8080
envFrom:
- secretRef:
name: app-secret
Secrets can straightly be injectied as environment variables into pods
envFrom:
- secretRef:
name: app-secret
env:
- name: DB_Password
valueFrom:
secretKeyRef:
name: app-secret
key: DB_Password
volumes:
- name: app-secret-volume
secret:
secretName: app-secret
=== A quick note about Secrets! ===
Remember that secrets encode data in base64 format. Anyone with the base64 encoded secret can easily decode it. As such the secrets can be considered as not very safe.
The concept of safety of the Secrets is a bit confusing in Kubernetes. The kubernetes documentation page and a lot of blogs out there refer to secrets as a “safer option” to store sensitive data. They are safer than storing in plain text as they reduce the risk of accidentally exposing passwords and other sensitive data. In my opinion it’s not the secret itself that is safe, it is the practices around it.
Secrets are not encrypted, so it is not safer in that sense. However, some best practices around using secrets make it safer. As in best practices like:
Not checking-in secret object definition files to source code repositories.
Enabling Encryption at Rest for Secrets so they are stored encrypted in ETCD.
Also the way kubernetes handles secrets. Such as:
A secret is only sent to a node if a pod on that node requires it.
Kubelet stores the secret into a tmpfs so that the secret is not written to disk storage.
Once the Pod that depends on the secret is deleted, kubelet will delete its local copy of the secret data as well.
Read about the protections and risks of using secrets here
Having said that, there are other better ways of handling sensitive data like passwords in Kubernetes, such as using tools like Helm Secrets, HashiCorp Vault. I hope to make a lecture on these in the future.
kubectl create secret generic db-secret2 \
--from-literal=DB_Host=sql01 \
--from-literal=DB_User=root \
--from-literal=DB_Password=password123 \
--dry-run=client -o yaml > db-secret.yaml
SECURITY CONTEXT
------------------
pod-definition.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
command: ["sleep","3600"]
securityContext:
runAsUser: 1000
capabilities:
add: ["MAC_ADMIN"]
Ressource requirements
--------------------
Minimum amount of CPU and memory requested by a containers
CPU : 0.5
MEM : 265m
You can modify these values during pod definition
pod-definition.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
command: ["sleep","3600"]
securityContext:
runAsUser: 1000
capabilities:
add: ["MAC_ADMIN"]
resources:
requests:
memory: "1Gi3"
cpu: 1
Ressource limits
--
by default, k8s lsets the limit to 1 vcpu from the node or or 512 Mi for the memory
pod-definition.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
command: ["sleep","3600"]
securityContext:
runAsUser: 1000
capabilities:
add: ["MAC_ADMIN"]
resources:
requests:
memory: "1Gi3"
cpu: 1
limits:
memory: "2Gi"
cpu: 2
Service Accounts
---------
> kubectl create serviceaccount dashboard-sa
Taints and Tolerations
----------------------
> kubectl taint nodes node-name key=value:taint-effect
kubectl taint nodes node1 app=blue:NoSchedule
kubectl taint nodes node01 spray=mortein:NoSchedule
taint-effect defines what will happen to the pod if they do not tolerate the taint
There are 3 taint-effects :
NoSchedule,
PreferNoSchedule,
NoExecute
Tolerations are added to pods
pod-definition.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
command: ["sleep","3600"]
securityContext:
runAsUser: 1000
capabilities:
add: ["MAC_ADMIN"]
resources:
requests:
memory: "1Gi3"
cpu: 1
limits:
memory: "2Gi"
cpu: 2
tolerations:
- key: "app"
operator: "Equal"
value: "blue"
effect: "NoSchedule"
A taint is shed on the master node at cluster creation.
kubectl describe node kubemaster | grep Taint
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: bee
name: bee
spec:
containers:
- image: nginx
name: bee
tolerations:
- key: "spray"
operator: "Equal"
value: "mortein"
effect: "NoSchedule"
To remove taint on nodes
> kubectl taint nodes controlplane node-role.kubernetes.io/master:NoSchedule-
Node Selectors
--------------
Label nodes
> kubectl label nodes <node-name <label-key>=<label-value>
kubectl label nodes node01 size=Large
Now we create the pod with the node Selectors
apiVersion: v1
kind: Pod
metadata:
name: data-processor
spec:
containers:
- image: data-processor
name: data-processor
nodeSelector:
size: Large
Node Affinity
-------------
apiVersion: v1
kind: Pod
metadata:
name: data-processor
spec:
containers:
- image: data-processor
name: data-processor
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: size
operator: In
values:
- Large
Node Affinity TYpes
---
Available
requiredDuringSchedulingIgnoredDuringExecution
preferredDuringSchedulingIgnoredDuringExecution
Planned
requiredDuringScheduingRequiredDuringExecution
https://medium.com/@harioverhere/ckad-certified-kubernetes-application-developer-my-journey-3afb0901014