Application 기능으로 이해하기 - PV/PVC, Deployment, Service, HPA 활용

Moongchi·2025년 6월 8일
0

kubernetes

목록 보기
12/14

▶ 1~4. local 동작 확인

// 1번 API - 파일 생성
kubectl exec -n {namespace} -it {podName} -- {명령어}
kubectl exec -n anotherclass-123 -it api-tester-1231-7ccdbb44d6-6rd97 -- curl localhost:8080/create-file-pod
kubectl exec -n anotherclass-123 -it api-tester-1231-7ccdbb44d6-6rd97 -- curl localhost:8080/create-file-pv

// 2번 - Container 임시 폴더 확인
[root@k8s-master ~]# kubectl exec -n anotherclass-123 -it api-tester-1231-7ccdbb44d6-6rd97 -- ls /usr/src/myapp/tmp
zqlvzattww.txt

// 2번 - Container 영구저장 폴더 확인
[root@k8s-master ~]# kubectl exec -n anotherclass-123 -it api-tester-1231-7ccdbb44d6-6rd97 -- ls /usr/src/myapp/files/dev
olblngdumw.txt  wxnzgfgpor.txt

// 2번 - master node 폴더 확인
[root@k8s-master ~]# ls /root/k8s-local-volume/1231
olblngdumw.txt  wxnzgfgpor.txt


// 3번 - Pod 삭제
[root@k8s-master ~]# k delete -n anotherclass-123  pod api-tester-1231-7ccdbb44d6-6rd97
pod "api-tester-1231-7ccdbb44d6-6rd97" deleted

// 4번 API - 파일 조회
[root@k8s-master ~]# kubectl exec -n anotherclass-123 -it api-tester-1231-7ccdbb44d6-46hdr -- curl localhost:8080/list-file-pod

[root@k8s-master ~]# kubectl exec -n anotherclass-123 -it api-tester-1231-7ccdbb44d6-46hdr -- curl localhost:8080/list-file-pv
wxnzgfgpor.txt olblngdumw.txt 

▶ 5. hostPath 동작 확인 - Deployment 수정 후 [1~4] 실행

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: anotherclass-123
  name: api-tester-1231
spec:
  template:
    spec:
      nodeSelector:
        kubernetes.io/hostname: k8s-master
      containers:
        - name: api-tester-1231
          volumeMounts:
            - name: files
              mountPath: /usr/src/myapp/files/dev
            - name: secret-datasource
              mountPath: /usr/src/myapp/datasource
      volumes:
        - name: files
          persistentVolumeClaim:  // 삭제
            claimName: api-tester-1231-files  // 삭제
          // 아래 hostPath 추가
          hostPath:
            path: /root/k8s-local-volume/1231
        - name: secret-datasource
          secret:
            secretName: api-tester-1231-postgresql
// 1번 API - 파일 생성
kubectl exec -n {namespace} -it {podName} -- {명령어}
[root@k8s-master 1231]# k exec -n anotherclass-123 -it api-tester-1231-849b7c45fd-fwlff -- curl localhost:8080/create-file-pod
fpftintacz.txt 

[root@k8s-master 1231]# k exec -n anotherclass-123 -it api-tester-1231-849b7c45fd-fwlff -- curl localhost:8080/create-file-pv
mitlggfxwu.txt wxnzgfgpor.txt olblngdumw.txt 

// 2번 - Container 임시 폴더 확인
[root@k8s-master 1231]# k exec -n anotherclass-123 -it api-tester-1231-849b7c45fd-fwlff -- ls /usr/src/myapp/tmp
fpftintacz.txt


// 2번 - Container 영구저장 폴더 확인
[root@k8s-master 1231]# k exec -n anotherclass-123 -it api-tester-1231-849b7c45fd-fwlff -- ls /usr/src/myapp/files/dev
mitlggfxwu.txt  olblngdumw.txt  wxnzgfgpor.txt

// 2번 - master node 폴더 확인
[root@k8s-master 1231]# ls /root/k8s-local-volume/1231
mitlggfxwu.txt  olblngdumw.txt  wxnzgfgpor.txt

// 3번 - Pod 삭제
[root@k8s-master 1231]# k delete -n anotherclass-123 pod api-tester-1231-849b7c45fd-fwlff
pod "api-tester-1231-849b7c45fd-fwlff" deleted

// 4번 API - 파일 조회
[root@k8s-master 1231]# k exec -n anotherclass-123 -it api-tester-1231-849b7c45fd-tln58 -- curl localhost:8080/list-file-pod

[root@k8s-master 1231]# k exec -n anotherclass-123 -it api-tester-1231-849b7c45fd-tln58 -- curl localhost:8080/list-file-pv
mitlggfxwu.txt wxnzgfgpor.txt olblngdumw.txt 

▶ 1. RollingUpdate 하기
(maxUnavailable: 25%, maxSurge: 25%)
=> pod 1/4씩 생성/제거 (전체 pod 개수 2개인 경우 1개씩 생성 제거)

// 1) HPA minReplica 2로 바꾸기 (이전 강의에서 minReplicas를 1로 바꿔놨었음)
kubectl patch -n anotherclass-123 hpa api-tester-1231-default -p '{"spec":{"minReplicas":2}}'

// 1) 그외 Deployment scale 명령
kubectl scale -n anotherclass-123 deployment api-tester-1231 --replicas=2
// 1) edit로 모드로 직접 수정
kubectl edit -n anotherclass-123 deployment api-tester-1231

// 2) 지속적으로 Version호출 하기 (업데이트 동안 리턴값 관찰)
while true; do curl http://192.168.56.30:31231/version; sleep 2; echo ''; done;

// 3) 별도의 원격 콘솔창을 열어서 업데이트 실행
[root@k8s-master 1231]# kubectl set image -n anotherclass-123 deployment/api-tester-1231 api-tester-1231=1pro/api-tester:v2.0.0
deployment.apps/api-tester-1231 image updated

// update 실행 포맷
// kubectl set image -n deployment/ =:

▶ 2. RollingUpdate (maxUnavailable: 0%, maxSurge: 100%) 하기
=> pod 2개가 동시에 생성되며 livenessProbe true가 생기면 1개씩 제거

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: anotherclass-123
  name: api-tester-1231
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25% -> 0%  # 수정
      maxSurge: 25% -> 100%      # 수정

[root@k8s-master 1231]# kubectl set image -n anotherclass-123 deployment/api-tester-1231 api-tester-1231=1pro/api-tester:v1.0.0
deployment.apps/api-tester-1231 image updated

▶ 3. Recreate 하기
=> Deployment를 Recreate로 변경 즉시 pod가 재생성되지 않음
=> 전체 pod 삭제 후 신규 버전 생성

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: anotherclass-123
  name: api-tester-1231
spec:
  replicas: 2
  strategy:
    type: RollingUpdate -> Recreate   # 수정
    rollingUpdate:        # 삭제
      maxUnavailable: 0%  # 삭제
      maxSurge: 100%      # 삭제

kubectl set image -n anotherclass-123 deployment/api-tester-1231 api-tester-1231=1pro/api-tester:v2.0.0

▶ 4. Rollback

// 이전 버전으로 롤백 (ver2.0 -> ver1.0)
kubectl rollout undo -n anotherclass-123 deployment/api-tester-1231

▶ 1. Pod 내부에서 Service 명으로 API 호출 [서비스 디스커버리]

// Version API 호출
[root@k8s-master 1231]# k exec -n anotherclass-123 -it api-tester-1231-7ccdbb44d6-5wxml -- curl http://api-tester-1231:80/version
[App Version] : Api Tester v1.0.0

▶ 2. Deployment에서 Pod의 ports 전체 삭제, Service targetPort를 http -> 8080으로 수정

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: anotherclass-123
  name: api-tester-1231
spec:
  template:
    spec:
      nodeSelector:
        kubernetes.io/hostname: k8s-master
      containers:
        - name: api-tester-1231
          ports:                // 삭제
          - name: http          // 삭제
            containerPort: 8080 // 삭제
---
apiVersion: v1
kind: Service
metadata:
  namespace: anotherclass-123
  name: api-tester-1231
spec:
  ports:
    - port: 80
      targetPort: http -> 8080 // 변경
      nodePort: 31231
  type: NodePort  

▶ 2. 그리고 다시 Pod 내부에서 Service 명으로 API 호출
[root@k8s-master 1231]# k exec -n anotherclass-123 -it api-tester-1231-7ccdbb44d6-5wxml -- curl http://api-tester-1231:80/version
[App Version] : Api Tester v1.0.0

▶ 1. 부하 발생

http://192.168.56.30:31231/cpu-load?min=3
// 3분 동안 부하 발생

  • Pod에 부하가 심할 경우 livenessProbe에 의해 파드가 restart 될 수 있습니다.

// 개인 PC 사양(core 수)에 따라 부하가 너무 오르거나/오르지 않을 경우 queryparam으로 수치 조정
http://192.168.56.30:31231/cpu-load?min=3&thread=5
// 3분 동안 5개의 쓰레드로 80% 부하 발생
// default : min=2, thread=10

▶ 1. 부하 확인

// 실시간 업데이트는 명령어로 확인하는 게 빨라요
kubectl top -n anotherclass-123 pods
kubectl get hpa -n anotherclass-123

// Grafana는 Prometheus를 거쳐 오기 때문에 좀 늦습니다
Grafana > Home > Dashboards > [Default] Kubernetes / Compute Resources / Pod
▶ 예상치 않은 상황 발생시 상태 원복 방법

// 1. hpa 삭제
kubectl delete -n anotherclass-123 hpa api-tester-1231-default

// 2. deployment replicas 2로 변경
kubectl scale -n anotherclass-123 deployment api-tester-1231 --replicas=2

// 3. hpa 다시 생성

kubectl apply -f - <<EOF
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  namespace: anotherclass-123
  name: api-tester-1231-default
  labels:
    part-of: k8s-anotherclass
    component: backend-server
    name: api-tester
    instance: api-tester-1231
    version: 1.0.0
    managed-by: dashboard
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-tester-1231
  minReplicas: 2
  maxReplicas: 4
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 60
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 120
EOF

▶ 2. [behavior] 미사용으로 적용

kubectl edit -n anotherclass-123 hpa api-tester-1231-default

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  namespace: anotherclass-123
  name: api-tester-1231-default
spec:
  behavior:  # 삭제
    scaleUp:   # 삭제
      stabilizationWindowSeconds: 120   # 삭제

▶ 2. 부하 발생

  1. 부하 발생 API
    http://192.168.56.30:31231/cpu-load
    // 2분 동안 10개의 쓰레드로 80% 부하 발생
    // default : min=2, thread=10

▶ 2. 부하 확인 (kubectl)

// 실시간 업데이트는 명령어로 확인하는 게 빨라요
kubectl top -n anotherclass-123 pods
kubectl get hpa -n anotherclass-123
▶ 2. 부하 확인 (grafana)

// 성능 (Prometheus를 거쳐 오기 때문에 좀 표시가 늦습니다)
Grafana > Home > Dashboards > [Default] Kubernetes / Compute Resources / Pod

// Replica 보기 (Grafana Dashbaord에서 [Kubernetes / Horizontal Pod Autoscaler] 다운로드
Grafana > Home > Dashboards > New > Import 클릭

  • Import via grafana.com 항목에 17125 입력 후 [Load] 클릭

0개의 댓글