如何在 Amazon EKS 中還原、調整其大小或建立 EBS 持久性儲存快照,以進行災難復原或處理超過 EBS 修改率的情形?

5 分的閱讀內容
0

我想在 Amazon Elastic Kubernetes Service (Amazon EKS) 中使用 Amazon Elastic Block Store (Amazon EBS) 持久性儲存快照,以進行災難復原。如何建立、還原這類快照或調整其大小? 或者,我超過了我的 Amazon EBS 修改率。但我仍然需要在 Amazon EKS 中還原、建立 Amazon EBS 持久性儲存快照或調整其大小。

簡短描述

您正在修改 Amazon EKS 中的 Amazon EBS 持久性儲存時,收到以下錯誤:

errorCode:Client.VolumeModificationRateExceeded
errorMessage:您已達到每個磁碟區限制的最大修改率。每個 EBS 磁碟區進行修改之間至少等待 6 小時

修改磁碟區之後,您必須等待至少六小時才能繼續修改磁碟區。您再次修改磁碟區之前,請確保磁碟區處於使用中可用狀態。

您的組織可能有災難復原 (DR) 目標和少於六小時的復原時間點目標 (RTO)。對於少於六小時的 RTO,請使用 Amazon EBS Container Storage Interface (CSI) 驅動程式建立快照並還原磁碟區。

解決方案

**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請確保您使用的是最新的 AWS CLI 版本

使用 Amazon EBS CSI 驅動程式和外部快照執行下列動作:

  1. 建立 PersistentVolumeClaim 的 Amazon EBS 快照。
  2. 還原 PersistentVolumeClaim
  3. PersistentVolumeClaim 繫結至工作負載。

先決條件:

使用外部快照安裝 Amazon EBS CSI 驅動程式

1.    檢查叢集是否擁有現有的 IAM OpenID Connect (OIDC) 提供者:

% cluster_name=ebs
% oidc_id=$(aws eks describe-cluster --name cluster_name --query "cluster.identity.oidc.issuer" --output text | cut -d '/' -f 5)
% aws iam list-open-id-connect-providers | grep $oidc_id

**注意:**使用您的叢集名稱取代 cluster_name

範例輸出:

"Arn": "arn:aws:iam::XXXXXXXXXX:oidc-provider/oidc.eks.eu-west-1.amazonaws.com/id/B7E2BC2980D17C9A5A3889998CB22B23"

**注意:**如果您沒有 IAM OIDC 提供者,請為叢集建立一個 IAM OIDC 提供者

2.    安裝外部快照。
**注意:**您安裝 Amazon EBS CSI 附加元件之前,必須先安裝外部快照。此外,您必須依照下列順序安裝外部快照元件:

CustomResourceDefinition (CRD),適用於 volumesnapshotclassesvolumesnapshotsvolumesnapshotcontents

mkdir crd
cd crd
wget https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/client/config/crd/kustomization.yaml
wget https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml
wget https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml
wget https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml
kubectl apply -k ../crd

RBAC,例如 ClusterRoleClusterRoleBinding

kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/deploy/kubernetes/snapshot-controller/rbac-snapshot-controller.yaml

控制器部署

kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/master/deploy/kubernetes/snapshot-controller/setup-snapshot-controller.yaml

3.    使用 eksctl 建立您的 Amazon EBS CSI 外掛程式 IAM 角色:

eksctl create iamserviceaccount \
  --name ebs-csi-controller-sa \
  --namespace kube-system \
  --cluster cluster_name \
  --attach-policy-arn arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy \
  --approve \
  --role-only \
  --role-name AmazonEKS_EBS_CSI_DriverRole

4.    使用 eksctl 新增 Amazon EBS CSI 附加元件:

eksctl create addon --name aws-ebs-csi-driver --cluster cluster_name --service-account-role-arn arn:aws:iam::account_id:role/AmazonEKS_EBS_CSI_DriverRole --force

**注意:**使用您的 AWS 帳戶 ID 取代 account_id

5.    確認 Amazon EBS CSI 驅動程式和外部快照 Pod 正在執行:

% kubectl get pods -A | egrep "csi|snapshot"

使用 Amazon EBS 持久性儲存建立 StatefulSet

1.    從 GitHub 網站下載清單檔案

2.    建立 StorageClassVolumeSnapshotClass

% kubectl apply -f manifests/classes/

範例輸出:

volumesnapshotclass.snapshot.storage.k8s.io/csi-aws-vsc created
storageclass.storage.k8s.io/ebs-sc created

3.    在您的叢集上部署 StatefulSet 以及 PersistentVolumeClaim

% kubectl apply -f manifests/app/

範例輸出:

service/cassandra created
StatefulSet.apps/cassandra created

4.    確認 Pod 處於執行中狀態:

% kubectl get pods

範例輸出:

NAME         READY  STATUS   RESTARTS  AGE
cassandra-0  1/1    Running  0         33m
cassandra-1  1/1    Running  0         32m
cassandra-2  1/1    Running  0         30m

5.    確認 PersistentVolumeClaim 已繫結到您的 PersisentVolume

% kubectl get pvc

範例輸出:

NAME                                              STATUS  VOLUME                                    CAPACITY ACCESS MODES STORAGECLASS  AGE

persistentvolumeclaim/cassandra-data-cassandra-0  Bound   pvc-b3ab4971-37dd-48d8-9f59-8c64bb65b2c8  2Gi      RWO          ebs-sc        28m
persistentvolumeclaim/cassandra-data-cassandra-1  Bound   pvc-6d68170e-2e51-40f4-be52-430790684e51  2Gi      RWO          ebs-sc        28m
persistentvolumeclaim/cassandra-data-cassandra-2  Bound   pvc-d0403adb-1a6f-44b0-9e7f-a367ad7b7353  2Gi      RWO          ebs-sc        26m
...

**注意:**請注意各 PersistentVolumeClaim 的名稱,與快照清單檔案中的 PersistentVolumeClaim 進行比較。

6.    若要測試 StatefulSet,請將內容寫入 PersistentVolumeClaim

for i in {0..2}; do kubectl exec "cassandra-$i" -- sh -c 'echo "$(hostname)" > /cassandra_data/data/file.txt'; done

建立快照

快照清單檔案中的 persistentVolumeClaimName 必須與您為 StatefulSet 中的每個 Pod 建立的 PersistentVolumeClaim 名稱相符。例如:

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: cassandra-data-snapshot-0
spec:
  volumeSnapshotClassName: csi-aws-vsc
  source:
    persistentVolumeClaimName: cassandra-data-cassandra-0

1.    從每個 PersistenVolumeClaim 建立快照:

% kubectl apply -f manifests/snapshot/

範例輸出:

volumesnapshot.snapshot.storage.k8s.io/cassandra-data-snapshot-0 created
volumesnapshot.snapshot.storage.k8s.io/cassandra-data-snapshot-1 created
volumesnapshot.snapshot.storage.k8s.io/cassandra-data-snapshot-2 created

2.    狀態完成後,請確認快照可在 Amazon Elastic Compute Cloud (Amazon EC2) 主控台上使用:

aws ec2 describe-snapshots --filters "Name=tag-key,Values=*ebs*" --query 'Snapshots[*].{VOL_ID:VolumeId,SnapshotID:SnapshotId,State:State,Size:VolumeSize,Name:[Tags[?Key==`Name`].Value] [0][0]}' --output table
---------------------------------------------------------------------------------------------------------------------------------------
|                                                          DescribeSnapshots                                                          |
+------------------------------------------------------------+-------+-------------------------+------------+-------------------------+
|                            Name                            | Size  |       SnapshotID        |   State    |         VOL_ID          |
+------------------------------------------------------------+-------+-------------------------+------------+-------------------------+
|  ebs-dynamic-snapshot-c6c9cb3c-2dab-4833-9124-40a0abde170d |  2    |  snap-057c5e2de3957d855 |  pending   |  vol-01edf53ee26a615f5  |
|  ebs-dynamic-snapshot-1c1ad0c5-a93a-468f-ab54-576db5d630d4 |  2    |  snap-02bf49a3b78ebf194 |  completed |  vol-0289efe54525dca4a  |
|  ebs-dynamic-snapshot-760c48e7-33ff-4b83-a6fb-6ef13b8d31b7 |  2    |  snap-0101c3d2efa40af19 |  completed |  vol-0fe68c9ac2f6375a4  |
+------------------------------------------------------------+-------+-------------------------+------------+-------------------------+

還原快照

您可以使用相同的 PersistentVolumeClaim 名稱,從現有的 PersistentVolumeClaim 所建立的快照還原 PersistentVolumeClaim。您重新建立 StatefulSet 時,PersistentVolumeClaim 會動態佈建 PersistentVolume,並自動繫結至 StatefulSet Pod。StatefulSet PersistenVolumeClaim 名稱格式為:「PVC_範本_名稱-STATEFULSET_名稱-複製_索引」。

若要還原快照,請遵循下列步驟:

1.    刪除現有的 StatefulSet 工作負載:

kubectl delete -f manifests/app/Cassandra_statefulset.yaml

**注意:**刪除工作負載也會刪除 StatefulSet Pod。您建立的快照會做為備份。

範例輸出:

statefulset.apps "cassandra" deleted

2.    強制刪除 PersistentVolumeClaim

for i in {0..2}
do
  kubectl delete pvc cassandra-data-cassandra-$i --force
done

**注意:**刪除 PersistentVolumeClaim 也會刪除 PersistentVolume

3.    使用與您之前建立的 PersistentVolumeClaim 相同的名稱,從快照還原 PersistentVolumeClaim

kubectl apply -f manifests/snapshot-restore/

範例輸出:

persistentvolumeclaim/cassandra-data-cassandra-0 created
persistentvolumeclaim/cassandra-data-cassandra-1 created
persistentvolumeclaim/cassandra-data-cassandra-2 created

4.    確認每個 PersistentVolumeClaim 都處於待完成狀態:

kubectl get pvc

範例輸出:

NAME                         STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
cassandra-data-cassandra-0   Pending                                      ebs-sc         24s
cassandra-data-cassandra-1   Pending                                      ebs-sc         23s
cassandra-data-cassandra-2   Pending                                      ebs-sc         22s

5.    使用原始清單檔案重新建立 StatefulSet:

kubectl apply -f manifests/app-restore/

**注意:**要調整儲存空間大小,請使用新的儲存空間大小定義 StatefulSet。

範例輸出:

StatefulSet.apps/cassandra created

6.    檢查 Amazon EBS 儲存空間的內容,以確認可使用快照和還原可運作:

for i in {0..2}; do kubectl exec "cassandra-$i" -- sh -c 'cat /cassandra_data/data/file.txt'; done
cassandra-0
cassandra-1
cassandra-2

調整 PersistentVolumeClaim 的大小

您可以修改 PersistentVolumeClaim.spec.resources.requests.storage,以自動反映您在 StatefulSet 清單檔案中定義的大小:

for i in {0..2}
do
  echo "Resizing cassandra-$i"
  kubectl patch pvc cassandra-data-cassandra-$i -p '{ "spec": { "resources": { "requests": { "storage": "4Gi" }}}}'
done

**注意:**4Gi 是範例儲存空間大小。定義適合您實際使用的儲存空間大小。

確認新的儲存大小反映於 Amazon EC2 主控台和 Pod 中:

% aws ec2 describe-volumes --filters "Name=tag-key,Values=*pvc*" --query 'Volumes[*].{ID:VolumeId,Size:Size,Name:[Tags[?Key==`Name`].Value] [0][0]}' --output table
-------------------------------------------------------------------------------------------
|                                     DescribeVolumes                                     |
+------------------------+--------------------------------------------------------+-------+
|           ID           |                         Name                           | Size  |
+------------------------+--------------------------------------------------------+-------+
|  vol-01266a5f1f8453e06 |  ebs-dynamic-pvc-359a87f6-b584-49fa-8dd9-e724596b2b43  |  4    |
|  vol-01b63a941450342d9 |  ebs-dynamic-pvc-bcc6f2cd-383d-429d-b75f-846e341d6ab2  |  4    |
|  vol-041486ec92d4640af |  ebs-dynamic-pvc-fa99a595-84b7-49ad-b9c2-1be296e7f1ce  |  4    |
+------------------------+--------------------------------------------------------+-------+

% for i in {0..2}
do
  echo "Inspecting cassandra-$i"
  kubectl exec -it cassandra-$i -- lsblk
  kubectl exec -it cassandra-$i -- df -h
done...

執行以下 Kubectl 命令以清除您的 StatefulSet

若要刪除您為 StatefulSet 建立的資源,請執行下列 kubectl 命令:

app-restore

kubectl delete -f manifests/app-restore

snapshot-restore

kubectl delete -f manifests/snapshot-restore

snapshot

kubectl delete -f manifests/snapshot

classes

kubectl delete -f manifests/classes

Cassandra

kubectl delete -f manifests/app/Cassandra_service.yaml

相關資訊

ModifyVolume

AWS 官方
AWS 官方已更新 1 年前