반응형
레이블(Label)이란?
- Node를 포함하여 pod, deployment 등 모든 리소스에 할당
- 리소스의 특성을 분류하고, Selector를 이용해서 선택
- Key-value 한쌍으로 적용
Label과 Selector
Label
metadata:
labels:
rel: stable
name: mainui
Selector
selector:
matchLabels:
key: value
matchExpressions:
- {key: name, opertor: In, values: [mainui]}
- {key: rel, operator: NotIn, values: ["beta","canary"]}
https://kubernetes.io/ko/docs/concepts/overview/working-with-objects/labels/
Label 명령어 테스트
커맨드 명령어를 통해서 pod를 하나 실행하고, label이 없는 pod를 하나와 label이 있는 pod 하나를 실행
※ yaml에서 label은 단순 문자열이 들어갈 경우 ""이 없어도됨. 단, TRUE, FALSE, YES, NO 등의 값은 제외
kubectl run cmdpod --image=nginx:1.14 --port=80
pod/cmdpod created
# cat pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
spec:
containers:
- name: nginx
image: nginx:1.14
ports:
- containerPort: 80
# cat pod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: label-pod-demo
labels:
name: mainui
rel: stable
spec:
containers:
- name: nginx
image: nginx:1.14
ports:
- containerPort: 80
# kubectl create -f pod1.yaml -f pod2.yaml
pod/pod-demo created
pod/label-pod-demo created
# kubectl get pods
NAME READY STATUS RESTARTS AGE
cmdpod 1/1 Running 0 6m35s
label-pod-demo 1/1 Running 0 9s
pod-demo 1/1 Running 0 9s
label 정보 확인
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 8m25s run=cmdpod
label-pod-demo 1/1 Running 0 119s name=mainui,rel=stable
pod-demo 1/1 Running 0 119s <none>
kubectl get pods -l name=[label명]
- label명이 같은 pod를 출력(selector)
# kubectl get pods -l name=mainui
NAME READY STATUS RESTARTS AGE
label-pod-demo 1/1 Running 0 3m4s
# kubectl get pods --selector name=mainui
NAME READY STATUS RESTARTS AGE
label-pod-demo 1/1 Running 0 3m54s
lebel 명령어를 이용하여 추가 가능
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 11m run=cmdpod
label-pod-demo 1/1 Running 0 4m58s name=mainui,rel=stable
pod-demo 1/1 Running 0 4m58s <none>
# kubectl label pod pod-demo name=test
pod/pod-demo labeled
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 11m run=cmdpod
label-pod-demo 1/1 Running 0 5m26s name=mainui,rel=stable
pod-demo 1/1 Running 0 5m26s name=test
--overwrite 속성 : 이미 존재하는 label 위에 덮어쓰기
# kubectl label pod pod-demo name=login
error: 'name' already has a value (test), and --overwrite is false
# kubectl label pod pod-demo name=login --overwrite
pod/pod-demo labeled
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 14m run=cmdpod
label-pod-demo 1/1 Running 0 7m37s name=mainui,rel=stable
pod-demo 1/1 Running 0 7m37s name=login
key=value 형식에 맞춰 label 복수 할당
- 이미 있는 label이라면 --overwrite 필수!
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 14m run=cmdpod
label-pod-demo 1/1 Running 0 7m37s name=mainui,rel=stable
pod-demo 1/1 Running 0 7m37s name=login
# kubectl label pod cmdpod name=order rel=beta
pod/cmdpod labeled
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 16m name=order,rel=beta,run=cmdpod
label-pod-demo 1/1 Running 0 10m name=mainui,rel=stable
pod-demo 1/1 Running 0 10m name=login
label명 뒤에 - (대시문자)
- label 삭제
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 16m name=order,rel=beta,run=cmdpod
label-pod-demo 1/1 Running 0 10m name=mainui,rel=stable
pod-demo 1/1 Running 0 10m name=login
# kubectl label pod cmdpod run-
pod/cmdpod unlabeled
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 17m name=order,rel=beta
label-pod-demo 1/1 Running 0 11m name=mainui,rel=stable
pod-demo 1/1 Running 0 11m name=login
label로 묶은 내용들은 selector로 확인 가능
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
cmdpod 1/1 Running 0 19m name=order,rel=beta
label-pod-demo 1/1 Running 0 13m name=mainui,rel=stable
pod-demo 1/1 Running 0 13m name=login,rel=stable
# kubectl get pods --selector rel=stable
NAME READY STATUS RESTARTS AGE
label-pod-demo 1/1 Running 0 13m
pod-demo 1/1 Running 0 13m
# kubectl get pods --selector rel=beta
NAME READY STATUS RESTARTS AGE
cmdpod 1/1 Running 0 20m
# kubectl delete pods --selector rel=beta
pod "cmdpod" deleted
# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
label-pod-demo 1/1 Running 0 14m name=mainui,rel=stable
pod-demo 1/1 Running 0 14m name=login,rel=stable
[참고]
- https://boansecurity.tistory.com/manage/newpost/?type=post&returnURL=%2Fmanage%2Fposts%2F
반응형
'Kubernetes' 카테고리의 다른 글
[Kubernetes] Annotation (0) | 2022.12.17 |
---|---|
[Kubernetes] kubernetes node label (0) | 2022.12.17 |
[Kubernetes] Ingress 실습 - 웹페이지 구현 (0) | 2022.12.14 |
[Kubernetes] Ingress 개념과 Ingress Controller 설치 (0) | 2022.12.14 |
[Kubernetes] Headless Service와 Kube Proxy (0) | 2022.12.14 |