Labels, Selectors
Labels, Selectors
개념
labels: 객체(Pod,Service등)에 붙이는 속성(Key-Value)selectors:labels를 기준으로 객체를 필터링하는 방법- AND 연산 - 여러
Selectors조건이 있으면 모두 만족하는 객체 찾음
- AND 연산 - 여러
왜 필요?
- 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에 사용되지 않고, 단순히 정보를 기록하는 메타데이터이다.- 버전, 연락처, 필요한 정보 등을 기록한다.
레퍼런스
- https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
- Udemy - Certified Kubernetes Administrator (CKA) with Practice Tests (Mumshad)