運維之道 | Elasticsearch 安裝部署

在這裏插入圖片描述
Filebeat+ELK是四個開源工具組成,簡單解釋如下:

Filebeat:ELK 協議棧的新成員,一個輕量級開源日誌文件數據蒐集器,基於 Logstash-Forwarder 源代碼開發,是對它的替代。在需要採集日誌數據的 server 上安裝 Filebeat,並指定日誌目錄或日誌文件後,Filebeat就能讀取數據,迅速發送到 Logstash 進行解析,亦或直接發送到 Elasticsearch 進行集中式存儲和分析。

Elasticsearch:是個開源分佈式搜索引擎,它的特點有:分佈式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。

Logstash:是一個完全開源的工具,它可以對你的日誌進行收集、過濾,並將其存儲供以後使用(如,搜索)。

Kibana:也是一個開源和免費的工具,它可以爲 Logstash 和 ElasticSearch 提供的日誌分析友好的Web 界面,可以幫助您彙總、分析和搜索重要數據日誌。

這裏之所以用Filebeat+ELK是因爲Filebeat相對於logstash而言,更輕量級,佔用系統資源少。


一、配置Java環境

安裝elasticsearch的時候需要有Java環境,jdk可以去oracle官方網站下載,也可以使用下面安裝包

jdk-8u241-linux-x64.tar.gz - 密碼:qgg8
1、對JAVA安裝包進行解壓,並移動至usr/ 目錄下
[root@localhost java]# tar -zxvf jdk-8u41-linux-x64.tar.gz
[root@localhost java]# mkdir -p /usr/java/
[root@localhost java]# mv jdk1.8.0_231 /usr/java/
2、修改環境配置
[root@localhost java]# vim /etc/profile

JAVA_HOME=/usr/java/jdk1.8.0_241
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAME_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
3、重載profile配置文件
[root@localhost ~]# source /etc/profile    ///重啓profile
[root@localhost ~]# javac				   ///測試java環境是否配置成功
用法: javac <options> <source files>
其中, 可能的選項包括:
  -g                         生成所有調試信息
  -g:none                    不生成任何調試信息
  -g:{lines,vars,source}     只生成某些調試信息
  -nowarn                    不生成任何警告
  -verbose                   輸出有關編譯器正在執行的操作的消息
  -deprecation               輸出使用已過時的 API 的源位置
  ......   配置成功!!!協議,轉載請附上原文出處鏈接及本聲明。

二、Elasticsearch 安裝

1、解壓Elasticsearch 安裝包
[root@localhost ~]# tar -xzvf elasticsearch-6.3.0.tar.gz
2、修改Elasticsearch配置文件
[root@localhost config]# egrep -v "#|^$" elasticsearch.yml 

bootstrap.memory_lock: yes
bootstrap.system_call_filter: yes
network.host: 192.168.182.11
http.port: 9200
3、添加elastic用戶,tar包啓動必須使用普通用戶運行
[root@localhost ~]# useradd elastic
[root@localhost ~]# chown -R elastic. /usr/local/src/elasticsearch-6.3.0
4、 修改sysctl.conf文件,添加如下內容:
[root@localhost ~]# vim /etc/sysctl.conf
 vm.max_map_count = 655360
 
[root@localhost ~]# sysctl -p /etc/sysctl.conf 		///重載配置文件
5、修改/etc/security/limits.conf文件,修改打開文件句柄數
[root@localhost ~]# vim /etc/security/limits.conf  --添加如下內容
 * soft nofile 65536
 * hard nofile 65536
 * soft nproc 65536
 * hard nproc 65536
6、切換到elastic普通用戶啓動Elasticsearch
[root@localhost ~]# su - elastic
[elk@localhost ~]$ cd /usr/local/src/elasticsearch-6.3.0/bin/
[elk@localhost bin]$ ./elasticsearch

在這裏插入圖片描述

7、查看進程

在這裏插入圖片描述

8、curl簡單的測試
[elk@localhost bin]$ curl 192.168.182.11:9200
{
  "name" : "B6awqaD",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "6KOkBvARTmqXV2S3eScceQ",
  "version" : {
    "number" : "6.3.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "424e937",
    "build_date" : "2018-06-11T23:38:03.357887Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}


三、Filebeat 安裝部署

1、解壓Filebeat 安裝包
[root@localhost ~]# tar -xzvf filebeat-6.3.0-linux-x86_64.tar.gz
2、編輯filebeat.yml文件,內容如下:
[root@localhost filebeat-6.3.0-linux-x86_64]# egrep -v "#|^$" filebeat.yml
filebeat.inputs:
- type: log
  enabled: false
  paths:
    - /var/log/messages
  fields:
     service_name: dxm-beta
     log_type: log
     service_id: 127.0.0.1
  scan_frequency: 60
  multiline.pattern: ^\{4}
  multiline.negate: true
  multiline.match: after
     
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
setup.template.settings:
  index.number_of_shards: 3
setup.kibana:
output.elasticsearch:
  hosts: ["192.168.182.11:9200"]
3、啓動filebeat服務
[root@localhost ~]# cd /usr/local/src/filebeat-6.3.0-linux-x86_64/
[root@localhost filebeat-6.3.0-linux-x86_64]# ./filebeat &

注意:filebeat沒有監聽端口,主要看日誌和進程,filebeat監聽的文件記錄信息在/usr/local/src/filebeat-6.3.0/data/registry裏面

[root@localhost filebeat-6.3.0-linux-x86_64]# ps -ef | grep filebeat
root      10803  10172  0 11:42 pts/2    00:00:00 ./filebeat
root      10810  10172  0 11:43 pts/2    00:00:00 grep --color=auto filebeat


四、Logstash 安裝

1、安裝logstash
[root@localhost ~]# tar -xzvf logstash-6.3.0.tar.gz
2、測試標準輸入輸出
[root@localhost ~]# /usr/local/src/logstash-6.3.0/bin/logstash -e 'input { stdin {} } output { stdout { codec => rubydebug} }'
hello world  ########### 輸入

在這裏插入圖片描述

3、測試輸出到文件
  • 新建一個logstash啓動指定logstash.conf的配置文件,內容如下:
[root@localhost config]# cat logstash.conf 
input {
        beats {
                port => "5044"
        }
}

output {
  stdout {
        codec => rubydebug
}
#輸出到elasticsearch,提供給kibana進行搜索
elasticsearch {
        hosts => [ "192.168.182.11:9200" ]
        index => "%{[fileds][service_name]}-%{+YYYY.MM.dd}"
                #在es中存儲的索引格式,按照"服務名-日期"進行索引  
        }
}
  • 指定配置文件logstash.conf進行測試:
[root@localhost logstash-6.3.0]# ./bin/logstash -f config/logstash.conf

在這裏插入圖片描述
這個啓動完一會後會輸出如下數據,這些數據就是filebeat從access.log裏採集過來的日誌,其實這些數據也輸入到了elasticsearch當中,可以用curl http://ip:9200/_search?pretty,進行驗證(ip爲elasticsearch的ip)

[root@localhost config]# curl http://192.168.182.11:9200/_search?pretty
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : 0.0,
    "hits" : [ ]
  }
}
  • 這時候我們也可以看到對應的端口9600和5044是開啓的

在這裏插入圖片描述



五、elasticsearch-head 插件安裝

通過head插件來查看集羣信息

[root@localhost src]# tar xvzf elasticsearch-head.tar.gz
[root@localhost src]# cd elasticsearch-head/
[root@localhost elasticsearch-head]# npm run start &
  • 修改elasticsearch服務配置文件,開啓跨域訪問支持,然後重啓elasticsearch服務
[root@localhost ~]# vim /etc/elasticsearch/elasticsearch.yml

http.cors.enabled: true     #最下方添加
http.cors.allow-origin: "*"

PS:否則elasticsearch無法連接

測試

9、瀏覽器訪問9100端口,將連接地址修改爲elasticsearch地址9200
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章