

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

# 教學課程：遷移至 Amazon OpenSearch Service
<a name="migration"></a>

索引快照是將自我管理的 OpenSearch 或舊版 Elasticsearch 叢集遷移至 Amazon OpenSearch Service 的常用方法。此程序大致包含下列步驟：

1. 建立現有叢集的快照，然後將快照上傳至 Amazon S3 儲存貯體。

1. 建立 OpenSearch Service 網域

1. 授與 OpenSearch Service 存取儲存貯體的許可，並確保您擁有使用快照的許可。

1. 在 OpenSearch Service 網域上還原快照。

此逐步解說提供更詳細的步驟和替代選項，如適用。

## 建立並上傳快照
<a name="migration-take-snapshot"></a>

雖然您可以使用儲存[庫-s3](https://docs.opensearch.org/latest/opensearch/snapshot-restore/#amazon-s3) 外掛程式直接將快照擷取到 S3，但您必須在每個節點上安裝外掛程式、調整 `opensearch.yml`(`elasticsearch.yml`如果使用 Elasticsearch 叢集）、重新啟動每個節點、新增您的 AWS 憑證，最後再拍攝快照。外掛程式是適合持續使用或遷移較大型叢集的絕佳選擇。

對於較小的叢集，一次性方法是拍攝[共用檔案系統快照](https://docs.opensearch.org/latest/opensearch/snapshot-restore/#shared-file-system)，然後使用 AWS CLI 將其上傳至 S3。如果您已經有快照，請跳至步驟 4。

****若要建立快照並上傳至 Amazon S3****

1. 將 `path.repo` 設定新增至所有節點上的 `opensearch.yml` (或 `Elasticsearch.yml`)，然後重新啟動每個節點。

   ```
   path.repo: ["/my/shared/directory/snapshots"]
   ```

1. 註冊[快照儲存庫](https://opensearch.org/docs/latest/opensearch/snapshot-restore/#register-repository)，此為建立快照前所需。儲存庫只是一個儲存位置：共用檔案系統、Amazon S3、Hadoop 分散式檔案系統 (HDFS) 等。在此情況下，我們將使用共用檔案系統 ("fs")：

   ```
   PUT _snapshot/my-snapshot-repo-name
   {
     "type": "fs",
     "settings": {
       "location": "/my/shared/directory/snapshots"
     }
   }
   ```

1. 建立快照：

   ```
   PUT _snapshot/my-snapshot-repo-name/my-snapshot-name
   {
     "indices": "migration-index1,migration-index2,other-indices-*",
     "include_global_state": false
   }
   ```

1. 安裝 [AWS CLI](https://aws.amazon.com/cli/)，然後執行 `aws configure` 以新增您的登入資料。

1. 瀏覽至快照目錄。然後執行下列命令以建立新的 S3 儲存貯體，並將快照目錄的內容上傳至該儲存貯體：

   ```
   aws s3 mb s3://amzn-s3-demo-bucket --region us-west-2
   aws s3 sync . s3://amzn-s3-demo-bucket --sse AES256
   ```

   視快照大小和網際網路連線速度而定，此操作可能需要一段時間。

## 建立網域
<a name="migration-create-domain"></a>

雖然主控台是建立網域最簡單的方式，但在這種情況下，您已經開啟終端機並 AWS CLI 安裝 。修改下列命令即可建立符合您需求的網域：

```
aws opensearch create-domain \
  --domain-name migration-domain \
  --engine-version OpenSearch_1.0 \
  --cluster-config InstanceType=c5.large.search,InstanceCount=2 \
  --ebs-options EBSEnabled=true,VolumeType=gp2,VolumeSize=100 \
  --node-to-node-encryption-options Enabled=true \
  --encryption-at-rest-options Enabled=true \
  --domain-endpoint-options EnforceHTTPS=true,TLSSecurityPolicy=Policy-Min-TLS-1-2-2019-07 \
  --advanced-security-options Enabled=true,InternalUserDatabaseEnabled=true,MasterUserOptions='{MasterUserName=master-user,MasterUserPassword=master-user-password}' \
  --access-policies '{"Version": "2012-10-17",		 	 	 "Statement":[{"Effect":"Allow","Principal": {"AWS": "arn:aws:iam::aws-region:user/UserName"},"Action":["es:ESHttp*"],"Resource":"arn:aws:es:aws-region:111122223333:domain/migration-domain/*"}]}' \
  --region aws-region
```

若未經修改，此命令會建立具有兩個資料節點的網際網路存取網域，每個節點都有 100 GiB 的儲存空間。它也會啟用具有 HTTP 基本身分驗證和所有加密設定的[精細存取控制](fgac.md)。如果您需要更進階的安全組態 (例如 VPC)，請使用 OpenSearch Service 主控台。

發出命令之前，請先變更網域名稱、主要使用者登入資料和帳戶號碼。指定您用於 S3 儲存貯體 AWS 區域 的相同 ，以及與您的快照相容的 OpenSearch/Elasticsearch 版本。

**重要**  
快照只能正向相容，而且只能往前一個主要版本。例如，您無法從 Elasticsearch 7.x 叢集上的 OpenSearch 1.*x* 叢集還原快照，只能從 OpenSearch 1.*x* 或 2.*x* 叢集還原快照。**次要版本也很重要。您無法在 5.3.2 OpenSearch Service 網域上從自我管理的 5.3.3 叢集中還原快照。我們建議您選擇您的快照支援的 OpenSearch 或 Elasticsearch 的最新版本。如需相容版本的表格，請參閱[使用快照來遷移資料](snapshot-based-migration.md)。

## 提供許可以存取 S3 儲存貯體。
<a name="migration-permissions"></a>

在 AWS Identity and Access Management (IAM) 主控台中，[建立具有下列許可和信任關係的角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html)。 [https://docs.aws.amazon.com/IAM/latest/UserGuide/roles-managingrole-editing-console.html#roles-managingrole_edit-trust-policy](https://docs.aws.amazon.com/IAM/latest/UserGuide/roles-managingrole-editing-console.html#roles-managingrole_edit-trust-policy)在建立角色時，選擇 **S3** 作為 **AWS Service**。將角色命名為 `OpenSearchSnapshotRole` 以便輕鬆找到。

**許可**

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [{
      "Action": [
        "s3:ListBucket"
      ],
      "Effect": "Allow",
      "Resource": [
        "arn:aws:s3:::amzn-s3-demo-bucket"
      ]
    },
    {
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:DeleteObject"
      ],
      "Effect": "Allow",
      "Resource": [
        "arn:aws:s3:::amzn-s3-demo-bucket/*"
      ]
    }
  ]
}
```

------

**信任關係**

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [{
      "Effect": "Allow",
      "Principal": {
        "Service": "es.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
```

------

然後授予您的個人 IAM 角色許可，以擔任 `OpenSearchSnapshotRole`。建立下列政策，並[將它連接](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)到您的身分：

**許可**

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [{
      "Effect": "Allow",
      "Action": "iam:PassRole",
      "Resource": "arn:aws:iam::123456789012:role/OpenSearchSnapshotRole"
    }
  ]
}
```

------

### 映射 OpenSearch Dashboards 中的快照角色 (如果使用精細存取控制)
<a name="migration-snapshot-role"></a>

如果您已啟用[精細存取控制](fgac.md#fgac-mapping)，即使您將 HTTP 基本身分驗證用於所有其他目的，您也需要將 `manage_snapshots` 角色映射至 IAM 角色，以便您可以使用快照。

**若要為您的身分授予許可以便使用快照**

1. 使用您在建立 OpenSearch Service 網域時指定的主要使用者憑證登入 Dashboards。您可以在 OpenSearch Service 主控台中找到 Dashboards URL。其格式為 `https://domain-endpoint/_dashboards/`。

1. 從主選單中選擇 **Security** (安全性)、**Roles** (角色)，然後選取 **manage\$1snapshots** 角色。

1. 選擇 **Mapped users** (已映射的使用者)、**Manage mapping** (管理映射)。

1. 在適當的欄位中新增您的個人 IAM 角色的網域 ARN。ARN 採用下列其中一種格式：

   ```
   arn:aws:iam::123456789123:user/user-name
   ```

   ```
   arn:aws:iam::123456789123:role/role-name
   ```

1. 選擇 **Map** (映射)，並確認角色顯示在 **Mapped users** (已映射的使用者) 中。

## 還原快照
<a name="migration-restore"></a>

此時，您有兩種方式可存取 OpenSearch Service 網域：使用主要使用者憑證進行 HTTP 基本身分驗證，或使用 IAM 憑證進行 AWS 身分驗證。由於快照使用 Amazon S3，它沒有主要使用者的概念，因此您必須使用 IAM 憑證向您的 OpenSearch Service 網域註冊快照儲存庫。

大多數程式設計語言都有程式庫來協助簽署請求，但更簡單的方法是使用 [Postman](https://www.postman.com/downloads/) 之類的工具，並將您的 IAM 登入資料放入**授權**區段。

![\[Postman interface showing Authorization settings for AWS API request with Signature type.\]](http://docs.aws.amazon.com/zh_tw/opensearch-service/latest/developerguide/images/migration2.png)


**還原快照**

1. 無論您選擇如何簽署請求，第一步都是註冊儲存庫：

   ```
   PUT _snapshot/my-snapshot-repo-name
   {
     "type": "s3",
     "settings": {
       "bucket": "amzn-s3-demo-bucket",
       "region": "us-west-2",
       "role_arn": "arn:aws:iam::123456789012:role/OpenSearchSnapshotRole"
     }
   }
   ```

1. 然後列出儲存庫中的快照，找到您要還原的快照。此時，您可以繼續使用 Postman，或切換到 [curl](https://curl.haxx.se/) 等工具。

   **速記**

   ```
   GET _snapshot/my-snapshot-repo-name/_all
   ```

   **curl**

   ```
   curl -XGET -u 'master-user:master-user-password' https://domain-endpoint/_snapshot/my-snapshot-repo-name/_all
   ```

1. 還原快照。

   **速記**

   ```
   POST _snapshot/my-snapshot-repo-name/my-snapshot-name/_restore
   {
     "indices": "migration-index1,migration-index2,other-indices-*",
     "include_global_state": false
   }
   ```

   **curl**

   ```
   curl -XPOST -u 'master-user:master-user-password' https://domain-endpoint/_snapshot/my-snapshot-repo-name/my-snapshot-name/_restore \
     -H 'Content-Type: application/json' \
     -d '{"indices":"migration-index1,migration-index2,other-indices-*","include_global_state":false}'
   ```

1. 最後，確認索引是否已依照預期還原。

   **速記**

   ```
   GET _cat/indices?v
   ```

   **curl**

   ```
   curl -XGET -u 'master-user:master-user-password' https://domain-endpoint/_cat/indices?v
   ```

此時，遷移即已完成。您可以設定用戶端以使用新的 OpenSearch Service 端點，[調整網域大小](sizing-domains.md)以符合您的工作負載，檢查索引的碎片計數，切換到 [IAM 主要使用者](fgac.md#fgac-concepts)，或開始在 OpenSearch Dashboards 中建置視覺效果。