零、宿主机配置(一定要)

[root@iZbp1gp1t778obaz5m8vk8Z docker]# vi /etc/sysctl.conf

增加项:
vm.max_map_count=655360
[root@iZbp1gp1t778obaz5m8vk8Z docker]# sysctl -p
[root@ecs-b3bf-0225795 ~]# vi /etc/security/limits.conf
# 修改用户文件描述数量限制及线程池数量限制 - 如果没有增加如下项
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536

一、Docker 部署 ElasticSearch

1、从仓库中查找所有ElasticSearch的镜像

[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ pwd
/home/hn_home/docker/elasticsearch

[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ docker search elasticsearch
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 

2、选择一个版本镜像进行拉取

[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ docker pull docker.io/library/elasticsearch:7.6.2
Trying to pull repository docker.io/library/elasticsearch ... 
7.6.2: Pulling from docker.io/library/elasticsearch
c808caf183b6: Pull complete 
d6caf8e15a64: Pull complete 
b0ba5f324e82: Pull complete 
d7e8c1e99b9a: Pull complete 
85c4d6c81438: Pull complete 
3119218fac98: Pull complete 
914accf214bb: Pull complete 
Digest: sha256:59342c577e2b7082b819654d119f42514ddf47f0699c8b54dc1f0150250ce7aa
Status: Downloaded newer image for docker.io/library/elasticsearch:7.6.2
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ ll
total 0

3、 查看镜像

[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis                                 latest              ef47f3b6dc11        5 weeks ago         104 MB
docker.io/mysql                                 latest              dd7265748b5d        2 months ago        545 MB
docker.elastic.co/elasticsearch/elasticsearch   7.6.2               f29a1ee41030        10 months ago       791 MB
docker.io/elasticsearch                         7.6.2               f29a1ee41030        10 months ago       791 MB
docker.io/mongo                                 4.2.1               965553e202a4        14 months ago       363 MB
docker.io/java                                  8                   d23bdf5b1b1b        4 years ago         643 MB
docker.io/mobz/elasticsearch-head               5                   b19a5c98e43b        4 years ago         824 MB

4、通过镜像,启动一个容器,并将9200和9300端口映射到本机(ElasticSearch的默认端口是9200,我们把宿主环境9200端口映射到Docker容器中的9200端口)

然后把文件的映射全部映射到宿主机

[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ mkdir data
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ mkdir logs
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ mkdir plugins
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ mkdir config
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ chmod -R 775 data
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ chmod -R 775 logs
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ chmod -R 775 plugins
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ chmod -R 775 config
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ ll
total 12
drwxrwxr-x 2 hn-zcsjw hn-zcsjw 4096 Jan 21 09:46 data
drwxrwxr-x 2 hn-zcsjw hn-zcsjw 4096 Jan 21 09:46 logs
drwxrwxr-x 2 hn-zcsjw hn-zcsjw 4096 Jan 21 09:46 plugins
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ docker run -p 9200:9200 -p 9300:9300 \
--privileged=true --name es7.6.2 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx1024m" \
-v /home/hn_home/docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /home/hn_home/docker/elasticsearch/data:/usr/share/elasticsearch/data \
-v /home/hn_home/docker/elasticsearch/logs:/usr/share/elasticsearch/logs \
-d elasticsearch:7.6.2
d0e751a0047da73bcaf7ae9109183e74aeb028d80dbdf074973b313220ef5f6f
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 

把容器内Elasticsearch的配置文件复制到本地:如果有修改配置文件的需求的话

#从容器内,将配置文件copy出来
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ docker cp -a es7.6.2:/usr/share/elasticsearch/config/ /home/hn_home/docker/elasticsearch/
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 

给Elastic配置可访问IP、以及ES_Name配置
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ sudo chmod 776 config/elasticsearch.yml 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ vi config/elasticsearch.yml
#如下配置写入
cluster.name: "docker-cluster"
network.host: 0.0.0.0

[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ cat config/elasticsearch.yml
cluster.name: "docker-cluster"
network.host: 0.0.0.0


然后删除已运行的容器
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ docker stop es7.6.2
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ docker rm es7.6.2

然后重新运行,参数全部使用本地的挂载
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ docker run -p 9200:9200 -p 9300:9300 \
--privileged=true --name es7.6.2 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx1024m" \
-v /home/hn_home/docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /home/hn_home/docker/elasticsearch/data:/usr/share/elasticsearch/data \
-v /home/hn_home/docker/elasticsearch/logs:/usr/share/elasticsearch/logs \
-v /home/hn_home/docker/elasticsearch/config:/usr/share/elasticsearch/config \
-d elasticsearch:7.6.2

7eaeef682d2897703059e137406dbb1e01fd23e2b7493a57c1e17ebea970dd0e
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 

5、 浏览器访问ip:9200

{
  "name" : "f03270ef8672",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "mgb4vDWvQCWTgvkPG3TU4Q",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

二、Docker部署ik中文分词插件

1、进入es容器内部,/plugins下新建ik文件夹

[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ cd plugins/
[hn-jw@iZbp1gp1t778obaz5m8vk8Z plugins]$ mkdir ik
[hn-jw@iZbp1gp1t778obaz5m8vk8Z plugins]$ ll
total 4
drwxrwxr-x 2 hn-zcsjw hn-zcsjw 4096 Jan 21 10:09 ik
[hn-jw@iZbp1gp1t778obaz5m8vk8Z plugins]$ cd ik
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
100%[===============================================================================>] 4,504,473   21.5KB/s   in 4m 3s  

2021-01-21 10:15:55 (18.1 KB/s) - ‘elasticsearch-analysis-ik-7.6.2.zip’ saved [4504473/4504473]

[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ unzip elasticsearch-analysis-ik-7.6.2.zip 
Archive:  elasticsearch-analysis-ik-7.6.2.zip
   creating: config/
  inflating: config/main.dic         
  inflating: config/quantifier.dic   
  inflating: config/extra_single_word_full.dic  
  inflating: config/IKAnalyzer.cfg.xml  
  inflating: config/surname.dic      
  inflating: config/suffix.dic       
  inflating: config/stopword.dic     
  inflating: config/extra_main.dic   
  inflating: config/extra_stopword.dic  
  inflating: config/preposition.dic  
  inflating: config/extra_single_word_low_freq.dic  
  inflating: config/extra_single_word.dic  
  inflating: elasticsearch-analysis-ik-7.6.2.jar  
  inflating: httpclient-4.5.2.jar    
  inflating: httpcore-4.4.4.jar      
  inflating: commons-logging-1.2.jar  
  inflating: commons-codec-1.9.jar   
  inflating: plugin-descriptor.properties  
  inflating: plugin-security.policy  
IK配置成功后还是要重启一下,否则不生效
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ docker stop es7.6.2
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ docker rm es7.6.2
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ 
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ docker run -p 9200:9200 -p 9300:9300 --name es7.6.2 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx1024m" -v /home/hn_home/docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins -v /home/hn_home/docker/elasticsearch/data:/usr/share/elasticsearch/data -v /home/hn_home/docker/elasticsearch/logs:/usr/share/elasticsearch/logs -v /home/hn_home/docker/elasticsearch/config:/usr/share/elasticsearch/config -d elasticsearch:7.6.2
a0f0592bc2f3f4467251437a32737d76b2cd0f4a0034cf0ea7197de1c16f8632
[hn-jw@iZbp1gp1t778obaz5m8vk8Z ik]$ 

三:docker 内Elasticsearch使用

# 查看集群健康状态
curl 127.0.0.1:9200/_cat/health?v
# 查看节点健康状态
curl 127.0.0.1:9200/_cat/nodes?v
# 查看索引信息
curl 127.0.0.1:9200/_cat/indices?v
[hn-zcsjw@iZbp1gp1t778obaz5m8vk8Z config]$  curl http://127.0.0.1:9200/
{
  "name" : "7ab1554168d9",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "mgb4vDWvQCWTgvkPG3TU4Q",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

Docker安装 Elasticsearch已基本完成,从中我们可以清晰感受到Docker的强大之处!

四:发一个挂掉后自动重启的包,指定容器内存大小的启动参数

[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
docker run --restart=on-failure:10 -d -m 2G --memory-swap 4G -p 9200:9200 \
-p 9300:9300 --privileged=true --name es7.6.2 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx1024m" \
-v /home/hn_home/docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /home/hn_home/docker/elasticsearch/data:/usr/share/elasticsearch/data \
-v /home/hn_home/docker/elasticsearch/logs:/usr/share/elasticsearch/logs \
-v /home/hn_home/docker/elasticsearch/config:/usr/share/elasticsearch/config \
-d elasticsearch:7.6.2

五:配置ElasticSearch的账号与密码

[root@iZbp14hdte9197qsflsd9tZ config]# vi /data/docker/elasticsearch/config/elasticsearch.yml 

#X-Pack Security启用密码设置 - 加上这一句
xpack.security.enabled: true
关掉已有的docker ES,重新启动一个新的容器
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$ 
docker run --restart=on-failure:10 -d -m 2G --memory-swap 4G -p 9200:9200 \
-p 9300:9300 --privileged=true --name es7.6.2 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx1024m" \
-v /home/hn_home/docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /home/hn_home/docker/elasticsearch/data:/usr/share/elasticsearch/data \
-v /home/hn_home/docker/elasticsearch/logs:/usr/share/elasticsearch/logs \
-v /home/hn_home/docker/elasticsearch/config:/usr/share/elasticsearch/config \
-d elasticsearch:7.6.2

我们进入容器内
[hn-jw@iZbp1gp1t778obaz5m8vk8Z elasticsearch]$  docker exec -it es7.6.2 bash

容器内,我们执行重置密码
[root@9932b59fed7b elasticsearch]# bin/elasticsearch-setup-passwords interactive

Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y

输入密码:
Enter password for [elastic]: 
Reenter password for [elastic]: 

然后我们的新密码就生效。

六:课外知识

9300 是TCP协议端口号,ES集群之间通讯端口号

9200 端口号,暴露ES RESTful接口端口号

我们常用的Head插件都连接9200、java客户端也连:9200

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐