什么是ELK?
ELK是一套開源的日志分析系統, 由 elasticsearch + logstash + kibana 組成.
- elasticsearch: 分布式搜索引擎.
- logstash: 日志收集過濾.
- kibana: 圖形化展示.
官網介紹: https://www.elastic.co/cn/downloads
elasticsearch簡介
Elasticsearch(簡稱:ES)是一個開源的分布式搜索引擎, Elasticsearch 還是一個分布式文檔數據庫.並提供了大量數據的存儲功能快速的搜索與分析功能.
起源於 Lucene, 基於 Java 語言開發的搜索引擎類庫, 創建於 1999 年, 2005 年成為 Apache 頂級開源項目.Lucene 具有高性能, 以擴展的優點,
主要功能
- 分布式搜索引擎
- 大數據近實時分析引擎
- 高性能
- 容易使用/容易擴展
- 聚合功能
- 分布式存儲及集群管理
Elasticsearch個版本特性
5x版本(2016-10)
- Lucene 6x,性能提升,默認打分機制從 TF-IDF 改為 BM 25.
- 支持 Ingest 節點 / Painless Scripting / Completion suggested 支持/ 原生的 Java REST 客戶端
- Type 標記成 deprecated, 支持 Keyword 的類型
- 性能優化
- 內部引擎移除了避免同一文檔並發更新的競爭鎖, 帶來 15% - 20% 的性能提升.
- Istan aggregation, 支持分片上聚合的緩存.
- 新增了 Profile API.
6X版本(2017-10)
- Lucene 7X
- 新功能
- 跨集群復制(CCR)
- 索引生命周期管理
- SQL 的支持
- 更友好的升級及數據遷移
- 在主要版本之間的遷移更為簡單,體驗升級.
- 全新的基於操作的數據復制框架,可加速恢復數據.
- 性能優化
- 有效存儲稀疏字段的新方法,降低了存儲成本.
- 在索引時進行排序,可加快排序的查詢性能.
7X版本(2019-4)
- Lucene 8.0
- 重大改進 - 正式廢除單個索引下多 Type 的支持.
- 7.1開始, Security 功能免費試用.
- ECK - Elasticsearch Operator on Kubernetes
- 新功能
- 新的集群協調機制.
- 功能完整的 REST 客戶端.
- Script Score Query.
- 性能優化
- 默認的 Primary Shard 數從 5 改為 1, 避免 Ober Sharding.
- 性能優化,更快的 TOP k.
部署elasticsearch
下載地址:https://www.elastic.co/cn/downloads/elasticsearch
elasticsearch7X版本自帶JDK環境,之前版本需要安裝JDK.
通用環境配置
1. 關閉防火牆和selinux [root@node6 ~]# setenforce 0 [root@node6 ~]# cat /etc/selinux/config SELINUX=disabled [root@node6 ~]# systemctl stop firewalld [root@node6 ~]# systemctl disable firewalld 2. 修改系統打開最大文件句柄 [root@node6 ~]# cat /etc/security/limits.conf # End of file * soft nofile 655350 * hard nofile 655350 * soft nproc 20000 * hard nproc 20000 3. 修改內核參數 [root@node6 ~]# cat /etc/sysctl.conf # For more information, see sysctl.conf(5) and sysctl.d(5). fs.file-max=419430 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 1 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.ip_local_port_range = 10000 65535 net.ipv4.tcp_max_syn_backlog = 16384 net.ipv4.tcp_max_tw_buckets = 36000 net.ipv4.route.gc_timeout = 100 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_synack_retries = 1 net.core.somaxconn = 16384 net.core.netdev_max_backlog = 16384 net.ipv4.tcp_max_orphans = 16384 net.ipv4.tcp_rmem = 4096 4096 16777216 net.ipv4.tcp_wmem = 4096 4096 16777216 net.ipv4.tcp_mem = 786432 2097152 3145728 #kernel.pty.max = 4 vm.max_map_count=262144 [root@node6 ~]# sysctl -p 4. 創建用戶 [root@node6 ~]# useradd elasticsearch -M -s /sbin/nologin 5. 創建數據及日志目錄 [root@node3 ~]# mkdir /data/elasticsearch/{data,logs} -p [root@node3 ~]# chown elasticsearch:elasticsearch -R /data/elasticsearch/
下載並安裝elasticsearch
[root@node6 ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.7.0-linux-x86_64.tar.gz [root@node6 ~]# rpm -ivh elasticsearch-7.7.0-x86_64.rpm
目錄說明
目錄 | 主要配置文件 | 描述 |
/usr/share/elasticsearch/bin/ | elasticsearch | 腳本文件存放目錄,包括 elasticesearch,安裝插件.運行統計數據等. |
/etc/elasticsearch/ |
elasticsearch.yml | 集群配置文件,jvm配置文件 |
/usr/share/elasticsearch/jdk/ | java | java運行環境 |
/data/elasticsearch/ | 日志和數據目錄 | 數據文件及日志目錄 |
/usr/share/elasticsearch/lib/ | java類庫 | |
/usr/share/elasticsearch/modules/ |
包含所有ES模塊 | |
/usr/share/elasticsearch/plugins/ | 包含所有已安裝的插件 | |
主配置文件
[root@node6 ~]# cat /etc/elasticsearch/elasticsearch.yml # ---------------------------------- Cluster ----------------------------------- # 集群名稱 cluster.name: es-cluster # ------------------------------------ Node ------------------------------------ # 節點 name node.name: node-6 # 節點是否參加 master 選舉 node.master: true # 是否為數據節點 node.data: true # ----------------------------------- Paths ------------------------------------ # 數據目錄 path.data: /data/elasticsearch/data # 日志文件存儲路徑 path.logs: /data/elasticsearch/logs # ----------------------------------- Memory ----------------------------------- # 是否啟動時鎖定內存 bootstrap.memory_lock: true # ---------------------------------- Network ----------------------------------- # 監聽地址 network.host: 0.0.0.0 # 監聽端口 http.port: 9200 # --------------------------------- Discovery ---------------------------------- # 自動發現節點 discovery.seed_hosts: ["172.16.0.206", "172.16.0.204","172.16.0.203"] # Bootstrap the cluster using an initial set of master-eligible nodes: # 初始化引導集群節點 cluster.initial_master_nodes: ["172.16.0.206", "172.16.0.204","172.16.0.203"]
JVM配置
配置建議:
官網配置建議: https://www.elastic.co/cn/blog/a-heap-of-trouble
- Xms 和 Xmx 設置成一樣
- Xmx 不要超過機器內存的 50 %.
- 不要超過 30G.
vim /etc/elasticsearch/jvm.options -Xms2g -Xmx2g
啟動並檢查集群狀態
# 啟動服務並設為開機自啟 [root@node6 ~]# systemctl start elasticsearch [root@node6 ~]# systemctl enable elasticsearch # 查看集群狀態 [root@node6 ~]# curl http://172.16.0.206:9200/_cluster/health?pretty { "cluster_name" : "es-cluster", "status" : "green", "timed_out" : false, "number_of_nodes" : 3, "number_of_data_nodes" : 3, "active_primary_shards" : 0, "active_shards" : 0, "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 } # 查看各nodes 數據和主節點 [root@node6 ~]# curl http://172.16.0.206:9200/_cat/nodes?v ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name 172.16.0.203 59 96 1 0.12 0.06 0.05 dilmrt - node-3 172.16.0.204 26 95 1 0.05 0.07 0.06 dilmrt * node-4 172.16.0.206 36 78 0 0.01 0.04 0.05 dilmrt - node-6
elasticsearch狀態說明
- green:表示每個index的shard和replica都是活躍狀態的。
- yellow:表示每個index的shard是活躍狀態的,replica是不可用狀態的。
- red:表示索引中有些shard是不可用狀態,導致數據丟失。
elasticsearch-head
elasticsearch-head 是集群管理, 數據可視化, 增刪改查, 查詢語句可視化工具. 從 ES5 版本后安裝方式 和 ES2 以上版本有所不同. ES2 可使用安裝插件方式進行安裝, 但是從 ES5 之后需要使用 NodeJs來啟動.
官網地址: https://github.com/mobz/elasticsearch-head
安裝部署
git clone git://github.com/mobz/elasticsearch-head.git cd elasticsearch-head yum install npm npm config set registry https://mirrors.huaweicloud.com/repository/npm/ npm install
修改配置文件
修改默認監聽地址 配置文件路徑: path/elasticsearch-head/Gruntfile.js 在server下options選項中添加: hostname: '0.0.0.0', connect: { server: { options: { hostname: '0.0.0.0', # 新添加內容 port: 9100, base: '.', keepalive: true } } } 修改默認連接地址 配置文件: path/elasticsearch-head/_site/app.js # 修改http://localhost:9200 為 http://es-ip:9200 this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://172.16.0.206:9200";
cerebro
cerebro 和 elasticsearch-head 類似.是一款基於Web的ElasticSearch管理監控工具 圖形化比elasticsearch-head更加友好.
項目地址: https://github.com/lmenezes/cerebro
安裝軟件
wget https://github.com/lmenezes/cerebro/releases/download/v0.9.1/cerebro-0.9.1.tgz tar xf cerebro-0.9.1.tgz cd cerebro-0.9.1/
修改配置文件
vim /usr/local/cerebro-0.9.1/conf/application.conf hosts = [ { host = "http://172.16.0.206:9200" name = "es-cluster" } ]
啟動程序
# 默認監聽 0.0.0.0 bin/cerebro # 也可指定監聽ip和端口號 bin/cerebro -Dhttp.port=1234 -Dhttp.address=127.0.0.1