본문 바로가기

⭐ Kubernetes & EKS/레이블 & 셀렉터 (Lable & Selector)

쿠버네티스 - 레이블과 셀렉터 연습문제

# 레이블과 셀렉터 연습문제

레이블 : 레이블(Label)사용자에게 의미 있고 관련성 높은 특징으로 식별할 수 있도록 오브젝트에 태그를 붙인다.
레이블은 파드와 같은 오브젝트에 붙일 수 있는 키/값 쌍이다. 레이블은 오브젝트의 하위 집합을 구성하고 선택하는데 사용된다.

셀렉터 : 셀렉터(Selector)사용자가 레이블에 따라서 리소스 리스트를 필터할 수 있게 한다.
셀렉터는 리소스 리스트를 질의할 때 리스트를 레이블에 따라서 필터하기 위해서 적용된다.

cp http-go-pod-v2.yaml nginx-pod.yaml

gedit nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
      protocol: TCP
kubectl create -f nginx-pod.yaml

kubectl get pod -w

# nginx 서비스를 포트포워딩을 사용하여 구현

sudo kubectl port-forward nginx 80:80

# nginx 레이블을 조회하라

kubectl get pod -l app=nginx

# nginx 레이블의 app을 확인하라

kubectl get pod -l app=nginx -L app

kubectl get pod -L app

# nginx 레이블에 새로운 정보를 추가하라

kubectl label pod nginx team=dev1

kubectl get pod --show-labels

# 추가된 레이블 정보를 확인 (레이블 추가와 작업조회 확인)
라벨링 하는 방법 : kubectl label pod [pod name] [레이블값=레이블값 정보]

ec2-user:~/environment/yaml $ kubectl label pod nginx team=dev1
pod/nginx labeled

ec2-user:~/environment/yaml $ kubectl get pod --show-labels
NAME              READY   STATUS    RESTARTS   AGE     LABELS
http-go-gpu       0/2     Pending   0          24h     security.istio.io/tlsMode=istio,service.istio.io/canonical-name=http-go-gpu,service.istio.io/canonical-revision=latest
nginx             2/2     Running   0          3m54s   app=nginx,security.istio.io/tlsMode=istio,service.istio.io/canonical-name=nginx,service.istio.io/canonical-revision=latest,team=dev1
nginx-redis-pod   3/3     Running   0          24m     security.istio.io/tlsMode=istio,service.istio.io/canonical-name=nginx-redis-pod,service.istio.io/canonical-revision=latest