Persistent Storage for Google Kubernetes Engine
1. Create PVs and PVCs
Connect to the lab GKE cluster
$export my_zone=us-central1-a$export my_cluster=standard-cluster-1$source <(kubectl completion bash)$gcloud container clusters get-credentials $my_cluster --zone $my_zoneFetching cluster endpoint and auth data.kubeconfig entry generated for standard-cluster-1.
Create and apply a manifest with a PVC
pvc-demo.yaml
apiVersion: v1kind: PersistentVolumeClaimmetadata:name: hello-web-diskspec:accessModes:- ReadWriteOnceresources:requests:storage: 30Gi
$git clone https://github.com/GoogleCloudPlatform/training-data-analyst$ln -s ~/training-data-analyst/courses/ak8s/v1.1 ~/ak8s$cd ~/ak8s/Storage/
현재 PVC를 보여 준다
$$kubectl get persistentvolumeclaim
현재는 없ㅎ다.
No resources found in default namespace.
PVC만든다.
kubectl apply -f pvc-demo.yaml
다시 조회
NAME STATUS VOLUME CAP ACCESS MODES CLASS AGEhello-web-disk Bound pvc-8...34 30Gi RWO standard 5s
생겼다.
2. Mount and verify Google Cloud persistent disk PVCs in Pods
Mount the PVC to a Pod
pod-volume-demo.yaml
kind: PodapiVersion: v1metadata:name: pvc-demo-podspec:containers:- name: frontendimage: nginxvolumeMounts:- mountPath: "/var/www/html"name: pvc-demo-volumevolumes:- name: pvc-demo-volumepersistentVolumeClaim:claimName: hello-web-disk
만든다.
$kubectl apply -f pod-volume-demo.yaml
만들어 졌다.
pod/pvc-demo-pod created
확인
$kubectl get podsNAME READY STATUS RESTARTS AGEpvc-demo-pod 1/1 Running 0 28s
셀세션 시작
kubectl exec -it pvc-demo-pod -- sh
echo Test webpage in a persistent volume!>/var/www/html/index.htmlchmod +x /var/www/html/index.htmlexet
Test the persistence of the PV
1. 지운다. pvc-demo-pod.
$kubectl delete pod pvc-demo-pod
pod "pvc-demo-pod" deleted
2. 확인 한다.
$kubectl get pods
지워졌다.
No resources found in default namespace.
3. PVC를 확인
$kubectl get persistentvolumeclaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEhello-web-disk Bound pvc- 30Gi RWO standard 7m43s
4. pvc-demo-pod 재 패포
$kubectl apply -f pod-volume-demo.yaml
Output
pod/pvc-demo-pod created
5. pvc-demoe-pod 쉘 접속
$kubectl exec -it pvc-demo-pod -- sh
접속되면 #으로 바뀐다.
#cat /var/www/html/index.html
Output
Test webpage in a persistent volume!
빠져 나온다.
#exit
3.Create StatefulSets with PVCs
이 작업에서는 StatefulSet에서 PVC를 사용합니다.
StatefulSet은 포드에 고유 식별자가 제공된다는 점을 제외하고는 배포와 같습니다.
Release the PVC
pvc-demo-pod 삭제
$kubectl delete pod pvc-demo-pod
지워졌졌음.
pod "pvc-demo-pod" deleted
확인
kubectl get pods
결과
No resources found in default namespace.
Create a StatefulSet
스테이플셋 생성 statefulset-demo.yaml
kind: ServiceapiVersion: v1metadata:name: statefulset-demo-servicespec:ports:- protocol: TCPport: 80targetPort: 9376type: LoadBalancer---apiVersion: apps/v1kind: StatefulSetmetadata:name: statefulset-demospec:selector:matchLabels:app: MyAppserviceName: statefulset-demo-servicereplicas: 3updateStrategy:type: RollingUpdatetemplate:metadata:labels:app: MyAppspec:containers:- name: stateful-set-containerimage: nginxports:- containerPort: 80name: httpvolumeMounts:- name: hello-web-diskmountPath: "/var/www/html"volumeClaimTemplates:- metadata:name: hello-web-diskspec:accessModes: [ "ReadWriteOnce" ]resources:requests:storage: 30Gi
생성
$kubectl apply -f statefulset-demo.yaml
로드벨런싱 되는 서비스와, 30G 볼륨이 생성되었음.
hello-web-disk
/var/www/html
마운트 욈
service/statefulset-demo-service createdstatefulset.apps/statefulset-demo created
Verify the connection of Pods in StatefulSets
StatefulSet에서 포드의 연결 확인
세부 정보 확인
$kubectl describe statefulset statefulset-demo
Output
Name: statefulset-demoNamespace: defaultCreationTimestamp: Sat, 05 Feb 2022 09:11:44 +0000Selector: app=MyAppLabels: <none>Annotations: <none>Replicas: 3 desired | 3 totalUpdate Strategy: RollingUpdatePods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 FailedPod Template:Labels: app=MyAppContainers:stateful-set-container:Image: nginxPort: 80/TCPHost Port: 0/TCPEnvironment: <none>Mounts:/var/www/html from hello-web-disk (rw)Volumes: <none>Volume Claims:Name: hello-web-diskStorageClass:Labels: <none>Annotations: <none>Capacity: 30GiAccess Modes: [ReadWriteOnce]Events:Type Reason Age From Message---- ------ ---- ---- -------Normal SuccessfulCreate 2m52s statefulset-controller create Claim hello-web-disk-statefulset-demo-0 Pod statefulset-demo-0 in StatefulSet statefulset-demo successNormal SuccessfulCreate 2m52s statefulset-controller create Pod statefulset-demo-0 in StatefulSet statefulset-demo successfulNormal SuccessfulCreate 2m38s statefulset-controller create Claim hello-web-disk-statefulset-demo-1 Pod statefulset-demo-1 in StatefulSet statefulset-demo successNormal SuccessfulCreate 2m38s statefulset-controller create Pod statefulset-demo-1 in StatefulSet statefulset-demo successfulNormal SuccessfulCreate 2m24s statefulset-controller create Claim hello-web-disk-statefulset-demo-2 Pod statefulset-demo-2 in StatefulSet statefulset-demo successNormal SuccessfulCreate 2m24s statefulset-controller create Pod statefulset-demo-2 in StatefulSet statefulset-demo successful
포드 리스트 확인
$keubectl get pods
NAME READY STATUS RESTARTS AGEstatefulset-demo-0 1/1 Running 0 4m14sstatefulset-demo-1 1/1 Running 0 4mstatefulset-demo-2 1/1 Running 0 3m46s
PVC 확인
$kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEhello-web-disk Bound pvc-80 30Gi RWO standard 20mhello-web-disk-statefulset-demo-0 Bound pvc-3a 30Gi RWO standard 5m4shello-web-disk-statefulset-demo-1 Bound pvc-42 30Gi RWO standard 4m50shello-web-disk-statefulset-demo-2 Bound pvc-4c 30Gi RWO standard 4m36s
스테이트풀 pvc 확인
$kubectl describe pvc hello-web-disk-statefulset-demo-0
4. Verify the persistence of Persistent Volume connections to Pods managed by StatefulSets
statefulset-demo-0 쉘 접속
$kubectl exec -it statefulset-demo-0 -- sh
# echo Test webpage in a persistent volume!>/var/www/html/index.htmlchmod +x /var/www/html/index.html### cat /var/www/html/index.htmlTest webpage in a persistent volume!## exit