본문 바로가기
서버인프라/kubernetes

[제 10강] Labels 사용

by techwold ted 2023. 1. 12.

이번에는 labels 사용에 대해 설명하려 합니다. kubernetes에서 label은 pod와 같은 객체에 key, value의 쌍으로 이뤄집니다.

 

기본 사용은 다음과 같습니다.

label.yaml 파일로 저장 한 후에 다음과 같이 실행 합니다.

apiVersion: v1
kind: Pod
metadata:
  name: label-demo
  labels:
    environment: production
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80
kubectl create -f label.yaml

아래 명령어로 생성 확인을 할 수 있습니다. 

kubectl get pod --show-labels
label-demo                                  1/1     Running     0          22m   app=nginx,environment=production

그럼 이제 yaml 파일이 아닌  run을 사용하여 nginx pod를  실행해 보겠습니다.

kubectl run label-demo-1 --image=nginx -l app=nginx,environment=production
kubectl run label-demo-2 --image=nginx -l app=nginx,environment=production

위와 같이 2개의 pod를 더 만들었습니다. 그럼 다시 확인 해 보도록 하겠습니다.

kubectl get pods --show-labels
label-demo                                  1/1     Running     0          29m     app=nginx,environment=production
label-demo-1                                1/1     Running     0          2m49s   app=nginx,environment=production
label-demo-2                                1/1     Running     0          2m39s   app=nginx,environment=production

그리고 이번에는 label 을 명칭을 조금 변경해서 몇개 더 만들어 보고 확인해 보겠습니다.

kubectl run label-demo-3 --image=nginx -l app=nginx,environment=test
kubectl run label-demo-4 --image=nginx -l app=nginx,environment=test

#위와 같이 두개를 생성한 후 확인 하겠습니다.
kubectl get pods --show-labels
label-demo                                  1/1     Running     0          31m     app=nginx,environment=production
label-demo-1                                1/1     Running     0          4m46s   app=nginx,environment=production
label-demo-2                                1/1     Running     0          4m36s   app=nginx,environment=production
label-demo-3                                1/1     Running     0          11s     app=nginx,environment=test
label-demo-4                                1/1     Running     0          4s      app=nginx,environment=test

위에서 보시면 environment=production 과 environment=test 가 나눠져 있습니다. 이제 labels을 구분 하여 확인하는 작업을 해보도록 하겠습니다.

kubectl get pods -l environment=test
NAME           READY   STATUS    RESTARTS   AGE
label-demo-3   1/1     Running   0          29s
label-demo-4   1/1     Running   0          22s

kubectl get pods -l environment=production
NAME           READY   STATUS    RESTARTS   AGE
label-demo     1/1     Running   0          31m
label-demo-1   1/1     Running   0          5m18s
label-demo-2   1/1     Running   0          5m8s

어떻게 사용해야 할지 구분이 되시나요? 간단한 작업으로 잘 사용하면 강력한 기능을 발휘할수 있습니다.

그럼 이제 조금 더 심도 있게 확인해 보겠습니다.

kubectl get pods --show-labels
label-demo                                  1/1     Running     0          59m   app=nginx,environment=production
label-demo-1                                1/1     Running     0          33m   app=nginx,environment=production
label-demo-2                                1/1     Running     0          32m   app=nginx,environment=production
label-demo-3                                1/1     Running     0          28m   app=nginx,environment=test
label-demo-4                                1/1     Running     0          28m   app=nginx,environment=test
redis                                       1/1     Running     0          98s   db=inmemory,environment=test

위와 같이 redis 를 하나 추가해 줍니다.

그리고 다음과 같이 확인해 보겠습니다.

kubectl get pods -l 'environment in (test)'
NAME           READY   STATUS    RESTARTS   AGE
label-demo-3   1/1     Running   0          30m
label-demo-4   1/1     Running   0          30m
redis          1/1     Running   0          3m20s

kubectl get pods -l 'environment in (test), app in (nginx)'
NAME           READY   STATUS    RESTARTS   AGE
label-demo-3   1/1     Running   0          31m
label-demo-4   1/1     Running   0          31m

kubectl get pods -l 'environment in (test)'
NAME           READY   STATUS    RESTARTS   AGE
label-demo-3   1/1     Running   0          32m
label-demo-4   1/1     Running   0          32m
redis          1/1     Running   0          76s

어떻게 사용되는지 아시겠죠?? 다들 잘 되셨을 거라 생각 됩니다.

 

 

많은분들이 도움 되셨으면 합니다.

 

Label 자세한 사항은 아래에서 확인 하시면 됩니다.

https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

 

Labels and Selectors

Labels are key/value pairs that are attached to objects, such as pods. Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users, but do not directly imply semantics to the core system. Labels can

kubernetes.io

 

'서버인프라 > kubernetes' 카테고리의 다른 글

service와 endpoint  (65) 2024.01.25
K8S 리셋  (30) 2023.12.24
[제 9강] Ingress  (0) 2023.01.05
[제 8강] Service  (2) 2023.01.04
[제 7강] NameSapace  (4) 2022.12.29

댓글