본문으로 건너뛰기

Labels, Selectors, Annotation

December 8, 2025

Labels, Selectors​


개념​

  • labels: 객체(Pod, Service 등)에 붙이는 속성(Key-Value)
  • selectors: labels를 기준으로 객체를 필터링하는 방법
    • AND 연산이다.
      • 여러 Selectors 조건이 있으면 모두 만족하는 객체 찾음

왜 필요?​

  • Kubernetes에서는 수많은 Pod, Service, ReplicaSet, Deployment 객체들이 생긴다. 이 객체들을 역할별로 묶고, 특정 조건에 맞는 객체만 조회하거나, 이 객체들 사이의 연결을 만드려면 labels와 selectors가 필요하다.

사용 예시시​

kubectl get pods --selector app=myapp
  • app: myapp 이라는 label을 가진 Pod들만 조회한다.
...
spec:
template:
metadata:
labels:
app: myapp
...
selector:
matchLabels:
app: myapp
...
  • 위와같이 ReplicaSet -> Pod / Deployment -> Pod를 연결할 때 사용한다.
  • spec.selector.matchLabels에 정의된 모든 key-value 쌍은spec.template.metadata.labels에 포함되어 있어야 한다. (즉, spec.selector.matchLabels는 spec.template.metadata.labels의 부분집합이어야 한다)
    • spec.template.metadata.labels에 env: dev, type: front-end 라는 조건이 있다고 하자, 이때 spec.selector.matchLabels에 end: dev만 있을 경우 생성이 가능하다. (다만, type: back-end와 같은 기존 Pod도 포함될 수 있으므로 일치시키는 것이 좋다)
...
spec:
template:
metadata:
labels:
app: myapp
...
selector:
app: myapp
...
  • Service에서도 동일하게 동작한다.

Annotation​

metadata:
annotations:
buildVersion: "1.2.3"
contact: "dev@example.com"
  • annotations는 selector에 사용되지 않고, 단순히 정보를 기록하는 메타데이터이다.
  • 버전, 연락처, 필요한 정보 등을 기록한다.

레퍼런스​