怎样通过Docker安装Elastic Search

2020年1月20日 | 分类: 【技术】

【依赖】

安装 Docker:

参考:https://amon.org/docker

【安装】

参考:https://thans.cn/elasticsearch/install.html
参考:https://www.cnblogs.com/jianxuanbing/p/9410800.html

拉取镜像:

sudo docker pull docker.elastic.co/elasticsearch/elasticsearch:7.5.1

输出:

7.5.1: Pulling from elasticsearch/elasticsearch
c808caf183b6: Pull complete
05ff3f896999: Pull complete
82fb7fb0a94e: Pull complete
c4d0024708f4: Pull complete
136650a16cfe: Pull complete
968db096c092: Pull complete
42547e91692f: Pull complete
Digest: sha256:b0960105e830085acbb1f9c8001f58626506ce118f33816ea5d38c772bfc7e6c
Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch:7.5.1
docker.elastic.co/elasticsearch/elasticsearch:7.5.1

运行 Elasticsearch :

ElasticSearch 的默认端口是 9200 ,把宿主环境 9200 端口映射到 Docker 容器中的 9200 端口,就可以访问到 Docker 容器中的 ElasticSearch 服务了。

默认 ElasticSearch 对应的名称会随机生成,比如:confident_leakey

sudo docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.5.1

如果加参数: –name es,可以把这个容器命名为 es :

sudo docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.5.1

检查是否运行成功:

另开SSH窗口,运行:

curl http://localhost:9200

输出:

{
  "name" : "0c4c8e087553",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "5bwf_aADTPSzEP8Irl9yJg",
  "version" : {
    "number" : "7.5.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "3ae9ac9a93c95bd0cdc054951cf95d88e1e18d96",
    "build_date" : "2019-12-16T22:57:37.835892Z",
    "build_snapshot" : false,
    "lucene_version" : "8.3.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

访问 http://localhost:9200 ,默认情况下无法外网访问。

查看当前运行的容器:

docker ps

输出:

CONTAINER ID        IMAGE                                                 COMMAND                  CREATED             STATUS              PORTS                                            NAMES
0c4c8e087553        docker.elastic.co/elasticsearch/elasticsearch:7.5.1   "/usr/local/bin/dock…"   2 hours ago         Up 2 hours          0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   confident_leakey

守护进程运行:

sudo docker run -itd -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.5.1

输出:

docker: Error response from daemon: driver failed programming external connectivity on endpoint hungry_cori (ae80fda6970d84728d21aa8e24dd0c83c1aa4829de5a6cf5a91c2f08c00ea87e): Bind for 0.0.0.0:9300 failed: port is already allocated.

参考:https://www.maoyuanrun.com/2017/01/12/docker-port-is-already-allocated/

查看进程,发现相关的容器并没有在运行,而 docker-proxy 却依然绑定着端口:

docker ps
ps -aux | grep -v grep | grep confident_leakey

停止 doker 进程,删除所有容器,然后删除 local-kv.db 这个文件,再启动 docker :

sudo service docker stop
docker rm $(docker ps -aq)
sudo rm /var/lib/docker/network/files/local-kv.db
sudo service docker start

【配置】

由于要进行配置,因此需要进入容器当中修改相应的配置信息:

docker exec -it confident_leakey /bin/bash

进行配置:

显示文件:

ls

输出:

LICENSE.txt  NOTICE.txt  README.textile  bin  config  data  jdk  lib  logs  modules  plugins

进入配置文件夹:

cd config

显示文件:

ls

输出:

elasticsearch.keystore  elasticsearch.yml  jvm.options  log4j2.properties  role_mapping.yml  roles.yml  users  users_roles

修改配置文件:

vi elasticsearch.yml

加入跨域配置

http.cors.enabled: true
http.cors.allow-origin: "*"

按 Esc ;输入::q! 保存退出。

按 Ctrl + c ,退出容器。

由于修改了配置,因此需要重启ElasticSearch容器:

docker restart confident_leakey