클라우드 + DevOps/Kubernetes (k8s)

Kubernetes :: 라벨 및 네임스페이스 제작 실습

gamjadori 2024. 4. 23. 16:58
728x90

<Kubernetes :: 라벨 및 네임스페이스 제작 실습 1>

<Label>

  • 복잡하고 다양한 포드를 효율적인 집합으로 다루기 위한 방법
  • key-value 기반의 속성 태그로, 하나 이상 설정 가능
  • 용도에 따른 리소스 선택 시 유용 > 객체를 식별하고 그룹화
  • 포드는 라벨을 가질 수 있고, 라벨 검색 조건에 따라서 특정 라벨을 가지고 있는 포드만을 선택 가능
  • 라벨을 선택해 특정 리소스만 배포하거나 업데이트할 수 있고
  • 라벨로 선택된 리소스만 Service에 연결하거나 특정 라벨로 선택된 리소스에만 네트워크 접근 권한을 부여하는 등 작업 가능
  • Pod에 설정된 라벨을 검색 조건으로 사용, 특정 Label을 가지고 있는 포드만을 선택 가능

 

1. label을 설정하여 포드 생성

PS C:\Users\admin\Desktop\k8s-pods> kubectl run mynginx2 --image=nginx:1.25.3-alpine --labels="key1=value1,key=value2"
pod/mynginx2 created

<확인>

PS C:\Users\admin\Desktop\k8s-pods> kubectl get po --show-labels -o wide

 

2. 네임스페이스 생성

  • Kubernetes 클러스터 내에서 리소스를 그룹화하고 격리하는 데 사용
PS C:\Users\admin\Desktop\k8s-pods> kubectl create namespace infra-team-ns1
namespace/infra-team-ns1 created
PS C:\Users\admin\Desktop\k8s-pods> kubectl get namespaces
NAME               STATUS      AGE
default            Active      3h34m
infra-team-ns1     Active      8s
kube-node-lease    Active      3h34m
kube-public        Active      3h34m
kube-system        Active      3h34m

PS C:\Users\admin\Desktop\k8s-pods> kubectl run label-pod-a --image=****/k8s-lab:initial --namespace=infra=team-ns1 --labels=type=infra1 --dry-run=client -o yaml > label-pod.yaml

 

3. label-pod.yaml 파일 생성

<label-pod.yaml>

  • 네임스페이스 "infra-team-ns1" 내에 속하는 세 개의 Pod와 하나의 Service를 정의
  • Pod는 "infra1"이라는 레이블 지정
  • 포트 7777을 열어 Pod에 접근
apiVersion: v1
kind: Pod
metadata:
  labels:
    type: infra1
  name: label-pod-a
	namespace: infra-team-ns1
spec:
  containers:
  - image: *****/k8s-lab:initial
    name: pod-a-container
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    type: infra1
  name: label-pod-b
	namespace: infra-team-ns1
spec:
  containers:
  - image: *****/k8s-lab:initial
    name: pod-b-container
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    type: infra1
  name: label-pod-c
	namespace: infra-team-ns1
spec:
  containers:
  - image: *****/k8s-lab:initial
    name: pod-c-container
---
apiVersion: v1
kind: Service
metadata:
	name: infra-svc1
	namespace: infra-team=ns1
spec:
	selector:
		type: infra1
	ports:
	- port: 7777

 

4. yaml 파일로 pod 생성

PS C:\Users\admin\Desktop\k8s-pods> kubectl apply -f label-pod.yaml
pod/label-pod-a created
pod/label-pod-b created
pod/label-pod-c created
service/infra-svc1 created

 

5. 생성 확인