Exercise 8.4: Using a ResourceQuota to Limit PVC Count and Usage

  1. CP 노드에 연결된 터미널로 이동

  2. 이전 실습에서 생성했던 리소스 삭제

    {
        kubectl delete deploy nginx-nfs
        kubectl delete pvc pvc-one
        kubectl delete pv nfs
    }
  3. Namespace 생성

    kubectl create ns small 
  4. 생성한 Namespace 상세 내용 확인

    kubectl describe ns small 
  5. PV 생성

    cat <<EOF | kubectl create -n small -f -
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: nfs
    spec:
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain
      nfs:
        path: /data
        server: mynfs
        readOnly: false
    EOF
  6. PVC 생성

    cat <<EOF | kubectl create -n small -f -
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-one
    spec:
      accessModes:
      - ReadWriteMany
      resources:
         requests:
           storage: 200Mi
    EOF
  7. ResourceQuota 생성

    cat <<EOF | kubectl apply -n small -f -
    apiVersion: v1
    kind: ResourceQuota
    metadata:
      name: storagequota
    spec:
      hard:
        persistentvolumeclaims: "10"
        requests.storage: "500Mi"
    EOF
  8. 위에서 생성한 Namespace에 ResourceQuota가 반영됐는지 확인

    kubectl describe ns small 
  9. Deployment 생성

    cat <<EOF | kubectl create -n small -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        run: nginx
      name: nginx-nfs
    spec:
      selector:
        matchLabels:
          run: nginx
      template:
        metadata:
          labels:
            run: nginx
        spec:
          containers:
          - image: nginx
            imagePullPolicy: Always
            name: nginx
            volumeMounts:
            - name: nfs-vol
              mountPath: /opt
            ports:
            - containerPort: 80
              protocol: TCP
          volumes:
          - name: nfs-vol
            persistentVolumeClaim:
              claimName: pvc-one
    EOF
  10. 생성된 Pod 확인

    kubectl get pod -l run=nginx -n small 
  11. 생성된 Pod 상세 내용 확인

    kubectl describe pod POD_NAME -n small 

    OR

    kubectl describe pod $(kubectl get pod -l run=nginx -n small -o=jsonpath='{.items[0].metadata.name}') -n small
  12. ResourceQuota 확인

    kubectl describe ns small 
  13. NFS 노드에 연결된 터미널로 이동

  14. /data/ 디렉토리에 300MB 크기의 파일 생성

    sudo dd if=/dev/zero of=/data/bigfile bs=1M count=300
  15. CP 노드에 연결된 터미널로 이동

  16. ResourceQuota에 수동으로 생성한 파일이 반영되는지 확인

    kubectl describe ns small 
  17. Deployment 삭제

    kubectl delete deploy nginx-nfs -n small 
  18. ResourceQuota 확인

    kubectl describe ns small
  19. PVC 삭제

    kubectl delete pvc pvc-one -n small
  20. ResourceQuota 확인

    kubectl describe ns small
  21. PV 상태 및 ReclaimPolicy 확인

    kubectl get pv nfs
  22. PV 삭제

    kubectl delete pv nfs
  23. PV 재생성 - spec.persistentVolumeReclaimPolicy 값을 Delete로 지정

    cat <<EOF | kubectl create -f -
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: nfs
    spec:
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Delete
      nfs:
        path: /data/
        server: mynfs
        readOnly: false
    EOF
  24. ResourceQuota 확인

    kubectl describe ns small
  25. PVC 생성

    cat <<EOF | kubectl create -n small -f -
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-one
    spec:
      accessModes:
      - ReadWriteMany
      resources:
         requests:
           storage: 200Mi
    EOF
  26. ResourceQuota 확인

    kubectl describe ns small
  27. ResourceQuota 변경 - requests.storage 값을 100Mi로 지정

    cat <<EOF | kubectl apply -n small -f -
    apiVersion: v1
    kind: ResourceQuota
    metadata:
      name: storagequota
    spec:
      hard:
        persistentvolumeclaims: "10"
        requests.storage: "100Mi"
    EOF
  28. ResourceQuota 확인

    kubectl describe ns small
  29. Deployment 생성

    cat <<EOF | kubectl create -n small -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        run: nginx
      name: nginx-nfs
    spec:
      selector:
        matchLabels:
          run: nginx
      template:
        metadata:
          labels:
            run: nginx
        spec:
          containers:
          - image: nginx
            imagePullPolicy: Always
            name: nginx
            volumeMounts:
            - name: nfs-vol
              mountPath: /opt
            ports:
            - containerPort: 80
              protocol: TCP
          volumes:
          - name: nfs-vol
            persistentVolumeClaim:
              claimName: pvc-one
    EOF
  30. Pod가 생성되었는지 확인

    kubectl get pod -l run=nginx -n small 
  31. Deployment 및 PVC 삭제

    {
        kubectl delete deploy nginx-nfs -n small
        kubectl delete pvc pvc-one -n small
    }
  32. PV가 삭제되었는지 확인

    kubectl get pv nfs
  33. PV에 발생한 Event 확인

    kubectl describe pv nfs
  34. PV 삭제

    kubectl delete pv nfs
  35. LimitRange 생성

    cat <<EOF | kubectl -n small apply -f -
    apiVersion: v1
    kind: LimitRange
    metadata:
      name: storagelimits
    spec:
      limits:
      - type: PersistentVolumeClaim
        max:
          storage: 2Gi
        min:
          storage: 1Gi
    EOF
  36. LimitRagne 및 ResourceQuota 확인

    kubectl describe ns small
  37. PV 재생성 - spec.persistentVolumeReclaimPolicy 값을 Recycle로 지정

    cat <<EOF | kubectl create -f -
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: nfs
    spec:
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Recycle
      nfs:
        path: /data
        server: mynfs
        readOnly: false
    EOF
  38. PV의 ReclaimPolicy 확인

    kubectl get pv nfs
  39. PVC 생성 시도

    cat <<EOF | kubectl create -n small -f -
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-one
    spec:
      accessModes:
      - ReadWriteMany
      resources:
         requests:
           storage: 200Mi
    EOF
  40. LimitRange 변경

    cat <<EOF | kubectl -n small apply -f -
    apiVersion: v1
    kind: LimitRange
    metadata:
      name: storagelimits
    spec:
      limits:
      - type: PersistentVolumeClaim
        max:
          storage: 2Gi
        min:
          storage: 100Mi
    EOF
  41. PVC 생성 시도

    cat <<EOF | kubectl create -n small -f -
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-one
    spec:
      accessModes:
      - ReadWriteMany
      resources:
         requests:
           storage: 200Mi
    EOF
  42. ResourceQuota의 requests.storage 값을 500Mi로 변경

    cat <<EOF | kubectl apply -n small -f -
    apiVersion: v1
    kind: ResourceQuota
    metadata:
      name: storagequota
    spec:
      hard:
        persistentvolumeclaims: "10"
        requests.storage: "500Mi"
    EOF
  43. PVC 생성

    cat <<EOF | kubectl create -n small -f -
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-one
    spec:
      accessModes:
      - ReadWriteMany
      resources:
         requests:
           storage: 200Mi
    EOF
  44. Deployment 생성

    cat <<EOF | kubectl create -n small -f -
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        run: nginx
      name: nginx-nfs
    spec:
      selector:
        matchLabels:
          run: nginx
      template:
        metadata:
          labels:
            run: nginx
        spec:
          containers:
          - image: nginx
            imagePullPolicy: Always
            name: nginx
            volumeMounts:
            - name: nfs-vol
              mountPath: /opt
            ports:
            - containerPort: 80
              protocol: TCP
          volumes:
          - name: nfs-vol
            persistentVolumeClaim:
              claimName: pvc-one
    EOF
  45. Pod가 생성되었는지 확인

    kubectl get pod -l run=nginx -n small
  46. NFS 서버에 생성한 파일이 보이는지 확인

    kubectl -n small exec deploy/nginx-nfs -- cat /opt/hello.txt
  47. Deployment 삭제

    kubectl delete deploy nginx-nfs -n small
  48. PVC 및 PV 상태 확인

    {
        kubectl get pvc -n small
        kubectl get pv
    }
  49. PVC 삭제

    kubectl delete pvc pvc-one -n small
  50. PV 상태 확인

    kubectl get pv
  51. PV에 발생한 Event 확인

    kubectl describe pv nfs
  52. NFS 노드에 연결된 터미널로 이동

  53. NFS 서버에 생성했던 파일이 존재하는지 확인

    ls /data/
  54. CP 노드에 연결된 터미널로 이동

  55. PV 삭제

    kubectl delete pv nfs
  56. Namespace 삭제

    kubectl delete ns small

Last updated