Skip to content

Commit ad68f45

Browse files
committed
adding labels to pods
1 parent 50de08e commit ad68f45

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

Diff for: kubia-manual-with-labels.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: kubia-manual-v2
5+
labels:
6+
creation_method: manual
7+
env: prod
8+
spec:
9+
containers:
10+
- image: knrt10/kubia
11+
name: kubia
12+
ports:
13+
- containerPort: 8080
14+
protocol: TCP

Diff for: readme.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ This is just a simple demonstration to get a basic understanding of how kubernet
4242
- [Using kubectl create to create the pod](#using-kubectl-create-to-create-the-pod)
4343
- [Retrieving a PODs logs with Kubectl logs](#retrieving-a-pods-logs-with-kubectl-logs)
4444
- [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)
4548

4649
## Requirements
4750

@@ -403,7 +406,7 @@ Going through all the individual properties in the previous YAML doesn’t make
403406

404407
#### Creating a simple YAML descriptor for a pod
405408

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.
407410

408411
```yaml
409412
apiVersion: v1
@@ -460,3 +463,60 @@ In a different terminal, you can now use curl to send an HTTP request to your po
460463
> You've hit kubia-manual
461464

462465
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:
498+
499+
`kubectl get po --show-labels`
500+
501+
```bash
502+
NAME READY STATUS RESTARTS AGE LABELS
503+
kubia-5k788 1/1 Running 1 8d run=kubia
504+
kubia-7zxwj 1/1 Running 1 5d run=kubia
505+
kubia-bsksp 1/1 Running 1 5d run=kubia
506+
kubia-manual 1/1 Running 0 7h <none>
507+
kubia-manual-v2 1/1 Running 0 3m creation_method=manual,env=prod
508+
```
509+
510+
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

Comments
 (0)