elasticsearch

docker install

Starting a single node cluster with Docker

1
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.16.2
1
docker run -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.16.2

Starting a multi-node cluster with Docker Compose

Create a docker-compose.yml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

version: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- xpack.security.enabled=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- xpack.security.enabled=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data02:/usr/share/elasticsearch/data
networks:
- elastic
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- xpack.security.enabled=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data03:/usr/share/elasticsearch/data
networks:
- elastic

volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local

networks:
elastic:
driver: bridge

Run docker-compose to bring up the cluster:

1
2
docker-compose up

Submit a _cat/nodes request to see that the nodes are up and running:

1
2
curl -X GET "localhost:9200/_cat/nodes?v=true&pretty"

efk_elasticsearch_1 exited with code 78 when install ElasticSearch

1
2
3
sudo sysctl -w vm.max_map_count=262144

sudo sysctl -w vm.max_map_count=524288

ref: https://techoverflow.net/2019/03/11/how-to-fix-elasticsearch-exited-with-code-78/

Download and install archive for Linux 单机

修改 vm.max_map_count

1
sudo sysctl -w vm.max_map_count=524288

a.切换到root用户,修改sysctl.conf配置文件

1
2
vi /etc/sysctl.conf

b.添加如下配置文件

1
2
vm.max_map_count=655360

c.执行如下脚本

1
2
sysctl -p

创建一个非root用户

ElasticSearch有个比较特殊的地方就是不能用root权限来运行,所以我们这边需要新建一个用户以及赋予对应权限。

a.新建一个elsearch用户组

1
2
groupadd elsearch

b.新建用户elsearch,并让他加入elsearch组

1
2
3
useradd elsearch -g elsearch -p elsearch

passwd elsearch

c.切换用户

1
su elsearch

d. root 权限

1
2
3
4
5
6
7
chmod -v u+w /etc/sudoers

vim /etc/sudoers
# 找到Allow root to run any commands anywhere 之后添加一行

'用户名' ALL=(ALL) ALL
# 如需新用户使用sudo时不用输密码,把最后一个ALL改为NOPASSWD:ALL即可

创建日志文件夹

1
2
mkdir -p ~/data/logs/elasticsearch
mkdir -p ~/data/elasticsearch/{data,work,plugins,scripts}

将下载的文件放在服务器如下目录: ~/apps/

1
2
3
4
5
6
7
sudo yum install -y wget vim
sudo yum install perl-Digest-SHA -y
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.16.2-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.16.2-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-7.16.2-linux-x86_64.tar.gz.sha512
tar -xzf elasticsearch-7.16.2-linux-x86_64.tar.gz
cd elasticsearch-7.16.2/

修改配置文件

1
2
3
4
cd /home/elsearch/apps/elasticsearch-7.16.2/config

vim elasticsearch.yml

ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cluster.name: summer-es
node.name: node-1

network.host: 0.0.0.0 //监听访问地址为任意网段,也可以按自己的要求要设置对应的网段

discovery.seed_hosts: ["192.169.171.132"]

cluster.initial_master_nodes: ["node-1"]

path.data: /home/elsearch/data/elasticsearch/data
path.logs: /home/elsearch/data/logs/elasticsearch

#如果没有对应的插件,那么下面两个就不用配置,否则会报错
#path.plugins: /data/elasticsearch/plugins
#path.scripts: /data/elasticsearch/scripts
1
2
3
discovery.seed_hosts:集群节点列表,每个值应采用host:port或host的形式(其中port默认为设置transport.profiles.default.port,如果未设置则返回transport.port)
discovery.seed_providers:集群节点列表的提供者,作用就是获取discovery.seed_hosts,比如使用文件指定节点列表
cluster.initial_master_nodes:初始化时master节点的选举列表,一般使用node.name(节点名称)配置指定,配置旨在第一次启动有效,启动之后会保存,下次启动会读取保存的数据

也可以通过如下配置来修改端口:

1
2
http.port: 9200

完整配置 elasticsearch.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: summer-es
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /home/elsearch/data/elasticsearch/data
#
# Path to log files:
#
path.logs: /home/elsearch/data/logs/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
network.host: 0.0.0.0
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["192.169.171.132"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["node-1"]
#
# For more information, consult the discovery and cluster formation module documentation.
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#
# ---------------------------------- Security ----------------------------------

启动任务

1
/home/elsearch/apps/elasticsearch-7.16.2/bin/elasticsearch
1
2
3
cd /home/elsearch/apps/elasticsearch-7.16.2/bin/

sh elasticsearch 或者用sh elasticsearch -d来后台启动

验证是否运行

下面两者需同时满足

a. 输入如下脚本,有对应服务出现

1
2
ps -ef|grep elasticsearch

b.输入http//:ip:9200出现下图所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name" : "node-1",
"cluster_name" : "summer-es",
"cluster_uuid" : "7PPB46ryTe-tIu2L45cM-Q",
"version" : {
"number" : "7.16.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "2b937c44140b6559905130a8650c64dbd0879cfb",
"build_date" : "2021-12-18T19:42:46.604893745Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

关闭elasticsearch

需要对ES节点进行重新启动或正常关机的时候,有三种方法可以关闭ES:

1.在控制台中,使用CTRL+C组合键.
2.通过发送TERM信号终止服务器进程.
3.使用REST APIcurl -XPOST ‘http://localhost:9200/_shutdown'

集群

参考文章

评论