如何在CentOS 7上安装Elastic Stack

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在CentOS 7上安装Elastic Stack相关的知识,希望对你有一定的参考价值。

参考技术A 步骤 1 - 操作系统初始化
禁用 CentOS 7 服务器上的 SELinux
我们将禁用 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 http://download.oracle.com/otn-pub/java/jdk/8u77-b02/jdk-8u77-linux-x64.rpm
\\下载java8的版本
rpm -ivh jdk-8u77-linux-x64.rpm \\rpm安装jdk环境
java -version \\查看java的版本 检查能否工作
步骤 3 - 安装和配置 Elasticsearch
在此步骤中,我们将安装和配置 Elasticsearch。 从 elastic.co 网站提供的 rpm 包安装 Elasticsearch,并将其配置运行在 localhost 上(以确保该程序安全,而且不能从外部访问)。
将 elastic.co 的密钥添加到服务器
elastic.co网站是一个https的网站(私有证书),我们需要添加证书秘钥才能安全的顺利下载。
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
步骤 3 - 安装和配置 Elasticsearch
在此步骤中,我们将安装和配置 Elasticsearch。 从 elastic.co 网站提供的 rpm 包安装 Elasticsearch,并将其配置运行在 localhost 上(以确保该程序安全,而且不能从外部访问)。
将 elastic.co 的密钥添加到服务器
elastic.co网站是一个https的网站(私有证书),我们需要添加证书秘钥才能安全的顺利下载。
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
下载安装 Elasticsearch 5.1
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.1.rpm
rpm -ivh elasticsearch-5.1.1.rpm
安装完成后我们编辑配置文件
配置文件名称:elasticsaerch.yml
cd /etc/elasticsearch/
vim elasticsearch.yml

bootstrap.memory_lock: true
\\去掉第 40 行的注释,启用 Elasticsearch 的内存锁。这将禁用 Elasticsearch 的内存交换。

network.host: localhost
http.port: 9200
\\在 Network 块中,取消注释 network.host 和 http.port 行。
编辑 elasticsearch.service 文件的内存锁配置。
vim /usr/lib/systemd/system/elasticsearch.service
MAX_LOCKED_MEMORY=unlimited
\\去掉第 60 行的注释,确保该值为 unlimited。
设置服务启动
Elasticsearch监听端口号9200,启用 CentOS 服务器上启用mlockall 来禁用内存交换,设置Elasticsearch开机自启动,然后启动服务。
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch
检查对外监听端口:
netstat -plntu
内存锁启用 mlockall,检查 Elasticsearch 是否正在运行。
curl -XGET 'localhost:9200/_nodes?filter_path=**.mlockall&pretty'
curl -XGET 'localhost:9200/?pretty'
步骤 4 - 安装和配置 Kibana 和 nginx
先安装Kibana,然后安装nginx,最后设置nginx反向代理kibana
安装并配置Kibana
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: 5601
server.host: "localhost"
elasticsearch.url: "http://localhost:9200"
将 Kibana 设为开机启动
sudo systemctl enable kibana
sudo systemctl start kibana
检查Kibana 对外监听端口 5601 确保其正常启动。
netstat -plntu
安装并配置nginx服务器
yum -y install epel-release
nginx 服务的yum包在epel包中可以找的 直接yum安装
yum -y install nginx httpd-tools
httpd-tools 软件包包含 Web 服务器的工具,可以为 Kibana 添加 htpasswd 基础认证。
编辑 Nginx 配置文件并删除 server 模块,这样我们添加新的虚拟主机配置。
cd /etc/nginx/
vim nginx.conf
\\删除 server 块。
创建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 -t
systemctl enable nginx
systemctl start nginx
步骤 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 服务端。
cd /etc/pki/tls
\\ 进入 tls 目录并编辑 openssl.cnf 文件。
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
步骤 6 - 在 CentOS 客户端上安装并配置 Filebeat
Beat
作为数据发送人的角色,是一种可以安装在客户端节点上的轻量级代理,将大量数据从客户机发送到 Logstash 或 Elasticsearch
服务器。有 4 种 beat,Filebeat 用于发送“日志文件”,Metricbeat 用于发送“指标”,Packetbeat
用于发送“网络数据”,Winlogbeat 用于发送 Windows 客户端的“事件日志”。
在本教程中,我将向您展示如何安装和配置 Filebeat,通过 SSL 连接将数据日志文件传输到 Logstash 服务器。
登录到客户端1的服务器上。 然后将证书文件从 elastic 服务器复制到客户端1的服务器上。
ssh root@client1IP
scp root@elk-serverIP:~/logstash-forwarder.crt .
.....

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

\\在 83 行和 85 行添加注释来禁用 Elasticsearch 输出,更改为 Logshtash:
-------------------------------------------------------------------------------------
#-------------------------- 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
--------------------------------------------------------------------------------------
PS:Filebeat 默认使用 Elasticsearch 作为输出目标。 在本教程中,我们将其更改为 Logshtash。
将 Filebeat 设定为开机启动并启动。
sudo systemctl enable filebeat
sudo systemctl start filebeat
步骤 7 - 在 Ubuntu 客户端上安装并配置 Filebeat
从服务端拷贝证书文件
ssh root@ubuntu-clientIP
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
\\ 在第 21 行的路径部分,添加新的日志文件。 我们将创建两个文件,记录 ssh 活动的 /var/log/secure 文件 ,以及服务器日志 /var/log/messages :
paths:
- /var/log/secure
- /var/log/messages
\\在第 26 行添加一个新配置来定义 syslog 类型的文件:
document-type: syslog

\\在 83 行和 85 行添加注释来禁用 Elasticsearch 输出,更改为 Logshtash:
-------------------------------------------------------------------------------------
#-------------------------- 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
--------------------------------------------------------------------------------------
PS:Filebeat 默认使用 Elasticsearch 作为输出目标。 在本教程中,我们将其更改为 Logshtash。
将 Filebeat 设定为开机启动并启动。
sudo systemctl enable filebeat
sudo systemctl start filebeat
检查服务状态:
systemctl status filebeat
打开您的网络浏览器,并访问您在 Nginx 中配置的 elastic stack 域名,我的是“elk-stack.co”。 使用管理员密码登录,然后按 Enter 键登录 Kibana 仪表盘。
创建一个新的默认索引 filebeat-*,然后点击“创建”按钮。
默认索引已创建。 如果 elastic stack 上有多个 beat,您可以在“星形”按钮上点击一下即可配置默认 beat。
转到 “发现” 菜单,您就可以看到 elk-client1 和 elk-client2 服务器上的所有日志文件。
来自 elk-client1 服务器日志中的无效 ssh 登录的 JSON 输出示例。
使用其他的选项,你可以使用 Kibana 仪表盘做更多的事情。
Elastic Stack 已安装在 CentOS 7 服务器上。 Filebeat 已安装在 CentOS 7 和 Ubuntu 客户端上。

centos下git报错Failed to connect to repository : Command "git ls-remote -h HEAD" returned sta

Failed to connect to repository : Command "git ls-remote -h HEAD" returned status code 128:
stdout:
stderr: fatal: repository 'http://ytgit.chinasoft.cn/chinasoft/innerapi.git/' not found
# 通过在git 服务器上进行clone项目报错如下
# git clone http://ytgit.chinasoft.cn/chinasoft/innerapi.git
Cloning into 'innerapi'...
fatal: repository 'http://ytgit.chinasoft.cn/chinasoft/innerapi.git/' not found
# git clone git@ytgit.chinasoft.cn:chinasoft/innerapi.git
Cloning into 'innerapi'...
GitLab: The project you were looking for could not be found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
经过搜索发现是本地git配置和仓库里的 .git/config 不一致
# 远程仓库配置
# more maxserver/.git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://ytgit.chinasoft.cn/chinasoft/maxserver.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
本地配置:
# git config --list
user.password=chinasoft1234
user.email=chengjn@chinasoft.cn
credential.helper=store
# 修改本地配置后问题解决
[root@sz_yt_jenkins01_12_99 ~]# cat .gitconfig
[user]
    #password = chinasoft1234
    #email = chengjn@chinasoft.cn
[credential]
    helper = store
[root@sz_yt_jenkins01_12_99 ~]# cat .git-credentials
http://yt_release_deployment:SgKHNzqV5gGDhXs@ytgit.chinasoft.cn
#http://chengjn:chinasoft1234@ytgit.chinasoft.cn
#http://chengjn:chinasoft1234@ytgit.chinasoft.cn

以上是关于如何在CentOS 7上安装Elastic Stack的主要内容,如果未能解决你的问题,请参考以下文章

Elastic Stack:Centos7安装Kibana7.7.1以及开机自启

Elastic:在 CentOS 上一步一步安装 Elastic Stack

Elastic:在 CentOS 上一步一步安装 Elastic Stack

Centos 7 安装 ELK 5.6.8 及基础的配置

Mac上制作Centos7系统U盘安装盘

Elastic-search在linux上的安装