elasticsearch 6.2.4和elasticsearch-head环境搭建 使用docker-compose方式

elasticsearch 6.2.4和elasticsearch-head测试环境搭建 使用docker-compose方式

一 背景说明

对于新手来说搭建一个elasticsearch的测试环境稍微费力,而且没必须要去下载那么多安装包,比如java node npm等,所以我决定做成docker-compose方式的启动,并且集成ik分词器,方便快捷,一键搞定。

二 环境需求

你电脑需要安装docker和docker-compose

三 使用步骤

1 目录结构

.
├── docker-compose-elastic.yml
└── elasticsearch.yml

elasticsearch.yml

先编写elasticsearch的配置文件,后续是需要挂载到docker容器中的
elasticsearch.yml的内容如下:

network.bind_host: 0.0.0.0
cluster.name: elastic-cluster
network.host: 0.0.0.0
discovery.zen.minimum_master_nodes: 1
bootstrap.memory_lock: true
discovery.type: single-node
http.cors.enabled: true
http.cors.allow-origin: "*"

docker-compose-elastic.yml

docker-compose-elastic.yml 这里docker-compose需要的配置文件
elasticsearch.yml的内容如下:

version: ‘2‘
services:
  elastic:
    image: bachue/elasticsearch-ik:6.2.4
    container_name: elastic
    environment:
      - TZ=‘Asia/Shanghai‘
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 1g
    volumes:
      - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - ./data:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
      - 9300:9300
  elasticsearch-head:
      image: wallbase/elasticsearch-head:6-alpine
      container_name: elasticsearch-head
      environment:
        TZ: ‘Asia/Shanghai‘
      ports:
        - ‘9100:9100‘

docker-compose配置说明

bachue/elasticsearch-ik:6.2.4 表示是带有ik分词的,不需要额外自己安装,
volumes 配置项主要是把本地文件挂载到容器中,包含配置文件和数据目录,data是数据目录,如果你只是测试,也可以不需要,但是你下次启动数据就丢失了。
wallbase/elasticsearch-head:6-alpine 从5.0之后head插件可以独立启动,我重新打包一个支持6.2.x版本的docker镜像上传的hub.docker.com中的。

四 启动

启动elasticsearch和elasticsearch-head,参数-d表示后台启动,当然你需要交互式启动可以把-d去掉,elasticsearch端口是9200,head插件的访问端口是9100
haima@haima-PC:/usr/local/docker/elasticsearch6.2.4$ docker-compose -f docker-compose-elastic.yml up -d
Creating elasticsearch-head ... done
Creating elastic            ... done
haima@haima-PC:/usr/local/docker/elasticsearch6.2.4$ docker ps -a
CONTAINER ID        IMAGE                                                 COMMAND                  CREATED              STATUS                       PORTS                    NAMES
2d2cb0233af1        bachue/elasticsearch-ik:6.2.4                         "/usr/local/bin/dock…"   About a minute ago   Exited (1) 53 seconds ago                             elastic
6b437ba795b3        wallbase/elasticsearch-head:6-alpine                  "/bin/sh -c ‘node_mo…"   About a minute ago   Up About a minute            0.0.0.0:9100->9100/tcp   elasticsearch-head

如上,es报错
查看日志

haima@haima-PC:/usr/local/docker/elasticsearch6.2.4$ docker logs 5b4f181a5cfc
[2021-07-22T23:18:48,414][INFO ][o.e.n.Node               ] [] initializing ...
[2021-07-22T23:18:48,438][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: Failed to create node environment
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.4.jar:6.2.4]
	at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:85) ~[elasticsearch-6.2.4.jar:6.2.4]
Caused by: java.lang.IllegalStateException: Failed to create node environment
	at org.elasticsearch.node.Node.<init>(Node.java:267) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.node.Node.<init>(Node.java:246) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:213) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:213) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:323) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) ~[elasticsearch-6.2.4.jar:6.2.4]
	... 6 more
Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
	at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84) ~[?:?]
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) ~[?:?]
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) ~[?:?]
	at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384) ~[?:?]
	at java.nio.file.Files.createDirectory(Files.java:674) ~[?:1.8.0_161]
	at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781) ~[?:1.8.0_161]
	at java.nio.file.Files.createDirectories(Files.java:767) ~[?:1.8.0_161]
	at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:204) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.node.Node.<init>(Node.java:264) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.node.Node.<init>(Node.java:246) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:213) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:213) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:323) ~[elasticsearch-6.2.4.jar:6.2.4]
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) ~[elasticsearch-6.2.4.jar:6.2.4]
	... 6 more
haima@haima-PC:/usr/local/docker/elasticsearch6.2.4$ ll
总用量 20
drwxrwxrwx 3 haima staff 4096 7月  23 07:18 .
drwxrwsrwx 9 root  staff 4096 7月  23 06:49 ..
drwxr-xr-x 2 root  root  4096 7月  23 07:18 data
-rwxrwxrwx 1 haima staff  645 7月  23 06:58 docker-compose.yml
-rwxrwxrwx 1 haima staff  225 7月  23 06:51 elasticsearch.yml

从上面可以看挂载的data目录没有权限
所以要给一下权限

root@haima-PC:/usr/local/docker/elasticsearch6.2.4# docker-compose down #移除之前的容器
Stopping elasticsearch-head ... done
Removing elasticsearch-head ... done
Removing elastic            ... done
Removing network elasticsearch624_default

haima@haima-PC:/usr/local/docker/elasticsearch6.2.4$ su root #切换root账号
密码:

root@haima-PC:/usr/local/docker/elasticsearch6.2.4# chmod -R 777 data/ #给data目录权限
root@haima-PC:/usr/local/docker/elasticsearch6.2.4# ll
总用量 20
drwxrwxrwx 3 haima staff 4096 7月  23 07:20 .
drwxrwsrwx 9 root  staff 4096 7月  23 06:49 ..
drwxrwxrwx 2 root  root  4096 7月  23 07:20 data
-rwxrwxrwx 1 haima staff  645 7月  23 06:58 docker-compose.yml
-rwxrwxrwx 1 haima staff  225 7月  23 06:51 elasticsearch.yml

root@haima-PC:/usr/local/docker/elasticsearch6.2.4# docker-compose up -d #启动容器
Creating network "elasticsearch624_default" with the default driver
Creating elastic            ... done
Creating elasticsearch-head ... done

root@haima-PC:/usr/local/docker/elasticsearch6.2.4# docker ps -a #查看结果,启动成功
CONTAINER ID        IMAGE                                                 COMMAND                  CREATED             STATUS                       PORTS                                            NAMES
010f6ccf0419        wallbase/elasticsearch-head:6-alpine                  "/bin/sh -c ‘node_mo…"   5 seconds ago       Up 3 seconds                 0.0.0.0:9100->9100/tcp                           elasticsearch-head
2e29d2f5899d        bachue/elasticsearch-ik:6.2.4                         "/usr/local/bin/dock…"   5 seconds ago       Up 2 seconds                 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   elastic

访问浏览器 http://localhost:9100/

安装linux 的 json格式化包

apt-get -y install jq

root@haima-PC:/usr/local/docker/elasticsearch6.2.4# curl "127.0.0.1:9200" | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   369  100   369    0     0  30021      0 --:--:-- --:--:-- --:--:-- 46125
{
  "error": {
    "root_cause": [
      {
        "type": "security_exception",
        "reason": "missing authentication token for REST request [/]",
        "header": {
          "WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""
        }
      }
    ],
    "type": "security_exception",
    "reason": "missing authentication token for REST request [/]",
    "header": {
      "WWW-Authenticate": "Basic realm=\"security\" charset=\"UTF-8\""
    }
  },
  "status": 401
}

参考文章:
https://blog.csdn.net/WK313753744/article/details/81316744 #docker安装参考文章
https://blog.csdn.net/s1078229131/article/details/90763811 #报错解决参考文章
https://blog.csdn.net/slsunxia/article/details/107609996

elasticsearch 6.2.4和elasticsearch-head环境搭建 使用docker-compose方式

上一篇:MySQL hash分区(四)


下一篇:MySQL range分区(二)