반응형

K8S 클러스터는 가상 클러스터를 생성하거나 클러스터를 분할하는 기능을 제공한다.
기본적으로 클러스터내의 리소스 그룹을 격리한다.

아래 명령어를 통해서 namespace를 가져올 수 있다

# 둘 다 동일한 명령어
kubectl get namespace
kubectl get ns

# 결과 
NAME                 STATUS   AGE
default              Active   40m
kube-node-lease      Active   40m
kube-public          Active   40m
kube-system          Active   40m
local-path-storage   Active   40m


아래 명령어로 네임스페이스를 생성한다.

kubectl create ns [네임스페이스 이름]
ex) kubectl create ns dev
ex) kubectl create ns qa

kubectl get ns

# 결과
NAME                 STATUS   AGE
default              Active   44m
dev                  Active   12s
kube-node-lease      Active   44m
kube-public          Active   44m
kube-system          Active   44m
local-path-storage   Active   44m
qa                   Active   9s

 

아래 명령어로 네임스페이스를 삭제한다.

kubectl detete ns [네임스페이스 이름]
ex) kubectl detete ns dev

 

Namespace deploy demo

아래 파일을 생성한다 그리고 qa namespace에 배포한다.

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: order-service-deployment
spec:
  minReadySeconds: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
  selector:
    matchLabels:
      app: order-service
  replicas: 3
  template:
    metadata:
      labels:
        app: order-service
    spec:
      containers:
        - name: order-service-container
          image: vinsdocker/k8s-app:v1
          ports:
            - name: "app-port"
              containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  selector:
    app: order-service
  ports:
    - port: 80
      targetPort: 80
---
apiVersion: v1
kind: Pod
metadata:
  name: demo-pod
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: demo
      image: vinsdocker/util
      args:
        - "sleep"
        - "3600"



kubectl apply -f 06-rolling-update-demo.yaml -n qa

그리고 version을 2로 바꾼 후 dev에 배포한다.

kubectl apply -f 06-rolling-update-demo.yaml -n dev


결과확인을 해보자

kubectl get all -n qa

#결과
NAME                                            READY   STATUS    RESTARTS   AGE
pod/demo-pod                                    1/1     Running   0          94s
pod/order-service-deployment-56864789dc-fz9xw   1/1     Running   0          94s
pod/order-service-deployment-56864789dc-md5kt   1/1     Running   0          94s
pod/order-service-deployment-56864789dc-tsqrj   1/1     Running   0          94s

NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/my-app   ClusterIP   10.96.199.164   <none>        80/TCP    94s

NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/order-service-deployment   3/3     3            3           94s

NAME                                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/order-service-deployment-56864789dc   3         3         3       94s
kubectl get all -n dev

# 결과
NAME                                           READY   STATUS    RESTARTS   AGE
pod/demo-pod                                   1/1     Running   0          47s
pod/order-service-deployment-d684fb7c6-4xvqh   1/1     Running   0          47s
pod/order-service-deployment-d684fb7c6-6zp6x   1/1     Running   0          47s
pod/order-service-deployment-d684fb7c6-wzzxn   1/1     Running   0          47s

NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/my-app   ClusterIP   10.96.159.225   <none>        80/TCP    47s

NAME                                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/order-service-deployment   3/3     3            3           47s

NAME                                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/order-service-deployment-d684fb7c6   3         3         3       47s

 

실제로 cluster-ip가 다른것을 확인할 수 있다.
포트포워딩을 사용해서 v1과 v2에 접근할수 있는지 확인해보자

# v1 화면을 볼 수 있다.
kubectl port-forward svc/my-app 8080:80 -n qa

# v2 화면을 볼 수 있다.
kubectl port-forward svc/my-app 8080:80 -n dev

 

namespace를 삭제하면 안의 모든 리소스들이 삭제된다.

yaml 파일에 metadata안의 namespace를 선언하면 -n 옵션 없이도 항상 해당 네임스페이스에서 생성한다.

반응형

'Kubernetes' 카테고리의 다른 글

[Kubernetes] ConfigMap  (0) 2024.12.19
[Kubernetes] Probes  (0) 2024.12.19
[Kubernetes] Service  (0) 2024.12.17
[Kubernetes] Deployment  (0) 2024.12.16
[Kubernetes] ReplicaSet  (0) 2024.12.12
얼은펭귄