ELK介绍及elsticsearch 集群部署(小节1)

141 阅读2分钟

Elastic中文官网: www.elastic.co/cn/

Elasticsearch中文官方下载页面:www.elastic.co/cn/download…

Kibana中文官方下载页面:www.elastic.co/cn/download…

一、elasticsearch部署

1.1 环境初始化

最小化安装 Ubuntu 操作系统的虚拟机,vcpu2,内存4G或更多,操作系统50G,两块网卡(NAT和仅主机),主机名设置为hostX.elk.com,其中host1和host2为elasticsearch服务器,为保证效果特额外添加一块单独的数据磁盘大小为50G并格式化挂载到 /data 目录。

host1(101)和host2(102)

安装openjdk

apt install openjdk-8-jdk -y

安装

安装包:elasticsearch-6.8.3.deb

cd /usr/local/src/

dpkg -i elasticsearch-6.8.3.deb

host1(101)

配置文件所在位置

vim /etc/elasticsearch/elasticsearch.yml

#elasticsearch集群名称
cluster.name: linux01

#node节点(名称不要和其他node一致)
node.name: node1

#elasticsearch数据目录
path.data: /esdata/data

#elasticsearch日志目录
path.logs: /esdata/logs

#内存目录在/etc/elasticsearch/jvm.options
#bootstrap.memory_lock: true

#监听地址
network.host: 192.168.37.101

#端口
http.port: 9200

#组播方式【只在两个主机之间发布(可写域名)】
discovery.zen.ping.unicast.hosts: ["192.168.37.101", "192.168.37.102"]

传文件到对方主机

scp /etc/elasticsearch/elasticsearch.yml 192.168.37.102:/etc/elasticsearch/

host2(102)

配置文件所在位置

vim /etc/elasticsearch/elasticsearch.yml

#node节点(名称不要和其他node一致)
node.name: node2

#监听地址
network.host: 192.168.37.102

查看id

id elasticsearch 
uid=111(elasticsearch) gid=115(elasticsearch) groups=115(elasticsearch)

host1(101)和host2(102)

创建目录挂磁盘

mkdir /esdata

可以看到没一块50G的硬盘

lsblk |grep 50
sdb      8:16   0   50G  0 disk 

格式化硬盘

mkfs.xfs /dev/sdb

挂载(开机自动)、推荐使用UUID挂载、使用 blkid /dev/sdb 可以得到UUID。

vim /etc/fstab
#添加下行
/dev/sdb    /esdata    xfs    defaults    0    0

挂载

mount -a

查看是否挂载

df -TH|grep esdata
/dev/sdb       xfs        54G   88M   54G   1% /esdata

启动脚本、查看权限

cat /usr/lib/systemd/system/elasticsearch.service
...
User=elasticsearch    <--
Group=elasticsearch    <--

递归修改权限

chown elasticsearch.elasticsearch /esdata/ -R

设为开机启动、启动服务

systemctl enable elasticsearch

systemctl start elasticsearch

浏览器测试

图片.png

图片.png

1.2 安装elasticsearch插件head

host1(101)和host2(102)

需要安装Docker

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

elasticsearch-head下载地址:github.com/mobz/elasti…

安装包:elasticsearch-head

cd /usr/local/src

#导入
docker load -i elasticsearch-head-5.tar.gz
...中间内容省略
d09533ddfc0d: Loading layer  143.3MB/143.3MB
eb415bbb4658: Loading layer  23.71MB/23.71MB
Loaded image: mobz/elasticsearch-head:5    <--

host1(101)

运行

docker run -d -p 9100:9100 mobz/elasticsearch-head:5

host1(101)和host2(102)

修改elasticsearch服务配置文件

vim /etc/elasticsearch/elasticsearch.yml
#最后面添加、以下两行
http.cors.enabled: true
http.cors.allow-origin: "*"

重启服务

systemctl restart elasticsearch

图片.png

由于宿主机内存不足,系统内核会将占用内存最大的进程强行kill掉,以保证系统的正常运行以及其他服务的正常运行。

1.3 监控elasticsearch集群状态

host1(101)

curl -sXGET http://192.168.37.101:9200/_cluster/health?pretty=true
{
  "cluster_name" : "linux01",
  "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.0
}

Python脚本

vim es_monitor.sh

#!/usr/bin/env python
#coding:utf-8

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
import subprocess

body = ""
false="false"
#注意IP地址
obj = subprocess.Popen(("curl -sXGET http://192.168.37.101:9200/_cluster/health?pretty=true"),shell=True, stdout=subprocess.PIPE)
data =  obj.stdout.read()
data1 = eval(data)
#取那个值、如'status'
status = data1.get("status")
#判断'status'是否为绿色,是返回值为50、不是则返回100
if status == "green":
    print("50")
else:
    print("100")

查看返回值是否为50

python3 es_monitor.sh
50    <--