在本教學課程中,您將瞭解如何設定本機共用 Edge 磁碟區。 此磁碟區類型提供 Kubernetes 叢集的本機多次讀取/寫入儲存體。 這種共享存儲類型仍然獨立於雲基礎設施,使其成為不適合雲目的地的暫存空間、臨時存儲和本地持久數據的理想選擇。 這也是在邊緣主動使用、變更或處理之資料的理想目的地。
本教學課程涵蓋下列工作:
- 建立本機共用邊緣磁碟區 PVC
- 建立範例部署
- 連線至您的 Pod
- 撰寫範例檔案
先決條件
- 本教學課程假設您已經有已啟用 Arc 的 Kubernetes 叢集。 若要將現有的 Kubernetes 叢集連線到 Azure Arc,請參閱 將 現有的 Kubernetes 叢集連線到 Azure Arc。
- 您應該已經安裝 Azure 容器儲存體延伸模組。 如果您還沒有進行此操作,請參閱 安裝 Edge 磁碟區。
建立本機共用邊緣磁碟區 PVC
本節會為您的本機共用磁碟區建立永續性磁碟區宣告或 PVC。 您可以指定要為此磁碟區佈建多少空間。
建立名為 localSharedPVC.yaml 的檔案,包含以下內容。 使用永續性磁碟區宣告的名稱來修改 metadata.name 值。 然後,在第 8 行中,指定符合您預期消費 Pod 的命名空間。 該 metadata.name 值會在下一個步驟的最後一 deploymentExample.yaml 行上引用。 參數 spec.resources.requests.storage 會決定持續性磁碟區的大小。 在此範例中為 2 GB,但可以根據您的需求進行修改:
備註
請只使用小寫字母和連字符。 如需詳細資訊,請參閱 Kubernetes 物件命名文件。
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
### Create a name for your PVC ###
name: <create-a-pvc-name-here>
### Use a namespace that matches your intended consuming pod, or "default" ###
namespace: <intended-consuming-pod-or-default-here>
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
storageClassName: unbacked-sc
然後,執行此命令以套用YAML檔案並建立PVC:
kubectl apply -f "localSharedPVC.yaml"
建立範例部署或使用您自己的部署
如果您已經有要部署的應用程式,您可以在此處使用它。 為了簡化並方便入手,我們在這裡提供一個範例部署。 若要使用我們的範例組態建立部署,請建立名為 deploymentExample.yaml 的檔案,其中包含下列內容。 為containers.name和volumes.persistentVolumeClaim.claimName填入值。 此 spec.replicas 參數會決定要建立的複本 Pod 數目。 在此範例中為 2,但可以修改以符合您的需求:
備註
請只使用小寫字母和連字符。 如需詳細資訊,請參閱 Kubernetes 物件命名文件。
apiVersion: apps/v1
kind: Deployment
metadata:
name: localsharededgevol-deployment ### This will need to be unique for every volume you choose to create
spec:
replicas: 2
selector:
matchLabels:
name: wyvern-testclientdeployment
template:
metadata:
name: wyvern-testclientdeployment
labels:
name: wyvern-testclientdeployment
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- wyvern-testclientdeployment
topologyKey: kubernetes.io/hostname
containers:
### Specify the container in which to launch the busy box. ###
- name: <create-a-container-name-here>
image: 'mcr.microsoft.com/mirror/docker/library/busybox:1.35'
command:
- "/bin/sh"
- "-c"
- "dd if=/dev/urandom of=/data/acsalocalsharedtestfile count=16 bs=1M && while true; do ls /data &>/dev/null || break; sleep 1; done"
volumeMounts:
### This name must match the following volumes::name attribute ###
- name: wyvern-volume
### This mountPath is where the PVC will be attached to the pod's filesystem ###
mountPath: /data
volumes:
### User-defined name that is used to link the volumeMounts. This name must match volumeMounts::name as previously specified. ###
- name: wyvern-volume
persistentVolumeClaim:
### This claimName must refer to your PVC metadata::name from lsevPVC.yaml.
claimName: <your-pvc-metadata-name-from-line-5-of-pvc-yaml>
然後,執行此命令以套用 YAML 檔案並建立部署:
kubectl apply -f "deploymentExample.yaml"
連線至您的 Pod
執行 kubectl get pods 以尋找 Pod 的名稱。 複製此名稱,因為下一個步驟需要它。
備註
因為來自 spec::replicas 的 是指定為 2,所以會有兩個 Pod 出現,使用 kubectl get pods。 您可以在下一個步驟中使用任一個 Pod 名稱。
執行下列命令,並使用您在上一步中複製的值來取代 POD_NAME_HERE:
kubectl exec -it POD_NAME_HERE -- sh
將目錄變更為 /data 掛載路徑,如 deploymentExample.yaml 中所指定。
撰寫範例檔案
現在您已連線到 Pod 並在掛接路徑中,您在這裡寫入的任何檔案都會出現在磁碟區中。
例如,建立名為 file1.txt 的檔案,並使用 echo "Hello World" > file1.txt寫入該檔案。
您現在可以看到您編寫的檔案在目錄中。
後續步驟
完成這些步驟之後,請開始使用 Azure 監視器和 Kubernetes 監視,或使用 Prometheus 和 Grafana 進行第三方監視。