- 一、配置node节点
- 1、更改几台服务器名字(方便识别)
- 2、安装elasticsearch
- 3、安装Elasticsearch-head插件与phantomjs
- 二、配置apache节点
- 1、安装logstash
- 2、修改配置文件
- 三、可视化数据
- 1、安装kibana
- 2、查看apache主机的日志文件
服务器1:hostnamectl set-hostname node1
服务器2:hostnamectl set-hostname node2
服务器3:hostnamectl set-hostname apache
完成之后,再进行刷新一下,看看是否成功
vim /etc/hosts #进入配置文件,添加主机名与IP地址,进行绑定 192.168.75.51 node1 192.168.75.52 node2 192.168.75.53 apache #配置完成之后,使用ping指定的主机名,查看是否能联通2、安装elasticsearch
java -version #检查当前的JAVA环境 #openjdk version "1.8.0_131" #OpenJDK Runtime Environment (build 1.8.0_131-b12) #OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode) rpm -ih elasticsearch-5.5.0.rpm #rpm安装程序
elasticsearch在安装成功之后,会在/etc/elasticsearch/文件下,产生几个目录以及配置文件,需要对elasticsearch.yml,配置文件进行修改
cd /etc/elasticsearch/
cp elasticsearch.yml{,.bak} #对配置文件先进行备份,以防后面配置出现问题,好用于恢复,并且对其重命名
加载守护进程
systemctl daemon-reload systemctl enable elasticsearch.service
完成之后,进行对配置文件进行修改
vim elasticsearch.yml
17 cluster.name: my-elk-cluster #修改集群的名字 23 node.name: node1 #开启之后,配置为对应的主机名即可 33 path.data: /data/elk_data #指定数据目录 37 path.logs: /var/log/elasticsearch #指定日志目录 43 bootstrap.memory_lock: false #前端框架的内存所,取消 55 network.host: 0.0.0.0 #监听所有的地址 59 http.port: 9200 #监听端口 68 discovery.zen.ping.unicast.hosts: ["node1", "node2"] #以单播的形式发现集群中的成员地址 完成之后,再检查一遍 grep -v "^#" ./elasticsearch.yml cluster.name: my-elk-cluster node.name: node1 path.data: /data/elk_data path.logs: /var/log/elasticsearch bootstrap.memory_lock: false network.host: 0.0.0.0 http.port: 9200 discovery.zen.ping.unicast.hosts: ["node1", "node2"]
mkdir -p /data/elk_data #创建上面指定的数据目录 chown elasticsearch.elasticsearch /data/elk_data/ #更改目录的属主属组权限 systemctl enable --now elasticsearch.service #启动服务 netstat -ntap|grep 9200 #查看端口,是否启动
http://192.168.75.51(52):9200/ #查看节点的信息
{
"name" : "node1",
"cluster_name" : "my-elk-cluster",
"cluster_uuid" : "GIkCf1XXT7qVqBCNElrSUg",
"version" : {
"number" : "5.5.0",
"build_hash" : "260387d",
"build_date" : "2017-06-30T23:16:05.735Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
http://192.168.75.51:9200/_cluster/health?pretty #查看集群的信息
cluster_name "my-elk-cluster"
status "green"
timed_out false
number_of_nodes 2
number_of_data_nodes 2
active_primary_shards 0
active_shards 0
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
curl http://192.168.75.51:9200/_cluster/stats?pretty #查看详细点的集群状态
3、安装Elasticsearch-head插件与phantomjs
Elasticsearch在5.0 版本后,Elasticsearch-head插件需要作为独立服务进行安装,需要使用npm.工具(Wodeas的包管理T具)安装。
yum install gcc gcc-c++ make -y #安装依赖环境
解压node并且编译安装
tar zxf node-v8.2.1.tar.gz cd node-v8.2.1/ ./configure make -j2 && make install
安装phantomjs
tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/src/ #解压安装包,并且指定安装目录 cd /usr/local/src/phantomjs-2.1.1-linux-x86_64/ ln -s /usr/local/src/phantomjs-2.1.1-linux-x86_64/bin/* /usr/local/bin/ #做个软连接到缓存命令目录,方便后面使用
安装Elasticsearch-head
tar zxvf elasticsearch-head.tar.gz -C /usr/local/src/ #解压文件到指定目录 cd /usr/local/src/elasticsearch-head/ npm install #安装程序 vi /etc/elasticsearch/elasticsearch.yml #进入配置文件,在末行添加下面的语句 http.cors.enabled: true #开启跨域访问支持,默认是false http.cors.allow-origin: "*" #指定跨域访问允许的域名地址为所有
systemctl restart elasticsearch.service #重启服务 systemctl status elasticsearch.service #查看服务信息 ss -natp | grep 9200 #查看端口信息
启动服务器
cd /usr/local/src/elasticsearch-head/ #必须到安装目录下启动服务,不然可能会导致服务启动失败 npm run start & #启动服务,并且将其放置到后台运行
上面语句执行之后,会使用到9100端口,可以先进行查看此端口信息,查看语句这里就略过了
执行完成之后,可以去客户机网站上测试一下网址
http://192.168.75.51:9100/ #登录上之后,my-elk-cluster这一行的那个集群的地址显示的位localhost:9200 ,需要修改为自己集群的IP地址加9100
现在还没有被访问过,所以现在显示的是没有访问记录
curl -X PUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
#添加一个访问记录
添加完成之后,去刷新页面,可以发现,现在已经改变了,是一个副本,五个分片,在数据浏览页面可以看到,刚刚我们添加的访问记录
先安装环境配置
yum install httpd -y
rpm -ivh logstash-5.5.1.rpm #rpm安装软件 cd /usr/share/logstash/ #移动到程序的安装目录 ln -s /usr/share/logstash/bin/logstash /usr/local/bin/ #做个软连接到缓存命令目录,方便后面使用
systemctl start httpd #启动httpd netstat -natp | grep 80 #查看是否开启
格式转换
logstash -e 'input { stdin{} } output { stdout{} }'
#输入采用的是标准输入,转换为另一种标准输出显示
logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
#输入结果www.baidu.com
{
"@timestamp" => 2022-01-03T10:57:30.972Z,
"@version" => "1",
"host" => "apache",
"message" => "www.baidu.com"
}
#将输入的网址的格式设置成Rubydebug的格式显示,目的是为了让下一个组件来进行接收。
2、修改配置文件
修改文件的权限
chmod o+r /var/log/messages #添加其他用户读取权限
自定义一个文件,来定义抓取和输出的对象
vim system.conf
input {
#定义一个抓取对象
file{
#抓取的文件形式
path => "/var/log/messages"
#位置为系统日志
type => "system"
#日志类型
start_position => "beginning"
#从最开始的位置开始
}
}
output {
#输出
elasticsearch {
#输出到es数据库
hosts => ["192.168.75.51:9200"]
#输出的服务器位置
index => "system-%{+YYYY.MM.dd}"
#输出的索引以及时间戳
}
}
systemctl restart logstash.service #重启一下
重启之后,回到网站进行查看:
索引,数据浏览等等都有增加新的记录。就不一一截图了
三、可视化数据 1、安装kibana在node1主机上安装kibana
rpm -ivh kibana-5.5.1-x86_64.rpm cd /etc/kibana/ cp kibana.yml kibana.yml.bak #复制一份配置文件,备用
vim /etc/kibana/kibana.yml #进入配置文件,打开下面命令 2 server.port: 5601 7 server.host: "0.0.0.0" #开启之后设置位监听所有 21 elasticsearch.url: "http://192.168.75.51:9200" #kibana和es对接的地方 30 kibana.index: ".kibana" #开启kibana自身的索引
开启kibana
systemctl start kibana.service systemctl enable kibana.service
访问kibana
http://192.168.75.51:5601/
输入完成之后,点击create
点击发现,来看发现的数据源
2、查看apache主机的日志文件在apache服务器上进行配置
cd /etc/logstash/conf.d/ vim apache_log.conf
input {
file{
path => "/etc/httpd/logs/access_log"
type => "access"
start_position => "beginning"
}
file{
path => "/etc/httpd/logs/error_log"
type => "error"
start_position => "beginning"
}
}
output {
if [type] == "access" {
elasticsearch {
hosts => ["192.168.75.51:9200"]
index => "apache_access-%{+YYYY.MM.dd}"
}
}
if [type] == "error" {
elasticsearch {
hosts => ["192.168.75.51:9200"]
index => "apache_error-%{+YYYY.MM.dd}"
}
}
}
apache日志收集开启之后,再进行下面的操作
/usr/share/logstash/bin/logstash -f apache_log.conf #指定logstash去收集apache的日志
再回到页面之后,就可以看到,日志信息里面已经有apache的日志记录了



