docker快速安装ElasticSearch和Kibana

安装ES

基本安装

我们这里以安装ES8为例,可以直接使用如下脚本。我这边将ES映射到宿主机9276端口,如果需要其他映射端口可以自行修改

1
2
3
4
5
6
#!/bin/bash

docker run -d -p 9276:9200 -p 9376:9300 -e "ES_JAVA_OPTS=-Xms256m -Xmx256m" -e "discovery.type=single-node" --name es8.3.2 \
-v /Users/wanshao/etc/elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /Users/wanshao/etc/elasticsearch/es8_data:/usr/share/elasticsearch/data \
elasticsearch:8.3.2

上面脚本使用的配置文件elasticsearch.yml如下:

1
2
3
cluster.name: "docker-cluster"
network.host: 0.0.0.0
xpack.security.enabled: true

配置权限

启动后进入ES容器执行设置密码脚本:

1
./bin/elasticsearch-setup-passwords interactive

安装kibana

kibana使用和ES相同版本的,避免兼容性问题。安装脚本如下:

1
2
3
4
5
#!/bin/bash

docker run -d -it --privileged=true --name=kibana8.3.2 -p 5676:5601 -e ELASTICSEARCH_URL=http://host.docker.internal:9276 \
-v /Users/wanshao/etc/elasticsearch/kibana.yml:/usr/share/kibana/config/kibana.yml \
kibana:8.3.2

使用的kibana.yml配置文件如下,密码和对应ES的host地址可以自己设置

1
2
3
4
5
server.name: kibana
server.host: "0.0.0.0"
elasticsearch.hosts: [ "http://host.docker.internal:9276" ]
elasticsearch.username: "kibana"
elasticsearch.password: "yourPassword"

ES常用命令

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

## 访问
curl -uelastic:yourPassword -X GET 'http://localhost:9276'


##创建索引
curl -X PUT "localhost:9276/myindex?pretty"
curl -uelastic:yourPassword -XPUT "localhost:9276/myindex?pretty"



## 查看索引
curl -X GET "localhost:9276/myindex?pretty"
curl -uelastic:yourPassword -X GET "localhost:9276/myindex?pretty"


## 添加mapping
### 5.x 以前可以指定多个type
curl -X PUT "localhost:9200/myindex/_mapping/my_user_tables?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"name": {
"type": "text"
},
"user_name": {
"type": "keyword"
},
"email": {
"type": "keyword"
}
}
}
'

curl -X PUT "localhost:9200/myindex/_mapping/my_worker_tables?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"worker_name": {
"type": "text"
},
"desc": {
"type": "text"
}
}
}
'


### 6.x 需要指定mapping type,一个索引只能有一个mapping type
curl -uelastic:yourPassword -X PUT "localhost:9200/myindex_uppercase/_mapping/my_user_tables?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"name": {
"type": "text"
},
"userName": {
"type": "keyword"
},
"email": {
"type": "keyword"
}
}
}
'



curl -X PUT "localhost:9201/myindex/_mapping/my_user_tables2?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"name2": {
"type": "text"
},
"user_name2": {
"type": "keyword"
},
"email2": {
"type": "keyword"
}
}
}
'


### 7.x 无需指定type,直接访问具体的mapping
curl -X PUT "localhost:9202/myindex/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"name": {
"type": "text"
},
"user_name": {
"type": "keyword"
},
"email": {
"type": "keyword"
}
}
}
'

curl -X PUT "localhost:9202/myindex/_mapping?pretty" -H 'Content-Type: application/json' -d'
{
"properties": {
"name2": {
"type": "text"
},
"user_name2": {
"type": "keyword"
},
"email2": {
"type": "keyword"
}
}
}
'


## 检查version
curl -XGET 'http://localhost:9200'

## 健康检查
curl -uelastic:yourPassword http://localhost:9200/_cluster/health?pretty
curl http://localhost:9201/_cluster/health?pretty

2. 安装kibina

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
## 拉取镜像
docker pull kibana:5.5.0
docker pull kibana:6.7.0
docker pull kibana:7.4.0

# 启动容器
docker run -d -it --privileged=true --name=kibana6.8 -p 5601:5601 -e ELASTICSEARCH_URL=http://192.168.0.149:9200 kibana:6.8.0
docker run -d -it --privileged=true --name=kibana6.7 -p 5602:5601 -e ELASTICSEARCH_HOSTS=http://192.168.0.149:9201 kibana:6.7.0
docker run -d -it --privileged=true --name=kibana7.4 -p 5603:5601 -e ELASTICSEARCH_HOSTS=http://192.168.0.149:9202 kibana:7.4.0

docker run -d -it --privileged=true --name=kibana7.6 -p 5676:5601 -e ELASTICSEARCH_HOSTS=http://192.168.0.149:9276 kibana:7.6


## 启动需要密码的kibana容器
## 先新建kibana.yml写入如下内容
server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
elasticsearch.username: "kibana"
elasticsearch.password: "Clougence2020"

## 启动容器并且指定配置文件
docker run -d -it --privileged=true --name=kibana7.6.2 -p 5676:5601 -e ELASTICSEARCH_URL=http://192.168.0.119:9276 \
-v /Users/wanshao/etc/elasticsearch/kibana.yml:/usr/share/kibana/config/kibana.yml \
kibana:7.6.2


kibana创建index with mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
PUT /subtitle
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties":
{
"createtime": { "type": "date", "format": "strict_date_optional_time||epoch_millis||date_hour_minute_second||yyyy-MM-dd HH:mm:ss", "locale": "zh_CN" },
"id": { "type": "keyword", "index": false, "doc_values": false },
"imdb_id": { "type": "keyword" },
"language": { "type": "keyword", "index": false, "doc_values": false }, "languageShort": { "type": "keyword" },
"moviename": { "type": "text" },
"path": { "type": "keyword", "index": false, "doc_values": false }

}

}
}
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
PUT live-mini_test_enterprise_goods_sku_view
{
"settings": {
"analysis": {
"analyzer": {
"custom_e": {
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer": {
"type": "ngram",
"min_gram": 1,
"max_gram": 2,
"token_chars": [
"letter",
"digit"
]
}
}
},
"number_of_replicas" : "1",
"number_of_shards" : 1
},
"mappings" : {
"properties" : {
"allocate_mode" : {
"type" : "short"
},
"allocate_video" : {
"type" : "text",
"analyzer" : "whitespace"
},
"big_pic" : {
"type" : "text",
"analyzer" : "standard"
},
"category_id" : {
"type" : "integer"
},
"category_name" : {
"type" : "text",
"analyzer" : "standard"
},
"coordinates" : {
"type" : "text"
},
"create_time" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZZZZZ||yyyy-MM-dd HH:mm:ssZZZZZ"
},
"del_time" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZZZZZ||yyyy-MM-dd HH:mm:ssZZZZZ"
},
"delivery_price" : {
"type" : "double"
},
"description" : {
"type" : "text",
"analyzer" : "standard"
},
"distribute_first" : {
"type" : "double"
},
"distribute_second" : {
"type" : "double"
},
"enterprise_goods_detail_img" : {
"type" : "text",
"analyzer" : "standard"
},
"enterprise_goods_id" : {
"type" : "integer"
},
"enterprise_id" : {
"type" : "integer"
},
"goods_description" : {
"type" : "text",
"analyzer" : "standard"
},
"goods_no" : {
"type" : "text",
"analyzer" : "standard"
},
"goods_order" : {
"type" : "integer"
},
"goods_type" : {
"type" : "integer"
},
"id" : {
"type" : "integer"
},
"img" : {
"type" : "text",
"analyzer" : "standard"
},
"is_del" : {
"type" : "short"
},
"is_delivery" : {
"type" : "short"
},
"is_distribution_threshold" : {
"type" : "short"
},
"is_distributor" : {
"type" : "short"
},
"is_distributor_self" : {
"type" : "short"
},
"is_grounding" : {
"type" : "short"
},
"is_limit_purchases" : {
"type" : "short"
},
"is_promote" : {
"type" : "short"
},
"is_sku" : {
"type" : "short"
},
"limit_purchases_cycle" : {
"type" : "short"
},
"limit_purchases_max_quantity" : {
"type" : "short"
},
"limit_purchases_type" : {
"type" : "short"
},
"name" : {
"type" : "text",
"analyzer" : "standard"
},
"origin_pic" : {
"type" : "text",
"analyzer" : "standard"
},
"original_price" : {
"type" : "double"
},
"price" : {
"type" : "double"
},
"refund_configure" : {
"type" : "integer"
},
"scan_goods" : {
"type" : "text",
"analyzer" : "custom_e"
},
"self_commission_properation" : {
"type" : "double"
},
"self_uniform_proportion" : {
"type" : "short"
},
"sku_no" : {
"type" : "text",
"analyzer" : "custom_e"
},
"small_pic" : {
"type" : "text",
"analyzer" : "standard"
},
"spec" : {
"type" : "text"
},
"ts" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZZZZZ||yyyy-MM-dd HH:mm:ssZZZZZ"
},
"uniform_proportion" : {
"type" : "short"
},
"update_time" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZZZZZ||yyyy-MM-dd HH:mm:ssZZZZZ"
},
"video" : {
"type" : "text",
"analyzer" : "standard"
}
}
}
}
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
PUT student
{
"settings": {
"analysis": {
"analyzer": {
"wanshao_analyzer": {
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer": {
"type": "ngram",
"min_gram": 1,
"max_gram": 2,
"token_chars": [
"letter",
"digit"
]
}
}
},
"number_of_replicas" : "1",
"number_of_shards" : 1
},
"mappings" : {
"properties" : {
"col_int": {
"type": "integer"
},
"col_varchar": {
"type": "text",
"analyzer": "wanshao_analyzer"
},
"id": {
"type": "integer"
},
"name": {
"type": "text",
"analyzer": "standard"
},
"score": {
"type": "integer"
}
}
}
}

带nested结构

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
PUT live-mini_test_enterprise_goods_sku_define
{
"settings": {
"analysis": {
"analyzer": {
"custom_e": {
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer": {
"type": "ngram",
"min_gram": 1,
"max_gram": 2,
"token_chars": [
"letter",
"digit"
]
}
}
},
"number_of_replicas" : "1",
"number_of_shards" : 1
},
"mappings" : {
"properties" : {
"allocate_mode" : {
"type" : "short"
},
"allocate_video" : {
"type" : "text",
"analyzer" : "whitespace"
},
"big_pic" : {
"type" : "text",
"analyzer" : "standard"
},
"category_id" : {
"type" : "integer"
},
"category_name" : {
"type" : "text",
"analyzer" : "standard"
},
"coordinates" : {
"type" : "text"
},
"create_time" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZZZZZ||yyyy-MM-dd HH:mm:ssZZZZZ"
},
"del_time" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZZZZZ||yyyy-MM-dd HH:mm:ssZZZZZ"
},
"delivery_price" : {
"type" : "double"
},
"description" : {
"type" : "text",
"analyzer" : "standard"
},
"distribute_first" : {
"type" : "double"
},
"distribute_second" : {
"type" : "double"
},
"enterprise_goods_detail_img" : {
"type" : "text",
"analyzer" : "standard"
},
"enterprise_goods_id" : {
"type" : "integer"
},
"enterprise_id" : {
"type" : "integer"
},
"goods_description" : {
"type" : "text",
"analyzer" : "standard"
},
"goods_no" : {
"type": "text",
"analyzer" : "custom_e",
"fields": {
"keyword":{
"type": "keyword"
}
}
},
"goods_order" : {
"type" : "integer"
},
"stock" : {
"type" : "integer"
},
"goods_type" : {
"type" : "integer"
},
"id" : {
"type" : "integer"
},
"img" : {
"type" : "text",
"analyzer" : "standard"
},
"is_del" : {
"type" : "short"
},
"is_delivery" : {
"type" : "short"
},
"is_distribution_threshold" : {
"type" : "short"
},
"is_distributor" : {
"type" : "short"
},
"is_distributor_self" : {
"type" : "short"
},
"is_grounding" : {
"type" : "short"
},
"is_limit_purchases" : {
"type" : "short"
},
"is_promote" : {
"type" : "short"
},
"is_sku" : {
"type" : "short"
},
"limit_purchases_cycle" : {
"type" : "short"
},
"limit_purchases_max_quantity" : {
"type" : "short"
},
"limit_purchases_type" : {
"type" : "short"
},
"name" : {
"type": "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart",
"fields": {
"keyword":{
"type": "keyword"
}
},
"fielddata": true
},
"origin_pic" : {
"type" : "text",
"analyzer" : "standard"
},
"original_price" : {
"type" : "double"
},
"price" : {
"type" : "double"
},
"refund_configure" : {
"type" : "integer"
},
"scan_goods" : {
"type": "text",
"analyzer" : "custom_e",
"fields": {
"keyword":{
"type": "keyword"
}
}
},
"self_commission_properation" : {
"type" : "double"
},
"self_uniform_proportion" : {
"type" : "short"
},
"sku_no" : {
"type": "text",
"analyzer" : "custom_e",
"fields": {
"keyword":{
"type": "keyword"
}
}
},
"sku_scan_goods" : {
"type": "text",
"analyzer" : "custom_e",
"fields": {
"keyword":{
"type": "keyword"
}
}
},
"small_pic" : {
"type" : "text",
"analyzer" : "standard"
},
"spec" : {
"type" : "text"
},
"ts" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZZZZZ||yyyy-MM-dd HH:mm:ssZZZZZ"
},
"uniform_proportion" : {
"type" : "short"
},
"update_time" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZZZZZ||yyyy-MM-dd HH:mm:ssZZZZZ"
},
"video" : {
"type" : "text",
"analyzer" : "standard"
},
"sku_detail" : {
"type" : "nested",
"properties": {
"id" : {
"type" : "integer"
},
"name":{
"type": "text",
"analyzer" : "ik_max_word",
"search_analyzer" : "ik_smart",
"fields": {
"keyword":{
"type": "keyword"
}
},
"fielddata": true
}
}
}
}
}
}

student nested

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
PUT student_nested
{
"settings": {
"analysis": {
"analyzer": {
"wanshao_analyzer": {
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer": {
"type": "ngram",
"min_gram": 1,
"max_gram": 2,
"token_chars": [
"letter",
"digit"
]
}
}
},
"number_of_replicas" : "1",
"number_of_shards" : 1
},
"mappings" : {
"properties" : {
"col_int": {
"type": "integer"
},
"col_varchar": {
"type": "text",
"analyzer": "wanshao_analyzer"
},
"id": {
"type": "integer"
},
"name": {
"type": "text",
"analyzer": "wanshao_analyzer"
},
"score": {
"type": "integer"
},
"sku_detail" : {
"type" : "nested",
"properties": {
"id" : {
"type" : "integer"
},
"name":{
"type": "text"
}
}
}
}
}
}

参考资料:
docker安装elasticsearch
一文搞懂 Elasticsearch 之 Mapping
Docker安装Kibana详解
Docker 容器中运行 Kibana