WSL に Elasticsearch 環境を整える

WSL 環境に Elasticsearch の環境を整えたくて試してみた。

www.elastic.co

こちらのサイトを試します。

1. Docker Image を 持ってくる

docker pull docker.elastic.co/elasticsearch/elasticsearch:8.6.1

2. Elasticsearch 用の docker network を作成

docker network create elastic

3. docker を実行

docker run --rm --name es01 --net elastic -p 9200:9200 -it docker.elastic.co/elasticsearch/elasticsearch:8.6.1

起動失敗

bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /usr/share/elasticsearch/logs/docker-cluster.log

他の方もどうようにElasitcsearch を起動するためのメモリが足りないので失敗する


対策として、実行時の vm.max_map_count の値を増やせばよいようだ

~ on 🅰  (tokyo)
❯ sudo sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144

~ on 🅰  (tokyo)
❯ sudo  sysctl -a | grep 'vm.max_map_count'
vm.max_map_count = 262144

再実行すると成功する

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ Elasticsearch security features have been automatically configured!
✅ Authentication is enabled and cluster connections are encrypted.

ℹ️  Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
*********************

ℹ️  HTTP CA certificate SHA-256 fingerprint:
*********************

ℹ️  Configure Kibana to use this cluster:
• Run Kibana and click the configuration link in the terminal when Kibana starts.
• Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
  *********************

ℹ️ Configure other nodes to join this cluster:
• Copy the following enrollment token and start new Elasticsearch nodes with `bin/elasticsearch --enrollment-token <token>` (valid for the next 30 minutes):
  *********************

  If you're running in Docker, copy the enrollment token and run:
  `docker run -e "ENROLLMENT_TOKEN=<token>" docker.elastic.co/elasticsearch/elasticsearch:8.6.1`
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

4. 証明書を取得

docker cp es01:/usr/share/elasticsearch/config/certs/http_ca.crt .

5. アクセス

❯ curl --cacert http_ca.crt -u elastic https://localhost:9200
Enter host password for user 'elastic':
{
  "name" : "b324833a4784",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "vaUPvdtwS2iI5CQnWLkSBA",
  "version" : {
    "number" : "8.6.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "180c9830da956993e59e2cd70eb32b5e383ea42c",
    "build_date" : "2023-01-24T21:35:11.506992272Z",
    "build_snapshot" : false,
    "lucene_version" : "9.4.2",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

成功

6. まとめ

簡単に立ち上がるだろうと思ったらメモリ周りでうまくいかなかったのが残念だった。実運用時は、サーバーの設定は確認しないといけないですね。