-
[따배쿠] Labelkubernetes 2024. 12. 10. 10:15
Label
- Node를 포함하여 pod, deployment 등 모든 리소스에 할당 가능
- 리소스의 특성을 분류할 목적으로 사용
- Selector를 이용해서 선택
- Key-Value 한 쌍으로 적용
Label과 Selector
Label 관리 명령어
예시
# cmd 용도 pod 생성
root@master:~# kubectl run cmdpod --image=nginx:1.14 --port=80 pod/cmdpod created
# pod1.yaml 생성 (label X)
root@master:~/Getting-Start-Kubernetes/9# cat > pod1.yaml apiVersion: v1 kind: Pod metadata: name: pod-demo spec: containers: - name: nginx image: nginx:1.14 ports: - containerPort: 80
# pod2.yaml 생성 (label O)root@master:~/Getting-Start-Kubernetes/9# vi 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
# pod1 및 pod2 생성
root@master:~/Getting-Start-Kubernetes/9# kubectl create -f pod1.yaml -f pod2.yaml pod/pod-demo created pod/label-pod-demo created
# 확인
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS cmdpod 1/1 Running 0 7m14s run=cmdpod label-pod-demo 1/1 Running 0 91s name=mainui,rel=stable pod-demo 1/1 Running 0 91s <none>
label 정보를 표시한 pod 조회
- cmdpod : command line에서 실행한 pod는 자동으로 label이 할당된다. (run=cmdpod)- l
- label-pod-demo : label을 할당한 pod (name=mainui, rel=stable)
- pod-demo : label을 할당하지 않은 pod
# 활용
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods -l name=mainui NAME READY STATUS RESTARTS AGE label-pod-demo 1/1 Running 0 4m18s
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --selector name=mainui NAME READY STATUS RESTARTS AGE label-pod-demo 1/1 Running 0 5m24s
*kubectl get [리소스 타입] -l name=[label명] : 해당 label명을 가진 pod 출력 (selector의 기능과 동일)
*kubectl get [리소스 타입] --selector name=[label명] : : 해당 label명을 가진 pod 출력
# command line으로 label 할당
root@master:~/Getting-Start-Kubernetes/9# kubectl label pod pod-demo name=test pod/pod-demo labeled
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS cmdpod 1/1 Running 0 13m run=cmdpod label-pod-demo 1/1 Running 0 7m46s name=mainui,rel=stable pod-demo 1/1 Running 0 7m46s name=test
*kubectl label [리소스 타입] [리소스 명] [key=value] : key:value라는 label을 리소스에 할당
# label 변경
root@master:~/Getting-Start-Kubernetes/9# kubectl label pod pod-demo name=login error: 'name' already has a value (test), and --overwrite is false
이미 name=test라는 label이 할당되어있는데, label을 변경하려고 하면 되지 않는다.
root@master:~/Getting-Start-Kubernetes/9# kubectl label pod pod-demo name=login --overwrite pod/pod-demo labeled
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS cmdpod 1/1 Running 0 17m run=cmdpod label-pod-demo 1/1 Running 0 11m name=mainui,rel=stable pod-demo 1/1 Running 0 11m name=login
*kubectl label [리소스 타입] [리소스 명] [key=value] --overwrite : 이미 할당된 Key의 label 변경
이럴때 --overwirte 옵션을 붙여주면 정상적으로 overwrite 된다.
# label 복수 할당
root@master:~/Getting-Start-Kubernetes/9# kubectl label pod cmdpod name=order rel=beta pod/cmdpod labeled root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS cmdpod 1/1 Running 0 19m name=order,rel=beta,run=cmdpod label-pod-demo 1/1 Running 0 14m name=mainui,rel=stable pod-demo 1/1 Running 0 14m name=login
*kubectl label [리소스 타입] [리소스 명] [key=value][key=value] … : label을 복수 할당하고 싶을 때는 이어서 작성해주면 된다.
# label 삭제
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS cmdpod 1/1 Running 0 21m name=order,rel=beta,run=cmdpod label-pod-demo 1/1 Running 0 15m name=mainui,rel=stable pod-demo 1/1 Running 0 15m name=login
root@master:~/Getting-Start-Kubernetes/9# kubectl label pod cmdpod run- pod/cmdpod unlabeled
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS cmdpod 1/1 Running 0 22m name=order,rel=beta label-pod-demo 1/1 Running 0 16m name=mainui,rel=stable pod-demo 1/1 Running 0 16m name=login
* label명 뒤에 - (대시) : label 삭제
# 활용
root@master:~/Getting-Start-Kubernetes/9# kubectl label pod pod-demo rel=stable pod/pod-demo labeled
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS cmdpod 1/1 Running 0 23m name=order,rel=beta label-pod-demo 1/1 Running 0 18m name=mainui,rel=stable pod-demo 1/1 Running 0 18m name=login,rel=stable
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --selector rel=stable NAME READY STATUS RESTARTS AGE label-pod-demo 1/1 Running 0 18m pod-demo 1/1 Running 0 18m
root@master:~/Getting-Start-Kubernetes/9# kubectl get pods --selector rel=beta NAME READY STATUS RESTARTS AGE cmdpod 1/1 Running 0 24m
참고 영상
https://www.youtube.com/watch?v=yf5syEBAg2Y&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=30
'kubernetes' 카테고리의 다른 글
[따배쿠] Annotation (0) 2024.12.10 [따배쿠] Node Label (0) 2024.12.10 [따배쿠] Ingress - 웹페이지 구현 (0) 2024.12.09 [따배쿠] Ingress - 개념 및 설치 (0) 2024.12.09 [따배쿠] Headless Service, Kube-Proxy (0) 2024.12.08