Scheduling-Labels and Selectors
2024. 12. 31. 03:20ㆍ쿠버네티스/쿠버네티스
728x90
반응형
1. Labels와 Selectors란?
Labels (레이블)
- 정의:
- Kubernetes 객체(Pod, Service, ReplicaSet 등)에 추가하는 키-값 쌍의 속성.
- 객체를 그룹화하고 특정 기준으로 필터링하는 데 사용.
- 예: app=frontend, env=production
- 특징:
- 객체에 메타데이터를 추가하여 분류 가능.
- 여러 레이블을 추가할 수 있음.
- 특정 레이블을 기반으로 Kubernetes 리소스 간 연결 설정.
Selectors (셀렉터)
- 정의:
- 레이블을 기반으로 특정 Kubernetes 객체를 선택하는 방법.
- 예: app=frontend로 모든 프론트엔드 Pod 선택.
- 특징:
- 단일 조건: 단일 레이블을 기준으로 객체 선택.
- 예: app=frontend
- 다중 조건: 여러 레이블을 조합하여 필터링.
- 예: app=frontend AND env=production
- 단일 조건: 단일 레이블을 기준으로 객체 선택.
2. Labels와 Selectors의 활용
1) Pod 정의 파일에서 Labels 추가
- Pod에 레이블 추가 예시:
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: frontend
env: production
spec:
containers:
- name: nginx-container
image: nginx
2) Selectors로 Pod 필터링
- 특정 레이블을 가진 Pod 조회 명령어:
kubectl get pods --selector app=frontend
3. Labels와 Selectors의 내부 동작
1) ReplicaSet과 Labels
- ReplicaSet은 특정 레이블을 가진 Pod를 관리.
- ReplicaSet 정의 파일에서 template.metadata.labels와 spec.selector.matchLabels를 연결하여 Pod와 ReplicaSet 간 관계 설정.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: frontend
env: production
template:
metadata:
labels:
app: frontend
env: production
spec:
containers:
- name: nginx-container
image: nginx
2) Service와 Labels
- Service는 레이블 셀렉터를 사용하여 특정 Pod와 연결.
- Service 정의 파일 예시:
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: frontend
env: production
ports:
- protocol: TCP
port: 80
targetPort: 8080
- 위 Service는 app=frontend 및 env=production 레이블을 가진 모든 Pod에 트래픽 전달.
4. Annotations (주석)
정의
- 객체에 부가 정보를 기록하기 위한 키-값 쌍.
- 레이블과 달리, 선택 및 필터링에는 사용되지 않음.
활용 사례
- 빌드 정보 저장 (예: 버전, 빌드 시간)
- 연락처 정보 (예: 이메일, 전화번호)
- 외부 툴과의 통합 정보


We have deployed a number of PODs. They are labelled with tier, env and bu. How many PODs exist in the dev environment (env)?
- kubecget pods --selector env=dev --no-headers | wc -l
How many PODs are in the finance business unit
- kubectl get pod --selector bu=finance
How many objects are in the prod environment including PODs, ReplicaSets and any other objects?
- kubectl get all --selector env=prod
Identify the POD which is part of the prod environment, the finance BU and of frontend tier?
- kubectl get all --selector env=prod,bu=finance,tier=frontend
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.
- The ReplicaSet "replicaset-" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"tier":"nginx"}: `selector` does not match template `labels`
- labels "tier: ningx"를 "tier: front-end'로 수정 또는 반대로 "tier: front-end'를 "tier: ningx"로 변경
반응형
'쿠버네티스 > 쿠버네티스' 카테고리의 다른 글
Scheduling-Node Selectors-Node Affinity (0) | 2024.12.31 |
---|---|
Scheduling-Taints and Tolerations (0) | 2024.12.31 |
Scheduling-Manual Scheduling (0) | 2024.12.31 |
Imperative vs Declarative (0) | 2024.12.31 |
Namespaces (0) | 2024.12.31 |