쿠버네티스 교육/강의 내용 정리

220621_2_k8s_파드_레이블, 어노테이션

kimhope 2022. 6. 21. 16:33
728x90

label


 

label: 레이블

label이란

  • 레이블은 쿠버네티스 클러스터의 모든 오브젝트(파드 포함)에 키/값 쌍으로 리소스를 식별하고 속성을 지정하는 데 사용
  • 오브젝트 갯수가 많아진다면 오브젝트를 식별하는 데 매우 어려울 수 있으며, 오브젝트의 적절한 레이블을 부여하여 성격을 정의하고 검색을 용이하게 할 수 있음
  • 레이블은 사용자에게는 중요하지만, 쿠버네티스 클러스터에 직접적인 의미는 없음

 

label이 포함한 yml 파일로 파드 생성

  • testapp-pod-lable1.yml 파일 생성
vagrant@kube-master1:~/test$ vi testapp-pod-label.yml

apiVersion: v1
kind: Pod
metadata:
  name: testapp-pod-label1
  labels:
    env: dev
    tier: frontend
spec:
  containers:
  - name: testapp
    image: c1t1d0s7/myweb
    ports:
    - containerPort: 8080
      protocol: TCP

 

  • 파드 생성
vagrant@kube-master1:~/test$ kubectl create -f testapp-pod-label.yml
pod/testapp-pod-label1 created

 

  • 실행 중인 파드 확인
vagrant@kube-master1:~/test$ kubectl get pods
NAME                 READY   STATUS              RESTARTS   AGE
testapp-pod          1/1     Running             0          51m
testapp-pod-label1   0/1     ContainerCreating   0          7s

 

  • 레이블 확인
  • LABELS 값 확인
vagrant@kube-master1:~/test$ kubectl get pods --show-labels
NAME                 READY   STATUS    RESTARTS   AGE    LABELS
testapp-pod          1/1     Running   0          53m    <none>
testapp-pod-label1   1/1     Running   0          2m8s   env=dev,tier=frontend

vagrant@kube-master1:~/test$ kubectl get pods --show-labels -l env=dev
NAME                 READY   STATUS    RESTARTS   AGE     LABELS
testapp-pod-label1   1/1     Running   0          3m48s   env=dev,tier=frontend

 

  • 기존 파드에 label 생성
  • name=label 추가
vagrant@kube-master1:~/test$ kubectl label pods testapp-pod-label1 name=label
pod/testapp-pod-label1 labeled

vagrant@kube-master1:~/test$ kubectl get pods --show-labels
NAME                 READY   STATUS    RESTARTS   AGE   LABELS
testapp-pod          1/1     Running   0          65m   <none>
testapp-pod-label1   1/1     Running   0          14m   env=dev,name=label,tier=frontend

 

  • 기존 파드의 label 수정
  • env=dev 값을 env=ops 값으로 수정
vagrant@kube-master1:~/test$ kubectl label pods testapp-pod-label1 env=ops --overwrite
pod/testapp-pod-label1 labeled

vagrant@kube-master1:~/test$ kubectl get pods --show-labels
NAME                 READY   STATUS    RESTARTS   AGE   LABELS
testapp-pod          1/1     Running   0          63m   <none>
testapp-pod-label1   1/1     Running   0          12m   env=ops,name=label,tier=frontend

 


annotation


 

annotation: 어노테이션

어노테이션이란

  • 주석
  • 오브젝트에 비-식별 메타데이터를 지정하여 추가적인 정보를 제공하기 위해 사용
  • 레이블은 레이블 셀렉터를 이용하여 식별 및 검색을 할 수 있지만, 어노테이션은 셀렉터를 가지고 있지 않음
  • 어노테이션 사용 예: 선언적 구성 정보, 타임 스탬프, 릴리즈 ID, 로킹, 모니터링, 디버깅 정보, 책임자, 관리자 정보 등

 

어노테이션을 포함한 yml 파일로 파드 생성

  • testapp-pod-anno.yml 파일 생성
 vagrant@kube-master1:~/test$ vi testapp-pod-anno.yml
 
 apiVersion: v1
kind: Pod
metadata:
  name: testapp-pod-anno
  annotations:
    test: abcde

spec:
  containers:
  - name: testapp
    image: c1t1d0s7/myweb
    ports:
    - containerPort: 8080
      protocol: TCP

 

  • 파드 생성
vagrant@kube-master1:~/test$ kubectl create -f testapp-pod-anno.yml
pod/testapp-pod-anno created

 

  • 실행 중인 파드 확인
vagrant@kube-master1:~/test$ kubectl get pods
NAME                 READY   STATUS    RESTARTS   AGE
testapp-pod          1/1     Running   0          160m
testapp-pod-anno     1/1     Running   0          14s
testapp-pod-label1   1/1     Running   0          109m
testapp-pod-label2   1/1     Running   0          16m

 

  • 어노테이션 값 확인
vagrant@kube-master1:~/test$ kubectl get pods testapp-pod-anno -o yaml | grep -i -A 3 'annotation'
  annotations:
    file: testapp-pod-anno.yml
    subtitle: annotation
  creationTimestamp: "2022-06-22T03:14:44Z"
  managedFields:
  - apiVersion: v1
--
        f:annotations:
          .: {}
          f:file: {}
          f:subtitle: {}

 

  • testapp-pod-anno 파드의 자세한 정보 확인
vagrant@kube-master1:~/test$ kubectl describe pods testapp-pod-anno
~
Annotations:  test: abcde
~

 

  • 실행 중인 파드의 어노테이션 수정
vagrant@kube-master1:~/test$ kubectl annotate pods testapp-pod test="defgh"
pod/testapp-pod annotated

 

  • 변경된 어노테이션 확인
vagrant@kube-master1:~/test$ kubectl get pods testapp-pod -o yaml | less

728x90