

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 SMB CSI 驅動程式
<a name="use-smb-csi"></a>

請依照本節中的程序，安裝、設定或刪除在 Amazon S3 檔案閘道上使用 SMB 檔案共用，以便在 Kubernetes 叢集中儲存所需的 CSI 驅動程式。如需詳細資訊，請參閱 GitHub 上的開放原始碼 SMB CSI 驅動程式文件，網址為 [https://github.com/kubernetes-csi/csi-driver-smb/blob/master/docs/install-csi-driver-master.md](https://github.com/kubernetes-csi/csi-driver-smb/blob/master/docs/install-csi-driver-master.md)。

**注意**  
建立`PersistentVolume`物件或`StorageClass`物件時，您可以指定`ReclaimPolicy`參數，以判斷刪除物件時外部儲存體會發生什麼情況。SMB CSI 驅動程式支援 `Retain`和 `Recycle`選項，但目前不支援 `Delete`選項。

## 安裝驅動程式
<a name="install-smb-csi"></a>

**安裝 Kubernetes SMB CSI 驅動程式：**

1. 從可存取 `kubectl` 的命令列終端機中，為您的 Kubernetes 叢集執行下列命令：

   **curl -skSL https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/deploy/install-driver.sh \| bash -s master --**

1. 等待上一個命令完成，然後使用下列命令來確保 CSI 驅動程式 Pod 正在執行：

   **kubectl -n kube-system get pod -o wide --watch -l app=csi-smb-controller**

   **kubectl -n kube-system get pod -o wide --watch -l app=csi-smb-node**

   輸出格式應類似以下內容：

   ```
   NAME                                       READY   STATUS    RESTARTS   AGE     IP             NODE
   csi-smb-controller-56bfddd689-dh5tk       4/4     Running   0          35s     10.240.0.19    k8s-agentpool-22533604-0
   csi-smb-controller-56bfddd689-8pgr4       4/4     Running   0          35s     10.240.0.35    k8s-agentpool-22533604-1
   csi-smb-node-cvgbs                        3/3     Running   0          35s     10.240.0.35    k8s-agentpool-22533604-1
   csi-smb-node-dr4s4                        3/3     Running   0          35s     10.240.0.4     k8s-agentpool-22533604-0
   ```

## 建立 SMB StorageClass 物件
<a name="create-storageclass-smb-csi"></a>

**若要為您的 Kubernetes 叢集建立新的 SMB StorageClass 物件：**

1. 建立名為 的組態檔案`storageclass.yaml`，其中包含類似下列範例的內容。針對顯示的 {{ExampleValues}} 替換您自己的部署特定資訊。

   ```
   ---
   apiVersion: storage.k8s.io/v1
   kind: StorageClass
   metadata:
       name: {{ExampleStorageClassName}}
   provisioner: smb.csi.k8s.io
   parameters:
       source: "{{//gateway-dns-name-or-ip-address/example-share-name}}"
       # if csi.storage.k8s.io/provisioner-secret is provided, will create a sub directory
       # with PV name under source
       csi.storage.k8s.io/provisioner-secret-name: "{{examplesmbcreds}}"
       csi.storage.k8s.io/provisioner-secret-namespace: "{{examplenamespace}}"
       csi.storage.k8s.io/node-stage-secret-name: "{{examplesmbcreds}}"
       csi.storage.k8s.io/node-stage-secret-namespace: "{{examplenamespace}}"
   volumeBindingMode: Immediate
   reclaimPolicy: Retain
   mountOptions:
       - dir_mode=0777
       - file_mode=0777
       - uid=1001
       - gid=1001
   ```

1. 從可存取 `kubectl`和 的命令列終端機`storageclass.yaml`，執行下列命令：

   **kubectl apply -f storageclass.yaml**
**注意**  
您也可以將上一個步驟的`.yaml`組態文字提供給大多數第三方 Kubernetes 管理和容器化平台，以建立 StorageClass。

1. 將 Kubernetes 叢集中的 Pod 設定為使用您建立的新 StorageClass。如需詳細資訊，請參閱 中的 Kubernetes 線上文件[https://kubernetes.io/docs/concepts/storage/](https://kubernetes.io/docs/concepts/storage/)。

## 建立 SMB PersistentVolume 和 PersistentVolumeClaim 物件
<a name="create-persistentvolume-volumeclaim-smb-csi"></a>

**若要建立新的 SMB PersistentVolume 和 PersistentVolumeClaim 物件：**

1. 建立兩個組態檔案。一個名為 `persistentvolume.yaml`，另一個名為 `persistentvolumeclaim.yaml`。

1. 對於 `persistentvolume.yaml`，新增類似下列範例的內容。將您自己的部署特定資訊替換為所示的 {{ExampleValues}}。

   ```
   ---
   apiVersion: v1
   kind: PersistentVolume
   metadata:
       name: {{pv-smb-example-name}}
       namespace: {{smb-example-namespace}} # PersistentVolume and PersistentVolumeClaim must use the same namespace parameter
   spec:
       capacity:
           storage: 100Gi
       accessModes:
           - ReadWriteMany
       persistentVolumeReclaimPolicy: Retain
       mountOptions:
           - dir_mode=0777
           - file_mode=0777
           - vers=3.0
       csi:
           driver: smb.csi.k8s.io
           readOnly: false
           volumeHandle: {{examplehandle}}  # make sure it's a unique id in the cluster
           volumeAttributes:
               source: "{{//gateway-dns-name-or-ip-address/example-share-name}}"
           nodeStageSecretRef:
               name: {{example-smbcreds}}
               namespace: {{smb-example-namespace}}
   ```

1. 對於 `persistentvolumeclaim.yaml`，新增類似下列範例的內容。將您自己的部署特定資訊替換為所示的 {{ExampleValues}}。

   ```
   ---
   kind: PersistentVolumeClaim
   apiVersion: v1 
   metadata: 
       name: {{examplename-pvc-smb-static}}
       namespace: {{smb-example-namespace}} # PersistentVolume and PersistentVolumeClaim must use the same namespace parameter
   spec: 
       accessModes: 
           - ReadWriteMany 
       resources: 
           requests: 
               storage: 10Gi 
           volumeName: {{pv-smb-example-name}} # make sure specfied volumeName matches the name of the PersistentVolume you created
           storageClassName: ""
   ```

1. 從可存取 `kubectl`和您建立的兩個`.yaml`檔案的命令列終端機，執行下列命令：

   **kubectl apply -f persistentvolume.yaml**

   **kubectl apply -f persistentvolumeclaim.yaml**
**注意**  
您也可以透過將上一個步驟的`.yaml`組態文字提供給大多數第三方 Kubernetes 管理和容器化平台，來建立 PersistentVolume 和 PersistentVolumeClaim 物件。

1. 將 Kubernetes 叢集中的 Pod 設定為使用您建立的新 PersistentVolumeClaim。如需詳細資訊，請參閱 中的 Kubernetes 線上文件[https://kubernetes.io/docs/concepts/storage/](https://kubernetes.io/docs/concepts/storage/)。

## 解除安裝驅動程式
<a name="uninstall-smb-csi"></a>

**若要解除安裝 Kubernetes SMB CSI 驅動程式：**
+ 從可存取 `kubectl` 的命令列終端機中，為您的 Kubernetes 叢集執行下列命令：

  **curl -skSL https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/deploy/uninstall-driver.sh \| bash -s --**