Profile picture

[k8s] Deep Dive - Label & Annotation

JaehyoJJAng2023년 04월 04일

▶︎ Label

라벨은 쿠버네티스 클러스터 내부에 사용자가 객체를 생성할때 그 객체를 구분하기 위해서 사용자가 임의로 원하는 값을 지정해서 사용한다.

  • Label은 Key-Value 로 구성된다.
  • Label은 사용자가 클러스터내에 객체를 만들 때 메타데이터로 붙일 수 있다. 생성된 이후 언제든 수정이 가능하고 코어 시스템에 직접적인 의미는 없다.
  • 오브젝트마다 Key-Value로 Label을 정의할 수 있고 Key는 고유한 값이어야 한다.
  • Label 키가 해당 리소스 내에서 고유하다면 하나 이상 원하는 만큼의 Label을 가질 수 있다.

아래 이미지를 보면 알 수 있듯이, Application은 수평으로, Release는 수직으로 구성되어 있는 것을 확인할 수 있다.
image

apiVersion: v1
kind: Pod
metadata:
  name: my-pod-1
  labels:
    tier: backend
    env: prod
spec:
  containers:
    - name: container1
      image: nginx:latest
      ports:
        - containerPort: 8080
          protocol: TCP

‣ Pod 레이블 확인

  • --show-labels를 통해 Pod에 부착되어 있는 Label 확인 가능
  • -L을 통해 특정 레이블로 이루어진 Pod 필터링 가능
# Pod에 레이블 표시
$ kubectl get pod --show-labels
NAME       READY   STATUS    RESTARTS   AGE    LABELS
my-pod-1   1/1     Running   0          110s   env=prod,tier=backend

# Pod 특정 레이블만 표시
$ kubectl get pods -L tier,env
NAME       READY   STATUS    RESTARTS   AGE     TIER      ENV
my-pod-1   1/1     Running   0          2m17s   backend   prod

‣ Pod 레이블 수정

  • --overwrite 옵션을 사용하면 기존 Pod의 Label 수정 가능

my-pod-1 파드의 env=prod 레이블을 env=debug로 변경해보자

$ kubectl label pod/my-pod-1 env=debug --overwrite
pod/my-pod-1 labeled

$ kubectl get pod --show-labels
NAME       READY   STATUS    RESTARTS   AGE     LABELS
my-pod-1   1/1     Running   0          3m38s   env=debug,tier=backend

‣ Pod 나열

  • Label Selector를 이용한 Pod 나열

중요 포인트는 Label이 Label Selector와 함께 사용된다는 점이다.

  • Label Selector는 특정 Label로 Tag된 Pod의 부분 집합을 선택하여 원하는 작업을 수행함.
  • Label Selector는 특정 값과 Label을 갖는지 여부에 따라 리소스를 필터링하는 기준이 됨.

Label Selector는 다음 기준에 따라 Resource를 선택한다.

  • 특정한 키를 포함하거나 포함하지 않는 Label
  • 특정한 키와 값을 가진 Label
  • 특정한 키를 가지고 있지만 다른 값을 가진 Label

• Label 확인

# 실험대상 2개의 Pod Label 확인\
$ kubectl get pod --show-labels
NAME       READY   STATUS    RESTARTS   AGE     LABELS
my-pod-1   1/1     Running   0          6m58s   env=prod,tier=backend
my-pod-2   1/1     Running   0          19s     creation_method=manual,tier=backend

• 균등기반 Label Selector

# env 키를 포함한 레이블
$ kubectl get pod --show-labels -l env
NAME       READY   STATUS    RESTARTS   AGE     LABELS
my-pod-1   1/1     Running   0          7m24s   env=prod,tier=backend

# env 키를 제외한 레이블
$ kubectl get pod --show-labels -l '!env'
NAME       READY   STATUS    RESTARTS   AGE   LABELS
my-pod-2   1/1     Running   0          56s   creation_method=manual,tier=backend

# env=debug 키를 포함한 레이블
$ kubectl get pod --show-labels -l env=debug
NAME       READY   STATUS    RESTARTS   AGE     LABELS
my-pod-1   1/1     Running   0          8m20s   env=debug,tier=backend

# env=debug 키를 가지지 않은 레이블
$ kubectl get pod --show-labels -l env!=debug
NAME       READY   STATUS    RESTARTS   AGE    LABELS
my-pod-2   1/1     Running   0          112s   creation_method=manual,tier=backend

# env=prod 키를 가지지 않은 레이블
$ kubectl get pod --show-labels -l env!=prod
NAME       READY   STATUS    RESTARTS   AGE     LABELS
my-pod-1   1/1     Running   0          8m40s   env=debug,tier=backend
my-pod-2   1/1     Running   0          2m1s    creation_method=manual,tier=backend

• 집합성 기반 Label Selector

  • In, NotIn
# env 키에 debug나 prod값이 포함된 레이블
$ kubectl get pods --show-labels -l 'env in (prod,debug)'
NAME       READY   STATUS    RESTARTS   AGE     LABELS
my-pod-1   1/1     Running   0          9m23s   env=debug,tier=backend

# env 값에 debug 값을 제외한 레이블
$ kubectl get pods --show-labels -l 'env notin(debug)'
NAME       READY   STATUS    RESTARTS   AGE     LABELS
my-pod-2   1/1     Running   0          2m55s   creation_method=manual,tier=backend

▶︎ Annotation

Annotation은 라벨처럼 사용자가 원하는 값을 설정하기 보다 쿠버네티스 시스템에서 필요한 정보들을 표시해 주기 위해서 사용한다.

일반적으로 Annotation에서 사용되는 키는 쿠버네티스 시스템이 인식할 수 있는 값들을 사용한다. 레이블과 같이 key-value 구조를 띄지만 차이점이 존재한다.

레이블은 ① 오브젝트를 묶는데 사용할 수 있으며 ② 레이블 셀렉터를 이용하여 검색과 식별이 가능하다. 하지만 Annotation은 그렇게 할 수 없다. 메타데이터의 입력만 가능할 뿐 주석과 같으며 검색이 되지 않는다.

Annotation은 쿠버네티스에 새로운 기능을 추가할 때 흔히 사용된다.

Annotation이 유용하게 사용되는 경우는 Pod나 다른 API 오브젝트에 설명을 추가해 두는 것이다. 이렇게 함으로서 클러스터를 사용하는 모든 사람이 개별 오브젝트에 관한 정보를 신속하게 찾아볼 수 있다.

Annotation은 다음과 같은 메타데이터를 기록할 수 있다.

  • 필드
  • 이미지 정보 (타임 스탬프, 릴리즈 ID, 빌드 버전, git 브랜치, 이미지 해시, 레지스터리 주소 등)
  • 디버깅에 필요한 정보 (이름, 버전, 빌드정보)
  • 사용자 지시 사항

‣ Annotation 확인

Annotation은 describe 옵션으로 확인이 가능하다.


Pod 생성 과정에서 쿠버네티스가 자동으로 추가한 Annotation 확인

$ kubectl describe pod/my-pod-1
Name:             my-pod-1
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node2/192.168.56.102
Start Time:       Thu, 05 Oct 2023 16:06:33 +0900
Labels:           env=debug
                  tier=backend
Annotations:      cni.projectcalico.org/containerID: 40ae69c5f5c1be9b697a84323ddeb02f58056cbedb6ac6a18e87a76c4becf67f
                  cni.projectcalico.org/podIP: 20.109.131.12/32
                  cni.projectcalico.org/podIPs: 20.109.131.12/32

‣ Annotation 추가 및 수정

kubectl annotate 명령을 사용하여 추가/수정이 가능하다.

# annotate 명령을 통한 Annotation 추가
$ kubectl annotate pod my-pod-1 mycompany="foo bar"
pod/john-pod-label annotated

# Annotation 추가 확인
$ kubectl describe pod my-pod-1
Name:             my-pod-1
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node2/192.168.56.102
Start Time:       Thu, 05 Oct 2023 16:06:33 +0900
Labels:           env=debug
                  tier=backend
Annotations:      cni.projectcalico.org/containerID: 40ae69c5f5c1be9b697a84323ddeb02f58056cbedb6ac6a18e87a76c4becf67f
                  cni.projectcalico.org/podIP: 20.109.131.12/32
                  cni.projectcalico.org/podIPs: 20.109.131.12/32
                  mycompany: foo bar

Loading script...