Post

Kubernetes - Labels

Lab assignment for creating labels using kubectl

Kubernetes - Labels

Prerequisites

  • Kubernetes
  • kubectl

Assignment

1️⃣ Lets start by creating a pod and verify the default labels.

1
kubectl run nginx --image nginx --port 80 -o yaml --dry-run=client
1
kubectl run nginx --image nginx --port 80

2️⃣ Expose the pod based on the labels (check the yaml definition)).

1
kubectl expose pod/nginx --dry-run=client -o yaml
1
kubectl expose pod/nginx
1
kubectl get all --selector run=nginx

3️⃣ Create Pod with different labels and query them using selectors.

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
cat <<EOF > coloured_pods.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: ubuntu
    colour: red
  name: ubuntu-red
spec:
  containers:
  - command:
    - sleep
    - infinity
    image: ubuntu
    name: ubuntu
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: ubuntu
    colour: green
  name: ubuntu-green
spec:
  containers:
  - command:
    - sleep
    - infinity
    image: ubuntu
    name: ubuntu
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: ubuntu
    colour: pink
  name: ubuntu-pink
spec:
  containers:
  - command:
    - sleep
    - infinity
    image: ubuntu
    name: ubuntu
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
EOF
1
kubectl apply -f coloured_pods.yaml
1
kubectl get pods -o wide
1
kubectl get all --selector colour=green
1
kubectl get all -l colour=green

4️⃣ CleanUp the resources

1
kubectl delete pod/nginx service/nginx pod/ubuntu-red pod/ubuntu-green pod/ubuntu-pink --now
1
rm coloured_pods.yaml
This post is licensed under CC BY 4.0 by the author.