Docker安裝Elasticsearch的實(shí)現(xiàn)示例
一、安裝 Elasticsearch
(一)創(chuàng)建 Docker 網(wǎng)絡(luò)
因?yàn)楹罄m(xù)還需要部署 Kibana 容器,所以需要讓 Elasticsearch 和 Kibana 容器互聯(lián)。創(chuàng)建一個(gè) Docker 網(wǎng)絡(luò):
docker network create es-net
(二)拉取 Elasticsearch 鏡像
以安裝 Elasticsearch 8.6.0 版本為例,執(zhí)行以下命令拉取鏡像:
docker pull elasticsearch:8.6.0
(三)創(chuàng)建掛載點(diǎn)目錄
創(chuàng)建用于掛載數(shù)據(jù)、配置和插件的目錄,并設(shè)置權(quán)限:
mkdir -p /usr/local/es/data /usr/local/es/config /usr/local/es/plugins chmod 777 /usr/local/es/data chmod 777 /usr/local/es/config chmod 777 /usr/local/es/plugins
(四)部署單點(diǎn) Elasticsearch 容器
運(yùn)行以下命令啟動(dòng) Elasticsearch 容器:
docker run -d \ --restart=always \ --name es \ --network es-net \ -p 9200:9200 \ -p 9300:9300 \ --privileged \ -v /usr/local/es/data:/usr/share/elasticsearch/data \ -v /usr/local/es/plugins:/usr/share/elasticsearch/plugins \ -e "discovery.type=single-node" \ -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \ elasticsearch:8.6.0
(五)關(guān)閉安全驗(yàn)證(可選)
進(jìn)入 Elasticsearch 容器,關(guān)閉安全驗(yàn)證功能:
docker exec -it es /bin/bash cd config echo 'xpack.security.enabled: false' >> elasticsearch.yml
(六)重啟 Elasticsearch 容器
退出容器后,重啟容器以使配置生效:
docker restart es
(七)測(cè)試 Elasticsearch 是否安裝成功
訪問 http://<服務(wù)器IP>:9200,如果返回類似以下內(nèi)容,說明安裝成功:
{
"name" : "es",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "xxxxxx",
"version" : {
"number" : "8.6.0",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "xxxxxx",
"build_date" : "xxxxxx",
"build_snapshot" : false,
"lucene_version" : "xxxxxx",
"minimum_wire_compatibility_version" : "xxxxxx",
"minimum_index_compatibility_version" : "xxxxxx"
},
"tagline" : "You Know, for Search"
}二、安裝 Kibana
(一)拉取 Kibana 鏡像
拉取與 Elasticsearch 版本匹配的 Kibana 鏡像:
docker pull kibana:8.6.0
(二)創(chuàng)建掛載點(diǎn)目錄
創(chuàng)建用于掛載 Kibana 數(shù)據(jù)和配置的目錄,并設(shè)置權(quán)限:
mkdir -p /usr/local/kibana/config /usr/local/kibana/data chmod 777 /usr/local/kibana/data chmod 777 /usr/local/kibana/config
(三)部署 Kibana 容器
運(yùn)行以下命令啟動(dòng) Kibana 容器:
docker run -d \ --restart=always \ --name kibana \ --network es-net \ -p 5601:5601 \ -e ELASTICSEARCH_HOSTS=http://es:9200 \ kibana:8.6.0
(四)測(cè)試 Kibana 是否安裝成功
訪問 http://<服務(wù)器IP>:5601,如果能夠正常訪問 Kibana 的 Web 界面,說明安裝成功。
三、安裝 IK 分詞器
(一)進(jìn)入 Elasticsearch 容器
docker exec -it es /bin/bash
(二)安裝 IK 分詞器
在容器內(nèi)執(zhí)行以下命令安裝 IK 分詞器:
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.6.0/elasticsearch-analysis-ik-8.6.0.zip
(三)重啟 Elasticsearch 容器
退出容器后,重啟 Elasticsearch 容器以使插件生效:
docker restart es
四、Elasticsearch 常用配置
(一)修改 Elasticsearch 配置文件
進(jìn)入容器的配置目錄,編輯 elasticsearch.yml 文件:
docker exec -it es /bin/bash cd config vim elasticsearch.yml
常見的配置項(xiàng)包括:
cluster.name:設(shè)置集群名稱node.name:設(shè)置節(jié)點(diǎn)名稱path.data:設(shè)置數(shù)據(jù)存儲(chǔ)路徑path.logs:設(shè)置日志存儲(chǔ)路徑network.host:設(shè)置綁定的網(wǎng)絡(luò)接口http.port:設(shè)置 HTTP 服務(wù)端口
(二)設(shè)置 JVM 堆大小
通過環(huán)境變量 ES_JAVA_OPTS 設(shè)置 JVM 堆大小,例如:
docker run -d \ --restart=always \ --name es \ --network es-net \ -p 9200:9200 \ -p 9300:9300 \ --privileged \ -v /usr/local/es/data:/usr/share/elasticsearch/data \ -v /usr/local/es/plugins:/usr/share/elasticsearch/plugins \ -e "discovery.type=single-node" \ -e "ES_JAVA_OPTS=-Xms2g -Xmx2g" \ elasticsearch:8.6.0
五、Elasticsearch 常用命令
(一)索引操作
創(chuàng)建索引
curl -X PUT "http://<服務(wù)器IP>:9200/my_index" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
'刪除索引
curl -X DELETE "http://<服務(wù)器IP>:9200/my_index"
查看索引
curl -X GET "http://<服務(wù)器IP>:9200/_cat/indices?v"
(二)文檔操作
添加文檔
curl -X POST "http://<服務(wù)器IP>:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
"field1": "value1",
"field2": "value2"
}
'查詢文檔
curl -X GET "http://<服務(wù)器IP>:9200/my_index/_doc/1"
更新文檔
curl -X POST "http://<服務(wù)器IP>:9200/my_index/_doc/1/_update" -H 'Content-Type: application/json' -d'
{
"doc": {
"field1": "new_value1"
}
}
'刪除文檔
curl -X DELETE "http://<服務(wù)器IP>:9200/my_index/_doc/1"
(三)集群狀態(tài)查詢
curl -X GET "http://<服務(wù)器IP>:9200/_cluster/health?pretty"
六、總結(jié)
通過以上步驟,你可以在 Docker 上成功安裝 Elasticsearch,并配置常用的網(wǎng)絡(luò)、數(shù)據(jù)掛載和插件安裝。掌握這些常用命令,可以幫助你更高效地管理和使用 Elasticsearch 集群。如果在使用過程中遇到任何問題,可以參考 Elasticsearch 官方文檔 或社區(qū)尋求幫助。
到此這篇關(guān)于Docker安裝Elasticsearch的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Docker安裝Elasticsearch內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux下docker 容器退出bash的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Linux下docker 容器退出bash的兩種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Docker Desktop啟動(dòng)失敗的解決(Docker failed to i
本文主要介紹了Docker Desktop啟動(dòng)失敗的解決(Docker failed to initialize Docker Desktop is shutting down),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
docker安裝單機(jī)版kafka并使用的詳細(xì)步驟
這篇文章主要為大家詳細(xì)介紹了docker安裝單機(jī)版kafka并使用的詳細(xì)步驟,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-06-06
Docker部署ELK7.3.0日志收集服務(wù)最佳實(shí)踐
這篇文章主要介紹了Docker部署ELK7.3.0日志收集服務(wù)最佳實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

