概述
Elasticsearch + Logstash + Kibana(ELK)是一套开源的日志管理方案。
环境准备
3 台服务器:
- CPU 4 核,内存 4GB,IP 10.16.4.21,主要跑 Elasticsearch-master,Logstash,kibana 服务;
- CPU 2 核,内存 2GB,IP 10.16.4.56,主要跑 Elasticsearch-data 服务;
- CPU 2 核,内存 8GB,IP 10.16.1.22,redis 缓存服务;
Elasticsearch 版本:6.2.4
Logstash 版本:6.2.4
Kibana 版本:6.2.4
redis 版本:4.0.2
更改主机名
$ hostnamectl set-hostname elk-manage
$ hostnamectl set-hostname elk-data
$ hostnamectl set-hostname elk-redis
关闭防火墙
$ systemctl stop firewalld
$ systemctl disable firewalld
安装 JDK
elasticsearch,logstash 的运行需要依赖 java 环境。
下载并解压 jdk 二进制包。
$ cd /opt
$ wget http://mirrors.aorise.org:8000/java/jdk/1.8.0/x64/jdk-8u144-linux-x64.tar.gz
$ tar zxvf jdk-8u144-linux-x64.tar.gz -C /usr/local
$ mv /usr/local/jdk1.8.0-8u144 /usr/local/java
$ cd ~
配置 java 环境变量。
在 ~/.bashrc 文件末尾添加如下内容:
$ export JAVA_HOME=/usr/local/java
$ export JRE_HOME=$JAVA_HOME/jre
$ export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/bin/tools.jar:$JRE_HOME/lib
$ export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
使配置生效。
$ source ~/.bashrc
安装 Elasticsearch
创建用户
elasticsearch 不能以 root 用户启动,故需要创建非 root 用户来启动 elasticsearch。
$ adduser elasticsearch
下载 elasticsearch 安装包
$ cd /opt
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz
sha512sum elasticsearch-6.2.4.tar.gz
$ tar zxvf elasticsearch-6.2.4.tar.gz -C /usr/local
$ chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-6.2.4
$ cd ~
修改 elasticsearch 配置文件
配置文件 .../elasticsearch-6.2.4/config/elasticsearch.yml
node-maser 配置文件如下:
cluster.name: es-cluster
node.name: node-master
node.master: true
node.data: false
path.logs: /var/log/elasticsearch
network.host: 10.16.4.21
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.16.4.21", "10.16.4.56"]
discovery.zen.minimum_master_nodes: 1
node-data 配置文件如下:
cluster.name: es-cluster
node.name: node-data
node.master: false
node.data: true
path.data: /data/es-data
path.logs: /var/log/elasticsearch
network.host: 10.16.4.56
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10。16.4.21", "10.16.4.56"]
discovery.zen.minimum_master_nodes: 1
创建对应的路径,并更改用户属性。
$ mkdir -p /var/log/elasticsearch
$ mkdir -p /data/es-data
$ chown -R elasticsearch:elasticsearch /var/log/elasticsearch
$ chown -R elasticsearch:elasticsearch /data/es-data
注:集群名称必须相同。
创建启动脚本
#!/bin/sh
# description: elasticsearch
export JAVA_HOME=/usr/local/java
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/bin/tools.jar:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
case "$1" in
start)
su elasticsearch<<!
cd /usr/local/elasticsearch-6.2.4
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
stop)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
;;
restart)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
su elasticsearch<<!
cd /usr/local/elasticsearch-6.2.4
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
*)
echo "start|stop|restart"
;;
esac
exit $?
添加执行权限。
$ chmod +x elasticsearch
设置开机启动。
$ echo "/usr/local/elasticsearch-6.2.4/elasticsearch start" >> /etc/rc.d/rc.local
检验 elasticsearch 安装
在浏览器执行 http://10.16.4.21:9200/_cluster/health?pretty,看到如下内容:
{
"cluster_name" : "es-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 1,
"active_primary_shards" : 3,
"active_shards" : 3,
"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
}
即安装成功!
遇到的问题
启动报 “max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]” 错误。
在 /etc/security/limits.conf 文件末尾添加如下内容:
elasticsearch hard nofile 65536
elasticseatch soft nofile 65536
可通过如下命令检验配置是否生效。
$ su - elasticsearch
$ ulimit -Hn
启动报 “max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]”。
切换至 root 用户。
在 /etc/sysctl.conf 文件末尾添加如下内容:
vm.max_map_count=262144
使配置生效。
$ sysctl -p
安装 Kibana
下载 kibana 安装包
$ cd /opt
$ wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.4-linux-x86_64.tar.gz
sha512sum kibana-6.2.4-linux-x86_64.tar.gz
$ tar zxvf kibana-6.2.4-linux-x86_64.tar.gz -C $ /usr/local
$ cd ~
修改 kibana 配置文件
server.port: 5601
server.host: "10.16.4.21"
elasticsearch.url: "http://10.16.4.21:9200"
启动 kibana
$ /usr/local/kibana-6.2.4/bin/kibana
设置开机启动。
$ echo "/usr/local/kibana6.2.4/bin/kibana" >> /etc/rc.d/rc.local
或者设置 systemctl 启动。
创建 kibana.service 文件:
$ vi /etc/systemd/system/kibana.service
添加如下内容:
[Unit]
Description=kibana
[Service]
Type=simple
ExecStart=/usr/local/kibana-6.2.4/bin/kibana
[Install]
WantedBy=multi-user.target
授权。
$ chmod +x /etc/systemd/system/kibana.service
启动。
$ systemctl start kibana
$ systemctl enable kibana
安装 Logstash
建议 Linux 类的服务器下载 rmp 包安装。
下载 logstash 安装包
$ cd /opt
$ wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.4.rpm
sha512sum logstash-6.2.4.rpm
$ rpm -ivh logstash-6.2.4.rpm
$ cd ~
配置 systemd 启动
rpm
安装时,创建启动脚本的配置文件是
$ /etc/logstash/startup.options
/usr/share/logstash/bin/system-install /etc/logstash/startup.options systemd
创建配置文件
默认是没有配置文件的。
Shipper 配置文件(logstash_shipper.conf)。
input{
file{
type => "redis"
path => "/var/log/redis/redis.log"
start_position => "beginning"
sincedb_path => "/dev/null"
}
}
output{
if [type] == "redis"{
redis{
host => "127.0.0.1"
data_type => "list"
key => "redis-elk"
port => 6379
db => 2
password => "123456"
}
}
}
Indexer 配置文件(logstash_indexer.conf)。
input{
redis{
host => "127.0.0.1"
data_type => "list"
key => "redis"
password => "123456"
db => 2
}
output{
if [type] == "redis" {
elasticsearch{
hosts => ["10.16.4.56"]
index => "redis-%{+YYYY-MM-dd}"
}
}
}
启动 Logstash
在启动前,可通过如下方式检查配置文件。
$ /usr/share/logstash/bin/logstash -t -f $ /etc/logstash/conf.d/logstash_shipper/indexer.conf
启动 logstash。
$ systemctl start logstash
设置开机启动。
$ systemctl enable logstash
至此,安装完成!
参考地址:
版本依赖:https://www.elastic.co/support/matrix#matrix_compatibility
http://www.ttlsa.com/bigdata/elk-platform-for-log-management/
ELK 下载地址:https://www.elastic.co/downloads/past-releases
ELK 安装:https://www.elastic.co/guide/en/x-pack/current/installing-xpack.html#xpack-installing-offline
elasticsearch 集群安装:https://blog.csdn.net/qq_24879495/article/details/77983941
elasticsearch 启动脚本:https://www.jianshu.com/p/06794b2a7588
创建 logstash 启动脚本:https://www.cnblogs.com/keithtt/p/7189489.html
创建 logstash 启动脚本:https://www.elastic.co/guide/en/logstash/current/running-logstash.html
参数详解:https://www.cnblogs.com/yangk1996/p/11184086.html