본문 바로가기
GCP

[GCP] GKE 일시 정지로 비용 줄이기, PVC 사용

by 우솨 2025. 2. 15.

GCE VM의 경우 일시정지 버튼으로 간편하게 비용 절약이 가능하지만 GKE의 경우 일반적인 방법으로 일시 정지가 불가능하다.

GKE 를 일시정지 하기 위한 조건

=> PVC(Persistent Volume Claim)를 사용하지 않을 시 데이터가 지워지니 주의
=> namespace를 지우거나 yaml 리소스를 삭제시 디스크가 사라지니 주의

1. Kubernetes에서 모든 Deployment의 파드를 0개로 축소 해야한다.

kubectl scale deployment --all --replicas=0 -n <namespace>
kubectl scale statefulset --all --replicas=0 -n <namespace>
kubectl scale daemonset --all --replicas=0 -n <namespace>

2. Kubernetes의 남은 모든 pod를 제거

kubectl delete pod --all -n <namespace>


<namespace>명만 수정하여 한번에 처리 코드(1,2번)
- 예시

for ns in <namespace1> <namespace2>; do
  kubectl scale deployment --all --replicas=0 -n $ns
  kubectl scale statefulset --all --replicas=0 -n $ns
  kubectl scale daemonset --all --replicas=0 -n $ns
done

kubectl delete pod --all -n <namespace1>
kubectl delete pod --all -n <namespace2>

3. 노드 풀 자동 확장 기능 종료 및 노드수 0 변경
=> gke - 클러스터 - 노드 - 노드풀 수정 순
노드 풀 자동 확장 기능을 먼저 종료시켜야 노드 수 0 으로 변경이 가능하다.

VM노들은 삭제되고 PV 설정된 pod 디스크들은 남는 것을 확인 가능하다.


POD들을 PVC로 저장하는 법

1. 처음부터 PV를 만들어 두거나 yaml파일에 pv추가 기능을 추가한다.
ex) [Strimzi-kafka] storage의 type을 persistent-claim으로 설정

    storage:
      type: persistent-claim
      size: 10Gi
      deleteClaim: false

ex) [grafana.yaml] pvc를 yaml파일에 직접 추가 - 볼륨 설정과 볼륨 마운틴은 필수

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  labels:
    app: strimzi
spec:
  replicas: 1
  selector:
    matchLabels:
      name: grafana
  template:
    metadata:
      labels:
        name: grafana
    spec:
      securityContext:
        runAsUser: 0
        runAsNonRoot: false
        fsGroup: 1001
      containers:
      - name: grafana
        image: grafana/grafana:7.3.7
        ports:
        - name: grafana
          containerPort: 3000
          protocol: TCP
        volumeMounts:
        - name: grafana-data
          mountPath: /var/lib/grafana

        readinessProbe:
          httpGet:
            path: /api/health
            port: 3000
          initialDelaySeconds: 5
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /api/health
            port: 3000
          initialDelaySeconds: 15
          periodSeconds: 20
      volumes:
      - name: grafana-data
        persistentVolumeClaim:
          claimName: grafana-data-pvc

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  labels:
    app: strimzi
  annotations:
    external-dns.alpha.kubernetes.io/hostname: "grafana.$MyDomain"  # Add the annotation for external DNS
spec:
  ports:
  - name: grafana
    port: 3000
    targetPort: 3000
    protocol: TCP
  selector:
    name: grafana
  type: LoadBalancer  # Set the service type to LoadBalancer

POD의 마운트된 PVC 확인하는 법

# 파드 접속
kubectl exec -it <pod name> -n <namespace> bash
# disc 확인
df -h

/dev/sdc 부분이 마운트된 폴더이다


GKE 를 재시작하기 (GKE 일시정지의 역순)

1. 노드 풀 자동 확장 기능 종료 및 노드수를 원래대로 변경
=> gke - 클러스터 - 노드 - 노드풀 수정 순
노드의 확장기능과 노드 수 늘리기는 동시에 불가능하기에 하나씩 진행해야한다.

2. Kubernetes에서 모든 Deployment의 복제본을 원래대로 변경

kubectl scale deployment --all --replicas=1 -n <namespace>
kubectl scale statefulset --all --replicas=1 -n <namespace>
kubectl scale daemonset --all --replicas=1 -n <namespace>

3. pod 확인

kubectl get pod --all -n <namespace>

<namespace>명만 수정하여 한번에 처리 코드(2,3번)

for ns in <namespace1> <namespace2>; do
  kubectl scale deployment --all --replicas=1 -n $ns
  kubectl scale statefulset --all --replicas=1 -n $ns
  kubectl scale daemonset --all --replicas=1 -n $ns
done

kubectl get pod --all -n <namespace1>
kubectl get pod --all -n <namespace2>

노드에 대한 비용은 줄일 수 있지만, cluster나 pvc 디스크 등에 대한 요금은 청구되니 주의 !

'GCP' 카테고리의 다른 글

GCP에서 VPC설정 (Public, Private, NAT 게이트웨이)  (0) 2024.12.25