

 **帮助改进此页面** 

要帮助改进本用户指南，请选择位于每个页面右侧窗格中的**在 GitHub 上编辑此页面**链接。

# 将示例有状态工作负载部署到 EKS 自动模式
<a name="sample-storage-workload"></a>

本教程将演示向 EKS 自动模式集群部署示例有状态应用程序的过程。该应用程序会将时间戳写入持久性卷，并演示 EKS 自动模式的自动 EBS 卷预置和持久性功能。

## 先决条件
<a name="_prerequisites"></a>
+ 一个 EKS 自动模式集群
+ 以恰当的权限配置了 AWS CLI
+  安装并配置了 `kubectl`
  + 有关更多信息，请参阅 [进行设置以使用 Amazon EKS](setting-up.md)。

## 第 1 步：配置环境
<a name="_step_1_configure_your_environment"></a>

1. 设置环境变量：

   ```
   export CLUSTER_NAME=my-auto-cluster
   export AWS_REGION="us-west-2"
   ```

1. 更新 kubeconfig：

   ```
   aws eks update-kubeconfig --name "${CLUSTER_NAME}"
   ```

## 第 2 步：创建存储类
<a name="_step_2_create_the_storage_class"></a>

`StorageClass` 定义了 EKS 自动模式将如何预置 EBS 卷。

EKS 自动模式不会为您创建 `StorageClass`。您必须创建引用 `ebs.csi.eks.amazonaws.com` 的 `StorageClass`，才能使用 EKS 自动模式的存储功能。

1. 创建一个名为 `storage-class.yaml` 的文件：

   ```
   apiVersion: storage.k8s.io/v1
   kind: StorageClass
   metadata:
     name: auto-ebs-sc
     annotations:
       storageclass.kubernetes.io/is-default-class: "true"
   provisioner: ebs.csi.eks.amazonaws.com
   volumeBindingMode: WaitForFirstConsumer
   parameters:
     type: gp3
     encrypted: "true"
   ```

1. 应用 `StorageClass`：

   ```
   kubectl apply -f storage-class.yaml
   ```

 **关键组件：**
+  `provisioner: ebs.csi.eks.amazonaws.com` – 使用 EKS 自动模式
+  `volumeBindingMode: WaitForFirstConsumer` – 将卷的创建延迟到容器组需要时
+  `type: gp3` – 指定 EBS 卷类型
+  `encrypted: "true"` – EBS 将使用默认的 `aws/ebs` 密钥来加密使用该类创建的卷。您可以自由选择，但我们建议您这样做。
+  `storageclass.kubernetes.io/is-default-class: "true"` – 默认情况下 Kubernetes 将使用此存储类，除非您在持久性卷声明中指定了其他卷类。如果要从其他存储控制器迁移，请谨慎设置此值。（可选）

## 第 3 步：创建持久性卷声明
<a name="_step_3_create_the_persistent_volume_claim"></a>

PVC 向 `StorageClass` 请求存储空间。

1. 创建一个名为 `pvc.yaml` 的文件：

   ```
   apiVersion: v1
   kind: PersistentVolumeClaim
   metadata:
     name: auto-ebs-claim
   spec:
     accessModes:
       - ReadWriteOnce
     storageClassName: auto-ebs-sc
     resources:
       requests:
         storage: 8Gi
   ```

1. 应用 PVC：

   ```
   kubectl apply -f pvc.yaml
   ```

 **关键组件：**
+  `accessModes: ReadWriteOnce` – 一次只能由一个节点挂载卷
+  `storage: 8Gi` – 请求一个 8 GiB 卷
+  `storageClassName: auto-ebs-sc` – 引用我们创建的 `StorageClass`

## 第 4 步：部署应用程序
<a name="_step_4_deploy_the_application"></a>

部署会运行一个将时间戳写入持久性卷的容器。

1. 创建一个名为 `deployment.yaml` 的文件：

   ```
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: inflate-stateful
   spec:
     replicas: 1
     selector:
       matchLabels:
         app: inflate-stateful
     template:
       metadata:
         labels:
           app: inflate-stateful
       spec:
         terminationGracePeriodSeconds: 0
         nodeSelector:
           eks.amazonaws.com/compute-type: auto
         containers:
           - name: bash
             image: public.ecr.aws/docker/library/bash:4.4
             command: ["/usr/local/bin/bash"]
             args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 60; done"]
             resources:
               requests:
                 cpu: "1"
             volumeMounts:
               - name: persistent-storage
                 mountPath: /data
         volumes:
           - name: persistent-storage
             persistentVolumeClaim:
               claimName: auto-ebs-claim
   ```

1. 应用部署：

   ```
   kubectl apply -f deployment.yaml
   ```

 **关键组件：**
+ 将时间戳写入文件的简单 bash 容器
+ 将 PVC 挂载到 `/data` 
+ 请求 1 个 CPU 核心
+ 使用 EKS 托管式节点的节点选择器

## 第 5 步：验证设置
<a name="_step_5_verify_the_setup"></a>

1. 检查容器组是否在运行：

   ```
   kubectl get pods -l app=inflate-stateful
   ```

1. 验证 PVC 是否已绑定：

   ```
   kubectl get pvc auto-ebs-claim
   ```

1. 检查 EBS 卷：

   ```
   # Get the PV name
   PV_NAME=$(kubectl get pvc auto-ebs-claim -o jsonpath='{.spec.volumeName}')
   # Describe the EBS volume
   aws ec2 describe-volumes \
     --filters Name=tag:CSIVolumeName,Values=${PV_NAME}
   ```

1. 验证数据是否正在写入：

   ```
   kubectl exec "$(kubectl get pods -l app=inflate-stateful \
     -o=jsonpath='{.items[0].metadata.name}')" -- \
     cat /data/out.txt
   ```

## 步骤 6：清除
<a name="_step_6_cleanup"></a>

运行以下命令以移除本教程中创建的所有资源：

```
# Delete all resources in one command
kubectl delete deployment/inflate-stateful pvc/auto-ebs-claim storageclass/auto-ebs-sc
```

## 幕后发生了什么
<a name="_whats_happening_behind_the_scenes"></a>

1. PVC 向 `StorageClass` 请求存储空间 

1. 当容器组被调度时：

   1. EKS 自动模式会预置一个 EBS 卷

   1. 创建一个持久性卷

   1. 将该卷挂载到节点

1. 容器组会挂载该卷并开始写入时间戳

## 快照控制器
<a name="_snapshot_controller"></a>

EKS 自动模式与 Kubernetes CSI Snapshotter（也称为快照控制器）兼容。但是，Amazon EKS 自动模式不包括快照控制器。您负责安装和配置快照控制器。有关更多信息，请参阅 [为 CSI 卷启用快照功能](csi-snapshot-controller.md)。

检查以下引用 EKS 自动模式存储功能的 `VolumeSnapshotClass`。

```
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
  name: auto-ebs-vsclass
driver: ebs.csi.eks.amazonaws.com
deletionPolicy: Delete
```

 [了解有关 Kubernetes CSI Snapshotter 的更多信息。](https://github.com/kubernetes-csi/external-snapshotter/blob/master/README.md#usage)