You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+61-1
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,9 @@ This is just a simple demonstration to get a basic understanding of how kubernet
42
42
-[Using kubectl create to create the pod](#using-kubectl-create-to-create-the-pod)
43
43
-[Retrieving a PODs logs with Kubectl logs](#retrieving-a-pods-logs-with-kubectl-logs)
44
44
-[Forwarding a Local Network to a port in the Pod](#forwarding-a-local-network-to-a-port-in-the-pod)
45
+
-[Introducing labels](#introducing-labels)
46
+
-[Specifying labels when creating a pod](#specifying-labels-when-creating-a-pod)
47
+
-[Modifying labels of existing pods](#modifying-labels-of-existing-pods)
45
48
46
49
## Requirements
47
50
@@ -403,7 +406,7 @@ Going through all the individual properties in the previous YAML doesn’t make
403
406
404
407
#### Creating a simple YAML descriptor for a pod
405
408
406
-
You’re going to create a file called kubia-manual.yaml (you can create it in any directory you want), or copy from this repo, where you’ll find the file with filename [kubia-manual.yaml](https://github.com/knrt10/kubernetes-basicLearning/blob/master/kubia-manual.yaml). The following listing shows the entire contents of the file.
409
+
You’re going to create a file called **kubia-manual.yaml** (you can create it in any directory you want), or copy from this repo, where you’ll find the file with filename [kubia-manual.yaml](https://github.com/knrt10/kubernetes-basicLearning/blob/master/kubia-manual.yaml). The following listing shows the entire contents of the file.
407
410
408
411
```yaml
409
412
apiVersion: v1
@@ -460,3 +463,60 @@ In a different terminal, you can now use curl to send an HTTP request to your po
460
463
> You've hit kubia-manual
461
464
462
465
Using port forwarding like this is an effective way to test an individual pod.
466
+
467
+
#### Introducing labels
468
+
469
+
Organizing pods and all other Kubernetes objects is done through labels. Labels are a simple, yet incredibly powerful, Kubernetes feature for organizing not only pods, but all other Kubernetes resources. A label is an arbitrary key-value pair you attach to a resource, which is then utilized when selecting resources using label selectors (resources are filtered based on whether they include the label specified in the selector).
470
+
471
+
#### Specifying labels when creating a pod
472
+
473
+
Now, you’ll see labels in action by creating a new pod with two labels. Create a new file called **kubia-manual-with-labels.yaml** with the contents of the following listing. You can also copy from [kubia-manual-with-labels.yaml](https://github.com/knrt10/kubernetes-basicLearning/blob/master/kubia-manual-with-labels.yaml)
474
+
475
+
```yaml
476
+
apiVersion: v1
477
+
kind: Pod
478
+
metadata:
479
+
name: kubia-manual-v2
480
+
labels:
481
+
creation_method: manual
482
+
env: prod
483
+
spec:
484
+
containers:
485
+
- image: knrt10/kubia
486
+
name: kubia
487
+
ports:
488
+
- containerPort: 8080
489
+
protocol: TCP
490
+
```
491
+
492
+
You’ve included the labels *creation_method=manual* and *env=data.labels* section. You’ll create this pod now:
493
+
494
+
`kubectl create -f kubia-manual-with-labels.yaml`
495
+
> pod/kubia-manual-v2 created
496
+
497
+
The **kubectl get po** command doesn’t list any labels by default, but you can see them by using the **--show-labels** switch:
Instead of listing all labels, if you’re only interested in certain labels, you can specify them with the **-L** switch and have each displayed in its own column. List pods again and show the columns for the two labels you’ve attached to your **kubia-manual-v2** pod:
511
+
512
+
`kubectl get po -L creation_method,env`
513
+
514
+
#### Modifying labels of existing pods
515
+
516
+
Labels can also be added to and modified on existing pods. Because the **kubia-manual** pod was also created manually, let’s add the **creation_method=manual** label to it:
517
+
518
+
`kubectl label po kubia-manual creation_method=manual`
519
+
520
+
Now, let’s also change the **env=prod** label to **env=debug** on the **kubia-manual-v2** pod, to see how existing labels can be changed. You need to use the **--overwrite** option when changing existing labels.
521
+
522
+
`kubectl label po kubia-manual-v2 env=debug --overwrite`
0 commit comments