Engineering/Kubernetes (K8S)

[Kubernetes] 쿠버네티스 레이블 & 어노테이션 (Label, Annotation) - Deep Dive

[앙금빵] 2021. 12. 13.

Label

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

 

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

 

아래 사진을 통해 Application은 수평으로, Release는 수직으로 구성되어 있는 것을 확인할 수 있다.

레이블 2개를 추가하여 파드를 2차원으로 구성

apiVersion: v1
kind: Pod
metadata:
  name: john-pod-label2

# 레이블 2개를 Pod에 붙였다.
  labels:
    tier: backend
    env: prod

spec:
  containers:
  - image: nginx:latest
    name: john
    ports:
    - containerPort: 8080
      protocol: TCP

 

Pod 레이블 확인

  • --show-labels 를 통해 Pod에 부착되어 있는 Label을 확인할 수 있다.
  • -L 을 통해 특정 레이블로 이루어진 Pod를 필터링 할 수 있다.
# Pod에 레이블 표시
root@k8s-m:~# kubectl get pod --show-labels
NAME             READY   STATUS    RESTARTS   AGE   LABELS
john-pod-label   1/1     Running   0          29s   env=prod,tier=backend

# Pod 특정 레이블만 관심 있는 경우
root@k8s-m:~# kubectl get pod -L tier,env
NAME             READY   STATUS    RESTARTS   AGE   TIER      ENV
john-pod-label   1/1     Running   0          44s   backend   prod

root@k8s-m:~# kubectl get pod -L env
NAME             READY   STATUS    RESTARTS   AGE   ENV
john-pod-label   1/1     Running   0          58s   prod

 

Pod 레이블 수정

  • --overwrite 옵션을 이용하면 기존 Pod 레이블 수정이 가능하다.

기존 레이블 env=prod 레이블을 env=debug 로 변경해보자.

root@k8s-m:~# kubectl label pod john-pod-label env=debug --overwrite
pod/john-pod-label labeled

root@k8s-m:~# kubectl get pod --show-labels
NAME             READY   STATUS    RESTARTS   AGE   LABELS
john-pod-label   1/1     Running   0          37m   env=debug,tier=backend

env=debug 로 변경되었음을 확인할 수 있다.

 

Label Selector를 이용한 Pod 나열

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

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

 

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

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

 

Label 확인

# 실험대상 2개의 Pod Label 확인

root@k8s-m:~# kubectl get pod --show-labels
NAME              READY   STATUS    RESTARTS   AGE     LABELS
john-pod-label    1/1     Running   0          57m     env=debug,tier=backend
john-pod-label2   1/1     Running   0          5m20s   creation_method=manual,tier=backend

 

균등기반 Label Selector

# env 키를 포함한 레이블
root@k8s-m:~# kubectl get pod --show-labels -l env
NAME             READY   STATUS    RESTARTS   AGE   LABELS
john-pod-label   1/1     Running   0          60m   env=debug,tier=backend

# env 키를 제외한 레이블
root@k8s-m:~# kubectl get pod --show-labels -l '!env'
NAME              READY   STATUS    RESTARTS   AGE     LABELS
john-pod-label2   1/1     Running   0          5m50s   creation_method=manual,tier=backend

# env=debug 키를 포함한 레이블
root@k8s-m:~# kubectl get pod --show-labels -l env=debug
NAME             READY   STATUS    RESTARTS   AGE   LABELS
john-pod-label   1/1     Running   0          61m   env=debug,tier=backend

# env=debug 키를 가지지 않은 레이블
root@k8s-m:~# kubectl get pod --show-labels -l env!=debug
NAME              READY   STATUS    RESTARTS   AGE   LABELS
john-pod-label2   1/1     Running   0          10m   creation_method=manual,tier=backend

# env=prod 키를 가지지 않은 레이블
root@k8s-m:~# kubectl get pod --show-labels -l env!=prod
NAME              READY   STATUS    RESTARTS   AGE     LABELS
john-pod-label    1/1     Running   0          61m     env=debug,tier=backend
john-pod-label2   1/1     Running   0          9m20s   creation_method=manual,tier=backend

 

집합성 기반 Label Selector (in, notin)

# env 키에 debug나 prod값이 포함된 레이블
root@k8s-m:~# kubectl get pods --show-labels -l 'env in (prod,debug)'
NAME             READY   STATUS    RESTARTS   AGE   LABELS
john-pod-label   1/1     Running   0          74m   env=debug,tier=backend

# env 값에 debug 값을 제외한 레이블
root@k8s-m:~# kubectl get pods --show-labels -l 'env notin(debug)'
NAME              READY   STATUS    RESTARTS   AGE   LABELS
john-pod-label2   1/1     Running   0          23m   creation_method=manual,tier=backend

 

Annotation

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

 

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

 

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

 

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

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

 

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

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

 

Annotation 확인

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

파드 생성 과정에서 쿠버네티스가 자동으로 추가한 Annotation을 확인할 수 있다.

root@k8s-m:~# kubectl describe pod john-pod-label
Name:         john-pod-label
Namespace:    default
Priority:     0
Node:         k8s-w2/192.168.100.102
Start Time:   Sun, 12 Dec 2021 14:02:29 +0000
Labels:       env=debug
              tier=backend
Annotations:  cni.projectcalico.org/containerID: a4762a8599062629fb750ea43e1cb52daa80257ce7c5f9a50c15d97889e32c1d
              cni.projectcalico.org/podIP: 172.16.46.54/32
              cni.projectcalico.org/podIPs: 172.16.46.54/32

 

Annotation 추가 및 수정

kubectl annotate 명령을 사용하여 추가 및 수정할 수 있다.

# annotate 명령을 통한 Annotation 추가
root@k8s-m:~# kubectl annotate pod john-pod-label mycompany="foo bar"
pod/john-pod-label annotated

# Annotation 추가 확인
root@k8s-m:~# kubectl describe pod john-pod-label
Name:         john-pod-label
Namespace:    default
Priority:     0
Node:         k8s-w2/192.168.100.102
Start Time:   Sun, 12 Dec 2021 14:02:29 +0000
Labels:       env=debug
              tier=backend
Annotations:  cni.projectcalico.org/containerID: a4762a8599062629fb750ea43e1cb52daa80257ce7c5f9a50c15d97889e32c1d
              cni.projectcalico.org/podIP: 172.16.46.54/32
              cni.projectcalico.org/podIPs: 172.16.46.54/32
              mycompany: foo bar

참고

- 쿠버네티스 인 액션

https://arisu1000.tistory.com/27841

https://nasa1515.tech/kubernetes-label/

댓글