kubernetes/udemy

[udemy] Namespace

bbiyak2da 2025. 2. 23. 14:18

Namespace

Kubernetes의 네임스페이스(Namespace)는 클러스터 내에서 리소스를 논리적으로 구분하기 위한 방법입니다.

네임스페이스를 사용하면 여러 팀이나 프로젝트가 동일한 클러스터 내에서 서로의 리소스와 충돌하지 않도록 할 수 있습니다.

 

주요 특징

  • 리소스 격리: 네임스페이스를 사용하면 서로 다른 네임스페이스에 있는 리소스가 이름 충돌 없이 존재할 수 있습니다. 예를 들어, 두 개의 네임스페이스에서 동일한 이름의 Pod를 생성할 수 있습니다.
  • 정책 적용: 네임스페이스를 통해 리소스에 대한 접근 제어 및 정책을 적용할 수 있습니다. 예를 들어, 특정 네임스페이스에 대한 사용자 권한을 설정하여 보안을 강화할 수 있습니다.
  • 리소스 할당: 네임스페이스는 리소스 쿼터를 설정하여 각 네임스페이스가 사용할 수 있는 리소스의 양을 제한할 수 있습니다. 이를 통해 클러스터의 자원을 효율적으로 관리할 수 있습니다.
  • 환경 분리: 개발, 테스트, 프로덕션 등 다양한 환경을 네임스페이스로 분리하여 관리할 수 있습니다.


기본적으로 Kubernetes 클러스터에는 default, kube-system, kube-public 등과 같은 기본 네임스페이스가 존재하며, 사용자는 필요에 따라 추가적인 네임스페이스를 생성할 수 있습니다.

 

Namespace & Limit Range & Resource Quota 생성

 

# 00-namespace-LimitRange-ResourceQuota.yaml 파일 생성

apiVersion: v1
kind: Namespace
metadata: 
  name: dev3
---  
apiVersion: v1
kind: LimitRange
metadata:
  name: default-cpu-mem-limit-range
  namespace: dev3
spec:
  limits:
    - default:
        memory: "512Mi" # If not specified the Container's memory limit is set to 512Mi, which is the default memory limit for the namespace.
        cpu: "500m"  # If not specified default limit is 1 vCPU per container 
      defaultRequest:
        memory: "256Mi" # If not specified default it will take from whatever specified in limits.default.memory
        cpu: "300m" # If not specified default it will take from whatever specified in limits.default.cpu
      type: Container                        
---
apiVersion: v1
kind: ResourceQuota
metadata:
  name: ns-resource-quota
  namespace: dev3
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 2Gi  
    pods: "5"    
    configmaps: "5" 
    persistentvolumeclaims: "5" 
    secrets: "5" 
    services: "5"

 

  • Namespace : dev3라는 이름의 네임스페이스를 생성합니다. 이 네임스페이스는 클러스터 내에서 리소스를 논리적으로 구분하는 데 사용됩니다.
  • LimitRange: dev3 네임스페이스 내에서 컨테이너에 대한 기본 리소스 제한을 설정합니다.
    • default: 컨테이너가 리소스 제한을 명시하지 않을 경우, 메모리와 CPU의 기본값을 각각 512Mi와 500m으로 설정합니다.
    • defaultRequest: 컨테이너가 리소스 요청을 명시하지 않을 경우, 메모리와 CPU의 기본 요청값을 각각 256Mi와 300m으로 설정합니다.
    • type: Container: 이 LimitRange가 컨테이너에 적용됨을 나타냅니다.
  • ResourceQuota: dev3 네임스페이스 내에서 사용할 수 있는 리소스의 양을 제한합니다.
    • requests.cpu: 네임스페이스 내의 모든 Pod가 요청할 수 있는 CPU의 총량을 1로 제한합니다.
    • requests.memory: 네임스페이스 내의 모든 Pod가 요청할 수 있는 메모리의 총량을 1Gi로 제한합니다.
    • limits.cpu: 네임스페이스 내의 모든 Pod가 사용할 수 있는 CPU의 총량을 2로 제한합니다.
    • limits.memory: 네임스페이스 내의 모든 Pod가 사용할 수 있는 메모리의 총량을 2Gi로 제한합니다.
    • pods: 네임스페이스 내에서 생성할 수 있는 Pod의 최대 수를 5로 제한합니다.
    • configmaps, persistentvolumeclaims, secrets, services: 각각의 리소스에 대해 최대 수를 5로 제한합니다.

# 생성

kubectl apply -f 00-namespace-LimitRange-ResourceQuota.yaml

 

# 확인

kubectl get quota -n dev3
---
NAME                AGE     REQUEST                                                                                                                                  LIMIT
ns-resource-quota   5m47s   configmaps: 1/5, persistentvolumeclaims: 0/5, pods: 3/5, requests.cpu: 900m/1, requests.memory: 768Mi/1Gi, secrets: 0/5, services: 1/5   limits.cpu: 1500m/2, limits.memory: 1536Mi/2Gi

 

애플리케이션 생성 (app1-nginx)

 

# 01-NginxApp1-Deployment.yaml 파일 작성

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1-nginx-deployment
  labels:
    app: app1-nginx
  namespace: dev3    
spec:
  replicas: 6
  selector:
    matchLabels:
      app: app1-nginx
  template:
    metadata:
      labels:
        app: app1-nginx
    spec:
      containers:
        - name: app1-nginx
          image: stacksimplify/kube-nginxapp1:1.0.0
          imagePullPolicy: Always
          ports:
            - containerPort: 80

 

 

app1-nginx 애플리케이션을 dev3 네임스페이스로 지정하여 배포한다.

 

# 생성

kubectl apply -f 01-NginxApp1-Deployment.yaml

 

LoadBalancer 생성 (app1-nginx)

 

# 02-NginxApp1-LoadBalancer-Service.yaml 파일 작성

apiVersion: v1
kind: Service
metadata:
  name: app1-nginx-service
  labels:
    app: app1-nginx
  namespace: dev3    
spec:
  type: LoadBalancer
  selector:
    app: app1-nginx
  ports:
    - port: 80
      targetPort: 80

 

App1에 대한 서비스를 생성해준다

type은 LB로 정의하였다.

 

# 생성

kubectl apply -f 02-NginxApp1-LoadBalancer-Service.yaml

 

확인

 

# deployment 확인

kubectl get deploy -n dev3
---
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
app1-nginx-deployment   3/6     3            3           5m36s

 

replica를 6개로 정의하였지만, 3개만 배포되었다.

 

# quota 확인

kubectl describe quota ns-resource-quota -n dev3
---
Name:                   ns-resource-quota
Namespace:              dev3
Resource                Used    Hard
--------                ----    ----
configmaps              1       5
limits.cpu              1500m   2
limits.memory           1536Mi  2Gi
persistentvolumeclaims  0       5
pods                    3       5
requests.cpu            900m    1
requests.memory         768Mi   1Gi
secrets                 0       5
services                1       5

 

이는 현재 pod 3개 배포시 현재 used만큼 사용하고 있는데,

4개를 배포하면 제한해놓은 cpu나 memory가 초과하게 되어버린다.

 

이와 같이 네임스페이스는 리소스 쿼터를 설정하여 각 네임스페이스가 사용할 수 있는 리소스의 양을 제한할 수 있다.

 


[참고 영상]

Udemy - Azure Kubernetes Service with Azure DevOps and Terraform

섹션 17 : Kubernetes namespace

 

[참고 문서]

https://github.com/stacksimplify/azure-aks-kubernetes-masterclass/tree/master/16-Kubernetes-Namespaces/16-03-Namespaces-ResourceQuota