跳至內容

如何在我的 OpenSearch Service 叢集中建立索引模式?

3 分的閱讀內容
0

我想在 Amazon OpenSearch Service 叢集中建立一個索引模式。

解決方法

先決條件:

  • AWS Identity and Access Management (IAM) 使用者必須具備 PUT 和 POST 權限才能建立索引模式。存取政策範例:
    {  "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "VisualEditor0",
          "Effect": "Allow",
          "Action": [
            "es:ESHttpHead",
            "es:ESHttpPost",
            "es:ESHttpGet",
            "es:ESHttpDelete",
            "es:ESHttpPut"
          ],
          "Resource": "arn:aws:es:region:account-id:domain/domain-name/*"
        }
      ]
    }
    **注意:**請將 region 替換為您的 AWS 區域,將 account-id 替換為您的 AWS 帳戶,將 domain-name 替換為您的網域名稱。
  • 您的叢集版本必須允許索引模式。

建立索引模式

使用 OpenSearch Dashboards

您可以使用 OpenSearch Dashboards,為已啟用或未啟用精細存取控制的 OpenSearch Service 或 Elasticsearch 叢集建立索引模式。如需操作說明,請參閱 OpenSearch 網站上的建立索引模式

使用 curl 命令

若要為未啟用精細存取控制的叢集建立索引模式,請根據叢集類型執行以下命令。

Elasticsearch 叢集:

curl -X POST  https://elasticsearch-end-point/_plugin/kibana/api/saved_objects/index-pattern/ \
-H "kbn-xsrf: true" \

-H "content-type: application/json" \

-d '{ "attributes": { "title": "sample-index*" } }'

**注意:**請將 sample-index 替換為您的索引名稱或模式。

OpenSearch Service 叢集:

curl -X POST  https://opensearch-end-point/_dashboards/api/saved_objects/index-pattern/ \
-H "kbn-xsrf: true" \

-H "content-type: application/json" \

-d '{ "attributes": { "title": "sample-index*" } }'

**注意:**請將 sample-index 替換為您的索引名稱或模式。

若為啟用精細存取控制的叢集,請依照以下步驟進行:

  1. 若要在 auth.txt 檔案中產生授權 Cookie,請根據叢集類型執行以下命令。
    Elasticsearch 叢集:
    curl -X POST  https://elasticsearch-end-point/_plugin/kibana/auth/login  \
    -H "kbn-xsrf: true" \
    -H "content-type: application/json" \
    -d '{"username":"usernameexample", "password":"passwordexample"}' \
    -c auth.txt
    **注意:**請將 usernameexample 替換為您的使用者名稱,將 passwordexample 替換為您的密碼。
    OpenSearch Service 叢集:
    curl -X POST  https://opensearch-end-point/_dashboards/auth/login  \
    -H "kbn-xsrf: true" \
    -H "content-type: application/json" \
    -d '{"username":"usernameexample", "password":"passwordexample"}' \
    -c auth.txt
    **注意:**請將 usernameexample 替換為您的使用者名稱,將 passwordexample 替換為您的密碼。
  2. 若要提交建立索引模式的請求,請根據叢集類型執行以下命令:
    Elasticsearch 叢集:
    curl -X POST  https://elasticsearch-end-point/_plugin/kibana/api/saved_objects/index-pattern/test  \
    -H "kbn-xsrf: true" \
    -H "content-type: application/json" \
    -d '{ "attributes": { "title": "sample-index*" } }' \
    -b auth.txt
    **注意:**請將 sample-index 替換為您的索引名稱或模式。
    OpenSearch Service 叢集:
    curl -X POST  https://opensearch-end-point/_dashboards/api/saved_objects/index-pattern/  \
    -H "kbn-xsrf: true" \
    -H "content-type: application/json" \
    -d '{ "attributes": { "title": "sample-index*" } }' \
    -b auth.txt
    **注意:**請將 sample-index 替換為您的索引名稱或模式。

使用 Python

先決條件:

  • 將執行 Python 程式碼的角色對應至精細存取控制叢集的後端角色
  • 執行以下命令來安裝所需的相依性:
    pip install boto3
    pip install opensearch-py
    pip install requests
    pip install requests-aws4auth

接著執行以下 Python 命令,以在 OpenSearch Service 叢集建立索引模式:

import boto3
import requests
from requests_aws4auth import AWS4Auth

host = 'https://domain-endpoint/' # include trailing /
region = 'aos-region' # example us-west-1
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)


path = '_dashboards/api/saved_objects/index-pattern' # _plugin/kibana/api/saved_objects/index-pattern for es versions
url = host + path
payload = {"attributes":{"title":"multi-logs-*","fields":"[]"}}
headers = {"Content-Type": "application/json", "osd-xsrf": "true", "security_tenant": "global" }
r = requests.post (url, auth=awsauth, json=payload, headers=headers)
print(r.status_code)
print(r.text)

**注意:**請將 domain-endpoint 替換為您的網域端點,並將 aos-region 替換為您的區域。對於 Elasticsearch 叢集,請將 _dashboards/api/saved_objects/index-pattern 替換為 _plugin/kibana/api/saved_objects/index-pattern

對索引模式建立問題進行疑難排解

您使用具備 SAML 2.0 或 Amazon Cognito 驗證的精細存取控制

如果叢集的網域使用 SAML 2.0 或 Amazon Cognito 進行驗證,請建立內部使用者以管理索引模式。

**注意:**若叢集已啟用精細存取控制,使用者必須具備 ESHttpPutESHttpPost 權限才能建立索引模式。

您無法在全域租用戶中建立索引模式

預設情況下,OpenSearch Dashboards 會在全域租用戶下建立索引模式。若要在全域租用戶之外建立索引模式,請執行以下命令:

curl -s -X POST https://opensearch-end-point/_dashboards/api/saved_objects/index-pattern/sample-index -d '{"attributes": {"title": "sample-index*"}}' \
-H "osd-xsrf:true" \
-H "securitytenant: private" \
-H "content-type:application/json" \
-b auth.txt

**注意:**請將 sample-index 替換為您的索引名稱或模式。

叢集中未包含 .kibana 別名

若要對此問題進行疑難排解,請完成以下步驟:

  1. 若要檢查叢集中是否存在 .kibana 別名,請執行以下命令:
    curl -XGET https://opensearch-end-point/_cat/aliases
    **注意:**若叢集啟用精細存取控制,請加入 -u 參數並輸入您的使用者名稱與密碼。命令範例:
    curl -XPOST -u 'master-user:master-user-password' 'domain-endpoint/_cat/indices
    如果 .kibana 索引不存在,請繼續執行步驟 4。
  2. 若要建立 .kibana 索引的備份,請執行以下命令:
    curl -XPOST "https://domain-endpoint/_reindex" -H 'Content-Type: application/json' -d'{
      "source": {
        "index": ".kibana"
      },
      "dest": {
     "index": ".kibana_backup"
      }
    }'
    **注意:**請將 domain-endpoint 替換為您的網域端點。若叢集啟用精細存取控制,請加入 -u 參數並輸入您的使用者名稱與密碼。
  3. 若要刪除 .kibana 索引,請執行以下命令:
    curl -XDELETE "https://domain-endpoint/.kibana"
    **注意:**請將 domain-endpoint 替換為您的網域端點。若叢集啟用精細存取控制,請加入 -u 參數並輸入您的使用者名稱與密碼。
  4. 若要建立 .kibana 別名並將其指向 .kibana_backup 索引,請執行以下命令:
    curl -XPOST "https://domain-endpoint/_aliases" -H 'Content-Type: application/json' -d'{
      "actions": [
        {
          "add": {
            "index": ".kibana_backup",
            "alias": ".kibana"
          }
        }
      ]
    }'
    **注意:**請將 domain-endpoint 替換為您的網域端點。若叢集啟用精細存取控制,請加入 -u 參數並輸入您的使用者名稱與密碼。

相關資訊

使用 OpenSearch Service 匯出與匯入 Kibana 儀表板

為什麼 ISM 政策中的變換索引動作會在 OpenSearch Service 中一直失敗?

AWS 官方已更新 3 個月前