본문 바로가기
IT 기술/k8s

[cka] Labels and Selectors

by Geunny 2024. 6. 8.
반응형

1. We have deployed a number of PODs. They are labelled with tier, env and bu. How many PODs exist in the dev environment (env)?
Use selectors to filter the output

 

--selector 옵션을 통해 등록된 pods의 Label을 선택 할 수 있다.

 

$ controlplane ~ ➜  kubectl get pods --selector env=dev --no-headers
db-1-sft8z    1/1   Running   0     89s
db-1-hx2th    1/1   Running   0     89s
app-1-8jvbt   1/1   Running   0     89s
app-1-8n9n4   1/1   Running   0     89s
db-1-fmhl6    1/1   Running   0     89s
app-1-d8hv5   1/1   Running   0     89s
db-1-svxd6    1/1   Running   0     89s

$ controlplane ~ ➜  kubectl get pods --selector env=dev --no-headers | wc -l
7

 

--no-headers 옵션을 통해 맨 윗줄 컬럼 설명줄도 생략할 수 있다.

 

answer : 7


2. How many PODs are in the finance business unit (bu)?

$ controlplane ~ ➜  k get pods --selector bu=finance --no-headers| wc -l
6

$ controlplane ~ ➜  k get pods --selector bu=finance
NAME          READY   STATUS    RESTARTS   AGE
app-1-8jvbt   1/1     Running   0          8m58s
app-1-zzxdf   1/1     Running   0          8m58s
app-1-8n9n4   1/1     Running   0          8m58s
db-2-gccws    1/1     Running   0          8m58s
app-1-d8hv5   1/1     Running   0          8m58s
auth          1/1     Running   0          8m58s

 

answer : 6

 

3.  How many objects are in the prod environment including PODs, ReplicaSets and any other objects?

$ controlplane ~ ➜  k get all --selector env=prod
NAME              READY   STATUS    RESTARTS   AGE
pod/app-1-zzxdf   1/1     Running   0          12m
pod/db-2-gccws    1/1     Running   0          12m
pod/auth          1/1     Running   0          12m
pod/app-2-whx5v   1/1     Running   0          12m

NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/app-1   ClusterIP   10.43.191.110   <none>        3306/TCP   12m

NAME                    DESIRED   CURRENT   READY   AGE
replicaset.apps/db-2    1         1         1       12m
replicaset.apps/app-2   1         1         1       12m

 

해당 selector를 포함하는 모든 오브젝트 확인을 위해 kubectl get all 커맨드를 사용.

 

answer : 7

 

4.  Identify the POD which is part of the prod environment, the finance BU and of frontend tier?

 

여러개의 selector 를 보려면 콤마(,) 를 통해 나열할 수 있다.

 

$ controlplane ~ ✖ k get pods --selector env=prod,bu=finance,tier=frontend
NAME          READY   STATUS    RESTARTS   AGE
app-1-zzxdf   1/1     Running   0          14m

 

 

answer : app-1-zzxdf

 

5. A ReplicaSet definition file is given replicaset-definition-1.yaml. Attempt to create the replicaset; you will encounter an issue with the file. Try to fix it. Once you fix the issue, create the replicaset from the definition file.

 

## "replicaset-definition-1.yaml" 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
   name: replicaset-1
spec:
   replicas: 2
   selector:
      matchLabels:
        tier: front-end
   template:
     metadata:
       labels:
        tier: nginx # selector 값이 위 machLabels 와 맞지 않는다. nginx -> front-end 로 변경
     spec:
       containers:
       - name: nginx
         image: nginx

'IT 기술 > k8s' 카테고리의 다른 글

[cka] Node Affinity  (0) 2024.06.08
[cka] Taints and Tolerations  (0) 2024.06.08
[cka] Manual Scheduling  (0) 2024.06.08
[cka] Imperative Command  (1) 2024.06.01
[cka] Services  (0) 2024.06.01

댓글