如何在我的 OpenSearch Service 集群中创建索引模式?
我想在我的 Amazon OpenSearch Service 集群中创建索引模式。
解决方法
先决条件:
- AWS Identity and Access Management (IAM) 用户必须具有 PUT 和 POST 权限才能创建索引模式。访问策略示例:
**注意:**将 region 替换为您的 AWS 区域,将 account-id 替换为您的 AWS 账户,将 domain-name 替换为您的域名。{ "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/*" } ] } - 您的集群版本必须允许索引模式。
创建索引模式
使用 OpenSearch 控制面板
您可以使用 OpenSearch 控制面板为具有/不具有精细访问控制的 OpenSearch Service 或 Elasticsearch 集群创建索引模式。有关说明,请参阅 OpenSearch 网站上的 Creating an index pattern(创建索引模式)。
使用 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 替换为您的索引名称或模式。
对于具有精细访问控制的集群,请完成以下步骤:
- 要在 auth.txt 文件中生成授权 Cookie,请根据集群类型运行以下命令。
Elasticsearch 集群:
**注意:**将 usernameexample 替换为您的用户名,将 passwordexample 替换为您的密码。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
OpenSearch Service 集群:
**注意:**将 usernameexample 替换为您的用户名,将 passwordexample 替换为您的密码。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 - 要提交索引模式创建请求,请根据您的集群类型运行以下命令:
Elasticsearch 集群:
**注意:**将 sample-index 替换为您的索引名称或模式。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
OpenSearch Service 集群:
**注意:**将 sample-index 替换为您的索引名称或模式。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
使用 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 进行身份验证,请创建内部用户来管理索引模式。
**注意:**对于激活精细访问控制的集群,用户必须具有 ESHttpPut 和 ESHttpPost 权限才能创建索引模式。
您无法在全局租户中创建索引模式
默认情况下,OpenSearch 控制面板会在全球租户下创建索引模式。要在全局租户之外创建索引模式,请运行以下命令:
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 别名
要对此问题进行故障排除,请完成以下步骤:
- 要检查集群中是否存在 .kibana 别名,请运行以下命令:
**注意:**对于具有精细访问控制的集群,请在用户名和密码中添加 -u 标志。命令示例:curl -XGET https://opensearch-end-point/_cat/aliases
如果 .kibana 索引不存在,请继续执行步骤 4。curl -XPOST -u 'master-user:master-user-password' 'domain-endpoint/_cat/indices - 要创建 .kibana 索引的备份,请运行以下命令:
**注意:**请将 domain-endpoint 替换为您的域端点。对于具有精细访问控制的集群,请在用户名和密码中添加 -u 标志。curl -XPOST "https://domain-endpoint/_reindex" -H 'Content-Type: application/json' -d'{ "source": { "index": ".kibana" }, "dest": { "index": ".kibana_backup" } }' - 要删除 .kibana 索引,请运行以下命令:
**注意:**请将 domain-endpoint 替换为您的域端点。对于具有精细访问控制的集群,请在用户名和密码中添加 -u 标志。curl -XDELETE "https://domain-endpoint/.kibana" - 要创建 .kibana 别名并将其指向 .kibana_backup 索引,请运行以下命令:
**注意:**请将 domain-endpoint 替换为您的域端点。对于具有精细访问控制的集群,请在用户名和密码中添加 -u 标志。curl -XPOST "https://domain-endpoint/_aliases" -H 'Content-Type: application/json' -d'{ "actions": [ { "add": { "index": ".kibana_backup", "alias": ".kibana" } } ] }'
相关信息
Export and import Kibana dashboards with OpenSearch Service(使用 OpenSearch Service 导出和导入 Kibana 控制面板)

