elasticsearch 单例部署

1. 环境准备

IP 主机名 安装软件 操作系统
192.168.2.20 master es,jdk1.8 ubuntu18.04

我们部署ELK 5.X 然后三台机器都得关闭防火墙或清空防火墙规则[线上机器谨慎操作]。 本文介绍单例es

检查
cmz@master:~$ java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

2. ES安装

2.1 安装

安装指南 https://www.elastic.co/guide/en/elastic-stack/current/installing-elastic-stack.html

ES 安装指南 https://www.elastic.co/guide/en/elasticsearch/reference/6.8/install-elasticsearch.html

推荐安装包安装,下载zip|tar包解压启动即可

cd /usr/local/src
mkdir elk
cd elk
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.16.tar.gz
tar xf elasticsearch-5.6.16.tar.gz 
mv elasticsearch-5.6.16 /opt/
cd /opt/
ln -sf elasticsearch-5.6.16/ elasticsearch
chown -R cmz.cmz /opt/
su - cmz # 切换到非root用户
./bin/elasticsearch  # 前台启动
# ./bin/elasticsearch & # 后台启动
详细操作过程
root@master:/usr/local/kafka/config# cd /usr/local/src
root@master:/usr/local/src# mkdir elk
root@master:/usr/local/src# cd elk/
root@master:/usr/local/src/elk# ls
root@master:/usr/local/src/elk# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.16.tar.gz
--2019-10-09 11:12:29--  https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.16.tar.gz
Resolving artifacts.elastic.co (artifacts.elastic.co)... 151.101.230.222, 2a04:4e42:36::734
Connecting to artifacts.elastic.co (artifacts.elastic.co)|151.101.230.222|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 33894983 (32M) [application/x-gzip]
Saving to: ‘elasticsearch-5.6.16.tar.gz’

elasticsearch-5.6.16.tar.gz                     100%[====================================================================================================>]  32.32M   151KB/s    in 3m 24s  

2019-10-09 11:15:54 (162 KB/s) - ‘elasticsearch-5.6.16.tar.gz’ saved [33894983/33894983]

root@master:/usr/local/src/elk# ls
elasticsearch-5.6.16.tar.gz
root@master:/usr/local/src/elk# tar xf elasticsearch-5.6.16.tar.gz 
root@master:/usr/local/src/elk# mv elasticsearch-5.6.16 /opt/
root@master:/usr/local/src/elk# chown -R cmz.cmz /opt/
root@master:/usr/local/src/elk# su - cmz
cmz@master:~$ cd /opt/
cmz@master:/opt$ ls
elasticsearch-5.6.16
cmz@master:/opt$ ln -sf elasticsearch-5.6.16/ elasticsearch
cmz@master:/opt/elasticsearch$ ./bin/elasticsearch

2.2 配置ES

elasticsearch配置文件在这两个地方,有两个配置文件

cmz@master:~$ cd /opt/elasticsearch/config/
cmz@master:/opt/elasticsearch/config$ ll
total 28
drwxr-xr-x 3 cmz cmz 4096 10月  9 11:21 ./
drwxr-xr-x 9 cmz cmz 4096 10月  9 11:19 ../
-rw-rw---- 1 cmz cmz 2849 10月  9 11:21 elasticsearch.yml
-rw-rw---- 1 cmz cmz 3064 3月  13  2019 jvm.options
-rw-rw---- 1 cmz cmz 4456 3月  13  2019 log4j2.properties
drwxrwxr-x 2 cmz cmz 4096 10月  9 11:19 scripts/
elasticsearch.yml 文件用于配置集群节点等相关信息的,elasticsearch 文件则是配置服务本身相关的配置,例如某个配置文件的路径以及java的一些路径配置什么的。

官网配置文档

https://www.elastic.co/guide/en/elasticsearch/reference/6.0/rpm.html
开始配置集群节点,在 master 上编辑配置文件:
cmz@master:/opt/elasticsearch/config$ egrep -v '#|^$' elasticsearch.yml 
network.host: 0.0.0.0

参数详解
elasticsearch的config文件夹里面有两个配置文件:elasticsearch.yml和logging.yml,第一个是es的基本配置文件,第二个是日志配置文件,es也是使用log4j来记录日志的,所以logging.yml里的设置按普通log4j配置文件来设置就行了。下面主要讲解下elasticsearch.yml这个文件中可配置的东西。

cluster.name: elasticsearch
配置es的集群名称,默认是elasticsearch,es会自动发现在同一网段下的es,如果在同一网段下有多个集群,就可以用这个属性来区分不同的集群。

node.name: "Franz Kafka"
节点名,默认随机指定一个name列表中名字,该列表在es的jar包中config文件夹里name.txt文件中,其中有很多作者添加的有趣名字。

node.master: true
指定该节点是否有资格被选举成为node,默认是true,es是默认集群中的第一台机器为master,如果这台机挂了就会重新选举master。

node.data: true
指定该节点是否存储索引数据,默认为true。

index.number_of_shards: 5
设置默认索引分片个数,默认为5片。

index.number_of_replicas: 1
设置默认索引副本个数,默认为1个副本。

path.conf: /path/to/conf
设置配置文件的存储路径,默认是es根目录下的config文件夹。

path.data: /path/to/data
设置索引数据的存储路径,默认是es根目录下的data文件夹,可以设置多个存储路径,用逗号隔开,例:
path.data: /path/to/data1,/path/to/data2

path.work: /path/to/work
设置临时文件的存储路径,默认是es根目录下的work文件夹。

path.logs: /path/to/logs
设置日志文件的存储路径,默认是es根目录下的logs文件夹

path.plugins: /path/to/plugins
设置插件的存放路径,默认是es根目录下的plugins文件夹

bootstrap.mlockall: true
设置为true来锁住内存。因为当jvm开始swapping时es的效率会降低,所以要保证它不swap,可以把ES_MIN_MEM和ES_MAX_MEM两个环境变量设置成同一个值,并且保证机器有足够的内存分配给es。同时也要允许elasticsearch的进程可以锁住内存,linux下可以通过`ulimit -l unlimited`命令。

network.bind_host: 192.168.0.1
设置绑定的ip地址,可以是ipv4或ipv6的,默认为0.0.0.0。


network.publish_host: 192.168.0.1
设置其它节点和该节点交互的ip地址,如果不设置它会自动判断,值必须是个真实的ip地址。

network.host: 192.168.0.1
这个参数是用来同时设置bind_host和publish_host上面两个参数。

transport.tcp.port: 9300
设置节点间交互的tcp端口,默认是9300。

transport.tcp.compress: true
设置是否压缩tcp传输时的数据,默认为false,不压缩。

http.port: 9200
设置对外服务的http端口,默认为9200。

http.max_content_length: 100mb
设置内容的最大容量,默认100mb

http.enabled: false
是否使用http协议对外提供服务,默认为true,开启。

gateway.type: local
gateway的类型,默认为local即为本地文件系统,可以设置为本地文件系统,分布式文件系统,hadoop的HDFS,和amazon的s3服务器,其它文件系统的设置方法下次再详细说。

gateway.recover_after_nodes: 1
设置集群中N个节点启动时进行数据恢复,默认为1。

gateway.recover_after_time: 5m
设置初始化数据恢复进程的超时时间,默认是5分钟。

gateway.expected_nodes: 2
设置这个集群中节点的数量,默认为2,一旦这N个节点启动,就会立即进行数据恢复。

cluster.routing.allocation.node_initial_primaries_recoveries: 4
初始化数据恢复时,并发恢复线程的个数,默认为4。

cluster.routing.allocation.node_concurrent_recoveries: 2
添加删除节点或负载均衡时并发恢复线程的个数,默认为4。

indices.recovery.max_size_per_sec: 0
设置数据恢复时限制的带宽,如入100mb,默认为0,即无限制。

indices.recovery.concurrent_streams: 5
设置这个参数来限制从其它分片恢复数据时最大同时打开并发流的个数,默认为5。

discovery.zen.minimum_master_nodes: 1
设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)

discovery.zen.ping.timeout: 3s
设置集群中自动发现其它节点时ping连接超时时间,默认为3秒,对于比较差的网络环境可以高点的值来防止自动发现时出错。

discovery.zen.ping.multicast.enabled: false
设置是否打开多播发现节点,默认是true。

discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]
设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点。

下面是一些查询时的慢日志参数设置
index.search.slowlog.level: TRACE
index.search.slowlog.threshold.query.warn: 10s
index.search.slowlog.threshold.query.info: 5s
index.search.slowlog.threshold.query.debug: 2s
index.search.slowlog.threshold.query.trace: 500ms

index.search.slowlog.threshold.fetch.warn: 1s
index.search.slowlog.threshold.fetch.info: 800ms
index.search.slowlog.threshold.fetch.debug:500ms
index.search.slowlog.threshold.fetch.trace: 200ms

2.3 启动es

/opt/elasticsearch/bin/elasticsearch

启动es

cmz@master:~$ /opt/elasticsearch/bin/elasticsearch
[2019-10-09T11:43:49,412][INFO ][o.e.n.Node               ] [] initializing ...
[2019-10-09T11:43:49,530][INFO ][o.e.e.NodeEnvironment    ] [Q99k3iN] using [1] data paths, mounts [[/ (/dev/sda2)]], net usable_space [857.7gb], net total_space [915.3gb], spins? [possibly
], types [ext4][2019-10-09T11:43:49,530][INFO ][o.e.e.NodeEnvironment    ] [Q99k3iN] heap size [1.9gb], compressed ordinary object pointers [true]
[2019-10-09T11:43:49,532][INFO ][o.e.n.Node               ] node name [Q99k3iN] derived from node ID [Q99k3iNRRouuMqNZie6K6Q]; set [node.name] to override
[2019-10-09T11:43:49,532][INFO ][o.e.n.Node               ] version[5.6.16], pid[28189], build[3a740d1/2019-03-13T15:33:36.565Z], OS[Linux/5.0.0-23-generic/amd64], JVM[Oracle Corporation/Ja
va HotSpot(TM) 64-Bit Server VM/1.8.0_112/25.112-b15][2019-10-09T11:43:49,532][INFO ][o.e.n.Node               ] JVM arguments [-Xms2g, -Xmx2g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly
, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/opt/elasticsearch][2019-10-09T11:43:50,104][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [aggs-matrix-stats]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [ingest-common]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [lang-expression]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [lang-groovy]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [lang-mustache]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [lang-painless]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [parent-join]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [percolator]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [reindex]
[2019-10-09T11:43:50,105][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [transport-netty3]
[2019-10-09T11:43:50,106][INFO ][o.e.p.PluginsService     ] [Q99k3iN] loaded module [transport-netty4]
[2019-10-09T11:43:50,106][INFO ][o.e.p.PluginsService     ] [Q99k3iN] no plugins loaded
[2019-10-09T11:43:51,097][INFO ][o.e.d.DiscoveryModule    ] [Q99k3iN] using discovery type [zen]
[2019-10-09T11:43:51,426][INFO ][o.e.n.Node               ] initialized
[2019-10-09T11:43:51,426][INFO ][o.e.n.Node               ] [Q99k3iN] starting ...
[2019-10-09T11:43:51,553][INFO ][o.e.t.TransportService   ] [Q99k3iN] publish_address {192.168.2.20:9300}, bound_addresses {[::]:9300}
[2019-10-09T11:43:51,560][INFO ][o.e.b.BootstrapChecks    ] [Q99k3iN] bound or publishing to a non-loopback address, enforcing bootstrap checks
[2019-10-09T11:43:54,594][INFO ][o.e.c.s.ClusterService   ] [Q99k3iN] new_master {Q99k3iN}{Q99k3iNRRouuMqNZie6K6Q}{snPiaXMcRu-oeuyOWMepAA}{192.168.2.20}{192.168.2.20:9300}, reason: zen-disc
o-elected-as-master ([0] nodes joined)[2019-10-09T11:43:54,605][INFO ][o.e.h.n.Netty4HttpServerTransport] [Q99k3iN] publish_address {192.168.2.20:9200}, bound_addresses {[::]:9200}
[2019-10-09T11:43:54,605][INFO ][o.e.n.Node               ] [Q99k3iN] started
[2019-10-09T11:43:54,670][INFO ][o.e.g.GatewayService     ] [Q99k3iN] recovered [0] indices into cluster_state

2.4 检查es状态

root@master:~# curl master:9200
{
  "name" : "Q99k3iN",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "qNGJCGblQAGp3vhiiAWz8g",
  "version" : {
    "number" : "5.6.16",
    "build_hash" : "3a740d1",
    "build_date" : "2019-03-13T15:33:36.565Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

配置

不配置 node.name,启动后,名字是随机的
不配置 cluster.name,启动后,名字默认是elasticsearch