[CKAD] Labels

HongSeokChoi·2025년 9월 3일

ckad

목록 보기
11/14

라벨(Label) vs 주석(Annotation) vs 선택기(Selector)

1. 라벨(Label)

  • 정의: 쿠버네티스 오브젝트에 붙이는 Key-Value 속성.
  • 용도: 객체를 분류, 검색, 그룹화할 때 사용.
    예) app=frontend, tier=backend, env=prod
  • 예시:
    • 블로그 태그, 쇼핑몰 필터, 유튜브 키워드와 비슷.
    • 사용 예시: kubectl get pods -l app=myappapp=myapp 라벨을 가진 포드 조회.

2. 선택기(Selector)

  • 정의: 특정 조건의 라벨을 가진 오브젝트를 선택하는 방법.
  • 연산자: =, !=, in, notin 등으로 조건 세밀하게 설정 가능.
  • 용도:
    • kubectl get pods -l app=myapp → 라벨 app=myapp 가진 포드를 선택.
    • 여러 오브젝트를 동적으로 연결하거나 필터링할 때 사용.

3. 라벨 vs 주석

  • 라벨:
    • 목적: 쿠버네티스의 내부 동작에 사용 (선택/필터링/연결).
    • 예시: app, tier, env
  • 주석:
    • 목적: 추가 설명 및 관리용 메타데이터 기록 (동작에 직접 관여하지 않음).
    • 예시: description, contact, buildVersion

ReplicaSet과 Labels, Selectors

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    component: replicaset
    env: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
        tier: frontend
        env: dev
    spec:
      containers:
        - name: myapp-container
          image: nginx:1.27-alpine

ReplicaSet 자체 라벨 (metadata.labels)

  • ReplicaSet 리소스를 식별하기 위한 라벨

  • 다른 오브젝트가 ReplicaSet을 찾을 때 사용

Selector.matchLabels

  • ReplicaSet이 관리할 Pod을 찾는 기준이 되는 라벨

  • Pod 템플릿에 동일 라벨이 정의되어야 함

Pod 템플릿 라벨 (template.metadata.labels)

  • ReplicaSet이 생성하는 Pod에 붙는 라벨

  • ReplicaSet의 selector.matchLabels와 일치해야만 관리 가능

동작 시나리오 예시

  • replicas: 3, selector.matchLabels=app:myapp

  • 클러스터에 app=myapp 라벨을 가진 Pod이 1개 있을 때, ReplicaSet은 2개를 더 만들고, 총 3개의 Pod를 관리하게 됨.

Service와 Labels, Selectors

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
  labels:
    component: service
    env: dev
spec:
  type: ClusterIP
  selector:
    app: myapp
  ports:
    - port: 80
      targetPort: 8080

Service 자체 라벨 (metadata.labels)

- Service 리소스를 식별하기 위한 라벨

- 다른 오브젝트에서 Service를 참조하거나 관리할 때 사용

Service Selector (spec.selector)

- Service가 연결할 Pod을 찾기 위한 기준

- Pod에 동일한 라벨이 있어야만 Service가 Pod로 트래픽을 전달

Pod ↔ Service 연결

- Pod의 라벨 app=myapp → Service selector app=myapp 일치시, 트래픽 전달.

Annotation (주석)

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
  annotations:
    description: "This pod runs the myapp frontend"
    contact: "team-frontend@example.com"
    buildVersion: "1.2.3"
spec:
  containers:
    - name: myapp-container
      image: nginx:1.27-alpine

Annotation 정의 (metadata.annotations):

  • Key-Value 쌍으로 메타데이터 기록

  • 선택/필터링에는 사용되지 않음

활용 예시:

  • description: Pod에 대한 설명

  • contact: 관리자의 연락처

  • buildVersion: 버전 정보

profile
Network/Cloud

0개의 댓글