elasticsearch之二快速入门及安装

个人专题目录


快速入门

1. 安装包下载

  1. Elasticsearch官网: https://www.elastic.co/products/elasticsearch
  2. Elastic官网:https://www.elastic.co/cn/

2. 安装Elasticsearch(单节点Linux环境)

使用6.5.4,Elasticsearch6.x要求Linux内核必须是3.5+版本以上。

在linux操作系统中,查看内核版本的命令是: uname -a

Elasticsearch在Linux中安装部署的时候,需要系统为其提供若干系统配置。如:应用可启动的线程数、应用可以在系统中划分的虚拟内存、应用可以最多创建多少文件等。

Elasticsearch是java开发的应用。在6.8.4版本中,要求JDK至少是1.8.0_131版本以上。

出于安全考虑,elasticsearch默认不允许以root账号运行。

  1. 解压elasticsearch-6.5.4.tar.gz到/opt/module目录下
$ tar -zxvf elasticsearch-6.5.4.tar.gz -C /opt/module/
  1. 在/opt/module/elasticsearch-6.5.4路径下创建data和logs文件夹
$ mkdir data
$ mkdir logs
#因为安全问题,Elasticsearch 不允许root用户直接运行,所以要创建新用户,在root用户中创建新用户,执行如下命令
useradd elasticsearch  # 新增elasticsearch用户
passwd  elasticsearch  # 为elasticsearch用户设置密码
chown -R elasticsearch:elasticsearch /opt/module/ #文件夹所有者
  1. 修改配置文件/opt/module/elasticsearch-6.5.4/config/elasticsearch.yml
$ pwd
/opt/module/elasticsearch-6.5.4/config
vim jvm.options
-Xms1g
-Xmx1g
$ vi elasticsearch.yml
#配置elasticsearch的集群名称,默认是elasticsearch。建议修改成一个有意义的名称
# ---------------------------------- Cluster -----------------------------------
cluster.name: my-application
# ------------------------------------ Node ------------------------------------
#节点名,elasticsearch会默认随机指定一个名字,建议指定一个有意义的名称,方便管理
node.name: node-102
# ----------------------------------- Paths ------------------------------------
path.data: /opt/module/elasticsearch-6.5.4/data
path.logs: /opt/module/elasticsearch-6.5.4/logs
# ----------------------------------- Memory -----------------------------------
# 设置为true可以锁住ES使用的内存,避免内存与swap分区交换数据。
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
# ---------------------------------- Network -----------------------------------
#设置为0.0.0.0允许外网访问
network.host: 192.168.1.102 
# --------------------------------- Discovery ----------------------------------
discovery.zen.ping.unicast.hosts: ["hadoop102"]
#Elasticsearch的http访问端口
http.port: 9200
#初始化新的集群时需要此配置来选举master
cluster.initial_master_nodes: ["node-1"]

(1)cluster.name

如果要配置集群需要两个节点上的elasticsearch配置的cluster.name相同,都启动可以自动组成集群,这里如果不改cluster.name则默认是cluster.name=my-application,

(2)nodename随意取但是集群内的各节点不能相同

(3)修改后的每行前面不能有空格,修改后的“:”后面必须有一个空格

  1. 配置linux系统环境(参考:http://blog.csdn.net/satiling/article/details/59697916

​ (1)切换到root用户,编辑limits.conf 添加类似如下内容,是修改系统中允许应用最多创建多少文件等的限制权限。Linux默认来说,一般限制应用最多创建的文件是65535个。但是Elasticsearch至少需要65536的文件创建权限。

# vi /etc/security/limits.conf

添加如下内容:

* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096

*代表任意用户,soft表示内存中虚拟文件(软文件),hard表示落地到磁盘的具体文件(硬文件), nofile表示权限,65536表示个数。

(2)切换到root用户,进入limits.d目录下修改配置文件。

在CentOS6.5版本中编辑下述的配置文件

vim /etc/security/limits.d/90-nproc.conf

在CentOS7+版本中编辑配置文件是:

vim /etc/security/limits.conf

是修改系统中允许用户启动的进程开启多少个线程。默认的Linux限制root用户开启的进程可以开启任意数量的线程,其他用户开启的进程可以开启1024个线程。必须修改限制数为4096+。因为Elasticsearch至少需要4096的线程池预备。Elasticsearch在5.x版本之后,强制要求在linux中不能使用root用户启动Elasticsearch进程。所以必须使用其他用户启动Elasticsearch进程才可以。

# vim /etc/security/limits.d/90-nproc.conf
修改如下内容:
* soft nproc 1024
#修改为
* soft nproc 4096

*任何用户 nproc创建线程 数量4096

Linux低版本内核为线程分配的内存是128K。4.x版本的内核分配的内存更大。如果虚拟机的内存是1G,最多只能开启3000+个线程数。至少为虚拟机分配1.5G以上的内存,保险起见建议2G以上。

(3)切换到root用户修改配置sysctl.conf

系统控制文件是管理系统中的各种资源控制的配置文件。Elasticsearch需要开辟一个65536字节以上空间的虚拟内存。Linux默认不允许任何用户和应用直接开辟虚拟内存。

# vim /etc/sysctl.conf 
添加下面配置:最大虚拟内存太小,限制一个进程可以拥有的VMA(虚拟内存区域)的数量
vm.max_map_count=655360
并执行命令:
# sysctl -p

然后,重新启动elasticsearch,即可启动成功。

  1. 启动集群
$ bin/elasticsearch
  1. 测试集群
$ curl http://localhost:9200
{
  "name" : "L6WdN7y",
  "cluster_name" : "Elasticsearch",
  "cluster_uuid" : "s7_GSd9YQnaH10VQBKCQ5w",
  "version" : {
    "number" : "6.3.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "eb782d0",
    "build_date" : "2018-06-29T21:59:26.107521Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

curl http://localhost:9200/_cat/nodes 获取节点信息
curl http://localhost:9200/_cat/shards 获取分片信息
curl http://localhost:9200/_cat/indices 获取索引信息
curl http://localhost:9200/_cat/health?v 获取健康状态信息

$ curl http://localhost:9200/_cluster/health
查询集群状态
{
    "cluster_name": "elasticsearch",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 1,
    "number_of_data_nodes": 1,
    "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
}

Status:集群状态。Green 所有分片可用。Yellow所有主分片可用。Red主分片不可用,集群不可用。
  1. 停止集群

​ kill -9 进程号

3. 安装Elasticsearch(多节点集群Linux环境)

1554688271544.png
1554688294804.png
1554688390747.png

4. Elasticsearch head插件安装

  1. 下载插件

https://github.com/mobz/elasticsearch-head

elasticsearch-head-master.zip

  1. nodejs官网下载安装包

https://nodejs.org/dist/

node-v6.9.2-linux-x64.tar.xz

  1. 将elasticsearch-head-master.zip和node-v6.9.2-linux-x64.tar.xz都导入到linux的/opt/software目录。

  2. 安装nodejs

$ tar -zxvf node-v6.9.2-linux-x64.tar.gz -C /opt/module/
  1. 配置nodejs环境变量
# vi /etc/profile
export NODE_HOME=/opt/module/node-v6.9.2-linux-x64
export PATH=$PATH:$NODE_HOME/bin
# source /etc/profile 
  1. 查看node和npm版本
# node -v
v6.9.2 
# npm -v
3.10.9 
  1. 解压head插件到/opt/module目录下
$ unzip elasticsearch-head-master.zip -d /opt/module/
  1. 查看当前head插件目录下有无node_modules/grunt目录:

没有:执行命令创建:

$ npm install grunt --save
  1. 安装head插件:
]$ npm install -g cnpm --registry=https://registry.npm.taobao.org
  1. 安装grunt:
$ npm install -g grunt-cli
  1. 编辑Gruntfile.js
$ vim Gruntfile.js
文件93行添加hostname:'0.0.0.0'

options: {
        hostname:'0.0.0.0',
        port: 9100,
        base: '.',
        keepalive: true
}
  1. 检查head根目录下是否存在base文件夹

没有:将 _site下的base文件夹及其内容复制到head根目录下

$ mkdir base
$ cp base/* ../base/
  1. 启动grunt server:
$ grunt server -d
Running "connect:server" (connect) task
[D] Task source: /opt/module/elasticsearch-head-master/node_modules/grunt-contrib-connect/tasks/connect.js
Waiting forever...
Started connect web server on http://localhost:9100 

如果提示grunt的模块没有安装:
Local Npm module “grunt-contrib-clean” not found. Is it installed? 
Local Npm module “grunt-contrib-concat” not found. Is it installed? 
Local Npm module “grunt-contrib-watch” not found. Is it installed? 
Local Npm module “grunt-contrib-connect” not found. Is it installed? 
Local Npm module “grunt-contrib-copy” not found. Is it installed? 
Local Npm module “grunt-contrib-jasmine” not found. Is it installed? 
Warning: Task “connect:server” not found. Use –force to continue. 
执行以下命令: 
npm install grunt-contrib-clean -registry=https://registry.npm.taobao.org
npm install grunt-contrib-concat -registry=https://registry.npm.taobao.org
npm install grunt-contrib-watch -registry=https://registry.npm.taobao.org 
npm install grunt-contrib-connect -registry=https://registry.npm.taobao.org
npm install grunt-contrib-copy -registry=https://registry.npm.taobao.org 
npm install grunt-contrib-jasmine -registry=https://registry.npm.taobao.org
最后一个模块可能安装不成功,但是不影响使用。
  1. 浏览器访问head插件:
http://hadoop102:9100
  1. 启动集群插件后发现集群未连接

在/opt/module/elasticsearch-5.2.2/config路径下修改配置文件elasticsearch.yml,在文件末尾增加

$ pwd
/opt/module/elasticsearch-5.2.2/config
$ vi elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
再重新启动elasticsearch。
  1. 关闭插件服务
ctrl+c
$ netstat -lntp | grep 9100
tcp        0      0 192.168.1.102:9100          0.0.0.0:*                   LISTEN 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容