MK
摩柯社区 - 一个极简的技术知识社区
AI 面试

ElasticSearch查看集群及节点信息的方法

2024-03-285.3k 阅读

ElasticSearch查看集群及节点信息的方法

1. ElasticSearch基础概念

ElasticSearch是一个分布式的开源搜索和分析引擎,它建立在Apache Lucene之上,以其高可用性、可扩展性和强大的搜索功能而闻名。在ElasticSearch中,集群(Cluster)是一组节点(Node)的集合,这些节点共同保存和处理数据。每个节点都可以存储数据,并参与集群的索引和搜索功能。

2. 查看集群信息

2.1 使用REST API查看集群健康状态 ElasticSearch提供了丰富的REST API来与集群进行交互。查看集群健康状态是了解集群整体运行状况的重要方式。可以使用如下的_cluster/health API:

GET /_cluster/health

响应结果示例:

{
    "cluster_name": "my_cluster",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 3,
    "number_of_data_nodes": 3,
    "active_primary_shards": 10,
    "active_shards": 20,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 0,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 100.0
}
  • cluster_name:集群的名称。
  • status:集群的健康状态,有green(所有主分片和副本分片都已分配)、yellow(所有主分片都已分配,但至少有一个副本分片未分配)、red(至少有一个主分片未分配)三种状态。
  • number_of_nodes:集群中的节点总数。
  • number_of_data_nodes:集群中的数据节点数。
  • active_primary_shards:活动的主分片数量。
  • active_shards:活动的分片(包括主分片和副本分片)数量。

2.2 获取集群状态信息 _cluster/stats API可以获取更详细的集群状态信息,包括索引、节点统计等:

GET /_cluster/stats

响应结果示例(部分):

{
    "_nodes": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "cluster_name": "my_cluster",
    "timestamp": 1658947393536,
    "status": "green",
    "indices": {
        "count": 5,
        "shards": {
            "total": 20,
            "primaries": 10,
            "replication": 1.0,
            "index": {
                "my_index_1": {
                    "shards": {
                        "total": 5,
                        "primaries": 5,
                        "replication": 1.0
                    }
                }
            }
        },
        "docs": {
            "count": 1000,
            "deleted": 50
        },
        "store": {
            "size_in_bytes": 10485760,
            "throttle_time_in_millis": 0
        },
        "fielddata": {
            "memory_size_in_bytes": 1024000,
            "evictions": 0
        },
        "query_cache": {
            "memory_size_in_bytes": 512000,
            "total_count": 100,
            "hit_count": 80,
            "miss_count": 20,
            "cache_size": 50,
            "cache_count": 50,
            "evictions": 0
        }
    },
    "nodes": {
        "count": {
            "total": 3,
            "data": 3,
            "master": 1,
            "ingest": 3
        },
        "versions": [
            "7.10.1"
        ],
        "jvm": {
            "max_uptime_in_millis": 123456789,
            "mem": {
                "heap_used_in_bytes": 536870912,
                "heap_max_in_bytes": 1073741824,
                "non_heap_used_in_bytes": 268435456,
                "non_heap_max_in_bytes": 536870912
            },
            "threads": 100,
            "gc": {
                "collectors": {
                    "young": {
                        "collection_count": 100,
                        "collection_time_in_millis": 500
                    },
                    "old": {
                        "collection_count": 10,
                        "collection_time_in_millis": 200
                    }
                }
            }
        },
        "process": {
            "cpu": {
                "percent": 50,
                "total_in_millis": 500000
            },
            "open_file_descriptors": 1000,
            "max_file_descriptors": 10240
        },
        "transport": {
            "server_open": 10,
            "rx_count": 10000,
            "rx_size_in_bytes": 104857600,
            "tx_count": 10000,
            "tx_size_in_bytes": 104857600
        },
        "http": {
            "current_open": 5,
            "total_opened": 100
        }
    }
}
  • indices:索引相关的统计信息,如索引数量、分片数量、文档数量、存储大小等。
  • nodes:节点相关的统计信息,包括节点数量、JVM内存使用情况、线程数、GC情况、进程CPU使用情况、文件描述符数量、网络传输统计等。

2.3 查看集群元数据 _cluster/state API用于获取集群的元数据,包括索引设置、映射、分片分配等详细信息:

GET /_cluster/state

响应结果示例(部分):

{
    "cluster_name": "my_cluster",
    "version": 10,
    "state_uuid": "abcdef1234567890",
    "master_node": "node_1",
    "blocks": {
        "global": {
            "read_only_allow_delete": false
        }
    },
    "nodes": {
        "node_1": {
            "name": "node_1",
            "transport_address": "192.168.1.10:9300",
            "attributes": {
                "rack": "rack1"
            }
        },
        "node_2": {
            "name": "node_2",
            "transport_address": "192.168.1.11:9300",
            "attributes": {
                "rack": "rack2"
            }
        }
    },
    "metadata": {
        "indices": {
            "my_index_1": {
                "settings": {
                    "index": {
                        "number_of_shards": "5",
                        "number_of_replicas": "1"
                    }
                },
                "mappings": {
                    "properties": {
                        "title": {
                            "type": "text"
                        },
                        "content": {
                            "type": "text"
                        }
                    }
                }
            }
        }
    },
    "routing_table": {
        "indices": {
            "my_index_1": {
                "shards": {
                    "0": [
                        {
                            "state": "STARTED",
                            "primary": true,
                            "node": "node_1",
                            "relocating_node": null,
                            "shard": 0,
                            "index": "my_index_1"
                        },
                        {
                            "state": "STARTED",
                            "primary": false,
                            "node": "node_2",
                            "relocating_node": null,
                            "shard": 0,
                            "index": "my_index_1"
                        }
                    ]
                }
            }
        }
    }
}
  • cluster_name:集群名称。
  • version:集群状态版本号。
  • master_node:当前主节点的名称。
  • nodes:集群中所有节点的信息。
  • metadata:索引的元数据,包括索引设置和映射。
  • routing_table:分片的路由信息,显示每个分片所在的节点。

3. 查看节点信息

3.1 使用REST API查看节点信息 _nodes API可以获取集群中节点的详细信息,包括节点的名称、地址、版本、角色等:

GET /_nodes

响应结果示例(部分):

{
    "_nodes": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "cluster_name": "my_cluster",
    "nodes": {
        "node_1": {
            "name": "node_1",
            "transport_address": "192.168.1.10:9300",
            "host": "192.168.1.10",
            "ip": "192.168.1.10",
            "version": "7.10.1",
            "build_flavor": "default",
            "build_type": "zip",
            "build_hash": "abcdef1234567890",
            "roles": [
                "master",
                "data",
                "ingest"
            ],
            "attributes": {
                "rack": "rack1"
            }
        },
        "node_2": {
            "name": "node_2",
            "transport_address": "192.168.1.11:9300",
            "host": "192.168.1.11",
            "ip": "192.168.1.11",
            "version": "7.10.1",
            "build_flavor": "default",
            "build_type": "zip",
            "build_hash": "abcdef1234567890",
            "roles": [
                "data",
                "ingest"
            ],
            "attributes": {
                "rack": "rack2"
            }
        }
    }
}
  • name:节点的名称。
  • transport_address:节点的传输地址,用于节点间通信。
  • version:节点的ElasticSearch版本。
  • roles:节点的角色,如masterdataingest等。
  • attributes:节点的自定义属性。

3.2 获取节点统计信息 _nodes/stats API可以获取节点的详细统计信息,包括索引、JVM、进程、网络等方面:

GET /_nodes/stats

响应结果示例(部分):

{
    "_nodes": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "cluster_name": "my_cluster",
    "nodes": {
        "node_1": {
            "timestamp": 1658947393536,
            "name": "node_1",
            "transport_address": "192.168.1.10:9300",
            "host": "192.168.1.10",
            "ip": "192.168.1.10",
            "indices": {
                "docs": {
                    "count": 500,
                    "deleted": 25
                },
                "store": {
                    "size_in_bytes": 5242880,
                    "throttle_time_in_millis": 0
                },
                "indexing": {
                    "index_total": 1000,
                    "index_time_in_millis": 5000,
                    "index_current": 0,
                    "delete_total": 50,
                    "delete_time_in_millis": 2500,
                    "delete_current": 0,
                    "noop_update_total": 0,
                    "is_throttled": false,
                    "throttle_time_in_millis": 0
                },
                "get": {
                    "total": 500,
                    "time_in_millis": 2500,
                    "exists_total": 400,
                    "exists_time_in_millis": 2000,
                    "missing_total": 100,
                    "missing_time_in_millis": 500,
                    "current": 0
                },
                "search": {
                    "open_contexts": 0,
                    "query_total": 200,
                    "query_time_in_millis": 10000,
                    "query_current": 0,
                    "fetch_total": 100,
                    "fetch_time_in_millis": 5000,
                    "fetch_current": 0,
                    "scroll_total": 0,
                    "scroll_time_in_millis": 0,
                    "scroll_current": 0,
                    "suggest_total": 0,
                    "suggest_time_in_millis": 0,
                    "suggest_current": 0
                }
            },
            "jvm": {
                "max_uptime_in_millis": 123456789,
                "mem": {
                    "heap_used_in_bytes": 268435456,
                    "heap_max_in_bytes": 536870912,
                    "non_heap_used_in_bytes": 134217728,
                    "non_heap_max_in_bytes": 268435456
                },
                "threads": 50,
                "gc": {
                    "collectors": {
                        "young": {
                            "collection_count": 50,
                            "collection_time_in_millis": 250
                        },
                        "old": {
                            "collection_count": 5,
                            "collection_time_in_millis": 100
                        }
                    }
                }
            },
            "process": {
                "cpu": {
                    "percent": 25,
                    "total_in_millis": 250000
                },
                "open_file_descriptors": 500,
                "max_file_descriptors": 10240
            },
            "transport": {
                "server_open": 5,
                "rx_count": 5000,
                "rx_size_in_bytes": 52428800,
                "tx_count": 5000,
                "tx_size_in_bytes": 52428800
            },
            "http": {
                "current_open": 2,
                "total_opened": 50
            }
        }
    }
}
  • indices:索引相关的统计信息,如文档数量、存储大小、索引操作次数和时间等。
  • jvm:JVM相关的统计信息,包括内存使用、线程数、GC情况等。
  • process:进程相关的统计信息,如CPU使用率、文件描述符数量等。
  • transport:网络传输相关的统计信息。
  • http:HTTP服务器相关的统计信息。

3.3 查看节点的插件信息 _nodes/plugins API用于查看节点上安装的插件信息:

GET /_nodes/plugins

响应结果示例(部分):

{
    "_nodes": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "cluster_name": "my_cluster",
    "nodes": {
        "node_1": {
            "name": "node_1",
            "transport_address": "192.168.1.10:9300",
            "plugins": [
                {
                    "name": "analysis-icu",
                    "description": "ICU Analysis plugin",
                    "version": "7.10.1",
                    "jvm": true
                },
                {
                    "name": "ingest-attachment",
                    "description": "Ingest Attachment plugin",
                    "version": "7.10.1",
                    "jvm": true
                }
            ]
        }
    }
}

每个插件信息包括插件名称、描述、版本以及是否为JVM插件。

4. 使用Elasticsearch-Py查看集群及节点信息(Python示例)

如果使用Python与ElasticSearch进行交互,可以使用elasticsearch库。首先,需要安装该库:

pip install elasticsearch

4.1 查看集群健康状态

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://localhost:9200'])
health = es.cluster.health()
print(health)

4.2 获取集群状态信息

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://localhost:9200'])
cluster_stats = es.cluster.stats()
print(cluster_stats)

4.3 查看集群元数据

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://localhost:9200'])
cluster_state = es.cluster.state()
print(cluster_state)

4.4 查看节点信息

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://localhost:9200'])
nodes_info = es.nodes.info()
print(nodes_info)

4.5 获取节点统计信息

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://localhost:9200'])
nodes_stats = es.nodes.stats()
print(nodes_stats)

4.6 查看节点的插件信息

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://localhost:9200'])
nodes_plugins = es.nodes.plugins()
print(nodes_plugins)

5. 使用Kibana查看集群及节点信息

Kibana是ElasticSearch官方提供的可视化工具,它可以方便地查看集群和节点信息。

5.1 查看集群健康状态 在Kibana的左侧导航栏中,选择“Management” -> “Elasticsearch” -> “Overview”。在“Overview”页面中,可以直观地看到集群的健康状态、节点数量、索引数量等信息。

5.2 查看集群状态和元数据 在“Management” -> “Elasticsearch” -> “Cluster”页面中,可以查看集群的详细状态信息,包括分片分配、索引设置等元数据。

5.3 查看节点信息 在“Management” -> “Elasticsearch” -> “Nodes”页面中,可以查看每个节点的详细信息,如节点名称、地址、角色、CPU和内存使用情况等。

通过以上方法,无论是使用REST API、编程语言客户端还是可视化工具Kibana,都能够全面深入地了解ElasticSearch集群及节点的运行状态和相关信息,这对于集群的监控、维护和优化至关重要。