自动化运维-ELK的部署实施

这是我参与11月更文挑战的第2天,活动详情链接查看:2021最后一次更文挑战

一、简介:

ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana

1、Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。

它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。

2、Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。

一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。

3、Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。

4、FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。

二、环境准备

1、本地DNS解析

1
2
3
4
bash复制代码cat /etc/hosts

192.168.100.100 node1.stu.com
192.168.100.10 node2.stu.com

2、文件描述符

1
2
3
4
5
ini复制代码/etc/systemd/system.conf
/etc/systemd/user.conf

DefaultLimitNOFILE=65536
DefaultLimitNPROC=65536

3、时间同步

1
2
3
javascript复制代码yum install ntpdate -y

*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com

三、ELASTICSEARCH

1、安装:


yum install -y java-1.8.0-openjdk
wget artifacts.elastic.co/downloads/e…
rpm -ivh elasticsearch-6.4.0.rpm

2、配置

1
2
3
4
5
6
7
8
9
10
11
12
yaml复制代码vim /etc/elasticsearch/elasticsearch.yml


cluster.name: elk-cluster
node.name: node1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
LimitMEMLOCK=infinity
network.host: 192.168.100.100
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.100.100", "192.168.100.10"]

bootstrap.memory_lock: true 服务启动的时候锁定足够的内存,防止数据写入swap
LimitMEMLOCK=infinity(无限)
与集群中其他节点的传输端口为9300,接受HTTP请求的端口为9200

1
2
3
4
5
6
js复制代码vim /etc/elasticsearch/jvm.options
-Xms1g
-Xmx1g

systemctl start elasticsearch
systemctl enable elasticsearch

3、测试

http://192.168.100.1:9200/

四、logstash

1、安装:

yum install java-1.8.0-openjdk.x86_64 -y
wget artifacts.elastic.co/downloads/l…
rpm -ivh logstash-6.4.0.rpm

2、配置

搜集系统内核日志:chmod 644 /var/log/messages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ini复制代码vim /etc/logstash/conf.d/syslog.conf
input {
file {
path => "/var/log/messages"
type => "systemlog"
start_position => "beginning"
stat_interval => "2"
}
}

output {
elasticsearch {
hosts => ["192.168.100.100:9200"]
index => "logstash-systemlog-%{+YYYY.MM.dd}"
}
}

检查配置文件语法是否错误:/usr/share/logstash/bin/logstash -tf /etc/logstash/conf.d/syslog.conf

systemctl start logstash
systemctl enable logstash

3、验证:curl -XGET ‘localhost:9600/?pretty’

9600端口:API来检索有关Logstash的运行时指标

五、kibana

1、安装:

rpm -ivh kibana-6.4.0-x86_64.rpm

2、配置

1
2
3
4
5
6
7
8
bash复制代码vim /etc/kibana/kibana.yml

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

systemctl start kibana
systemctl enable kibana

3、验证

http://192.168.100.100:5601/status

4、nginx+kibana的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
ini复制代码 
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name kibana.stu.com;
auth_basic "kibana auth password";
auth_basic_user_file /etc/nginx/kibana.auth;
#root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://kibana;
}
}
upstream kibana {
server 192.168.100.1:5601 weight=1 max_fails=2 fail_timeout=2;
}
}

总结: 关于ELK的安装部署非常简单,在日常的工作中,用的也比较多,值得花时间去学习。

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%