一、概述

ELK 是一个由三个开源软件东西组成的数据处理和可视化渠道,包括 ElasticsearchLogstashKibana。这些东西都是由 Elastic 公司创立和保护的。

  • Elasticsearch 是一个分布式的搜索和剖析引擎,能够将很多数据存储在一个或多个节点上,支撑实时搜索、剖析和聚合,供给高性能的全文搜索、杂乱查询和剖析能力。

  • Logstash 是一个数据采集和处理东西,能够将来自各种数据源的日志数据搜集、转换、过滤和存储到 Elasticsearch 中,从而完成对数据的会集管理和剖析。

  • Kibana 是一个数据可视化和剖析渠道,能够运用其可视化界面来创立仪表盘、图表、地图和警报,对 Elasticsearch 中的数据进行交互式剖析和可视化展现。

ELK 渠道结合了这三个东西的功能,供给了一个完好的解决方案,能够满意各种数据处理和剖析的需求。ELK 渠道被广泛应用于日志剖析、安全监控、业务剖析等范畴。

Elasticsearch(ELK)集群环境部署

二、预备

1)机器环境预备

  我们预备3台机器,并都装好JDK且设置好hostname。

机器IP hostname
192.168.182.132 node-1
192.168.182.133 node-2
192.168.182.134 node-3

2)创立elsearch用户

(Linux 下不要运用 root 用户运转 Elasticsearch, 否则会报反常 can not run elasticsearch as root

# 创立用户组
groupadd elsearch
# 创立用户,-p : 登录暗码
useradd elsearch -g elsearch -p elsearch

三、elasticsearch 布置

1)下载并装置

官网下载:www.elastic.co/cn/download…

# 下载
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-linux-x86_64.tar.gz
# 解压
tar zxvf elasticsearch-7.4.2-linux-x86_64.tar.gz && cd elasticsearch-7.4.2/
# 切换用户组:一切者,跟上面创立的用户相对应
chown -R elsearch:elsearch .

2)修正装备文件以支撑集群

1.留意修正network.host为各节点IP地址
2.留意修正discovery.zen.ping.unicast.hosts列表

备份:

cp -a config/elasticsearch.yml config/elasticsearch.yml.bak

装备:

cat <<EOF >>/etc/elasticsearch/elasticsearch.yml
## 集群称号
cluster.name: my-cs-cluster 
## 节点称号(每个节点称号不能相同)
node.name: 'node-1'
## 允许 JVM 锁住内存,禁止操作体系交流出去
# bootstrap.memory_lock: true
## 是否有资历成为主节点
## 经过 node.master 能够装备该节点是否有资历成为主节点,如果装备为 true,则主机有资历成为主节点
## 留意这里是有资历成为主节点,不是必定会成为主节点
node.master: true
## 是否是数据节点
## 当 node.master 和 node.data 均为 false,则该主机会作为负载均衡节点
node.data: true
## 设置拜访的地址和端口
network.host: 192.168.182.132
http.port: 9200
## 集群地址设置
## 装备之后集群的主机之间能够主动发现
discovery.zen.ping.unicast.hosts: ["192.168.182.132", "192.168.182.133", "192.168.182.134"]
# 参数设置一系列符合主节点条件的节点的主机名或 IP 地址来引导发动集群。手动指定能够成为 mater 的一切节点的 name 或者 ip,这些装备将会在第一次推举中进行核算
# cluster.initial_master_nodes: ["node-1"]
# 装备集群的主机地址,装备之后集群的主机之间能够主动发现(可选项)
# discovery.seed_hosts: ["192.168.182.132:9300","192.168.182.133:9300","192.168.182.134:9300"]
# 装备初始化集群的master节点,node.name的值
# cluster.initial_master_nodes: ["node-1"]
## 装备大多数节点(一般为主节点的节点总数/ 2 + 1)来防止“裂脑”:
discovery.zen.minimum_master_nodes: 2
## 在彻底集群重启后阻止初始康复,直到发动N个节点
gateway.recover_after_nodes: 2
# 外网拜访设置
http.cors.enabled: true# 留意:*最好加上引号,要不然6.x版本肯能发动不了
http.cors.allow-origin: '*'
EOF
vim /etc/elasticsearch/elasticsearch.yml

四、发动出现的问题及解决方案

1)JDK版本过低

报错信息:

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
[2019-11-04T00:12:07,213][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [node-1] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.4.2.jar:7.4.2]
    at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.4.2.jar:7.4.2]
    at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.4.2.jar:7.4.2]
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.4.2.jar:7.4.2]
    at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.4.2.jar:7.4.2]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.4.2.jar:7.4.2]
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.4.2.jar:7.4.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
    at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.4.2.jar:7.4.2]
    at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.4.2.jar:7.4.2]
    at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.4.2.jar:7.4.2]
    at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.4.2.jar:7.4.2]
    ... 6 more

解决:下载装置OpenJDK11

wget https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_linux-x64_bin.tar.gz
tar -xzvf jdk-11.0.4_linux-x64_bin.tar.gz /opt/

修正ES发动脚本加上jdk11的装备:

为了方便大家参考,这里贴上完好的装备文件

#!/bin/bash
# CONTROLLING STARTUP:
#
# This script relies on a few environment variables to determine startup
# behavior, those variables are:
#
#   ES_PATH_CONF -- Path to config directory
#   ES_JAVA_OPTS -- External Java Opts on top of the defaults set
#
# Optionally, exact memory values can be set using the `ES_JAVA_OPTS`. Note that
# the Xms and Xmx lines in the JVM options file must be commented out. Example
# values are "512m", and "10g".
#
#   ES_JAVA_OPTS="-Xms8g -Xmx8g" ./bin/elasticsearch
# 装备自己的jdk11
export JAVA_HOME=/home/elsearch/jdk-11.0.1
export PATH=$JAVA_HOME/bin:$PATH
source "`dirname "$0"`"/elasticsearch-env
if [ -z "$ES_TMPDIR" ]; then
  ES_TMPDIR=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.TempDirectory`
fi
ES_JVM_OPTIONS="$ES_PATH_CONF"/jvm.options
JVM_OPTIONS=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.JvmOptionsParser "$ES_JVM_OPTIONS"`
ES_JAVA_OPTS="${JVM_OPTIONS//\$\{ES_TMPDIR\}/$ES_TMPDIR}"
# 增加jdk判别
if [ -x "$JAVA_HOME/bin/java" ]; then
        JAVA="/home/elsearch/jdk-11.0.1/bin/java"
else
        JAVA=`which java`
fi
# manual parsing to find out, if process should be detached
if ! echo $* | grep -E '(^-d |-d$| -d |--daemonize$|--daemonize )' > /dev/null; then
  exec \
    "$JAVA" \
    $ES_JAVA_OPTS \
    -Des.path.home="$ES_HOME" \
    -Des.path.conf="$ES_PATH_CONF" \
    -Des.distribution.flavor="$ES_DISTRIBUTION_FLAVOR" \
    -Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
    -Des.bundled_jdk="$ES_BUNDLED_JDK" \
    -cp "$ES_CLASSPATH" \
    org.elasticsearch.bootstrap.Elasticsearch \
    "$@"
else
  exec \
    "$JAVA" \
    $ES_JAVA_OPTS \
    -Des.path.home="$ES_HOME" \
    -Des.path.conf="$ES_PATH_CONF" \
    -Des.distribution.flavor="$ES_DISTRIBUTION_FLAVOR" \
    -Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
    -Des.bundled_jdk="$ES_BUNDLED_JDK" \
    -cp "$ES_CLASSPATH" \
    org.elasticsearch.bootstrap.Elasticsearch \
    "$@" \
    <&- &
  retval=$?
  pid=$!
  [ $retval -eq 0 ] || exit $retval
  if [ ! -z "$ES_STARTUP_SLEEP_TIME" ]; then
    sleep $ES_STARTUP_SLEEP_TIME
  fi
  if ! ps -p $pid > /dev/null ; then
    exit 1
  fi
  exit 0
fi
exit $?

2)装备过低

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解决:

  • 文件描述符装备

    • 暂时收效

      • 运用 root 用户
      • 运转 ulimit -n 65536
    • 永久收效

      • 运用 root 用户

      • 进入 /etc/security/limits.conf

      • 增加

        * hard nofile 65536

        * hard nofile 65536

  • 虚拟内存装备

    • 暂时收效
      • 运用 root 用户
      • 运转 sysctl -w vm.max_map_count=262144
    • 永久收效
      • 运用 root 用户
      • 进入 /etc/sysctl.conf
      • 增加或更新一行 vm.max_map_count=262144
  • 主动发现装备

    • 单节点
    • 进入 elasticsearch 装置目录,翻开 config/elasticsearch.yml
    • 增加或更新一行 discovery.type: single-node

3)JVM内存大小指定太大,但本机内存不够用

[root@file elasticsearch-7.1.1]# ./bin/elasticsearch
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# logs/hs_err_pid27766.log

解决:

# 修正jvm.options文件装备即可,从1g改成了100m
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms100m
-Xmx100m

五、装置 elasticsearch-head

1)下载装置

# 下载wget https://github.com/mobz/elasticsearch-head.git# 解压
unzip elasticsearch-head-master.zip && cd elasticsearch-head-master
# 装置
npm install grunt --save

2)修正服务器监听地址

修正elasticsearch-head下Gruntfile.js文件,默许监听在127.0.0.1下9200端口

Elasticsearch(ELK)集群环境部署

或者修正 elasticsearch-head-master/Gruntfile.js,在connect特点中,增加hostname: ‘0.0.0.0’

1、修正衔接地址

cd _site
vim app.js
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";

2、发动

#执行
grunt server

输出信息:

>> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

3、浏览器拜访 http://192.168.182.132:9100

Elasticsearch(ELK)集群环境部署

4、elasticsearch-head 设置后台发动和关闭

进入elasticsearch-head-master目录

# vim elasticsearch-head_start.sh
#!/bin/bash
echo "START elasticsearch-head "
nohup grunt server &exit

关闭(可忽略):

#!/bin/bash
echo "STOP elasticsearch-head "
ps -ef |grep head|awk '{print $2}'|xargs kill -9

在别的两台机子上装置ES

scp -r /home/elsearch/elasticsearch-7.4.2 root@192.168.182.133:/home/elsearch/
scp -r /home/elsearch/elasticsearch-7.4.2 root@192.168.182.134:/home/elsearch/

修正装备

# 修正节点称号
node.name: "node-2" # node-3
# 修正节点特点
node.master: false
# 修正能拜访的ip地址
network.host: "192.168.182.133" # network.host: "192.168.182.134"

经过以上装置装备,ES集群就建立起来了

Elasticsearch(ELK)集群环境部署

最终附上装备完好说明:

cluster.name: elasticsearch
# 装备的集群称号,默许是elasticsearch,es服务会经过播送方法主动衔接在同一网段下的es服务,经过多播方法进行通讯,同一网段下能够有多个集群,经过集群称号这个特点来区别不同的集群。
node.name: "Franz Kafka"
# 当前装备地点机器的节点名,你不设置就默许随机指定一个name列表中名字,该name列表在es的jar包中config文件夹里name.txt文件中,其中有很多作者增加的有趣名字。
node.master: true
# 指定该节点是否有资历被推举成为node(留意这里只是设置成有资历, 不代表该node必定便是master),默许是true,es是默许集群中的第一台机器为master,如果这台机挂了就会从头推举master。
node.data: true
# 指定该节点是否存储索引数据,默许为true。
index.number_of_shards: 5
# 设置默许索引分片个数,默许为5片。
index.number_of_replicas: 1
# 设置默许索引副本个数,默许为1个副本。如果采用默许设置,而你集群只装备了一台机器,那么集群的健康度为yellow,也便是一切的数据都是可用的,可是某些复制没有被分配
# (健康度可用 curl 'localhost:9200/_cat/health?v' 查看, 分为绿色、黄色或赤色。绿色代表一切正常,集群功能完全,黄色意味着一切的数据都是可用的,可是某些复制没有被分配,赤色则代表由于某些原因,某些数据不可用)。
path.conf: /path/to/conf
# 设置装备文件的存储途径,默许是es根目录下的config文件夹。
path.data: /path/to/data
# 设置索引数据的存储途径,默许是es根目录下的data文件夹,能够设置多个存储途径,用逗号离隔,例:
# path.data: /path/to/data1,/path/to/data2
path.work: /path/to/work
# 设置暂时文件的存储途径,默许是es根目录下的work文件夹。
path.logs: /path/to/logs
# 设置日志文件的存储途径,默许是es根目录下的logs文件夹 
path.plugins: /path/to/plugins
# 设置插件的寄存途径,默许是es根目录下的plugins文件夹, 插件在es里边遍及运用,用来增强原体系核心功能。
bootstrap.mlockall: true
# 设置为true来锁住内存不进行swapping。由于当jvm开始swapping时es的功率 会降低,所以要确保它不swap,能够把ES_MIN_MEM和ES_MAX_MEM两个环境变量设置成同一个值,并且确保机器有满足的内存分配给es。 一起也要允许elasticsearch的进程能够锁住内# # 存,linux下发动es之前能够经过`ulimit -l unlimited`指令设置。
network.bind_host: 192.168.0.1
# 设置绑定的ip地址,能够是ipv4或ipv6的,默许为0.0.0.0,绑定这台机器的任何一个ip。
network.publish_host: 192.168.0.1
# 设置其它节点和该节点交互的ip地址,如果不设置它会主动判别,值有必要是个实在的ip地址。
network.host: 192.168.0.1
# 这个参数是用来一起设置bind_host和publish_host上面两个参数。
transport.tcp.port: 9300
# 设置节点之间交互的tcp端口,默许是9300。
transport.tcp.compress: true
# 设置是否紧缩tcp传输时的数据,默许为false,不紧缩。
http.port: 9200
# 设置对外服务的http端口,默许为9200。
http.max_content_length: 100mb
# 设置内容的最大容量,默许100mb
http.enabled: false
# 是否运用http协议对外供给服务,默许为true,敞开。
gateway.type: local
# gateway的类型,默许为local即为本地文件体系,能够设置为本地文件体系,分布式文件体系,hadoop的HDFS,和amazon的s3服务器等。
gateway.recover_after_nodes: 1
# 设置集群中N个节点发动时进行数据康复,默许为1。
gateway.recover_after_time: 5m
# 设置初始化数据康复进程的超时时刻,默许是5分钟。
gateway.expected_nodes: 2
# 设置这个集群中节点的数量,默许为2,一旦这N个节点发动,就会当即进行数据康复。
cluster.routing.allocation.node_initial_primaries_recoveries: 4
# 初始化数据康复时,并发康复线程的个数,默许为4。
cluster.routing.allocation.node_concurrent_recoveries: 2
# 增加删除节点或负载均衡时并发康复线程的个数,默许为4。
indices.recovery.max_size_per_sec: 0
# 设置数据康复时约束的带宽,如入100mb,默许为0,即无约束。
indices.recovery.concurrent_streams: 5
# 设置这个参数来约束从其它分片康复数据时最大一起翻开并发流的个数,默许为5。
discovery.zen.minimum_master_nodes: 1
# 设置这个参数来确保集群中的节点能够知道其它N个有master资历的节点。默许为1,关于大的集群来说,能够设置大一点的值(2-4)
discovery.zen.ping.timeout: 3s
# 设置集群中主动发现其它节点时ping衔接超时时刻,默许为3秒,关于比较差的网络环境能够高点的值来防止主动发现时犯错。
discovery.zen.ping.multicast.enabled: false
# 设置是否翻开多播发现节点,默许是true。
discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]
# 设置集群中master节点的初始列表,能够经过这些节点来主动发现新加入集群的节点。

elasticsearch7版本引入的新集群和谐子体系了解一哈,新增两个如下装备项

discovery.seed_hosts
cluster.initial_master_nodes

官方文档栗子:

discovery.seed_hosts:
   - 192.168.1.10:9300
   - 192.168.1.11 
   - seeds.mydomain.com 
cluster.initial_master_nodes: 
   - master-node-a
   - master-node-b
   - master-node-c

六、装置 Kibana

1)Kibana 下载

官网下载:www.elastic.co/cn/download…

2)Kibana 装置

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.4.2-linux-x86_64.tar.gz
tar -zxvf kibana-7.4.2-linux-x86_64.tar.gz && cd  kibana-7.4.2-linux-x86_64

3)Kibana 修正装备

# vi config/kibana.yml
# 允许外部拜访
server.host: "0.0.0.0"# 修正默许相关的ES地址elasticsearch.hosts: ["http://192.168.182.132:9200"]

4)Kibana 发动

# 默许不支撑root发动,能够后边加参数允许root发动 
./kibana --allow-root

Elasticsearch(ELK)集群环境部署

七、logstash 布置

1)logstash 下载解压

wget https://artifacts.elastic.co/downloads/logstash/logstash-7.4.2.tar.gz
tar -zxf  logstash-7.4.2.tar.gz && cd logstash-7.4.2

2)解压测试数据集

wget http://files.grouplens.org/datasets/movielens/ml-latest-small.zip
unzip ml-latest-small.zip && cd ml-latest-small

3)创立并编辑 logstash.conf 文件,增加如下内容(Ruby 语法)

input {
  file {
    path => "/home/elsearch/ml-latest-small/movies.csv"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}
filter {
  csv {
    separator => ","
    columns => ["id","content","genre"]
  }
  mutate {
    split => { "genre" => "|" }
    remove_field => ["path", "host","@timestamp","message"]
  }
  mutate {
    split => ["content", "("]
    add_field => { "title" => "%{[content][0]}"}
    add_field => { "year" => "%{[content][2]}"}
  }
  mutate {
    convert => {
      "year" => "integer"
    }
    strip => ["title"]
    remove_field => ["path", "host","@timestamp","message","content"]
  }
}
output {
   elasticsearch {
     hosts => "http://192.168.182.132:9200"
     index => "movies"
     document_id => "%{id}"
   }
  stdout {}
}

4)导入数据

【留意】导入后不会主动退出,用 Ctrl-C 手动退出

./bin/logstash -f ../ml-latest-small/logstash.conf

到这里ELK布置就完毕了,有任何疑问欢迎给我留言或私信,可重视我的大众号【大数据与云原生技术共享】查阅更多相关技术文章哦~

Elasticsearch(ELK)集群环境部署