kubernetes

[따배쿠] namespace

bbiyak2da 2024. 11. 10. 12:59

 

namespace

  • 쿠버네티스의 api 중 하나이며, 클러스터 내에서 리소스를 논리적으로 분리하기 위한 방법
  • 쿠버네티스 클러스터 하나를 여러 팀이나 사용자가 함께 공유
  • 용도에 따라 실행해야 하는 앱을 구분할 때 사용 ex) prod, dev 등등
  • namespace를 삭제하면 그 안에 있는 pod 등등 다 날아간당 !! 주의 !! 

 

특징

  • 리소스 격리
    • namespace를 사용하면 동일한 이름의 리소스(예: Pod, Service 등)를 서로 다른 네임스페이스에서 정의할 수 있습니다. 이를 통해 리소스의 충돌을 방지할 수 있습니다.
  • 권한 관리
    • namespace는 RBAC(Role-Based Access Control)과 함께 사용되어 특정 사용자나 그룹이 접근할 수 있는 리소스를 제한할 수 있습니다. 각 네임스페이스에 대해 권한을 설정함으로써 보안을 강화할 수 있습니다.
  • 리소스 할당
    • namespace를 통해 클러스터 내에서 리소스 사용량을 관리하고, 특정 네임스페이스에 대해 리소스 쿼터를 설정할 수 있습니다. 이를 통해 자원의 낭비를 방지할 수 있습니다.
  • 환경 구분
    • 개발, 테스트, 운영 환경을 각각의 네임스페이스로 분리하여 관리할 수 있습니다. 각 환경에 필요한 리소스를 독립적으로 관리할 수 있습니다.

 

생성 방법

  • namespace 생성 방법에는 CLI와 YAML이 있다.

<CLI>

kubectl create namespace blue

 

<YAML>

kubectl create namespace green --dry-run -o yaml > green-ns.yaml
vi green-ns.yaml
kubectl create -f green-ns.yaml

 

 

예시 1

 

# namespace 생성 

 

1. CLI

root@master:~# kubectl create namespace blue
namespace/blue created

 

2. YAML

root@master:~# kubectl create namespace orange --dry-run -o yaml
W1110 03:18:52.108680   15750 helpers.go:703] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: null
  name: orange
spec: {}
status: {}
root@master:~# kubectl create namespace orange --dry-run -o yaml > orange-ns.yaml
W1110 03:21:27.387367   16585 helpers.go:703] --dry-run is deprecated and can be replaced with --dry-run=client.

 

* --dry-run : 실제로 실행하지 않고 실행 가능 여부만 확인하는 옵션

* -o yaml : 실행 결과를 yaml로 보여줘

* --dry-run -o yaml > [yaml 파일명].yaml : 실행 결과를 yaml 파일로 생성해줘

(yaml 파일을 생성해놓으면, 현재 운영중인 쿠버네티스 환경 안에 넣어놓고 생성하고 싶을 때 쓸 수 있어 편하다.)

 

즉, CLI 명령어로 namespace를 생성한 뒤 yaml파일로 만든 작업이다.

root@master:~# ls
orange-ns.yaml
root@master:~# vi orange-ns.yaml
-------------------------------------------
apiVersion: v1
kind: Namespace
metadata:
  name: orange
-------------------------------------------
root@master:~# kubectl create -f orange-ns.yaml
namespace/orange created
root@master:~# kubectl get namespace
NAME              STATUS   AGE
blue              Active   9m44s
default           Active   6d22h
kube-node-lease   Active   6d22h
kube-public       Active   6d22h
kube-system       Active   6d22h
orange            Active   55s

 

해당 yaml 파일 가지고 namespace를 배포할 수 있다. 

 

예시 2

 

#1. CLI를 통해 namespace 생성

root@master:~# kubectl create -f nginx.yaml -n blue
pod/mypod created

 

CLI 명령어로 namespace 지정해주고, 현재 디렉터리에 존재하는 yaml 파일로 pod 생성

 

#2. yaml 파일에 먼저 namespace를 지정해준 뒤, pod 생성

root@master:~# vi nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod
  namespace: orange
spec:
  containers:
   - image: nginx:1.14
     name: nginx
     ports:
      - containerPort: 80
      - containerPort: 443
root@master:~# kubectl create -f nginx.yaml
pod/mypod created

root@master:~# kubectl get pods -n orange
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          38s

 

기타

  • namespace 관리 명령어
kubectl get namespace
kubectl delete namespace

 

  • 쿠버네티스는 설치될 때 기본적으로 4가지의 namespace가 자동으로 생성됨
root@master:~# kubectl get namespaces
NAME              STATUS   AGE
default           Active   6d21h
kube-node-lease   Active   6d21h
kube-public       Active   6d21h
kube-system       Active   6d21h
  • default
    • 기본 네임스페이스로, 다른 네임스페이스를 지정하지 않은 경우 리소스가 이곳에 생성됩니다.
    • 주로 테스트나 간단한 작업에 사용됩니다.
  • kube-system
    • Kubernetes 시스템 컴포넌트와 관련된 리소스가 위치하는 네임스페이스입니다.
    • 예를 들어, kube-dns, kube-proxy, CoreDNS와 같은 필수 서비스가 이 네임스페이스에 포함됩니다.
  • kube-public
    • 모든 사용자에게 읽기 권한이 있는 네임스페이스입니다. 주로 클러스터의 공용 정보를 저장하는 데 사용됩니다
    • 예를 들어, 클러스터 정보나 공용 설정 등을 저장할 수 있습니다.
  • kube-node-lease
    • 노드의 생존성을 관리하기 위한 Lease 객체가 위치하는 네임스페이스입니다.
    • 각 노드는 주기적으로 Lease 객체를 업데이트하여 자신의 상태를 클러스터에 알립니다.

 

  • kubectl get pod시, 기본적으로 default namespace의 pod를 가져온다.
root@master:~# kubectl get pod
No resources found in default namespace.
root@master:~# kubectl get pod --namespace default
No resources found in default namespace.
root@master:~# kubectl get pod -n default
No resources found in default namespace.
root@master:~# vi nginx.yaml
-----------------------------------
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
   - image: nginx:1.14
     name: nginx
     ports:
      - containerPort: 80
      - containerPort: 443
-----------------------------------
root@master:~# kubectl create -f nginx.yaml
pod/mypod created
root@master:~# kubectl get pods -n default
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          2m55s

 

--namespace 옵션을 줄여서 -n으로 쓸 수 있다.


namespace switch

  • 기본으로 사용하는 namespace를 default가 아닌 다른 이름의 namespace로 switch

 

예시

# 1단계. namespace를 포함한 context 등록

kubectl config views로 조회되는 정보를 통해, kubectl config set-context를 설정한다.

kubectl config view
kubectl config set-context [context 이름] --cluster=[클러스터 이름] --user=[user 이름] --namespace=[namespace 이름]

 

 

[예시]

root@master:~# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://10.100.0.104:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: DATA+OMITTED
    client-key-data: DATA+OMITTED
root@master:~# kubectl config set-context blue@kubernetes --cluster=kubernetes --user=kubernetes-admin --namespace=blue
Context "blue@kubernetes" created.

 

다시 kubectl config view 명령어로 조회해보면,

root@master:~# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://10.100.0.104:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    namespace: blue
    user: kubernetes-admin
  name: blue@kubernetes
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: DATA+OMITTED
    client-key-data: DATA+OMITTED

 

context가 하나 더 추가된 것을 확인 가능하다. (namespace blue)

 

# 2단계. 등록된 namespace로 context 변경

kubectl config use-context NAME

 

[예시]

 

root@master:~# kubectl config use-context blue@kubernetes
Switched to context "blue@kubernetes".
root@master:~# kubectl config current-context
blue@kubernetes
root@master:~/Getting-Start-Kubernetes# kubectl get pod
No resources found in blue namespace.

 

이제 blue namespace가 기본 namespace가 된 셈,,

 

 

# 번외. 다시 default namespace로 switch

root@master:~# kubectl config use-context kubernetes-admin@kubernetes
Switched to context "kubernetes-admin@kubernetes".

 

 

다시 default namespace로 스위칭