引言:
Elasticsearch(ES)作為分布式搜索引擎,其核心價值在于通過集群部署實現高可用性和數據冗余。 本實驗對比兩種典型部署方案: 原生Linux部署:直接安裝ES服務,適用于生產環境,資源利用率高,但需手動管理多節點。?Docker容器化部署:通過容器快速搭建集群,適合測試或動態擴展場景,具備資源隔離和快速回滾優勢。 掌握原生Linux環境下ES集群(≥2節點)的搭建與配置。
克隆一臺新虛擬機(ip10.1.1.42)
也需關閉Slinux和防火墻
一. 創建集群所需要的目錄
1.1 創建ES的掛載目錄
mkdir -p /usr/local/elasticsearch_jiqun_3_node/node-{1..3}/{config,plugins,data,log}
chmod 777 /usr/local/elasticsearch_jiqun_3_node/node-{1..3}/{config,plugins,data,log}
mkdir -p /usr/local/elasticsearch_jiqun_3_node/node-{1..3}/{config,plugins,data,log}
chmod 777 /usr/local/elasticsearch_jiqun_3_node/node-{1..3}/{config,plugins,data,log}
1.2 創建kibana的掛載目錄
mkdir -p /usr/local/elasticsearch_jiqun_3_node/kibana/config/
chmod 777 /usr/local/elasticsearch_jiqun_3_node/kibana/config/
mkdir -p /usr/local/elasticsearch_jiqun_3_node/kibana/config/
chmod 777 /usr/local/elasticsearch_jiqun_3_node/kibana/config/
二.修改Linux的句柄數
vim /etc/sysctl.conf
添加如下內容:(vm.max_map_count=655360)
接下來再關閉swap
swapoff -a
三.修改最大線程數
因為ES運行期間可能創建大量線程,如果線程數支持較少可能報錯
vim /etc/security/limits.conf
# 添加以下內容
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
# 添加以下內容
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
重啟
reboot
四.添加IK分詞器
下載方式1
鏈接: https://pan.baidu.com/s/1zxetPFZyRPyxwFFIL113DQ 提取碼: 9m6a
下載方式2
在github中下載對應版本的分詞器,網址:Releases · infinilabs/analysis-ik · GitHub
將下載的分詞器復制到ES安裝目錄的plugins目錄中并進行解壓
將這個ik目錄分別添加到ES的三個節點的plugins目錄里
cp -R ik/*?/usr/local/elasticsearch_jiqun_3_node/node-1/plugins/
cp -R ik/* /usr/local/elasticsearch_jiqun_3_node/node-2/plugins/
cp -R ik/*?/usr/local/elasticsearch_jiqun_3_node/node-3/plugins/
cp -R ik/* /usr/local/elasticsearch_jiqun_3_node/node-1/plugins/
cp -R ik/* /usr/local/elasticsearch_jiqun_3_node/node-2/plugins/
cp -R ik/* /usr/local/elasticsearch_jiqun_3_node/node-3/plugins/
五. 編寫配置文件
5.1 節點文件
Node-1
vi /usr/local/elasticsearch_jiqun_3_node/node-1/config/elasticsearch.yml
cluster.name: elastic
node.name: node-1
node.master: true
node.data: true
node.max_local_storage_nodes: 3
path.data: /usr/share/elasticsearch/data
path.logs: /usr/share/elasticsearch/log
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["node-1","node-2","node-3"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
gateway.recover_after_nodes: 2
xpack.security.enabled: false
cluster.name: elastic
node.name: node-1
node.master: true
node.data: true
node.max_local_storage_nodes: 3
path.data: /usr/share/elasticsearch/data
path.logs: /usr/share/elasticsearch/log
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["node-1","node-2","node-3"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
gateway.recover_after_nodes: 2
xpack.security.enabled: false
Node-2
vi /usr/local/elasticsearch_jiqun_3_node/node-2/config/elasticsearch.yml
cluster.name: elastic
node.name: node-2
node.master: true
node.data: true
node.max_local_storage_nodes: 3
path.data: /usr/share/elasticsearch/data
path.logs: /usr/share/elasticsearch/log
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["node-1","node-2","node-3"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
gateway.recover_after_nodes: 2
xpack.security.enabled: false
cluster.name: elasticnode.name: node-2node.master: truenode.data: truenode.max_local_storage_nodes: 3path.data: /usr/share/elasticsearch/datapath.logs: /usr/share/elasticsearch/lognetwork.host: 0.0.0.0http.port: 9200transport.tcp.port: 9300discovery.seed_hosts: ["node-1","node-2","node-3"]cluster.initial_master_nodes: ["node-1","node-2","node-3"]gateway.recover_after_nodes: 2xpack.security.enabled: false
Node-3
vi /usr/local/elasticsearch_jiqun_3_node/node-3/config/elasticsearch.yml
cluster.name: elastic
node.name: node-3
node.master: true
node.data: true
node.max_local_storage_nodes: 3
path.data: /usr/share/elasticsearch/data
path.logs: /usr/share/elasticsearch/log
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["node-1","node-2","node-3"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
gateway.recover_after_nodes: 2
xpack.security.enabled: false
cluster.name: elastic
node.name: node-3
node.master: true
node.data: true
node.max_local_storage_nodes: 3
path.data: /usr/share/elasticsearch/data
path.logs: /usr/share/elasticsearch/log
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.seed_hosts: ["node-1","node-2","node-3"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
gateway.recover_after_nodes: 2
xpack.security.enabled: false
該配置文件定義了 3 個?Elasticsearch?節點,它們組成一個集群,每個節點都將數據存儲在獨立的卷上。
5.2 Kibana文件
vi ?/usr/local/elasticsearch_jiqun_3_node/kibana/config/kibana.yml
server.host: 0.0.0.0
# 監聽端口
server.port: 5601
server.name: "kibana"
# kibana訪問es服務器的URL,就可以有多個,以逗號","隔開
elasticsearch.hosts: ["http://node-1:9200","http://node-2:9201","http://node-3:9202"]
monitoring.ui.container.elasticsearch.enabled: true
# kibana訪問Elasticsearch的賬號與密碼(如果ElasticSearch設置了的話)
elasticsearch.username: "kibana"
elasticsearch.password: "12345"
# kibana日志文件存儲路徑
logging.dest: stdout
# 此值為true時,禁止所有日志記錄輸出
logging.silent: false
# 此值為true時,禁止除錯誤消息之外的所有日志記錄輸出
logging.quiet: false
# 此值為true時,記錄所有事件,包括系統使用信息和所有請求
logging.verbose: false
ops.interval: 5000
# kibana web語言
i18n.locale: "zh-CN"
5.3 編寫Docker部署文檔
cd /usr/local/elasticsearch_jiqun_3_node/
vi docker-compose.yml
version: "3"
services:node-1:image: elasticsearch:7.17.5container_name: es_jiqun_3node-node-1environment:- "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"- "TZ=Asia/Shanghai"ulimits:memlock:soft: -1hard: -1nofile:soft: 65536hard: 65536ports:- "9200:9200"logging:driver: "json-file"options:max-size: "50m"volumes:- /usr/local/elasticsearch_jiqun_3_node/node-1/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml- /usr/local/elasticsearch_jiqun_3_node/node-1/plugins:/usr/share/elasticsearch/plugins- /usr/local/elasticsearch_jiqun_3_node/node-1/data:/usr/share/elasticsearch/data- /usr/local/elasticsearch_jiqun_3_node/node-1/log:/usr/share/elasticsearch/lognetworks:- elasticnode-2:image: elasticsearch:7.17.5container_name: es_jiqun_3node-node-2environment:- "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"- "TZ=Asia/Shanghai"ulimits:memlock:soft: -1hard: -1nofile:soft: 65536hard: 65536ports:- "9201:9200"logging:driver: "json-file"options:max-size: "50m"volumes:- /usr/local/elasticsearch_jiqun_3_node/node-2/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml- /usr/local/elasticsearch_jiqun_3_node/node-2/plugins:/usr/share/elasticsearch/plugins- /usr/local/elasticsearch_jiqun_3_node/node-2/data:/usr/share/elasticsearch/data- /usr/local/elasticsearch_jiqun_3_node/node-2/log:/usr/share/elasticsearch/lognetworks:- elasticnode-3:image: elasticsearch:7.17.5container_name: es_jiqun_3node-node-3environment:- "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"- "TZ=Asia/Shanghai"ulimits:memlock:soft: -1hard: -1nofile:soft: 65536hard: 65536ports:- "9202:9200"logging:driver: "json-file"options:max-size: "50m"volumes:- /usr/local/elasticsearch_jiqun_3_node/node-3/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml- /usr/local/elasticsearch_jiqun_3_node/node-3/plugins:/usr/share/elasticsearch/plugins- /usr/local/elasticsearch_jiqun_3_node/node-3/data:/usr/share/elasticsearch/data- /usr/local/elasticsearch_jiqun_3_node/node-3/log:/usr/share/elasticsearch/lognetworks:- elastickibana:container_name: es_jiqun_3node_node-kibanaimage: kibana:7.17.5volumes:- /usr/local/elasticsearch_jiqun_3_node/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.ymlports:- 5601:5601networks:- elasticcerebro:image: lmenezes/cerebro:0.9.4container_name: es_jiqun_3node_node-cerebroenvironment:TZ: 'Asia/Shanghai'ports:- '9000:9000'networks:- elastic
networks:elastic:
driver: bridge
六.?啟動服務
七.測試集群
輸入http://10.1.1.42:9200