🗒️
「DockerでElasticSearchをインストールする」を実践してみる
前提条件
- WSL(ディストリビューションはUbuntu)上にDockerがインストールされている状態です
- Elasticのバージョンは8.13.2です
公式のドキュメント
単一ノードクラスターの起動
Dockerネットワークの作成
docker network create elastic
実行結果
$ docker network create elastic
ac1c2b8d157533b9065842830f63b59d3c812ad3dcacc8664363e5bd9acb704e
Elasticsearch Dockerイメージをプル
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.13.2
実行結果
$ docker pull docker.elastic.co/elasticsearch/elasticsearch:8.13.2
8.13.2: Pulling from elasticsearch/elasticsearch
632255af43b7: Pull complete
647ae1aba397: Pull complete
6a960bb0c920: Pull complete
4ca545ee6d5d: Pull complete
78d97d54a176: Pull complete
b0090edc29a5: Pull complete
a60eedb987f4: Pull complete
755760cea5e1: Pull complete
feb4f5f52110: Pull complete
f9d98cfd8121: Pull complete
Digest: sha256:8b4d01da83506c509c3fe8125b23ca598c02fff212b3489b7b496c3ff769eaa2
Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch:8.13.2
docker.elastic.co/elasticsearch/elasticsearch:8.13.2
Elasticsearchコンテナを起動
docker run --name es01 --net elastic -p 9200:9200 -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.13.2
実行結果
$ docker run --name es01 --net elastic -p 9200:9200 -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.13.2
Apr 28, 2024 6:20:15 AM sun.util.locale.provider.LocaleProviderAdapter <clinit>
WARNING: COMPAT locale provider will be removed in a future release
{"@timestamp":"2024-04-28T06:20:16.236Z", "log.level": "INFO", "message":"Using [jdk] native provider and native methods for [Linux]", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"main","log.logger":"org.elasticsearch.nativeaccess.NativeAccess","elasticsearch.node.name":"0f103c74a41c","elasticsearch.cluster.name":"docker-cluster"}
{"@timestamp":"2024-04-28T06:20:16.411Z", "log.level": "INFO", "message":"Java vector incubator API enabled; uses preferredBitSize=512; FMA enabled", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"main","log.logger":"org.apache.lucene.internal.vectorization.PanamaVectorizationProvider","elasticsearch.node.name":"0f103c74a41c","elasticsearch.cluster.name":"docker-cluster"}
{"@timestamp":"2024-04-28T06:20:16.713Z", "log.level": "INFO", "message":"version[8.13.2], pid[82], build[do
(略)
パスワードと登録トークンをコピー
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
実行結果
$ docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
WARNING: Owner of file [/usr/share/elasticsearch/config/users] used to be [root], but now is [elasticsearch]
WARNING: Owner of file [/usr/share/elasticsearch/config/users_roles] used to be [root], but now is [elasticsearch]
This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y
Password for the [elastic] user successfully reset.
New value: -jiUt2RHnTB5OhJtTObP
---
$ docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
eyJ2ZXIiOiI4LjEzLjIiLCJhZHIiOlsiMTcyLjE5LjAuMjo5MjAwIl0sImZnciI6IjBjN2ZiYWFkMzIzZjgwNjQwZWU3NjdkMjI3NmE2N2IxNmY2ZmM4ZTQ2MTdkMmMyNjE0YWQ5ZDg2MmNkYWJmMmEiLCJrZXkiOiJkX0J3STQ4QkVVcUJtaVZOOXc1MjozaC02bm11MFNjdTBwejhrNFVyeDJ3In0=
パスワードを環境変数設定
export ELASTIC_PASSWORD="your_password"
実行結果
$ export ELASTIC_PASSWORD=-jiUt2RHnTB5OhJtTObP
SSL証明書をコンテナからローカルマシンにコピー
docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
実行結果
$ docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .
Successfully copied 3.58kB to /mnt/c/.
動作確認
curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200
実行結果
$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200
{
"name" : "86e1e9162f3f",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "peZKRAfORCCBY3_3j6fjZQ",
"version" : {
"number" : "8.13.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "16cc90cd2d08a3147ce02b07e50894bc060a4cbf",
"build_date" : "2024-04-05T14:45:26.420424304Z",
"build_snapshot" : false,
"lucene_version" : "9.10.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
ノードの追加
新しいノードの登録トークンを生成する
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node
実行結果
$ docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node
eyJ2ZXIiOiI4LjEzLjIiLCJhZHIiOlsiMTcyLjE5LjAuMjo5MjAwIl0sImZnciI6IjBjN2ZiYWFkMzIzZjgwNjQwZWU3NjdkMjI3NmE2N2IxNmY2ZmM4ZTQ2MTdkMmMyNjE0YWQ5ZDg2MmNkYWJmMmEiLCJrZXkiOiJlZkR5SjQ4QkVVcUJtaVZOREE3Xzp0MUVDbm1pY1RLdUtUdy13eG8xbVdRIn0=
新しいElasticsearchコンテナを起動
docker run -e ENROLLMENT_TOKEN="<token>" --name es02 --net elastic -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.13.2
実行確認
docker run -e ENROLLMENT_TOKEN="eyJ2ZXIiOiI4LjEzLjIiLCJhZHIiOlsiMTcyLjE5LjAuMjo5MjAwIl0sImZnciI6IjBjN2ZiYWFkMzIzZjgwNjQwZWU3NjdkMjI3NmE2N2IxNmY2ZmM4ZTQ2MTdkMmMyNjE0YWQ5ZDg2MmNkYWJmMmEiLCJrZXkiOiJlZkR5SjQ4QkVVcUJtaVZOREE3Xzp0MUVDbm1pY1RLdUtUdy13eG8xbVdRIn0=" --name es02 --net elastic -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.13.2
Apr 29, 2024 3:46:25 AM sun.util.locale.provider.LocaleProviderAdapter <clinit>
WARNING: COMPAT locale provider will be removed in a future release
{"@timestamp":"2024-04-29T03:46:26.202Z", "log.level": "INFO", "message":"Using [jdk] native provider and native methods for [Linux]", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"main","log.logger":"org.elasticsearch.nativeaccess.NativeAccess","elasticsearch.node.name":"121cb192b9b9","elasticsearch.cluster.name":"docker-cluster"}
ノード追加の確認
curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200/_cat/nodes
実行結果
$ curl --cacert http_ca.crt -u elastic:$ELASTIC_PASSWORD https://localhost:9200/_cat/nodes
172.19.0.2 30 91 0 0.23 0.07 0.02 cdfhilmrstw * 86e1e9162f3f
172.19.0.3 23 95 2 0.23 0.07 0.02 cdfhilmrstw - 121cb192b9b9
Kibanaを実行
Kibana Dockerイメージをプル
docker pull docker.elastic.co/kibana/kibana:8.13.2
実行結果
$ docker pull docker.elastic.co/kibana/kibana:8.13.2
8.13.2: Pulling from kibana/kibana
632255af43b7: Already exists
db5dd536dc89: Pull complete
0d14b4ee3e35: Pull complete
aaaf7cd972e2: Pull complete
2004e165fcfc: Pull complete
4ca545ee6d5d: Pull complete
bc14db93cb1b: Pull complete
7ee4063da1f2: Pull complete
14a0138d0d03: Pull complete
75e1d993d2e6: Pull complete
2a29eeba638e: Pull complete
34b49a8e25a7: Pull complete
2746073a14bd: Pull complete
6230135ee40d: Pull complete
Digest: sha256:8d1ec6898e433c577bbe272c25366b880341fb8f8e0815314ce62ae50399caef
Status: Downloaded newer image for docker.elastic.co/kibana/kibana:8.13.2
docker.elastic.co/kibana/kibana:8.13.2
Kibanaコンテナを起動
docker run --name kib01 --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.13.2
実行結果
$ docker run --name kib01 --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.13.2
Kibana is currently running with legacy OpenSSL providers enabled! For details and instructions on how to disable see https://www.elastic.co/guide/en/kibana/8.13/production.html#openssl-legacy-provider
{"log.level":"info","@timestamp":"2024-04-29T05:10:26.075Z","log.logger":"elastic-apm-node","ecs.version":"8.10.0","agentVersion":"4.4.0","env":{"pid":7,"proctitle":"/usr/share/kibana/bin/../node/bin/node","os":"linux 5.15.146.1-microsoft-standard-WSL2","arch":"x64","host":"cb5a2b583e59","timezone":"UTC+00","runtime":"Node.js v20.12.1"},"config":{"active":{"source":"start","value":true},"breakdownMetrics":{"source":"start","value":false},"captureBody":{"source":"start","value":"off","commonName":"capture_body"},"captureHeaders":{"source":"start","value":false},"centralConfig":{"source":"start","value":false},"contextPropagationOnly":{"source":"start","value":true},"environment":{"source":"start","value":"production"},"globalLabels":{"source":"start","value":[["git_rev","d4d06bdf0d1d5dcb4532f00d2cbaa83fc61bb877"]],"sourceValue":{"git_rev":"d4d06bdf0d1d5dcb4532f00d2cbaa83fc61bb877"}},"logLevel":{"source":"default","value":"info","commonName":"log_level"},"metricsInterval":{"source":"start","value":120,"sourceValue":"120s"},"serverUrl":{"source":"start","value":"https://kibana-cloud-apm.apm.us-east-1.aws.found.io/","commonName":"server_url"},"transactionSampleRate":{"source":"start","value":0.1,"commonName":"transaction_sample_rate"},"captureSpanStackTraces":{"source":"start","sourceValue":false},"secretToken":{"source":"start","value":"[REDACTED]","commonName":"secret_token"},"serviceName":{"source":"start","value":"kibana","commonName":"service_name"},"serviceVersion":{"source":"start","value":"8.13.2","commonName":"service_version"}},"activationMethod":"require","message":"Elastic APM Node.js Agent v4.4.0"}
Native global console methods have been overridden in production environment.
[2024-04-29T05:10:26.721+00:00][INFO ][root] Kibana is starting
[2024-04-29T05:10:26.758+00:00][INFO ][node] Kibana process configured with roles: [background_tasks, ui]
[2024-04-29T05:10:30.530+00:00][INFO ][plugins-service] The following plugins are disabled: "cloudChat,cloudExperiments,cloudFullStory,profilingDataAccess,profiling,securitySolutionServerless,serverless,serverlessObservability,serverlessSearch".
[2024-04-29T05:10:30.578+00:00][INFO ][http.server.Preboot] http server running at http://0.0.0.0:5601
[2024-04-29T05:10:30.658+00:00][INFO ][plugins-system.preboot] Setting up [1] plugins: [interactiveSetup]
[2024-04-29T05:10:30.669+00:00][INFO ][preboot] "interactiveSetup" plugin is holding setup: Validating Elasticsearch connection configuration…
[2024-04-29T05:10:30.692+00:00][INFO ][root] Holding setup until preboot stage is completed.
i Kibana has not been configured.
Go to http://0.0.0.0:5601/?code=388122 to get started.
Kibanaにアクセス
URLはhttp://localhost:5601/?code=388122
です。
トークンの登録
トークンの再生成
$ docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana
eyJ2ZXIiOiI4LjEzLjIiLCJhZHIiOlsiMTcyLjE5LjAuMjo5MjAwIl0sImZnciI6IjBjN2ZiYWFkMzIzZjgwNjQwZWU3NjdkMjI3NmE2N2IxNmY2ZmM4ZTQ2MTdkMmMyNjE0YWQ5ZDg2MmNkYWJmMmEiLCJrZXkiOiJvX0JGS0k4QkVVcUJtaVZOb0E2WTpUWEI3U211YVR3V2o2cXRnUEFYSC1RIn0=
トークンを入力して先へ
Kibanaにログイン
パスワードの再生成
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
実行結果
$ docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
This tool will reset the password of the [elastic] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]y
Password for the [elastic] user successfully reset.
New value: eUb19GTAN+06cWT4Qz02
Discussion