๐ŸŒฑ ์ธํ”„๋Ÿฐ/โš“ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์–ด๋‚˜๋” ํด๋ž˜์Šค (์ง€์ƒํŽธ)

[๋ฏธ์…˜4] Application ๊ธฐ๋Šฅ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ - PVC/PV, Deployment, Service, HPA

mallin 2025. 6. 8. 20:03

1. PV, PVC

1-1. API ๋กœ ํŒŒ์ผ ์ƒ์„ฑ

http://192.168.56.30:31231/create-file-pod
http://192.168.56.30:31231/create-file-pv
  • wmdaihnepr.txt, xhfwqtqaeo.txt ํŒŒ์ผ ์ƒ์„ฑ

1-2. ํŒŒ์ผ ์ƒ์„ฑ ํ™•์ธ

 

Continer ์ž„์‹œ ํด๋” ํ™•์ธ => wmdaihnepr.txt

 

Contianer ์˜๊ตฌ์ €์žฅ ํด๋” ํ™•์ธ => xhfwqtqaeo.txt

 

master node ํด๋” ํ™•์ธ => xhfwqtqaeo.txt

 

1-3. Pod ์‚ญ์ œ

1-4. ํŒŒ์ผ ์กฐํšŒ

http://192.168.56.30:31231/list-file-pod
http://192.168.56.30:31231/list-file-pv

 

๋‘ ํŒŒ์ผ ๋ชจ๋‘ ์กฐํšŒ๋˜์ง€ ์•Š์Œ

 

1-5. hostPath ์„ค์ • ํ›„ ๋‹ค์‹œ ๋™์ž‘

- name: files
	hostPath:
		path: /root/k8s-local-volume/1231

 

http://192.168.56.30:31231/list-file-pod
http://192.168.56.30:31231/list-file-pv
  • list-file-pod ๋Š” ์กฐํšŒ๋˜์ง€ ์•Š์ง€๋งŒ 
  • list-file-pv ๋Š” ํŒŒ์ผ๋ช…์ด ์กฐํšŒ๋œ๋‹ค

2. Deployment

2-1. RollingUpdate ํ•˜๊ธฐ

HPA minReplica ๋ฅผ 2๋กœ ๋ฐ”๊พธ๊ธฐ

kubectl patch -n anotherclass-123 hpa api-tester-1231-default -p '{"spec":{"minReplicas":2}}'

 

์ง€์†์ ์œผ๋กœ Version ํ˜ธ์ถœํ•˜๊ธฐ 

while true; do curl http://192.168.56.30:31231/version; sleep 2; echo ''; done;

 

์ด๋ฏธ์ง€ ์—…๋ฐ์ดํŠธ ์‹คํ–‰

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

๋ฒ„์ „์ด v1.0.0 ๊ณผ v.2.0.0 ์ด ํ˜ผ์šฉ๋˜์–ด์„œ ๋…ธ์ถœ๋จ

 

2-2. RollingUpdate (maxUnavailable: 0%, maxSurge: 100%) ํ•˜๊ธฐ

      maxUnavailable: 25% -> 0%  # ์ˆ˜์ •
      maxSurge: 25% -> 100%      # ์ˆ˜์ •
kubectl set image -n anotherclass-123 deployment/api-tester-1231 api-tester-1231=1pro/api-tester:v1.0.0
  • maxUnavailable : 0%, maxSurge : 100% ๋กœ ํ•˜๋ฉด Blue/Green ์— ๊ฐ€๊นŒ์šด ํšจ๊ณผ
  • ๋‘ ๋ฒ„์ „์ด ๋™์‹œ์— ํ˜ธ์ถœ๋˜์ง€ ์•Š๋„๋ก ์„ค์ •

๋ฒ„์ „ v1.0.0 ๊ณผ v2.0.0 ์ด ํ˜ผ์šฉ๋˜์–ด์„œ ๋…ธ์ถœ๋˜์ง€ ์•Š์Œ

 

2-3. Recreate ํ•˜๊ธฐ

  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
  • ๋ฐ”๋กœ ๊ธฐ์กด Pod ๋ฅผ ์‚ญ์ œ์‹œํ‚ค๋Š” ๋™์‹œ์— ์ƒˆ Pod 2๊ฐœ๋ฅผ ๋งŒ๋“ ๋‹ค
  • ์—…๋ฐ์ดํŠธ ์ค‘ ์„œ๋น„์Šค ์ค‘๋‹จ

 

2-4. Rollback

kubectl rollout undo -n anotherclass-123 deployment/api-tester-1231

 


3. Service

3-1. Pod ๋‚ด๋ถ€์—์„œ Service ๋ช…์œผ๋กœ API ํ˜ธ์ถœ

 

3-2. Deployment์—์„œ Pod์˜ ports ์ „์ฒด ์‚ญ์ œ, Service targetPort๋ฅผ http -> 8080์œผ๋กœ ์ˆ˜์ •

containers:
  - name: api-tester-1231
    ports:                // ์‚ญ์ œ
    - name: http          // ์‚ญ์ œ
      containerPort: 8080 // ์‚ญ์ œ
  ports:
    - port: 80
      targetPort: http -> 8080 // ๋ณ€๊ฒฝ
      nodePort: 31231

 

์ƒˆ๋กœ ๋œฌ Pod ๋‚ด๋ถ€์—์„œ Service ๋ช…์œผ๋กœ API ํ˜ธ์ถœ

 

์ •์ƒ์ ์œผ๋กœ ํ˜ธ์ถœ๋จ ๐Ÿ™†‍โ™€๏ธ

 


4. HPA

4-1. ๋ถ€ํ•˜ ๋ฐœ์ƒ & ๋ถ€ํ•˜ ํ™•์ธ

๋ถ€ํ•˜ ๋ฐœ์ƒ

http://192.168.56.30:31231/cpu-load?min=3

 

๋ถ€ํ•˜ ํ™•์ธ

6% -> 57 % -> 99% ๋กœ ์˜ค๋ฆ„

 

์ƒํƒœ ์›๋ณต ๋ฐฉ๋ฒ•

๋”๋ณด๊ธฐ
// 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

93% -> 12% ๋–จ์–ด์ง

 

4-2. [behavior] ๋ฏธ์‚ฌ์šฉ์œผ๋กœ ์ ์šฉ

spec:
  behavior:  # ์‚ญ์ œ
    scaleUp:   # ์‚ญ์ œ
      stabilizationWindowSeconds: 120   # ์‚ญ์ œ

 

๋ถ€ํ•˜ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด ์—„์ฒญ๋‚˜๊ฒŒ ๋นจ๋ฆฌ ์ฆ๊ฐ€ํ•จ