ELK-ElasticSearch安装

1、环境准备

1.1、主机准备

Ubuntu操作系统  内存:4G   CPU:1核
es 192.168.10.25
es-bak 192.168.10.21

1.2、同步时间

# 设置时区
timedatectl set-timezone Asia/Shanghai

# 同步系统时间
apt install -y ntpdate cron
systemctl start cron
systemctl enable cron

cat << 'CAT_END' > /var/spool/cron/crontabs/root 
#ntp Server update
*/5 * * * * /usr/sbin/ntpdate ntp5.aliyun.com  2>&1 > /dev/null
#ntp end
CAT_END

ntpdate ntp5.aliyun.com

1.3、设置主机名

hostnamectl set-hostname es

2、安装JDK

2.1、准备软件

# 这里使用此版本
# openjdk-11.0.0.1_linux-x64_bin.tar.gz

mkdir /data/{softs,server} -p && cd /data/softs
curl -O https://download.java.net/openjdk/jdk11.0.0.1/ri/openjdk-11.0.0.1_linux-x64_bin.tar.gz

# es自带jdk,位置:/usr/share/elasticsearch/jdk

2.2、安装JDK软件

mkdir /data/{softs,server} -p

cd /data/softs
tar xf openjdk-11.0.0.1_linux-x64_bin.tar.gz -C /data/server/
ln -s /data/server/jdk-11.0.0.1 /usr/local/java

cat << 'CAT_END' >> /etc/profile
export JAVA_HOME=/usr/local/java
export ES_JAVA_HOME=/usr/share/elasticsearch/jdk
export PATH=$JAVA_HOME/bin:$PATH
CAT_END
source /etc/profile
java -version

3、ElasticSearch安装

# 这里推荐 两台服务器来部署 elasticsearch

3.1、方式1:yum、apt配置仓库安装

3.1.1、yum安装参考官网

3.1.2、apt安装参考官网

https://www.elastic.co/guide/en/elasticsearch/reference/7.17/deb.html#deb-repo
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg sudo apt-get install apt-transport-https echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-7.x.list sudo apt-get update && sudo apt-get install elasticsearch

3.2、方式2:下载.deb包离线安装

cd /data/softs/
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.10-amd64.deb
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.10-amd64.deb.sha512
shasum -a 512 -c elasticsearch-7.17.10-amd64.deb.sha512 
dpkg -i elasticsearch-7.17.10-amd64.deb

3.3、配置es环境变量

cat << 'CAT_END' > /etc/profile.d/elasticsearch.sh
export PATH=/usr/share/elasticsearch/bin/:$PATH
CAT_END
source /etc/profile.d/elasticsearch.sh

3.4、配置文件位置解析

# dpkg -L elasticsearch | grep -Ei 'etc|service'
/usr/share/elasticsearch/bin/elasticsearch-service-tokens
/etc
/etc/elasticsearch
/etc/elasticsearch/elasticsearch-plugins.example.yml
/etc/elasticsearch/elasticsearch.yml                 # 核心配置文件
/etc/elasticsearch/jvm.options                       # jvm相关的配置
/etc/elasticsearch/role_mapping.yml
/etc/elasticsearch/roles.yml
/etc/elasticsearch/users
/etc/elasticsearch/users_roles
/etc/elasticsearch/log4j2.properties                 # 日志相关的配置
/etc/default
/etc/default/elasticsearch                           # 环境变量配置文件
/usr/lib/systemd/system/elasticsearch.service        # 服务启动文件
/etc/init.d
/etc/init.d/elasticsearch
/etc/elasticsearch/jvm.options.d

4、配置ElasticSearch

4.1、es配置

root@es:~# grep -i '^[a-Z]' /etc/elasticsearch/elasticsearch.yml    
cluster.name: first.es.com
node.name: 192.168.10.25
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["192.168.10.25", "192.168.10.21"]
cluster.initial_master_nodes: ["192.168.10.25"]
http.cors.enabled: true
http.cors.allow-origin: "*"

4.2、es-bak配置

root@es-bak:~# grep -i '^[a-Z]' /etc/elasticsearch/elasticsearch.yml
cluster.name: first.es.com
node.name: 192.168.10.21
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["192.168.10.25", "192.168.10.21"]
cluster.initial_master_nodes: ["192.168.10.25"]
http.cors.enabled: true
http.cors.allow-origin: "*"

4.3、配置属性解析

# grep -i '^[a-Z]' /etc/elasticsearch/elasticsearch.yml    
cluster.name: frist.es.com           # 设定elasticsearch集群的名称
node.name: 192.168.10.21             # 设定节点名字
path.data: /var/lib/elasticsearch    # 设定elasticsearch的存储目录,包括数据目录和日志目录
path.logs: /var/log/elasticsearch    # 设定elasticsearch的存储目录,包括数据目录和日志目录
network.host: 0.0.0.0                # 允许所有主机都能访问我们的elasticsearch
http.port: 9200                      # 设置elasticsearch对外的访问端口
discovery.seed_hosts: ["192.168.10.25", "192.168.10.21"]   # 设定主机发现
cluster.initial_master_nodes: ["192.168.10.25"]            # 设置初始化主节点
http.cors.enabled: true              # 开启跨域访问支持,默认为false
http.cors.allow-origin: "*"          # 跨域访问允许的域名地址,(允许所有域名)以上使用正则


# 注意事项
对于 node 主机只需要更改一处 node.name 即可
如果是重新还原es集群,启动前将 path.data 和 path.logs 目录下的数据清空,避免数据不一致

5、启动服务并且检查

5.1、启动服务

systemctl start elasticsearch

5.2、检查端口

# ss -tunlp | grep java
tcp   LISTEN 0      4096                     *:9300            *:*    users:(("java",pid=1595,fd=287))         
tcp   LISTEN 0      4096                     *:9200            *:*    users:(("java",pid=1595,fd=315))  

5.3、浏览9200端口

root@es:~# curl 192.168.10.25:9200
{
  "name" : "192.168.10.25",
  "cluster_name" : "first.es.com",
  "cluster_uuid" : "7s6Z15FxToCpkqVeSa73KQ",
  "version" : {
    "number" : "7.17.10",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "fecd68e3150eda0c307ab9a9d7557f5d5fd71349",
    "build_date" : "2023-04-23T05:33:18.138275597Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

6、请求命令管理

6.1、查看集群状态

curl -XGET 192.168.10.25:9200/_cat/health
curl -XGET 192.168.10.25:9200/_cat/health?v
# 注意:在所有的地址后面,增加 ? 和 ?v ,会逐渐显示更多的详细内容

6.2、查看集群节点

curl -XGET 192.168.10.25:9200/_cat/nodes

6.3、索引管理

6.3.1、创建索引

curl -XPUT 192.168.10.25:9200/index_test

6.3.2、查看索引

curl -XGET 192.168.10.25:9200/_cat/indices

6.3.3、格式化显示索引

curl -XGET 192.168.10.25:9200/index_test?pretty

6.3.4、删除索引 

curl -XDELETE 192.168.10.25:9200/index_test

6.3.5、批量删除索引

for i_name in $(curl -XGET 192.168.10.25:9200/_cat/indices | awk -F' ' '{print $3}' | grep test);
do 
curl -XDELETE 192.168.10.25:9200/${i_name}; 
done

6.3.6、创建索引并且设置切片属性

curl -H 'Content-Type: application/json' -d '{
"settings" : {
"number_of_shards" : "3",
"number_of_replicas" : "1"
}
}' -X PUT 192.168.10.25:9200/index_test
# 注意:在设置切片属性的时候,一定要注意在历史数据中,最好不要存在同名的索引,否则报错

7、功能插件

7.1、插件管理简介

elasticsearch最擅长的场景就是索引的操作,而索引的使用场景在不同的公司非常不同,所以
elasticsearch的索引场景可以基于不同的插件来实现对应的功能,而插件也是ELK非常重要的属性。

elasticsearch提供了两种插件的管理方式:
  - 专用的插件管理可以使用命令 elasticsearch-plugin,它可以对默认的插件进行管理;
  - 定制的插件管理可以基于离线方式安装插件。
  
常见插件:
  分词插件:analysis-icu、analysis-ik等
  管理插件:head、kopf、bigdest等

注意:
  随着elasticsearch的版本更新,很多之前可以直接安装的插件,目前无法直接安装了,
  需要采取自定义的方式来安装

旧的插件地址:https://www.elastic.co/guide/en/elasticsearch/plugins/2.4/management.html
官方地址:https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html

7.2、基础命令

7.2.1、在线安装插件方式

elasticsearch-plugin install [plugin_name]/[version]

7.2.2、离线安装插件方式

方法1:elasticsearch-plugin install file:///path/to/plugin.zip
方法2:将下载的插件解压到elasticsearch的plugins目录即可

7.2.3、查看已安装插件

elasticsearch-plugin list

7.2.4、删除插件

elasticsearch-plugin remove [plugin_name]

# 注意:删除插件的时候,推荐先关闭结点,然后再关闭。

7.3、安装插件实战

7.3.1、安装中文语法分析后重启服务

elasticsearch-plugin install analysis-smartcn
elasticsearch-plugin install analysis-icu
systemctl restart elasticsearch.service

7.3.2、查看插件列表

elasticsearch-plugin list

7.3.3、查看插件安装目录

root@es:~# ll /usr/share/elasticsearch/plugins/
drwxr-xr-x 4 root root 4096 Jun  5 15:54 ./
drwxr-xr-x 7 root root 4096 Jun  5 12:41 ../
drwxr-xr-x 2 root root 4096 Jun  5 15:54 analysis-icu/
drwxr-xr-x 2 root root 4096 Jun  5 15:53 analysis-smartcn/

7.3.4、测试中文分词【icu_analyzer】

# curl -X POST 'http://192.168.10.25:9200/_analyze?pretty=true' -H 'content-type: application/json' -d '{"analyzer":"icu_analyzer","text":"中华人民共和国国歌"}'
-----输入结果
{
  "tokens" : [
    {
      "token" : "中华",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "人民",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "共和国",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "国歌",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    }
  ]
}

7.3.5、测试中文分词【smartcn】

root@es:~# curl -X POST 'http://192.168.10.25:9200/_analyze?pretty=true' -H 'content-type: application/json' -d '{"analyzer":"smartcn","text":"中华人民共和国国歌"}'
-----显示结果
{
  "tokens" : [
    {
      "token" : "中华人民共和国",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "国歌",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "word",
      "position" : 1
    }
  ]
}

7.4、head插件安装

7.4.1、简介

head插件,在elasticsearch中,应用的还算可以,但是自动2.x之后的版本,就不再支持了,需要我们自己来进行独立的部署。
代码资料:https://github.com/mobz/elasticsearch-head

7.4.2、安装npm和git

apt-get install -y aptitude
aptitude install libssl-dev
apt install npm git -y

7.4.3、安装head插件

mkdir /data/server/elasticsearch/plugins -p 
cd /data/server/elasticsearch/plugins
git clone git@gitee.com:githubd/elasticsearch-head.git
cd elasticsearch-head
npm config set registry https://registry.npm.taobao.org 
npm install --save core-js
npm install --force

# 配置软件的访问地址
#  vim /data/server/elasticsearch/plugins/elasticsearch-head/Gruntfile.js 
  connect: {
    server: {
      options: {
        hostname: '*', # 增加这行
        port: 9100,
        base: '.',
        keepalive: true
      }
    }
  }
});

启动服务
npm run start >>/dev/null 2>&1 &

7.4.4、检查端口

root@es:~# ss -tunlp | grep grunt
tcp   LISTEN 0      511                      *:9100            *:*    users:(("grunt",pid=10082,fd=18))  

7.4.5、访问9100页面

7.4.6、属性解析

一定要先保证连接按钮左侧的es地址是正确的,然后再来点击"连接"* 代表索引es的主节点,黑色加粗的方框0表示,索引的主副本。
黄色代表有从分片丢失,但是没有主分片数据丢失,即当前没有数据丢失。
红色代表主分配丢失,即有数据丢失;绿色代表所有主分片和从分片的数据正常

7.4.7、测试插入数据

{
  "username": "ygbh",
  "age": 18
}

# 返回结果
{
  "_index": "username",
  "_type": "ygbh",
  "_id": "D3ABi4gBr53uPqiLy0Eh",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}


# 属性解析:
_index:创建了一个索引
index _type:创建了一个类型text1 
total:分片2个
Successful:成功2个
Failed:失败0个 
Created:状态成功

# 注意: 我们可以在这里进行各种各样的编辑,"POST"的下拉框可以选择多种http操作属性,

7.4.8、创建systemd服务脚本

cat << 'CAT_END' > /lib/systemd/system/es-head.service
[Unit]
Description= elasticsearch head server project
[Service]
User=root
Type=simple
ExecStart=/bin/bash -c "cd /data/server/elasticsearch/plugins/elasticsearch-head && /usr/bin/npm run start >>/dev/null 2>&1"
ExecStop=/bin/kill -s SIGINT $MAINPID
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target 
CAT_END

# 启动服务
systemctl daemon-reload
systemctl start es-head.service
systemctl status es-head.service 

 

posted @ 2023-06-05 18:28  小粉优化大师  阅读(32)  评论(0编辑  收藏  举报