如何在 CentOS 7 上安装 Elastic Stack
Lucene 搜索引擎 日志
**Elasticsearch** 是基于 Lucene 由 Java 开发的开源搜索引擎。它提供了一个分布式、多租户的全文搜索引擎（LCTT 译注：多租户是指多租户技术，是一种软件架构技术，用来探讨与实现如何在多用户的环境下共用相同的系统或程序组件，并且仍可确保各用户间数据的隔离性。），并带有 HTTP 仪表盘的 Web 界面（Kibana）。数据会被 Elasticsearch 查询、检索，并且使用 JSON 文档方案存储。Elasticsearch 是一个可扩展的搜索引擎，可用于搜索所有类型的文本文档，包括日志文件。Elasticsearch 是 Elastic Stack 的核心，Elastic Stack 也被称为 ELK Stack。


**Logstash** 是用于管理事件和日志的开源工具。它为数据收集提供实时传递途径。 Logstash 将收集您的日志数据，将数据转换为 JSON 文档，并将其存储在 Elasticsearch 中。


**Kibana** 是 Elasticsearch 的开源数据可视化工具。Kibana 提供了一个漂亮的仪表盘 Web 界面。 你可以用它来管理和可视化来自 Elasticsearch 的数据。 它不仅美丽，而且强大。


在本教程中，我将向您展示如何在 CentOS 7 服务器上安装和配置 Elastic Stack 以监视服务器日志。 然后，我将向您展示如何在操作系统为 CentOS 7 和 Ubuntu 16 的客户端上安装 “Elastic beats”。


![](/data/attachment/album/201704/29/004453qr9m7m8322rs2ere.jpg)


**前提条件**


* 64 位的 CentOS 7，4 GB 内存 - elk 主控机
* 64 位的 CentOS 7 ，1 GB 内存 - 客户端 1
* 64 位的 Ubuntu 16 ，1 GB 内存 - 客户端 2


### 步骤 1 - 准备操作系统


在本教程中，我们将禁用 CentOS 7 服务器上的 SELinux。 编辑 SELinux 配置文件。



```
vim /etc/sysconfig/selinux

```

将 `SELINUX` 的值从 `enforcing` 改成 `disabled` 。



```
SELINUX=disabled

```

然后重启服务器：



```
reboot

```

再次登录服务器并检查 SELinux 状态。



```
getenforce

```

确保结果是 `disabled`。


### 步骤 2 - 安装 Java


部署 Elastic stack 依赖于Java，Elasticsearch 需要 Java 8 版本，推荐使用 Oracle JDK 1.8 。我将从官方的 Oracle rpm 包安装 Java 8。


使用 `wget` 命令下载 Java 8 的 JDK。



```
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http:%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u77-b02/jdk-8u77-linux-x64.rpm"

```

然后使用 `rpm` 命令安装：



```
rpm -ivh jdk-8u77-linux-x64.rpm

```

最后，检查 java JDK 版本，确保它正常工作。



```
java -version

```

您将看到服务器的 Java 版本。


### 步骤 3 - 安装和配置 Elasticsearch


在此步骤中，我们将安装和配置 Elasticsearch。 从 elastic.co 网站提供的 rpm 包安装 Elasticsearch，并将其配置运行在 localhost 上（以确保该程序安全，而且不能从外部访问）。


在安装 Elasticsearch 之前，将 elastic.co 的密钥添加到服务器。



```
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

```

接下来，使用 `wget` 下载 Elasticsearch 5.1，然后安装它。



```
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.1.rpm
rpm -ivh elasticsearch-5.1.1.rpm

```

Elasticsearch 已经安装好了。 现在进入配置目录编辑 `elasticsaerch.yml` 配置文件。



```
cd /etc/elasticsearch/
vim elasticsearch.yml

```

去掉第 40 行的注释，启用 Elasticsearch 的内存锁。这将禁用 Elasticsearch 的内存交换。



```
bootstrap.memory_lock: true

```

在 `Network` 块中，取消注释 `network.host` 和 `http.port` 行。



```
network.host: localhost
http.port: 9200

```

保存文件并退出编辑器。


现在编辑 `elasticsearch.service` 文件的内存锁配置。



```
vim /usr/lib/systemd/system/elasticsearch.service

```

去掉第 60 行的注释，确保该值为 `unlimited`。



```
MAX_LOCKED_MEMORY=unlimited

```

保存并退出。


Elasticsearch 配置到此结束。Elasticsearch 将在本机的 9200 端口运行，我们通过在 CentOS 服务器上启用 `mlockall` 来禁用内存交换。重新加载 systemd，将 Elasticsearch 置为开机启动，然后启动服务。



```
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch

```

等待 Eelasticsearch 启动成功，然后检查服务器上打开的端口，确保 9200 端口的状态是 `LISTEN`。



```
netstat -plntu

```

![Check elasticsearch running on port 9200](/data/attachment/album/201704/29/004509cumkpy8plxqqbxxz.png)


然后检查内存锁以确保启用 `mlockall`，并使用以下命令检查 Elasticsearch 是否正在运行。



```
curl -XGET 'localhost:9200/_nodes?filter_path=**.mlockall&pretty'
curl -XGET 'localhost:9200/?pretty'

```

会看到如下结果。


![Check memory lock elasticsearch and check status](/data/attachment/album/201704/29/004512mt7f2mtur3t0fm7m.png)


### 步骤 4 - 安装和配置 Kibana 和 Nginx


在这一步，我们将在 Nginx Web 服务器上安装并配置 Kibana。 Kibana 监听在 localhost 上，而 Nginx 作为 Kibana 的反向代理。


用 `wget` 下载 Kibana 5.1，然后使用 `rpm` 命令安装：



```
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.1.1-x86_64.rpm
rpm -ivh kibana-5.1.1-x86_64.rpm

```

编辑 Kibana 配置文件。



```
vim /etc/kibana/kibana.yml

```

去掉配置文件中 `server.port`、`server.host` 和 `elasticsearch.url` 这三行的注释。



```
server.port: 5601
server.host: "localhost"
elasticsearch.url: "http://localhost:9200"

```

保存并退出。


将 Kibana 设为开机启动，并且启动 Kibana 。



```
sudo systemctl enable kibana
sudo systemctl start kibana

```

Kibana 将作为 node 应用程序运行在端口 5601 上。



```
netstat -plntu

```

![Kibana running as node application on port 5601](/data/attachment/album/201704/29/004515rfpdup3bi2f2zuw2.png)


Kibana 安装到此结束。 现在我们需要安装 Nginx 并将其配置为反向代理，以便能够从公共 IP 地址访问 Kibana。


Nginx 在 Epel 资源库中可以找到，用 `yum` 安装 epel-release。



```
yum -y install epel-release

```

然后安装 Nginx 和 httpd-tools 这两个包。



```
yum -y install nginx httpd-tools

```

httpd-tools 软件包包含 Web 服务器的工具，可以为 Kibana 添加 htpasswd 基础认证。


编辑 Nginx 配置文件并删除 `server {}` 块，这样我们可以添加一个新的虚拟主机配置。



```
cd /etc/nginx/
vim nginx.conf

```

删除 `server { }` 块。


![Remove Server Block on Nginx configuration](/data/attachment/album/201704/29/004517s2ihfkof5fj5zag1.png)


保存并退出。


现在我们需要在 `conf.d` 目录中创建一个新的虚拟主机配置文件。 用 `vim` 创建新文件 `kibana.conf`。



```
vim /etc/nginx/conf.d/kibana.conf

```

复制下面的配置。



```
server {
    listen 80;

    server_name elk-stack.co;

    auth_basic "Restricted Access";
    auth_basic_user_file /etc/nginx/.kibana-user;

    location / {
        proxy_pass http://localhost:5601;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

```

保存并退出。


然后使用 `htpasswd` 命令创建一个新的基本认证文件。



```
sudo htpasswd -c /etc/nginx/.kibana-user admin
“输入你的密码”

```

测试 Nginx 配置，确保没有错误。 然后设定 Nginx 开机启动并启动 Nginx。



```
nginx -t
systemctl enable nginx
systemctl start nginx

```

![Add nginx virtual host configuration for Kibana Application](/data/attachment/album/201704/29/004520arcbpi9v0x119ctc.png)


### 步骤 5 - 安装和配置 Logstash


在此步骤中，我们将安装 Logstash，并将其配置为：从配置了 filebeat 的 logstash 客户端里集中化服务器的日志，然后过滤和转换 Syslog 数据，并将其移动到存储中心（Elasticsearch）中。


下载 Logstash 并使用 rpm 进行安装。



```
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.1.1.rpm
rpm -ivh logstash-5.1.1.rpm

```

生成新的 SSL 证书文件，以便客户端可以识别 elastic 服务端。


进入 `tls` 目录并编辑 `openssl.cnf` 文件。



```
cd /etc/pki/tls
vim openssl.cnf

```

在 `[v3_ca]` 部分添加服务器标识。



```
[ v3_ca ]

# Server IP Address
subjectAltName = IP: 10.0.15.10

```

保存并退出。


使用 `openssl` 命令生成证书文件。



```
openssl req -config /etc/pki/tls/openssl.cnf -x509 -days 3650 -batch -nodes -newkey rsa:2048 -keyout /etc/pki/tls/private/logstash-forwarder.key -out /etc/pki/tls/certs/logstash-forwarder.crt

```

证书文件可以在 `/etc/pki/tls/certs/` 和 `/etc/pki/tls/private/` 目录中找到。


接下来，我们会为 Logstash 创建新的配置文件。创建一个新的 `filebeat-input.conf` 文件来为 filebeat 配置日志源，然后创建一个 `syslog-filter.conf` 配置文件来处理 syslog，再创建一个 `output-elasticsearch.conf` 文件来定义输出日志数据到 Elasticsearch。


转到 logstash 配置目录，并在 `conf.d` 子目录中创建新的配置文件。



```
cd /etc/logstash/
vim conf.d/filebeat-input.conf

```

输入配置，粘贴以下配置：



```
input {
  beats {
    port => 5443
    ssl => true
    ssl_certificate => "/etc/pki/tls/certs/logstash-forwarder.crt"
    ssl_key => "/etc/pki/tls/private/logstash-forwarder.key"
  }
}

```

保存并退出。


创建 `syslog-filter.conf` 文件。



```
vim conf.d/syslog-filter.conf

```

粘贴以下配置：



```
filter {
  if [type] == "syslog" {
    grok {
      match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
      add_field => [ "received_at", "%{@timestamp}" ]
      add_field => [ "received_from", "%{host}" ]
    }
    date {
      match => [ "syslog_timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
    }
  }
}

```

我们使用名为 `grok` 的过滤器插件来解析 syslog 文件。


保存并退出。


创建输出配置文件 `output-elasticsearch.conf`。



```
vim conf.d/output-elasticsearch.conf

```

粘贴以下配置：



```
output {
  elasticsearch { hosts => ["localhost:9200"]
    hosts => "localhost:9200"
    manage_template => false
    index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
    document_type => "%{[@metadata][type]}"
  }
}

```

保存并退出。


最后，将 logstash 设定为开机启动并且启动服务。



```
sudo systemctl enable logstash
sudo systemctl start logstash

```

![Logstash started on port 5443 with SSL Connection](/data/attachment/album/201704/29/004523i98f4qasoq8qt94t.png)


### 步骤 6 - 在 CentOS 客户端上安装并配置 Filebeat


Beat 作为数据发送人的角色，是一种可以安装在客户端节点上的轻量级代理，将大量数据从客户机发送到 Logstash 或 Elasticsearch 服务器。有 4 种 beat，`Filebeat` 用于发送“日志文件”，`Metricbeat` 用于发送“指标”，`Packetbeat` 用于发送“网络数据”，`Winlogbeat` 用于发送 Windows 客户端的“事件日志”。


在本教程中，我将向您展示如何安装和配置 `Filebeat`，通过 SSL 连接将数据日志文件传输到 Logstash 服务器。


登录到客户端1的服务器上。 然后将证书文件从 elastic 服务器复制到客户端1的服务器上。



```
ssh root@client1IP

```

使用 `scp` 命令拷贝证书文件。



```
scp root@elk-serverIP:~/logstash-forwarder.crt .
输入 elk-server 的密码

```

创建一个新的目录，将证书移动到这个目录中。



```
sudo mkdir -p /etc/pki/tls/certs/
mv ~/logstash-forwarder.crt /etc/pki/tls/certs/

```

接下来，在客户端 1 服务器上导入 elastic 密钥。



```
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

```

下载 Filebeat 并且用 `rpm` 命令安装。



```
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.1.1-x86_64.rpm
rpm -ivh filebeat-5.1.1-x86_64.rpm

```

Filebeat 已经安装好了，请转到配置目录并编辑 `filebeat.yml` 文件。



```
cd /etc/filebeat/
vim filebeat.yml

```

在第 21 行的路径部分，添加新的日志文件。 我们将创建两个文件，记录 ssh 活动的 `/var/log/secure` 文件 ，以及服务器日志 `/var/log/messages` 。



```
  paths:
    - /var/log/secure
    - /var/log/messages

```

在第 26 行添加一个新配置来定义 syslog 类型的文件。



```
  document-type: syslog

```

Filebeat 默认使用 Elasticsearch 作为输出目标。 在本教程中，我们将其更改为 Logshtash。 在 83 行和 85 行添加注释来禁用 Elasticsearch 输出。


禁用 Elasticsearch 输出：



```
#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
  # Array of hosts to connect to.
#  hosts: ["localhost:9200"]

```

现在添加新的 logstash 输出配置。 去掉 logstash 输出配置的注释，并将所有值更改为下面配置中的值。



```
output.logstash:
  # The Logstash hosts
  hosts: ["10.0.15.10:5443"]
  bulk_max_size: 1024
  ssl.certificate_authorities: ["/etc/pki/tls/certs/logstash-forwarder.crt"]
  template.name: "filebeat"
  template.path: "filebeat.template.json"
  template.overwrite: false

```

保存文件并退出 vim。


将 Filebeat 设定为开机启动并启动。



```
sudo systemctl enable filebeat
sudo systemctl start filebeat

```

### 步骤 7 - 在 Ubuntu 客户端上安装并配置 Filebeat


使用 `ssh` 连接到服务器。



```
ssh root@ubuntu-clientIP

```

使用 `scp` 命令拷贝证书文件。



```
scp root@elk-serverIP:~/logstash-forwarder.crt .

```

创建一个新的目录，将证书移动到这个目录中。



```
sudo mkdir -p /etc/pki/tls/certs/
mv ~/logstash-forwarder.crt /etc/pki/tls/certs/

```

在服务器上导入 elastic 密钥。



```
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

```

下载 Filebeat .deb 包并且使用 `dpkg` 命令进行安装。



```
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.1.1-amd64.deb
dpkg -i filebeat-5.1.1-amd64.deb

```

转到配置目录并编辑 `filebeat.yml` 文件。



```
cd /etc/filebeat/
vim filebeat.yml

```

在路径配置部分添加新的日志文件路径。



```
  paths:
    - /var/log/auth.log
    - /var/log/syslog

```

设定文档类型为 `syslog` 。



```
  document-type: syslog

```

将下列几行注释掉，禁用输出到 Elasticsearch。



```
#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
  # Array of hosts to connect to.
#  hosts: ["localhost:9200"]

```

启用 logstash 输出，去掉以下配置的注释并且按照如下所示更改值。



```
output.logstash:
  # The Logstash hosts
  hosts: ["10.0.15.10:5443"]
  bulk_max_size: 1024
  ssl.certificate_authorities: ["/etc/pki/tls/certs/logstash-forwarder.crt"]
  template.name: "filebeat"
  template.path: "filebeat.template.json"
  template.overwrite: false

```

保存并退出 vim。


将 Filebeat 设定为开机启动并启动。



```
sudo systemctl enable filebeat
sudo systemctl start filebeat

```

检查服务状态：



```
systemctl status filebeat

```

![Filebeat is running on the client Ubuntu](/data/attachment/album/201704/29/004526pa3c2cze7u7yyvkz.png)


### 步骤 8 - 测试


打开您的网络浏览器，并访问您在 Nginx 中配置的 elastic stack 域名，我的是“elk-stack.co”。 使用管理员密码登录，然后按 Enter 键登录 Kibana 仪表盘。


![Login to the Kibana Dashboard with Basic Auth](/data/attachment/album/201704/29/004530wvvf0ini30k8npni.png)


创建一个新的默认索引 `filebeat-*`，然后点击“创建”按钮。


![Create First index filebeat for Kibana](/data/attachment/album/201704/29/004533ff3rzp89ppf3lm2k.png)


默认索引已创建。 如果 elastic stack 上有多个 beat，您可以在“星形”按钮上点击一下即可配置默认 beat。


![Filebeat index as default index on Kibana Dashboard](/data/attachment/album/201704/29/004534lchbbto58h7b5cz8.png)


转到 “发现” 菜单，您就可以看到 elk-client1 和 elk-client2 服务器上的所有日志文件。


![Discover all Log Files from the Servers](/data/attachment/album/201704/29/004536i8mh48fx54kr42hz.png)


来自 elk-client1 服务器日志中的无效 ssh 登录的 JSON 输出示例。


![JSON output for Failed SSH Login](/data/attachment/album/201704/29/004547r1g0erhfl72vi15v.png)


使用其他的选项，你可以使用 Kibana 仪表盘做更多的事情。


Elastic Stack 已安装在 CentOS 7 服务器上。 Filebeat 已安装在 CentOS 7 和 Ubuntu 客户端上。




---


via: <https://www.howtoforge.com/tutorial/how-to-install-elastic-stack-on-centos-7/>


作者：[Muhammad Arul](https://www.howtoforge.com/tutorial/how-to-install-elastic-stack-on-centos-7/) 译者：[Flowsnow](https://github.com/Flowsnow) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# How to Install Elastic Stack on CentOS 7

### This tutorial exists for these OS versions

[CentOS 8](/how-to-install-elastic-stack-on-centos-8/)**CentOS 7**

### On this page

[Step 1 - Prepare the Operating System](#step-nbspprepare-the-operating-system)[Step 2 - Install Java](#step-install-java)[Step 3 - Install and Configure Elasticsearch](#step-install-and-configure-elasticsearch)[Step 4 - Install and Configure Kibana with Nginx](#step-install-and-configure-kibana-with-nginx)[Step 5 - Install and Configure Logstash](#step-install-and-configure-logstash)[Step 6 - Install and Configure Filebeat on the CentOS Client](#step-install-and-configure-filebeat-on-the-centos-client)[Step 7 - Install and Configure Filebeat on the Ubuntu Client](#step-install-and-configure-filebeat-on-the-ubuntu-client)[Step 8 - Testing Elastic Stack](#step-testing-elastic-stack)[Reference](#reference)

**Elasticsearch** is an open source search engine based on Lucene, developed in Java. It provides a distributed and multitenant full-text search engine with an HTTP Dashboard web-interface (Kibana). The data is queried, retrieved and stored with a JSON document scheme. Elasticsearch is a scalable search engine that can be used to search for all kind of text documents, including log files. Elasticsearch is the heart of the 'Elastic Stack' or ELK Stack.

**Logstash** is an open source tool for managing events and logs. It provides real-time pipelining for data collections. Logstash will collect your log data, convert the data into JSON documents, and store them in Elasticsearch.

**Kibana** is an open source data visualization tool for Elasticsearch. Kibana provides a pretty dashboard web interface. It allows you to manage and visualize data from Elasticsearch. It's not just beautiful, but also powerful.

In this tutorial, I will show you how to install and configure Elastic Stack on a CentOS 7 server for monitoring server logs. Then I'll show you how to install 'Elastic beats' on a CentOS 7 and an Ubuntu 16.04 client operating system.

**Prerequisites**

- CentOS 7 64 bit with 4GB of RAM - elk-master
- CentOS 7 64 bit with 1 GB of RAM - client1
- Ubuntu 16.04 64 bit with 1GB of RAM - client2

## Step 1 - Prepare the Operating System

In this tutorial, we will disable SELinux on the CentOS 7 server. Edit the SELinux configuration file.

vim /etc/sysconfig/selinux

Change SELinux value from enforcing to disabled.

SELINUX=disabled

Then reboot the server.

reboot

Log in to the server again and check the SELinux state.

getenforce

Make sure the result is disabled.

## Step 2 - Install Java

Java is required for the Elastic stack deployment. Elasticsearch requires Java 8, it is recommended to use the Oracle JDK 1.8. I will install Java 8 from the official Oracle rpm package.

Download Java 8 JDK with the wget command.

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http:%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u77-b02/jdk-8u77-linux-x64.rpm"

Then install it with this rpm command;

rpm -ivh jdk-8u77-linux-x64.rpm

Finally, check java JDK version to ensure that it is working properly.

java -version

You will see Java version of the server.

## Step 3 - Install and Configure Elasticsearch

In this step, we will install and configure Elasticsearch. I will install Elasticsearch from an rpm package provided by elastic.co and configure it to run on localhost (to make the setup secure and ensure that it is not reachable from the outside).

Before installing Elasticsearch, add the elastic.co key to the server.

rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

Next, download Elasticsearch 5.1 with wget and then install it.

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.1.rpm

rpm -ivh elasticsearch-5.1.1.rpm

Elasticsearch is installed. Now go to the configuration directory and edit the elasticsaerch.yml configuration file.

cd /etc/elasticsearch/

vim elasticsearch.yml

Enable memory lock for Elasticsearch by removing a comment on line 40. This disables memory swapping for Elasticsearch.

bootstrap.memory_lock: true

In the 'Network' block, uncomment the network.host and http.port lines.

network.host: localhost

http.port: 9200

Save the file and exit the editor.

Now edit the elasticsearch.service file for the memory lock configuration.

vim /usr/lib/systemd/system/elasticsearch.service

Uncomment LimitMEMLOCK line.

LimitMEMLOCK=infinity

Save and exit.

Edit the sysconfig configuration file for Elasticsearch.

vim /etc/sysconfig/elasticsearch

Uncomment line 60 and make sure the value is 'unlimited'.

MAX_LOCKED_MEMORY=unlimited

Save and exit.

The Elasticsearch configuration is finished. Elasticsearch will run on the localhost IP address on port 9200, we disabled memory swapping for it by enabling mlockall on the CentOS server.

Reload systemd, enable Elasticsearch to start at boot time, then start the service.

sudo systemctl daemon-reload

sudo systemctl enable elasticsearch

sudo systemctl start elasticsearch

Wait a second for Eelasticsearch to start, then check the open ports on the server, make sure 'state' for port 9200 is 'LISTEN'.

netstat -plntu

Then check the memory lock to ensure that mlockall is enabled, and check that Elasticsearch is running with the commands below.

curl -XGET 'localhost:9200/_nodes?filter_path=**.mlockall&pretty'

curl -XGET 'localhost:9200/?pretty'

You will see the results below.

## Step 4 - Install and Configure Kibana with Nginx

In this step, we will install and configure Kibana with a Nginx web server. Kibana will listen on the localhost IP address and Nginx acts as a reverse proxy for the Kibana application.

Download Kibana 5.1 with wget, then install it with the rpm command:

wget https://artifacts.elastic.co/downloads/kibana/kibana-5.1.1-x86_64.rpm

rpm -ivh kibana-5.1.1-x86_64.rpm

Now edit the Kibana configuration file.

vim /etc/kibana/kibana.yml

Uncomment the configuration lines for server.port, server.host and elasticsearch.url.

server.port: 5601

server.host: "localhost"

elasticsearch.url: "http://localhost:9200"

Save and exit.

Add Kibana to run at boot and start it.

sudo systemctl enable kibana

sudo systemctl start kibana

Kibana will run on port 5601 as node application.

netstat -plntu

The Kibana installation is finished. Now we need to install Nginx and configure it as reverse proxy to be able to access Kibana from the public IP address.

Nginx is available in the Epel repository, install epel-release with yum.

yum -y install epel-release

Next, install the Nginx and httpd-tools package.

yum -y install nginx httpd-tools

The httpd-tools package contains tools for the web server, we will use htpasswd basic authentication for Kibana.

Edit the Nginx configuration file and remove the **'server { }**' block, so we can add a new virtual host configuration.

cd /etc/nginx/

vim nginx.conf

Remove the server { } block.

Save the file and exit vim.

Now we need to create a new virtual host configuration file in the conf.d directory. Create the new file 'kibana.conf' with vim.

vim /etc/nginx/conf.d/kibana.conf

Paste the configuration below.

server {

listen 80;


server_name elk-stack.co;


auth_basic "Restricted Access";

auth_basic_user_file /etc/nginx/.kibana-user;


location / {

proxy_pass http://localhost:5601;

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection 'upgrade';

proxy_set_header Host $host;

proxy_cache_bypass $http_upgrade;

}

}

Save and exit.

Then create a new basic authentication file with the htpasswd command.

sudo htpasswd -c /etc/nginx/.kibana-user admin

TYPE YOUR PASSWORD

Test the Nginx configuration and make sure there is no error. Then add Nginx to run at the boot time and start Nginx.

nginx -t

systemctl enable nginx

systemctl start nginx

## Step 5 - Install and Configure Logstash

In this step, we will install Logsatash and configure it to centralize server logs from clients with filebeat, then filter and transform the Syslog data and move it into the stash (Elasticsearch).

Download Logstash and install it with rpm.

wget https://artifacts.elastic.co/downloads/logstash/logstash-5.1.1.rpm

rpm -ivh logstash-5.1.1.rpm

Generate a new SSL certificate file so that the client can identify the elastic server.

Go to the tls directory and edit the openssl.cnf file.

cd /etc/pki/tls

vim openssl.cnf

Add a new line in the '[ v3_ca ]' section for the server identification.

[ v3_ca ]


# Server IP Address

subjectAltName = IP: 10.0.15.10

Save and exit.

Generate the certificate file with the openssl command.

openssl req -config /etc/pki/tls/openssl.cnf -x509 -days 3650 -batch -nodes -newkey rsa:2048 -keyout /etc/pki/tls/private/logstash-forwarder.key -out /etc/pki/tls/certs/logstash-forwarder.crt

The certificate files can be found in the '/etc/pki/tls/certs/' and '/etc/pki/tls/private/' directories.

Next, we will create new configuration files for Logstash. We will create a new 'filebeat-input.conf' file to configure the log sources for filebeat, then a 'syslog-filter.conf' file for syslog processing and the 'output-elasticsearch.conf' file to define the Elasticsearch output.

Go to the logstash configuration directory and create the new configuration files in the 'conf.d' subdirectory.

cd /etc/logstash/

vim conf.d/filebeat-input.conf

Input configuration: paste the configuration below.

input {

beats {

port => 5443

ssl => true

ssl_certificate => "/etc/pki/tls/certs/logstash-forwarder.crt"

ssl_key => "/etc/pki/tls/private/logstash-forwarder.key"

}

}

Save and exit.

Create the syslog-filter.conf file.

vim conf.d/syslog-filter.conf

Paste the configuration below.

filter {

if [type] == "syslog" {

grok {

match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }

add_field => [ "received_at", "%{@timestamp}" ]

add_field => [ "received_from", "%{host}" ]

}

date {

match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]

}

}

}

We use a filter plugin named '**grok**' to parse the syslog files.

Save and exit.

Create the output configuration file 'output-elasticsearch.conf'.

vim conf.d/output-elasticsearch.conf

Paste the configuration below.

output {

elasticsearch { hosts => ["localhost:9200"]

hosts => "localhost:9200"

manage_template => false

index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"

document_type => "%{[@metadata][type]}"

}

}

Save and exit.

Finally add logstash to start at boot time and start the service.

sudo systemctl enable logstash

sudo systemctl start logstash

## Step 6 - Install and Configure Filebeat on the CentOS Client

Beats are data shippers, lightweight agents that can be installed on the client nodes to send huge amounts of data from the client machine to the Logstash or Elasticsearch server. There are 4 beats available, 'Filebeat' for 'Log Files', 'Metricbeat' for 'Metrics', 'Packetbeat' for 'Network Data' and 'Winlogbeat' for the Windows client 'Event Log'.

In this tutorial, I will show you how to install and configure 'Filebeat' to transfer data log files to the Logstash server over an SSL connection.

Login to the client1 server. Then copy the certificate file from the elastic server to the client1 server.

ssh root@client1IP

Copy the certificate file with the scp command.

scp root@elk-serverIP:~/logstash-forwarder.crt .

TYPE elk-server password

Create a new directory and move certificate file to that directory.

sudo mkdir -p /etc/pki/tls/certs/

mv ~/logstash-forwarder.crt /etc/pki/tls/certs/

Next, import the elastic key on the client1 server.

rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

Download Filebeat and install it with rpm.

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.1.1-x86_64.rpm

rpm -ivh filebeat-5.1.1-x86_64.rpm

Filebeat has been installed, go to the configuration directory and edit the file 'filebeat.yml'.

cd /etc/filebeat/

vim filebeat.yml

In the paths section on line 21, add the new log files. We will add two files '/var/log/secure' for ssh activity and '/var/log/messages' for the server log.

paths:

- /var/log/secure

- /var/log/messages

Add a new configuration on line 26 to define the syslog type files.

document-type: syslog

Filebeat is using Elasticsearch as the output target by default. In this tutorial, we will change it to Logshtash. Disable Elasticsearch output by adding comments on the lines 83 and 85.

Disable elasticsearch output.

#-------------------------- Elasticsearch output ------------------------------

#output.elasticsearch:

# Array of hosts to connect to.

# hosts: ["localhost:9200"]

Now add the new logstash output configuration. Uncomment the logstash output configuration and change all value to the configuration that is shown below.

output.logstash:

# The Logstash hosts

hosts: ["10.0.15.10:5443"]

bulk_max_size: 1024

ssl.certificate_authorities: ["/etc/pki/tls/certs/logstash-forwarder.crt"]

template.name: "filebeat"

template.path: "filebeat.template.json"

template.overwrite: false

Save the file and exit vim.

Add Filebeat to start at boot time and start it.

sudo systemctl enable filebeat

sudo systemctl start filebeat

## Step 7 - Install and Configure Filebeat on the Ubuntu Client

Connect to the server by ssh.

ssh root@ubuntu-clientIP

Copy the certificate file to the client with the scp command.

scp root@elk-serverIP:~/logstash-forwarder.crt .

Create a new directory for the certificate file and move the file to that directory.

sudo mkdir -p /etc/pki/tls/certs/

mv ~/logstash-forwarder.crt /etc/pki/tls/certs/

Add the elastic key to the server.

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

Download the Filebeat .deb package and install it with the dpkg command.

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.1.1-amd64.deb

dpkg -i filebeat-5.1.1-amd64.deb

Go to the filebeat configuration directory and edit the file 'filebeat.yml' with vim.

cd /etc/filebeat/

vim filebeat.yml

Add the new log file paths in the paths configuration section.

paths:

- /var/log/auth.log

- /var/log/syslog

Set the document type to syslog.

document-type: syslog

Disable elasticsearch output by adding comments to the lines shown below.

#-------------------------- Elasticsearch output ------------------------------

#output.elasticsearch:

# Array of hosts to connect to.

# hosts: ["localhost:9200"]

Enable logstash output, uncomment the configuration and change the values as shown below.

output.logstash:

# The Logstash hosts

hosts: ["10.0.15.10:5443"]

bulk_max_size: 1024

ssl.certificate_authorities: ["/etc/pki/tls/certs/logstash-forwarder.crt"]

template.name: "filebeat"

template.path: "filebeat.template.json"

template.overwrite: false

Save the file and exit vim.

Add Filebeat to start at boot time and start it.

sudo systemctl enable filebeat

sudo systemctl start filebeat

Check the service status.

systemctl status filebeat

## Step 8 - Testing Elastic Stack

Open your web browser and visit the elastic stack domain that you used in the Nginx configuration, mine is 'elk-stack.co'. Login as admin user with your password and press Enter to log in to the Kibana dashboard.

Create a new default index 'filebeat-*' and click on the 'Create' button.

Th default index has been created. If you have multiple beats on the elastic stack, you can configure the default beat with just one click on the 'star' button.

Go to the '**Discover**' menu and you will see all the log file from the elk-client1 and elk-client2 servers.

An example of JSON output from the elk-client1 server log for an invalid ssh login.

And there is much more than you can do with Kibana dashboard, just play around with the available options.

Elastic Stack has been installed on a CentOS 7 server. Filebeat has been installed on a CentOS 7 and an Ubuntu client.