一、ELK平台搭建准备
1.1 平台环境:
OS:CentOS release 6.4(Final)
ElasticSearch:6.3.2
Logstash:6.3.2
Kibana:6.3.2
JRE:1.8
 
注:由于Logstash的运行依赖于Java环境, 而Logstash 1.5以上版本不低于java 1.7,因此推荐使用最新版本的Java。因为我们只需要Java的运行环境,所以可以只安装JRE,不过这里我依然使用JDK
 
1.2 ELK下载:https://www.elastic.co/downloads/
 
cd /data/package/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.2.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.3.2.tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.3.2-linux-x86_64.tar.gz
1.3 安装准备
 
#配置iptables,保证内网之间可以互通
[root@Elk_Server]# iptables -F;iptables -I INPUT -s 192.168.0.0/16 -j ACCEPT
[root@Elk_Server]# service iptables save;service iptables restart   
#关闭selinux
[root@Elk_Server]# setenforce 0  
[root@Elk_Server]# vim /etc/sysconfig/selinux 
SELINUX=disabled
1.4 时间同步
 
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum install ntpdate -y;ntpdate time.windows.com
echo '01 00 * * * ntpdate time.windows.com' >>/etc/crontab
1.5 配置yum源
 
#导入公钥
[root@Elk_Server ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
 
[root@Elk_Server ~]# vim /etc/yum.repos.d/elasticsearch.repo
 
[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
1.6 安装JAVA 1.8.0_151, 请见 https://www.linuxidc.com/Linux/2019-08/159733.htm
 
二、安装Elasticsearch单机版集群
ASP站长网这个其实也是ELK中的核心,启动的时候一定要注意,因为es不可以进行root账户启动,所以你还需要开启一个elsearch账户。**
 
2.1.1 yum方式安装
 
[root@Elk_Server ~]# sudo yum install elasticsearch -y
2.1.2 源码安装(本文为编译安装配置)
 
ASP站长网#将解压后的文件复制三份
tar zxvf elasticsearch-6.3.2.tar.gz -C /usr/local/
mv /usr/local/elasticsearch-6.3.2 /usr/local/es-master
cp -rf /usr/local/es-master /usr/local/es-data1
cp -rf /usr/local/es-master /usr/local/es-data2
 
groupadd elasticsearch
useradd elasticsearch -g elasticsearch
2.2 创建elasticsearch数据目录
 
mkdir -p /data{,1,2}/elasticsearch/data
mkdir -p /data{,1,2}/elasticsearch/logs
chown -R elasticsearch:elasticsearch /data{,1,2}/elasticsearch
2.3 编辑Elasticsearch集群配置文件
 
cp /usr/local/es-master/config/elasticsearch.yml /usr/local/es-master/config/elasticsearch.yml-bak
#创建软连接
mkdir /etc/elasticsearch/
ln -s /usr/local/es-master/config/elasticsearch.yml /etc/elasticsearch/es-master.yml
ln -s /usr/local/es-data1/config/elasticsearch.yml /etc/elasticsearch/es-data1.yml
ln -s /usr/local/es-data2/config/elasticsearch.yml /etc/elasticsearch/es-data2.yml
主节点配置:
 
vim /etc/elasticsearch/es-master.yml
 
# ======================== Elasticsearch Configuration =========================
#
cluster.name: elk-cluster
node.name: node-master
node.attr.rack: r1
 
# ----------------------------------- Paths ------------------------------------
#设置data存放的路径为/data/elasticsearch/data
path.data: /data/elasticsearch/data
#设置logs日志的路径为/data/elasticsearch/logs
path.logs: /data/elasticsearch/logs
 
# ---------------------------------- Network -----------------------------------
#本机地址
network.host: 192.168.6.108
#开启监听的端口为9200
http.port: 9200
#tcp通讯端口
transport.tcp.port: 9330
#是否作为主机
node.master: true
#是否作为数据节点
node.data: false
node.max_local_storage_nodes: 3
 
#在删除索引时,是否需要明确指定名称.该值为false时,则可通过正则或_all删除: 
action.destructive_requires_name: true
 
# --------------------------------- Discovery ----------------------------------
#集群信息,默认的通讯接口是9300
discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"]
 
#在新集群搭建初期,总会出现某几个节点与其他节点通信异常导致节点频繁加入、退出集群。这个过程是自动执行的。通过配置discovery.zen.ping_timeout来控制节点加入某个集群或者开始选举的响应时间(默认3s)。
discovery.zen.ping_timeout: 60s
 
#这个关闭了自动创建索引。为的也是安全考虑,否则即使是内网,也有很多扫描程序,一旦开启,扫描程序会自动给你创建很多索引。
action.auto_create_index: false
 
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
 
#设置集群中N个节点启动时进行数据恢复,默认为1。
gateway.recover_after_nodes: 2
#设置初始化数据恢复进程的超时时间,默认是5分钟。
gateway.recover_after_time: 3m
#设置集群中节点的数量,默认为2,一旦这N个节点启动,就会立即进行数据恢复。
gateway.expected_nodes: 3
 
# ---------------------------------- Various -----------------------------------
#开启跨域访问支持,默认为false
http.cors.enabled: true
#跨域访问允许的域名地址,(允许所有域名)以上使用正则
http.cors.allow-origin: /.*/  
数据1节点配置:
 
vim /etc/elasticsearch/es-data1.yml
 
# ======================== Elasticsearch Configuration =========================
#
cluster.name: elk-cluster
node.name: node-data1
node.attr.rack: r1
 
# ----------------------------------- Paths ------------------------------------
#设置data存放的路径为/dat1/elasticsearch/data
path.data: /data1/elasticsearch/data
#设置logs日志的路径为/data1/elasticsearch/logs
path.logs: /data1/elasticsearch/logs
 
# ---------------------------------- Network -----------------------------------
#本机地址
network.host: 192.168.6.108
#开启监听的端口为9201
http.port: 9201
#tcp通讯端口
transport.tcp.port: 9331
#是否作为主机
node.master: false
#是否作为数据节点
node.data: true
node.max_local_storage_nodes: 3
 
#在删除索引时,是否需要明确指定名称.该值为false时,则可通过正则或_all删除: 
action.destructive_requires_name: true
 
# --------------------------------- Discovery ----------------------------------
#集群信息,默认的通讯接口是9300
discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"]
 
discovery.zen.ping_timeout: 60s
 
action.auto_create_index: false
 
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
 
#设置集群中N个节点启动时进行数据恢复,默认为1。
gateway.recover_after_nodes: 2
#设置初始化数据恢复进程的超时时间,默认是5分钟。
gateway.recover_after_time: 3m
gateway.expected_nodes: 3
 
# ---------------------------------- Various -----------------------------------
#开启跨域访问支持,默认为false
http.cors.enabled: true
#跨域访问允许的域名地址,(允许所有域名)以上使用正则
http.cors.allow-origin: /.*/  
数据2节点配置:
 
vim /etc/elasticsearch/es-data2.yml
 
# ======================== Elasticsearch Configuration =========================
#
cluster.name: elk-cluster
node.name: node-data2
node.attr.rack: r1
 
# ----------------------------------- Paths ------------------------------------
#设置data存放的路径为/data2/elasticsearch/data
path.data: /data2/elasticsearch/data
#设置logs日志的路径为/data2/elasticsearch/logs
path.logs: /data2/elasticsearch/logs
 
# ---------------------------------- Network -----------------------------------
#本机地址
network.host: 192.168.6.108
#开启监听的端口为9202
http.port: 9202
#tcp通讯端口
transport.tcp.port: 9332
#是否作为主机
node.master: false
#是否作为数据节点
node.data: true
node.max_local_storage_nodes: 3
 
#在删除索引时,是否需要明确指定名称.该值为false时,则可通过正则或_all删除: 
action.destructive_requires_name: true
 
# --------------------------------- Discovery ----------------------------------
#集群信息,默认的通讯接口是9300
discovery.zen.ping.unicast.hosts: ["192.168.6.108:9330", "192.168.6.108:9331","192.168.6.108:9332"]
 
discovery.zen.ping_timeout: 60s
 
#这个关闭了自动创建索引。为的也是安全考虑,否则即使是内网,也有很多扫描程序,一旦开启,扫描程序会自动给你创建很多索引。
action.auto_create_index: false
 
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
 
#设置集群中N个节点启动时进行数据恢复,默认为1。
gateway.recover_after_nodes: 2
#设置初始化数据恢复进程的超时时间,默认是5分钟。
gateway.recover_after_time: 3m
gateway.expected_nodes: 3
 
# ---------------------------------- Various -----------------------------------
#开启跨域访问支持,默认为false
http.cors.enabled: true
#跨域访问允许的域名地址,(允许所有域名)以上使用正则
http.cors.allow-origin: /.*/  
使用外网搭建集群时需注意:
network.host需要修改为0.0.0.0,同时要暴露你的外网地址,代码如下:
 
network.host: 0.0.0.0
network.publish_host: xx.xx.xx.xx
discovery.zen.ping.unicast.hosts修改为各个服务器的外网地址和通讯端口即可。
 
2.4 安装head插件
head插件能够生成集群的统计数据,并提供浏览器查询,同时还能对elasticsearch索引进行结构化查询。
 
cd /usr/local/es-master/
mkdir head &&cd head
git clone https://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head/
#install node v7.x
curl --silent --location https://rpm.nodesource.com/setup_7.x | bash -
sudo yum install nodejs
npm config set registry=http://registry.npm.taobao.org
npm install    ##运行缓慢
npm install -g grunt-cli
##修改配置文件
[root@Elk_Server elasticsearch-head]# vim Gruntfile.js 
       connect: {
                        server: {
                                options: {
                                        port: 9100,
                                        hostname: '192.168.6.108',   ##添加这行,冒号后面有空格
                                        base: '.',
                                        keepalive: true
                                }
                        }
                }
2.5 系统参数调整
 
##修改es三个节点的JVM运行内存,这个可以根据需要更改,但是-Xms和-Xmx的值必须一样,不然启动报错
[root@Elk_Server ]# vim /usr/local/es-master/config/jvm.options
[root@Elk_Server ]# vim /usr/local/es-data1/config/jvm.options
[root@Elk_Server ]# vim /usr/local/es-data2/config/jvm.options
-------------------------------------------------------------------------
-Xms2g
-Xmx2g
 
##修改Linux最大打开文件数
vim /etc/security/limits.conf
* soft nofile 655350   ##在末尾添加
* hard nofile 655350
##这一步需要重启linux使配置生效
reboot
[root@Elk_Server~]# ulimit -n
655350
 
##修改sysctl.conf文件
[root@Elk_Server ~]# vim /etc/sysctl.conf 
vm.max_map_count=655350
[root@Elk_Server ~]# sysctl -p
2.6 将ES的head插件启动
 
cd  /usr/local/es-master/head/elasticsearch-head && grunt server &
 
[3] 5946
##会开启9100端口。
2.7 启动Elasticsearch(ES只能使用普通用户启动)
 
chown -R elasticsearch:elasticsearch /usr/local/es-*
chown -R elasticsearch:elasticsearch /data{,1,2}/elasticsearch
##切换到普通用户启动elasticsearch
su - elasticsearch
nohup  /usr/local/es-master/bin/elasticsearch &           # -d或&以后台的方式进行启动Elasticsearch
[1] 6202
##启动成功是会开启9200,9300两个端口的。
##传输端口为9300接受http请求的端口为9200
##在浏览器打开可以利用head插件查看elasticsearch的集群及索引情况
2.8 主节点启动好后,用同样的方法启动子节点就可以了。
如果报错,解决方式: https://www.linuxidc.com/Linux/2019-08/159734.htm
 
2.9 验证启动
 
ps axu |grep elasticsearch
 
方法一:
curl 'http://192.168.6.108:9200/_search?pretty'
 
-----
 
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : 0.0,
    "hits" : [ ]
  }
}
 
方法二:
//或者通过浏览器访问:http://192.168.6.108:9200/
#检查es版本信息
curl -u <user>:<passwd> http://192.168.6.108:9200
 
#此时观察ES集群状态:
curl http://192.168.6.108:9200/_cluster/health?pretty
 
#观察集群内各索引状态:
curl http://192.168.6.108:9200/_cat/indices?pretty
 
#查询elasticsearch节点状态:
curl -XGET http://192.168.6.108:9200/_cat/shards |grep UNASSIGNED
 
#查看节点列表
http://IP:9200/_cat/nodes?v
curl 'IP:9200/_cat/nodes?v'
 
#列出所有索引及存储大小
http://IP:9200/_cat/indices?v
curl 'IP:9200/_cat/indices?v'--查询所有索引及数据大小
 
#创建索引
创建索引名为XX,默认会有5个分片,1个索引
curl -XPUT 'IP:9200/XX?pretty'
 
#添加一个类型
curl -XPUT 'IP:9200/XX/external/2?pretty' -d '
{
   "gwyy": "John"
}'
 
#更新一个类型
curl -XPOST 'IP:9200/XX/external/1/_update?pretty' -d '
{
   "doc": {"name": "Jaf"}
}'
 
#删除指定索引
curl -XDELETE 'IP:9200/_index?pretty'

dawei

【声明】:九江站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。