

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 SMB CSI 驱动程序
<a name="use-smb-csi"></a>

按照本节中的程序，在 Kubernetes 集群中安装、配置或删除在 Amazon S3 文件网关中使用 SMB 文件共享作为存储所需的 CSI 驱动程序。有关更多信息，请参阅上的开源 SMB CSI 驱动程序文档，网址 GitHub 为[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. 在可以访问 Kubernetes 集群的 `kubectl` 的命令行终端中，运行以下命令：

   **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**
**注意**  
您还可以 StorageClass 通过向大多数第三方 Kubernetes 管理和容器化平台提供上一步中的`.yaml`配置文本来创建。

1. 将 Kubernetes 集群中的容器配置为使用您创建的新 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**
**注意**  
您还可以通过向大多数第三方 Kubernetes 管理和容器化平台提供上一步中的`.yaml`配置文本来创建和 PersistentVolumeClaim对象。 PersistentVolume 

1. 将 Kubernetes 集群中的容器配置为使用您创建的新 PersistentVolumeClaim 容器。有关更多信息，请参阅 Kubernetes 在线文档，网址为 [https://kubernetes.io/docs/concepts/storage/](https://kubernetes.io/docs/concepts/storage/)。

## 卸载驱动程序
<a name="uninstall-smb-csi"></a>

**要卸载 Kubernetes SMB CSI 驱动程序，请执行以下操作：**
+ 在可以访问 Kubernetes 集群的 `kubectl` 的命令行终端中，运行以下命令：

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