學習了這么多,終於開始搭建生產環境了,這一篇主要講解配置項,以及支持中文分詞的ik安裝,集群的搭建。
配置項確實挺多的,但把幾個常用配置熟悉就好,而且就像elasticsearch官方文檔所說,不存在某個使得性能大幅度提升的配置。
配置項
一下子看到這么多配置項,可能就嚇壞了,不要怕,用到的並不多。
cluster.name: elasticsearch
# 配置的集群名稱,默認是elasticsearch,es服務會通過廣播方式自動連接在同一網段下的es服務,通過多播方式進行通信,同一網段下可以有多個集群,通過集群名稱這個屬性來區分不同的集群。
node.name: "Franz Kafka"
# 當前配置所在機器的節點名,你不設置就默認隨機指定一個name列表中名字,該name列表在es的jar包中config文件夾里name.txt文件中,其中有很多作者添加的有趣名字。
node.master: true
指定該節點是否有資格被選舉成為node(注意這里只是設置成有資格, 不代表該node一定就是master),默認是true,es是默認集群中的第一台機器為master,如果這台機掛了就會重新選舉master。
node.data: true
# 指定該節點是否存儲索引數據,默認為true。
index.number_of_shards: 5
# 設置默認索引分片個數,默認為5片。
index.number_of_replicas: 1
# 設置默認索引副本個數,默認為1個副本。如果采用默認設置,而你集群只配置了一台機器,那么集群的健康度為yellow,也就是所有的數據都是可用的,但是某些復制沒有被分配
# (健康度可用 curl 'localhost:9200/_cat/health?v' 查看, 分為綠色、黃色或紅色。綠色代表一切正常,集群功能齊全,黃色意味着所有的數據都是可用的,但是某些復制沒有被分配,紅色則代表因為某些原因,某些數據不可用)。
path.conf: /path/to/conf
# 設置配置文件的存儲路徑,默認是es根目錄下的config文件夾。
path.data: /path/to/data
# 設置索引數據的存儲路徑,默認是es根目錄下的data文件夾,可以設置多個存儲路徑,用逗號隔開,例:
# path.data: /path/to/data1,/path/to/data2
path.work: /path/to/work
# 設置臨時文件的存儲路徑,默認是es根目錄下的work文件夾。
path.logs: /path/to/logs
# 設置日志文件的存儲路徑,默認是es根目錄下的logs文件夾
path.plugins: /path/to/plugins
# 設置插件的存放路徑,默認是es根目錄下的plugins文件夾, 插件在es里面普遍使用,用來增強原系統核心功能。
bootstrap.mlockall: true
# 設置為true來鎖住內存不進行swapping。因為當jvm開始swapping時es的效率 會降低,所以要保證它不swap,可以把ES_MIN_MEM和ES_MAX_MEM兩個環境變量設置成同一個值,並且保證機器有足夠的內存分配給es。 同時也要允許elasticsearch的進程可以鎖住內# # 存,linux下啟動es之前可以通過`ulimit -l unlimited`命令設置。
network.bind_host: 192.168.0.1
# 設置綁定的ip地址,可以是ipv4或ipv6的,默認為0.0.0.0,綁定這台機器的任何一個ip。
network.publish_host: 192.168.0.1
# 設置其它節點和該節點交互的ip地址,如果不設置它會自動判斷,值必須是個真實的ip地址。
network.host: 192.168.0.1
# 這個參數是用來同時設置bind_host和publish_host上面兩個參數。
transport.tcp.port: 9300
# 設置節點之間交互的tcp端口,默認是9300。
transport.tcp.compress: true
# 設置是否壓縮tcp傳輸時的數據,默認為false,不壓縮。
http.port: 9200
# 設置對外服務的http端口,默認為9200。
http.max_content_length: 100mb
# 設置內容的最大容量,默認100mb
http.enabled: false
# 是否使用http協議對外提供服務,默認為true,開啟。
gateway.type: local
# gateway的類型,默認為local即為本地文件系統,可以設置為本地文件系統,分布式文件系統,hadoop的HDFS,和amazon的s3服務器等。
gateway.recover_after_nodes: 1
# 設置集群中N個節點啟動時進行數據恢復,默認為1。
gateway.recover_after_time: 5m
# 設置初始化數據恢復進程的超時時間,默認是5分鍾。
gateway.expected_nodes: 2
# 設置這個集群中節點的數量,默認為2,一旦這N個節點啟動,就會立即進行數據恢復。
cluster.routing.allocation.node_initial_primaries_recoveries: 4
# 初始化數據恢復時,並發恢復線程的個數,默認為4。
cluster.routing.allocation.node_concurrent_recoveries: 2
# 添加刪除節點或負載均衡時並發恢復線程的個數,默認為4。
indices.recovery.max_size_per_sec: 0
# 設置數據恢復時限制的帶寬,如入100mb,默認為0,即無限制。
indices.recovery.concurrent_streams: 5
# 設置這個參數來限制從其它分片恢復數據時最大同時打開並發流的個數,默認為5。
discovery.zen.minimum_master_nodes: 1
# 設置這個參數來保證集群中的節點可以知道其它N個有master資格的節點。默認為1,對於大的集群來說,可以設置大一點的值(2-4)
discovery.zen.ping.timeout: 3s
# 設置集群中自動發現其它節點時ping連接超時時間,默認為3秒,對於比較差的網絡環境可以高點的值來防止自動發現時出錯。
discovery.zen.ping.multicast.enabled: false
# 設置是否打開多播發現節點,默認是true。
discovery.zen.ping.unicast.hosts: ["host1", "host2:port", "host3[portX-portY]"]
# 設置集群中master節點的初始列表,可以通過這些節點來自動發現新加入集群的節點。
elasticsearch7版本引入的新集群協調子系統了解一哈,新增兩個如下配置項
discovery.seed_hosts
cluster.initial_master_nodes
官方文檔栗子:
discovery.seed_hosts:
- 192.168.1.10:9300
- 192.168.1.11
- seeds.mydomain.com
cluster.initial_master_nodes:
- master-node-a
- master-node-b
- master-node-c
偽分布式集群搭建
這里不得不提elasticsearch的廣播和單播機制,這也是困擾了我一個禮拜的坑,生產環境下應當采用單播方式,因此僅僅配置network.host無法在多機集群環境下發現其他節點,必須配置network.publish_host。
這里采用yml+docker-compose完成偽分布式集群的搭建。真分布式集群安裝僅需稍作修改,這里就不贅述了。
注意點:在宿主機上【宿主機】修改/etc/sysctl.conf 添加vm.max_map_count=262144。啟動sysctl -p
master elasticsearch.yml
cluster.name: docker-cluster
node.name: master
node.master: true
node.data: true
network.host: 0.0.0.0
network.publish_host: 192.168.31.45 # 這里是我內網ip
cluster.initial_master_nodes:
- master
http.cors.enabled: true
http.cors.allow-origin: "*"
master docker-compose.yml
version: '3.7'
services:
es:
image: docker.elastic.co/elasticsearch/elasticsearch:7.1.1
container_name: master
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- esdata:/usr/share/elasticsearch/data
- ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9200:9200
- 9300:9300
volumes:
esdata:
slave elasticsearch.yml
cluster.name: docker-cluster
node.name: slave
node.master: false
node.data: true
network.host: 0.0.0.0
network.publish_host: 192.168.31.45
http.port: 9201
transport.tcp.port: 9301
discovery.seed_hosts:
- 192.168.31.45:9300
http.cors.enabled: true
http.cors.allow-origin: "*"
slave docker-compose.yml
version: '3.7'
services:
es:
image: docker.elastic.co/elasticsearch/elasticsearch:7.1.1
container_name: slave
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- esdata2:/usr/share/elasticsearch/data
- ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9201:9201
- 9301:9301
volumes:
esdata2:
運行elasticsearch-head
for Elasticsearch 5.x: docker run -p 9100:9100 mobz/elasticsearch-head:5
for Elasticsearch 2.x: docker run -p 9100:9100 mobz/elasticsearch-head:2
for Elasticsearch 1.x: docker run -p 9100:9100 mobz/elasticsearch-head:1
for fans of alpine there is mobz/elasticsearch-head:5-alpine
open http://localhost:9100/
至此集群搭建完成,接下來就是分詞操作。