[인프런 워밍업 클럽 4기 - DevOps] 미션 4

[인프런 워밍업 클럽 4기 - DevOps] 미션 4

PV, PVC

1번 API 파일 생성

[root@k8s-master ~] curl http://192.168.56.30:31231/create-file-pod
hrgosbpjox.txt
[root@k8s-master ~] curl http://192.168.56.30:31231/create-file-pv
xprrmxjsna.txt

2번 Container 임시 폴더 확인

[root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-66c489cbb8-5krzn -- ls /usr/src/myapp/tmp
hrgosbpjox.txt
[root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-66c489cbb8-5krzn -- ls /usr/src/myapp/files/dev
xprrmxjsna.txt
[root@k8s-master ~] ls /root/k8s-local-volume/1231/
xprrmxjsna.txt

2번 master node 폴더 확인

[root@k8s-master ~] ls /root/k8s-local-volume/1231/
xprrmxjsna.txt
  1. pod 삭제

    [root@k8s-master ~] k delete -n anotherclass-123 pod api-tester-1231-66c489cbb8-5krzn
    pod "api-tester-1231-66c489cbb8-5krzn" deleted

4번 API - 파일 조회

[root@k8s-master ~] k exec -n anotherclass-123  -it api-tester-1231-66c489cbb8-9kpxd -- curl localhost:8080/list-file-pod
[root@k8s-master ~] k exec -n anotherclass-123  -it api-tester-1231-66c489cbb8-9kpxd -- curl localhost:8080/list-file-pv
xafgvinxxz.txt thschtbuoo.txt pablwvxlec.txt pogdzjqhqs.txt xprrmxjsna.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~4 반복]

API - 파일 생성
[root@k8s-master ~] k exec -n anotherclass-123  -it api-tester-1231-66c489cbb8-9kpxd -- curl localhost:8080/list-file-pod
[root@k8s-master ~] k exec -n anotherclass-123  -it api-tester-1231-66c489cbb8-9kpxd -- curl localhost:8080/list-file-pv
xafgvinxxz.txt thschtbuoo.txt pablwvxlec.txt pogdzjqhqs.txt xprrmxjsna.txt

Container 임시 폴더 확인
root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-6cf7495fcd-7gtwq -- ls /usr/src/myapp/tmp
yrwjythrtf.txt

Container 영구저장 폴더 확인
[root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-6cf7495fcd-7gtwq -- ls /usr/src/myapp/files/dev
lbjgryyqvl.txt	pogdzjqhqs.txt	xafgvinxxz.txt  pablwvxlec.txt	thschtbuoo.txt	xprrmxjsna.txt

Master node 폴더 확인
[root@k8s-master ~] ls /root/k8s-local-volume/1231
lbjgryyqvl.txt  pablwvxlec.txt  pogdzjqhqs.txt  thschtbuoo.txt  xafgvinxxz.txt  xprrmxjsna.txt

API - 파일 조회
root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-6cf7495fcd-7gtwq -- curl localhost:8080/list-file-pod
yrwjythrtf.txt 

[root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-6cf7495fcd-7gtwq -- curl localhost:8080/list-file-pv
lbjgryyqvl.txt xafgvinxxz.txt thschtbuoo.txt pablwvxlec.txt pogdzjqhqs.txt xprrmxjsna.txt

 

RollingUpdate 하기

HPA minReplica 2로 바꾸기
[root@k8s-master ~] k patch -n anotherclass-123 hpa api-tester-1231-default -p '{"spec":{"minReplicas":2}}'
horizontalpodautoscaler.autoscaling/api-tester-1231-default patched

그외 Deployment scale 명령
[root@k8s-master ~] k scale -n anotherclass-123 deployment api-tester-1231 --replicas=2
deployment.apps/api-tester-1231 scaled

edit로 모드로 직접 수정
kubectl edit -n anotherclass-123 deployment api-tester-1231

지속적으로 Version호출 하기
while :
do 
curl http://192.168.56.30:31231/version
sleep 2
echo ''
done

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

RollingUpdate (maxUnavailable: 0%, maxSurge: 100%) 하기

strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0%
      maxSurge: 100%


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

 

Recreate 하기

 strategy:
    type: Recreate


[root@k8s-master ~] 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

 

Rollback

[root@k8s-master ~] kubectl rollout undo -n anotherclass-123 deployment/api-tester-1231
deployment.apps/api-tester-1231 rolled back

 

Service

Pod 내부에서 Service 명으로 API 호출

[root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-6cf7495fcd-88cqq -- curl http://api-tester-1231:80/version
[App Version] : Api Tester v1.0.0

 

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


[root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-6cf7495fcd-88cqq -- curl http://api-tester-1231:80/version
[App Version] : Api Tester v1.0.0

 

HPA

[root@k8s-master ~] k top -n anotherclass-123 pods
NAME                               CPU(cores)   MEMORY(bytes)
api-tester-1231-6cf7495fcd-88cqq   1m           140Mi

[root@k8s-master ~] k top -n anotherclass-123 pods
NAME                               CPU(cores)   MEMORY(bytes)
api-tester-1231-6cf7495fcd-88cqq   104m         161Mi


[root@k8s-master ~]# k get hpa -n anotherclass-123
NAME                      REFERENCE                    TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
api-tester-1231-default   Deployment/api-tester-1231   56%/60%   2         4         2          3d8h

 

[behavior] 미사용으로 적용

spec:
#  behavior:
#    scaleDown:
#      policies:
#      - periodSeconds: 15
#        type: Percent
#        value: 100
#      selectPolicy: Max
#    scaleUp:
#      policies:
#      - periodSeconds: 15
#        type: Pods
#        value: 4
#      - periodSeconds: 15
#        type: Percent
#        value: 100
#      selectPolicy: Max
#      stabilizationWindowSeconds: 120

[root@k8s-master ~] kubectl edit -n anotherclass-123 hpa api-tester-1231-default
horizontalpodautoscaler.autoscaling/api-tester-1231-default edited

부하 발생 API

[root@k8s-master ~] k exec -n anotherclass-123 -it api-tester-1231-6cf7495fcd-88cqq -- curl http://192.168.56.30:31231/cpu-load
[root@k8s-master ~] k get hpa -n anotherclass-123
NAME                      REFERENCE                    TARGETS    MINPODS   MAXPODS   REPLICAS   AGE
api-tester-1231-default   Deployment/api-tester-1231   101%/60%   2         4         4          3d8h

[root@k8s-master ~] k top -n anotherclass-123  pods
NAME                               CPU(cores)   MEMORY(bytes)
api-tester-1231-6cf7495fcd-5b6bw   202m         157Mi
api-tester-1231-6cf7495fcd-88cqq   1m           140Mi
api-tester-1231-6cf7495fcd-br5d2   1m           127Mi
api-tester-1231-6cf7495fcd-wzx9k   1m           127Mi

 

 

 

 

 

 

 

댓글을 작성해보세요.

채널톡 아이콘