ElasticSearch安全特性讲解与使用

  • 用户认证,支持basic、LDAP/AD、Kerberos、SAML
  • 基于角色权限管理,支持Index级别、Document级别
  • 传输加密

    • 集群内部节点与节点之间SSL/TLS
    • 集群与Client之间加密传输https

      ElasticSearch Security 开启安全特性

  • Basic License ```python

    node1

    cluster.name: mycluster node.name: node1 node.master: true node.ingest: false node.data: false node.ml: false network.host: _site discovery.seed_hosts: [“10.10.1.127”, “10.10.1.128”, “10.10.1.129”] cluster.initial_master_nodes: [“node1”]

xpack.security.enabled: true xpack.security.transport.ssl.enabled: true xpack.security.transport.ssl.verification_mode: certificate xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12 xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

node2

cluster.name: mycluster node.name: node2 node.master: false node.ingest: true node.data: true node.ml: false network.host: _site discovery.seed_hosts: [“10.10.1.127”, “10.10.1.128”, “10.10.1.129”] cluster.initial_master_nodes: [“node1”]

xpack.security.enabled: true xpack.security.transport.ssl.enabled: true xpack.security.transport.ssl.verification_mode: certificate xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12 xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

node3

cluster.name: mycluster node.name: node2 node.master: false node.ingest: true node.data: true node.ml: false network.host: _site discovery.seed_hosts: [“10.10.1.127”, “10.10.1.128”, “10.10.1.129”] cluster.initial_master_nodes: [“node1”]

xpack.security.enabled: true xpack.security.transport.ssl.enabled: true xpack.security.transport.ssl.verification_mode: certificate xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12 xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

  1. <a name="4oZy8"></a>
  2. ## ElasticSearch基于角色的权限管理讲解
  3. - RBAC/Role Based Access Control
  4. - Resource 面向指定的资源
  5. - index / aliases / documents / fields
  6. - Permissions 所具有的操作权限列表,如可读、可写等
  7. - Privileges 具体的权限,如可读权限
  8. - Role最终被绑定到相关User才能生效
  9. <a name="y8Ly7"></a>
  10. ## Elasticsearch冷热架构讲解与实战
  11. - 创建索引时可以通过指定 index.routing.allocation.require 属性来分配分片到相关的节点上
  12. - 比如从hot,定时迁移到warm上
  13. ```python
  14. PUT test/_settings
  15. {
  16. "index.routing.allocation.require.bux_type": "hot"
  17. }

Elasticsearch CCS跨集群搜索讲解与实战

  • 通过一个集群搜索多个集群的数据
  • 集群间通讯是依赖remote cluster机制实现的
  • 可以在elasticsearch.yml或者动态修改相关配置
  • 查询语法

    • remote_cluster_name:index_name
    • :blog
      PUT _cluster/settings
      {
      "persistent": {
      "cluster": {
       "remote": {
         "cluster_one": {
           "seeds": [
             "127.0.0.1:9300"
           ],
           "transport.compress": true,
           "skip_unavailable": true
         },
         "cluster_two": {
           "seeds": [
             "127.0.0.1:9301"
           ],
           "transport.compress": true,
           "skip_unavailable": true
         }
       }
      }
      }
      }
      

      Elasticsearch Snapshot&Restore讲解与实战

  • Elasticsearch拥有副本机制来保障集群的高可用,但无法解决如下的数据丢失

    • 主副本所在的机器存储全部损坏
    • 误删除索引数据
    • 升级失败,数据无法回滚
  • 定期对数据做备份,按需恢复可以很好的解决如上问题
  • Elasticsearch提供了Snapshot和Restore API用于对集群数据完成备份与恢复
    • 数据备份的过程可以简单理解成将本地数据文件同步到远程仓库(repository)的过程
    • 支持全量和增量备份
    • repository常见也有如下类型:
      • fs / S3 / HDFS / Azure / Google Cloud Storage ```python

        创建备份数据仓库

        PUT /_snapshot/my_fs_backup { “type”: “fs”, “settings”: { “location”: “/home/elastic/backup”, “compress”: true } }

创建数据快照

PUT /_snapshot/my_backup/snapshot_2?wait_for_completion=true { “indices”: “index_1”, “ignore_unavailable”: true, “include_global_state”: false }

恢复备份数据

POST /snapshot/my_fs_backup/snapshot_2/_restore { “indices”: “index_1”, “ignore_unavailable”: true, “include_global_state”: false, “rename_pattern”: “(.+)”, “rename_replacement”: “restored$1” }

<a name="NEyzC"></a>
## Elasticsearch分片分配策略讲解与实战

- 分配策略最终是决定分片分配到那个节点,主要有如下的策略
   - Cluster Level Shard Allocation
   - Shard Allocation Awareness
   - Index Level Shard Allocation
<a name="IzDZS"></a>
### Cluster Level Shard Allocation

- 指定分配到部分节点
- 指定排除部分节点
- 通过ip、node name、id等等来过滤相关节点
```python
PUT _cluster/settings
{
  "transient" : {
    "cluster.routing.allocation.exclude._ip" : "10.0.0.1"
  }
}

Shard Allocation Awareness

  • 实现跨可用区的高可用架构
  • 可用区可以按需定义,如物理机、机架、机房等

    PUT _cluster/settings
    {
    "persistent": {
      "cluster.routing.allocation.awareness.attributes": "rack",
      "cluster.routing.allocation.awareness.force.rack.values": ["rack1", "rack2"]
    }
    }
    

    Index Level Shard Allocation

  • 指定某个索引职能分配到指定节点

    • 比如冷热架构的实现
  • 指定某个索引不能分配到指定节点
  • 指定每个节点在该索引级别可以分配的分片数上限

    PUT nginx-log/_settings
    {
    "index.routing.acclocation.require.type": "hot",
    "index.routing.acclocation.exclude._name": "node1",
    "index.routing.acclocation.total_shards_per_node": 3
    }
    

    Elasticsearch分片未分配原因诊断与实战

  • 当Elasticsearch中有分片处于未分配状态时,集群健康状态值为变成yellow或者red

  • 通过如下api可以查看集群状态情况,有三种

    • green健康状态,指所有主副分片都正常分配
    • yellow指所有主分片都正常分配,但是有副本分片为正常分配
    • red有主分片未分配
      GET _cluster/health
      GET _cat/indices?v&health=red
      GET _cat/indices?v&health=yellow
      GET _cat/indices?v&health=green
      
  • 可以通过如下命令查看未分配的分片信息

    • state为UNASSIGNED代表未分配
    • unassigned.reason和unassigned.details会显示部分未分配的原因
  • 常见未分配原因有
    • 节点离开集群,其上的分片无法使用
    • 分配规则配置有误
      • 如基于节点属性等配置后,无足够可用节点
      • total_shards_per_node配置后,导致无法找到可用的节点
  • Elasticsearch提供了_cluster/allocation/explain接口用于快速探索未分配分片及解释原因
    GET _cluster/allocation/explain
    

练习题

1. 搭建一个 3 节点集群并开启安全功能

创建3个节点组成的集群,要求如下:

  • cluster 名称为 training_cluster_one
  • node1的 node name 为 node1,其他依次为node2,node3
  • node1 角色仅为 master eligible 节点,node2 node3为数据节点、协调、预处理节点,3者构成集群
  • 3个节点设置属性如下面表格
  • 开启 elastic 安全功能
    • 将 elastic 用户密码设置为 elastic
    • 新建用户 test,设置其密码为 test123
      • 该用户可以登录 kibana
      • 该用户对 test 开头索引 read/write 的权限

2. 新建索引

  • 在 training_cluster_one 的集群上创建索引 logs-2021-01-01,满足如下条件
    • 主分片数为 3,副本为 0
    • 该索引只分布在 box_type 为 hot 的节点上
    • 别名 logs 指向该索引

3. 破坏集群状态

  • 请在不停止节点的情况下,想尽可能多的办法将 training_cluster_one 的集群状态变为 非 green 状态

4. 搭建新集群

  • 停止 training_cluster_one 集群,清空 data 目录,并建立如下 3 个集群 | 类型 | hostname | 集群名 | 节点名 | | —- | —- | —- | —- | | elasticsearch | es1 | cluster_one | node1 | | elasticsearch | es2 | cluster_two | node1 | | elasticsearch | es3 | cluster_three | node1 | | kibana | kibana | 连接到 cluster_one | |

启动上述所有集群,并验证是否成功

5. 备份与恢复

请在 cluster_one 中执行如下操作:

PUT _template/blog-template
{
  "index_patterns": [
    "blog-*"
  ],
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "dynamic_templates": [
      {
        "integers": {
          "match": "int_*",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    ],
    "properties": {
      "title": {
        "type": "text"
      }
    }
  }
}
PUT blog-test
POST blog-test/_bulk
{"index":{}}
{"title":"elasticsearch best practice","author_firstname":"tony","author_lastname":"song","tags":"elasticsearch ,logstash","int_age":18,"locale":"zh,en"}
{"index":{}}
{"title":"elastic stack release V5","author_firstname":"shay","author_lastname":"banon","tags":"elk ,elasticsearch,release","int_age":28,"locale":"zhc,en"}
{"index":{}}
{"title":"kibana tutorial","author_firstname":"zen","author_lastname":"wei","tags":"","int_age":38,"locale":"zhc,en"}
GET blog-test
GET blog-test/_search

确保数据成功写入

  • 现在需要对集群 cluster_one 进行备份与恢复操作,要求如下:
    • 将数据备份到 $HOME/backup 目录中
    • 对索引 blog_test 做全量备份
    • 备份成功后,将备份的数据恢复到 restore_blog_test 索引中

6. 跨集群搜索

  • 请在 cluster_one 中执行如下语句

    PUT test_ccs/_doc/1
    {
    "from":"cluster_one"
    }
    
  • 请在 cluster_two 中执行如下语句

    PUT test_ccs/_doc/1
    {
    "from":"cluster_two"
    }
    
  • 请在 cluster_three 中执行如下语句

    PUT test_ccs/_doc/1
    {
    "from":"cluster_three"
    }
    
  • 要求可以通过 cluster_one 同时查询 cluster_two 和 cluster_three 的 test_ccs 的索引数据