0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Elasticsearchを一番シンプルに試す

Last updated at Posted at 2023-12-11

やりたいこと

  • Elasticsearchをシングルノードで設定し、外部からデータを追加・検索できるようにする

検証環境

  • VagrantでホスティングしたUbuntu Server 22.04 LTS

インストール

$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
$ sudo apt update
$ sudo apt install apt-transport-https -y
$ echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
$ sudo apt update
$ sudo apt install elasticsearch -y

設定する

  • ※ 変更点のみ抜粋
  • シングルノードで動作するよう設定
  • リモートからHTTP(TCPポート9200番)でのアクセスを受け付けるよう設定
  • 認証やHTTPS通信をオフにするよう設定(検証を簡単にするため)
$ sudo vim /etc/elasticsearch/elasticsearch.yml
# cluster.initial_master_nodes: ["ubuntu2204.localdomain"]
discovery.type: single-node
http.host: 0.0.0.0
http.port: 9200
xpack.security.enabled: false
  • UFWを有効化する設定
  • TCPポート9200番への外部からのアクセスを許可する設定
$ sudo ufw status
Status: inactive

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

$ sudo ufw allow 9200/tcp
Rule added
Rule added (v6)

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
9200/tcp                   ALLOW       Anywhere                  
9200/tcp (v6)              ALLOW       Anywhere (v6)

起動する

$ sudo systemctl start elasticsearch

$ sudo systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-12-11 00:53:44 UTC; 45s ago
       Docs: https://www.elastic.co
   Main PID: 3703 (java)
      Tasks: 74 (limit: 2237)
     Memory: 1.3G
        CPU: 1min 10.987s
     CGroup: /system.slice/elasticsearch.service
             ├─3703 /usr/share/elasticsearch/jdk/bin/java -Xms4m -Xmx64m -XX:+UseSerialGC -Dcli.name=server -Dcli.script=/usr/share/elasticsearch/bin/elasticsearch -Dcli.libs=lib/tools/server-cli -Des.path.home=/usr/>
             ├─3761 /usr/share/elasticsearch/jdk/bin/java -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -Djava.security.manager=allow -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true ->
             └─3781 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

Dec 11 00:53:09 ubuntu2204.localdomain systemd[1]: Starting Elasticsearch...
Dec 11 00:53:13 ubuntu2204.localdomain systemd-entrypoint[3703]: Dec 11, 2023 12:53:13 AM sun.util.locale.provider.LocaleProviderAdapter <clinit>
Dec 11 00:53:13 ubuntu2204.localdomain systemd-entrypoint[3703]: WARNING: COMPAT locale provider will be removed in a future release
Dec 11 00:53:44 ubuntu2204.localdomain systemd[1]: Started Elasticsearch.

$ ss -nlp | grep 9200
tcp   LISTEN 0      4096                                            *:9200                   *:* 

動作確認

# ヘルスチェック
$ curl -X GET "192.168.56.10:9200/_cluster/health?pretty"
{
  "cluster_name" : "elasticsearch",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

# messagesインデックスを作成
$ curl -X PUT "192.168.56.10:9200/messages?pretty"
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "messages"
}

# データを作成
$ curl -X POST "192.168.56.10:9200/messages/_doc/1/?pretty" -H "Content-Type: application/json" -d '{"message": "hello, world"}'
{
  "_index" : "messages",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

# データを確認
$ curl -X GET "192.168.56.10:9200/messages/_doc/1?pretty" 
{
  "_index" : "messages",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "message" : "hello, world"
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?