kubernetes label 정리

혼자 보기위해 정리하는 쿠버네티스2 오늘은 label 편

label은 key:value 로 구성되며 오브젝트나 컨트롤러를 만들 때 메타데이터 필드에서 설정함
label key는 컨트롤러들이 pod를 관리할 때 자신이 관리해야할 pod를 구분하는 역할을 하며 label을 변경하면 pod를 인식하지 못함

특정 label의 리소스만 선택해서 관리하게 하는 기능이 selector 인데 selector는 두가지 종류가 있다.

  • 등호 기반
    • environment=develop
    • release=stable
  • 집합 기반
    • environment in (develop, stage)
    • release notin (latest,canary)
    • gpu
    • !gpu

label test

deployment-label01.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-label01
  labels:
    app: nginx
spec:
  replicas: 1
  selector:  
    matchLabels:
      app: nginx 
  template:
    metadata:
      labels:
        app: nginx 
        environment: develop
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

development-label02.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-label02
  labels:
    app: nginx
spec:
  replicas: 1
  selector:  
    matchLabels:
      app: nginx 
  template:
    metadata:
      labels:
        app: nginx 
        environment: production
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

label-service01.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: label-develop-service
  labels:
    app: label-develop-service
spec:
  type: ClusterIP
  selector:
    environment: develop 
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

label-service02.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: label-production-service
  labels:
    app: label-production-service
spec:
  type: ClusterIP
  selector:
    environment: production 
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

label-all-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: label-all-service
  labels:
    app: label-all-service
spec:
  type: ClusterIP
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
    protocol : TCP

deployment, service 생성

$ kubectl get deploy,svc,pod
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-label01   1/1     1            1           2m27s
deployment.apps/nginx-label02   1/1     1            1           2m24s


NAME                               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes                 ClusterIP   10.96.0.1       <none>        443/TCP        47h
service/label-develop-service      ClusterIP   10.96.190.209   <none>        80/TCP         2m14s
service/label-production-service   ClusterIP   10.96.97.82     <none>        80/TCP         2m11s

NAME                                 READY   STATUS    RESTARTS   AGE
pod/nginx-label01-7ccb68dd8b-zgrz5   1/1     Running   0          2m27s
pod/nginx-label02-6579d574db-bstpr   1/1     Running   0          2m24s

describe svc

  • label-develop-service
$ kubectl describe service/label-develop-service
Name:              label-develop-service
Namespace:         default
Labels:            app=label-develop-service
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"label-develop-service"},"name":"label-develop-service","...
Selector:          environment=develop
Type:              ClusterIP
IP:                10.96.190.209
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         172.17.0.10:80
Session Affinity:  None
Events:            <none>
  • label-production-service
$ kubectl describe service/label-production-service
Name:              label-production-service
Namespace:         default
Labels:            app=label-production-service
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"label-production-service"},"name":"label-production-serv...
Selector:          environment=production
Type:              ClusterIP
IP:                10.96.97.82
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         172.17.0.11:80
Session Affinity:  None
Events:            <none>
  • label-all-service
$ kubectl describe svc label-all-service
Name:              label-all-service
Namespace:         default
Labels:            app=label-all-service
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"label-all-service"},"name":"label-all-service","namespac...
Selector:          app=nginx
Type:              ClusterIP
IP:                10.96.3.232
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         172.17.0.10:80,172.17.0.11:80
Session Affinity:  None
Events:            <none>

=> 각각의 service가 해당하는 pod의 label 과 연결맺은 것 확인