brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Oct 22. 2023

38탄-2. EKS DB - EKS 설치 2/3

EKS에서 사용하는 스토리지 알아보자~


<1>  ADD ON 읽어보기

<2> 멱등성 실습 = 유지되려는 성질

<3> 스토리지 이론

<4> 기본 파드(컨테이너) 환경의 임시 파일시스템 사용 실습

<5> AWS라 스토리지를 EBS사용하자.

<6> 볼륨 증가 가능 , 축소는 불가능



<1>  ADD ON 읽어보기


1

CNI : Container Network Interface 는 k8s 네트워크 환경을 구성해준다 

https://kubernetes.io/docs/concepts/cluster-administration/networking/


2

DNS : 쿠버네티스 서비스를 위해 DNS 레코드를 제공해주며, Service Discovery 기능을 제공을 한다. 대표적으로 CoreDNS 가 있다 

https://coredns.io/


3

CSI : Container Storage Interface 


https://kubernetes.io/docs/concepts/storage

https://github.com/container-storage-interface/spec/blob/master/spec.md




<2> 멱등성 실습 = 유지되려는 성질


1

# 터미널1 (모니터링)

watch -d 'kubectl get pod'



# 터미널2

# Deployment 배포(Pod 3개)

kubectl create deployment my-webs --image=nginx --replicas=3

kubectl get pod -w



# 파드 증가 및 감소

kubectl scale deployment my-webs --replicas=6 && kubectl get pod -w

kubectl scale deployment my-webs --replicas=3

kubectl get pod


# 강제로 파드 삭제 : 바라는상태 + 선언형에 대한 대략적인 확인! ⇒ 어떤 일이 벌어지는가? 

kubectl delete pod --all && kubectl get pod -w

kubectl get pod


# 실습 완료 후 Deployment 삭제

kubectl delete deploy my-webs





<3> 스토리지 이론



1

파드 내부의 데이터는 파드가 정지되면 모두 삭제됨

https://kubernetes.io/ko/docs/concepts/storage/ephemeral-volumes/



2

데이터베이스(파드)처럼 데이터 보존이 필요

퍼시스턴트 볼륨(Persistent Volume, PV) - 어느 노드에서도 연결하여 사용 가능, 예시) NFS, AWS EBS, Ceph




출처 : https://aws.amazon.com/ko/blogs/tech/persistent-storage-for-kubernetes/





<4> 기본 파드(컨테이너) 환경의 임시 파일시스템 사용 실습


# 파드 배포

# date 명령어로 현재 시간을 10초 간격으로 /home/pod-out.txt 파일에 저장



curl -s -O https://raw.githubusercontent.com/gasida/PKOS/main/3/date-busybox-pod.yaml



cat date-busybox-pod.yaml | yh


(11-05-access@myeks:default) [root@myeks-bastion-EC2 ~]# cat date-busybox-pod.yaml | yh

apiVersion: v1

kind: Pod

metadata:

  name: busybox

spec:

  terminationGracePeriodSeconds: 3

  containers:

  - name: busybox

    image: busybox

    command:

    - "/bin/sh"

    - "-c"

    - "while true; do date >> /home/pod-out.txt; cd /home; sync; sync; sleep 10; done"


kubectl apply -f date-busybox-pod.yaml




# 파일 확인

kubectl get pod


kubectl exec busybox --tail -f /home/pod-out.txt


Sun Nov  5 10:39:41 UTC 2023

Sun Nov  5 10:39:51 UTC 2023

Sun Nov  5 10:40:01 UTC 2023




# 파드 삭제 후 다시 생성 후 파일 정보 확인 > 이전 기록이 보존되어 있는지?

kubectl delete pod busybox

kubectl apply -f date-busybox-pod.yaml

kubectl exec busybox -- tail -f /home/pod-out.txt


// 임시파일 시스템이라 이전 기록은 날아간다~~

Sun Nov  5 10:40:34 UTC 2023




# 실습 완료 후 삭제

kubectl delete pod busybox




<5> AWS라 스토리지를 EBS사용하자.



# 워커노드의 EBS 볼륨 확인 : tag(키/값) 필터링 - 링크

aws ec2 describe-volumes --filters Name=tag:Name,Values=$CLUSTER_NAME-ng1-Node --output table

aws ec2 describe-volumes --filters Name=tag:Name,Values=$CLUSTER_NAME-ng1-Node --query "Volumes[*].Attachments" | jq

aws ec2 describe-volumes --filters Name=tag:Name,Values=$CLUSTER_NAME-ng1-Node --query "Volumes[*].{ID:VolumeId,Tag:Tags}" | jq

aws ec2 describe-volumes --filters Name=tag:Name,Values=$CLUSTER_NAME-ng1-Node --query "Volumes[].[VolumeId, VolumeType, Attachments[].[InstanceId, State][]][]" | jq

aws ec2 describe-volumes --filters Name=tag:Name,Values=$CLUSTER_NAME-ng1-Node --query "Volumes[].{VolumeId: VolumeId, VolumeType: VolumeType, InstanceId: Attachments[0].InstanceId, State: Attachments[0].State}" | jq




# 워커노드에서 파드에 추가한 EBS 볼륨 확인

aws ec2 describe-volumes --filters Name=tag:ebs.csi.aws.com/cluster,Values=true --output table

aws ec2 describe-volumes --filters Name=tag:ebs.csi.aws.com/cluster,Values=true --query "Volumes[*].{ID:VolumeId,Tag:Tags}" | jq

aws ec2 describe-volumes --filters Name=tag:ebs.csi.aws.com/cluster,Values=true --query "Volumes[].{VolumeId: VolumeId, VolumeType: VolumeType, InstanceId: Attachments[0].InstanceId, State: Attachments[0].State}" | jq




1

터미널 하나 떠 띠워 모니터링 하자~


# 워커노드에서 파드에 추가한 EBS 볼륨 모니터링

while true; do aws ec2 describe-volumes --filters Name=tag:ebs.csi.aws.com/cluster,Values=true --query "Volumes[].{VolumeId: VolumeId, VolumeType: VolumeType, InstanceId: Attachments[0].InstanceId, State: Attachments[0].State}" --output text; date; sleep 1; done



2

PVC 생성한다.

Pod 생성 한다 = EBS 볼륨이 생겨 사용 된다. = 모니터링 결과 



# PVC 생성

cat <<EOT > awsebs-pvc.yaml

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: ebs-claim

spec:

  accessModes:

    - ReadWriteOnce

  resources:

    requests:

      storage: 4Gi

  storageClassName: gp3

EOT



kubectl apply -f awsebs-pvc.yaml

persistentvolumeclaim/ebs-claim created



kubectl get pvc,pv

NAME                              STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE

persistentvolumeclaim/ebs-claim   Pending                                      gp3            13s




# 파드 생성


cat <<EOT > awsebs-pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: app

spec:

  terminationGracePeriodSeconds: 3

  containers:

  - name: app

    image: centos

    command: ["/bin/sh"]

    args: ["-c", "while true; do echo \$(date -u) >> /data/out.txt; sleep 5; done"]

    volumeMounts:

    - name: persistent-storage

      mountPath: /data

  volumes:

  - name: persistent-storage

    persistentVolumeClaim:

      claimName: ebs-claim

EOT



kubectl apply -f awsebs-pod.yaml




모니터링 결과 생성 된다.




# PVC, 파드 확인

kubectl get pvc,pv,pod

kubectl get VolumeAttachment


# 추가된 EBS 볼륨 상세 정보 확인 

aws ec2 describe-volumes --volume-ids $(kubectl get pv -o jsonpath="{.items[0].spec.csi.volumeHandle}") | jq



# PV 상세 확인 : nodeAffinity 내용의 의미는?


kubectl get pv -o yaml | yh

...

    nodeAffinity:

      required:

        nodeSelectorTerms:

        - matchExpressions:

          - key: topology.ebs.csi.aws.com/zone

            operator: In

            values:

            - ap-northeast-2b

...


kubectl get node --label-columns=topology.ebs.csi.aws.com/zone,topology.kubernetes.io/zone

kubectl describe node | more



# 파일 내용 추가 저장 확인

kubectl exec app -- tail -f /data/out.txt

Sun Nov 5 10:46:40 UTC 2023

Sun Nov 5 10:46:45 UTC 2023

Sun Nov 5 10:46:50 UTC 2023

Sun Nov 5 10:46:55 UTC 2023

Sun Nov 5 10:47:00 UTC 2023

Sun Nov 5 10:47:05 UTC 2023

Sun Nov 5 10:47:10 UTC 2023

Sun Nov 5 10:47:15 UTC 2023

Sun Nov 5 10:47:20 UTC 2023

Sun Nov 5 10:47:25 UTC 2023

Sun Nov 5 10:47:30 UTC 2023

Sun Nov 5 10:47:35 UTC 2023

Sun Nov 5 10:47:40 UTC 2023



# 아래 명령어는 확인까지 다소 시간이 소요됨

kubectl df-pv




## 파드 내에서 볼륨 정보 확인

kubectl exec -it app -- sh -c 'df -hT --type=overlay'

Filesystem     Type     Size  Used Avail Use% Mounted on

overlay        overlay   30G  5.6G   25G  19% /



kubectl exec -it app -- sh -c 'df -hT --type=ext4'

Filesystem     Type  Size  Used Avail Use% Mounted on

/dev/nvme1n1   ext4  3.9G   28K  3.9G   1% /data




# 파드 강제 삭제 후 재생성하면서 기존 PVC 부착

k delete pod app



kubectl apply -f awsebs-pod.yaml


kubectl exec app -- tail -f /data/out.txt


//pod삭제해도 pv가 있어서 데이터도 보존된다.

Sun Nov 5 10:50:05 UTC 2023

Sun Nov 5 10:50:10 UTC 2023

Sun Nov 5 10:50:15 UTC 2023

Sun Nov 5 10:50:20 UTC 2023

Sun Nov 5 10:50:55 UTC 2023

Sun Nov 5 10:51:00 UTC 2023

Sun Nov 5 10:51:05 UTC 2023

Sun Nov 5 10:51:10 UTC 2023

Sun Nov 5 10:51:15 UTC 2023

Sun Nov 5 10:51:20 UTC 2023

Sun Nov 5 10:51:25 UTC 2023

Sun Nov 5 10:51:30 UTC 2023





<6> 볼륨 증가 가능 , 축소는 불가능


https://github.com/kubernetes-sigs/aws-ebs-csi-driver/tree/master/examples/kubernetes/resizing

https://kubernetes.io/blog/2018/07/12/resizing-persistent-volumes-using-kubernetes/


1

# 현재 pv 의 이름을 기준하여 4G > 10G 로 증가 : .spec.resources.requests.storage의 4Gi 를 10Gi로 변경


kubectl get pvc ebs-claim -o jsonpath={.spec.resources.requests.storage} ; echo

kubectl get pvc ebs-claim -o jsonpath={.status.capacity.storage} ; echo

kubectl patch pvc ebs-claim -p '{"spec":{"resources":{"requests":{"storage":"10Gi"}}}}'



# 확인 : 볼륨 용량 수정 반영이 되어야 되니, 수치 반영이 조금 느릴수 있다

kubectl exec -it app -- sh -c 'df -hT --type=ext4'

Filesystem     Type  Size  Used Avail Use% Mounted on

/dev/nvme1n1   ext4  3.9G   28K  3.9G   1% /data



시간 지난후 재확인

kubectl exec -it app -- sh -c 'df -hT --type=ext4'

Filesystem     Type  Size  Used Avail Use% Mounted on

/dev/nvme1n1   ext4  9.8G   28K  9.8G   1% /data




kubectl df-pv

 PV NAME                                   PVC NAME   NAMESPACE  NODE NAME                                         POD NAME  VOLUME MOUNT NAME   SIZE  USED  AVAILABLE  %USED  IUSED  IFREE   %IUSED

 pvc-f2b42009-f99a-44ef-9de2-f371fff2144f  ebs-claim  default    ip-192-168-1-147.ap-northeast-2.compute.internal  app       persistent-storage  9Gi   28Ki  9Gi        0.00   12     655348  0.00




aws ec2 describe-volumes --volume-ids $(kubectl get pv -o jsonpath="{.items[0].spec.csi.volumeHandle}") | jq


콘솔에서 확인하자~~



삭제

kubectl delete pod app

kubectl get pvc,pv

kubectl delete pvc ebs-claim



다음은

https://brunch.co.kr/@topasvga/3487



주말 CloudNet  스터디 내용 참고하여  정리한 부분입니다.

https://gasidaseo.notion.site/gasidaseo/CloudNet-Blog-c9dfa44a27ff431dafdd2edacc8a1863  




감사합니다.

매거진의 이전글 38탄-1. EKS DB - EKS 설치 1/3

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari