今天我們來安裝一下elasticsearch,我們采用RPM包安裝的方式來,版本為6.5.4。系統為centos7.5版本。
1、首先設置系統環境
1)編輯/etc/sysctl.conf文件添加下面一行內容
vm.max_map_count=262144
這個參數會影響java線程數量,用於限制一個進程可以擁有的虛擬內存區域的大小。系統默認是65530,可以修改更高的值
添加內容完成后使用命令立即並永久生效
sysctl -p
2)編輯/etc/security/limits.conf文件添加一下內容:
#修改最大文件描述符 * soft nofile 65535 * hard nofile 65535 #修改最大線程數 * soft nproc 4096 * hard nproc 4096 #最大鎖定內存地址空間 elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
2、安裝java環境
1)下載jdk。至少1.7版本以上,這里使用jdk1.8版本。可以選擇使用oracle JDK或者Open JDK
2)我們這里直接使用yum一鍵安裝來完成
yum install java-1.8.0-openjdk.x86_64
5)最后執行java -version命令看看是否立即生效:
[root@: /usr/local]#java -version java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
3、安裝elasticsearch的RPM包
[root@:vg_adn_tidbCkhsTest /usr/local/src]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch [root@:vg_adn_tidbCkhsTest /usr/local/src]#wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.rpm --2019-01-14 14:57:17-- https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.rpm Resolving artifacts.elastic.co (artifacts.elastic.co)... 107.21.239.197, 54.225.214.74, 54.225.221.128, ... Connecting to artifacts.elastic.co (artifacts.elastic.co)|107.21.239.197|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 113304673 (108M) [application/octet-stream] Saving to: ‘elasticsearch-6.5.4.rpm’ 100%[=================================================================================================>] 113,304,673 73.5MB/s in 1.5s 2019-01-14 14:57:19 (73.5 MB/s) - ‘elasticsearch-6.5.4.rpm’ saved [113304673/113304673] [root@:vg_adn_tidbCkhsTest /usr/local/src]#wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.rpm.sha512 --2019-01-14 14:57:26-- https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.rpm.sha512 Resolving artifacts.elastic.co (artifacts.elastic.co)... 107.21.237.188, 107.21.239.197, 54.225.214.74, ... Connecting to artifacts.elastic.co (artifacts.elastic.co)|107.21.237.188|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 153 [application/octet-stream] Saving to: ‘elasticsearch-6.5.4.rpm.sha512’ 100%[=================================================================================================>] 153 --.-K/s in 0s 2019-01-14 14:57:26 (29.0 MB/s) - ‘elasticsearch-6.5.4.rpm.sha512’ saved [153/153] [root@:vg_adn_tidbCkhsTest /usr/local/src]#sha512sum -c elasticsearch-6.5.4.rpm.sha512 elasticsearch-6.5.4.rpm: OK [root@:vg_adn_tidbCkhsTest /usr/local/src]#rpm -ivh elasticsearch-6.5.4.rpm Preparing... ################################# [100%] Creating elasticsearch group... OK Creating elasticsearch user... OK Updating / installing... 1:elasticsearch-0:6.5.4-1 ################################# [100%] ### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd sudo systemctl daemon-reload sudo systemctl enable elasticsearch.service ### You can start elasticsearch service by executing sudo systemctl start elasticsearch.service Created elasticsearch keystore in /etc/elasticsearch
到最好一部我們看到已經安裝成功了。
4、配置elasticsearch服務配置文件
1)修改/etc/sysconfig/elasticsearch文件內容
ES_JAVA_OPTS="-Xms4g -Xmx4g"
MAX_LOCKED_MEMORY=unlimited
替換4g為總內存的一半(Elasticsearch官方建議是主機總內存的一半)
2)使用命令systemctl edit elasticsearch,此時會進入編輯模式,輸入一下內容后保存
[Service]
LimitMEMLOCK=infinity
5、配置elasticsearch文件,在/etc/elasticsearch目錄下。
# cat /etc/elasticsearch/elasticsearch.yml cluster.name: mylab #集群名稱,只有cluster.name相同時,節點才能加入集群。請設置為具有描述性的名字。不建議在不同環境中使用相同的集群名。 node.name: mylabb01 #節點描述名稱,默認情況下,Elasticsearch將使用隨機生成的UUID的前7個字符作為節點id。設為服務器的主機名 node.name: ${HOSTNAME} node.attr.rack: r1 #指定節點的部落屬性,機架位置,比集群范圍更大。 path.data: /var/lib/elasticsearch #Elasticsearch的數據文件存放目錄 如果是默認位置,在將Elasticsearch升級到新版本時,很可能會把數據刪除。 path.logs: /var/log/elasticsearch #日志目錄 bootstrap.memory_lock: true #啟動后鎖定內存,禁用swap交換,提高ES性能。 network.host: 0.0.0.0 #指定監聽的地址 http.port: 9200 #監聽的WEB端口 discovery.zen.ping.unicast.hosts: ["IP:9300"]: #默認網絡配置中,Elasticsearch將綁定到回環地址,並掃描9300-9305端口,試圖連接同一台服務器上的其他節點,可以自動發現並加入集群。 - 10.0.0.46:9300 #此端口為TCP傳輸端口,用於集群內節點發現、節點間信息傳輸、ES Java API也是通過此端口傳輸數據,transport.tcp.port定義。9200為HTTP端口。 - host1.vtlab.io discovery.zen.minimum_master_nodes: 2 #為防止數據丟失,discovery.zen.minimum_master_nodes設置至關重要,主節點的最小選舉數。避免腦裂,應將此值設為(master_eligible_nodes / 2) + 1,換言之,如果有3個節點,(3/2)+1 or 2 #以下選項僅在完全重啟集群時生效 #本地網關模塊在整個群集重新啟動時存儲群集狀態和分片數據。 #以下靜態設置必須在每個主節點上設置,控制新選擇的主節點在試圖恢復集群狀態和集群數據之前應該等待多長時間: gateway.expected_nodes: 0 #集群中預期的(數據或主)節點數量。一旦加入集群的節點數量達到預期,本地碎片的恢復就會開始。默認值為0 gateway.expected_master_nodes: 0 #集群中預期的主節點數量。一旦加入集群的主節點數量達到預期,本地碎片的恢復就會開始。默認值為0 gateway.expected_data_nodes: 0 #集群中預期的數據節點數量。一旦預期的數據節點數量加入集群,本地碎片的恢復就會開始。默認值為0 gateway.recover_after_time: 5 #如果沒有達到預期的節點數量,則恢復過程將等待配置的時間量,然后再嘗試恢復。如果配置了一個expected_nodes設置,則默認值為5m。 gateway.recover_after_nodes: 1 #只要有這么多數據或主節點加入集群,就可以恢復。 gateway.recover_after_master_nodes: 1 #只要有這么多主節點加入集群,就可以恢復。 gateway.recover_after_data_nodes: 1 #只要有這么多數據節點加入集群,就可以恢復。 action.destructive_requires_name: true #禁用通過api以通配符刪除所有索引。刪除索引時需要指定被刪除的索引名稱。
6、載入服務
systemtl daemon-reload
7、啟動節點
systemctl start elasticsearch
8、檢查是否啟動成功
[root@:vg_adn_tidbCkhsTest /etc/elasticsearch]#curl http://172.31.26.133:9200 { "name" : "node-1", "cluster_name" : "my-application", "cluster_uuid" : "dc6CndO5TzS8T1GcBXiQKg", "version" : { "number" : "6.5.4", "build_flavor" : "default", "build_type" : "rpm", "build_hash" : "d2ef93d", "build_date" : "2018-12-17T21:17:40.758843Z", "build_snapshot" : false, "lucene_version" : "7.5.0", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
額外補充,我們對配置文件進行解析:
(1)cluster.name: esbigdata 配置elasticsearch集群名稱,默認是elasticsearch。這里修改為esbigdata,elasticsearch會自動發現在同一網段下的集群名為esbigdata的主機,如果在同一網段下有多個集群,就可以通過這個屬性來區分不同的集群。線上生產環境時建議更改。 (2)node.name: server1 節點名,任意指定一個即可,這里是server1,我們這個集群環境中有三個節點,分別是server1、server2和server3,記得根據主機的不同,要修改相應的節點名稱。 (3)node.master: true 指定該節點是否有資格被選舉成為master,默認是true,elasticsearch集群中默認第一台啟動的機器為master角色,如果這台服務器宕機就會重新選舉新的master。我們這個集群環境中,定義了server1和server3兩個master節點,因此這兩個節點上node.master的值要設置為true。 (4)node.data: true 指定該節點是否存儲索引數據,默認為true,表示數據存儲節點,如果節點配置node.master:false並且node.data:<br/>false,則該節點就是client node。這個client node類似於一個“路由器”,負責將集群層面的請求轉發到主節點,將數據相關的請求轉發到數據節點。在我們這個集群環境中,定義了server1、server2和server3均為數據存儲節點,因此這三個節點上node.data的值要設置為true。 (5)path.data:/data1/elasticsearch, /data2/elasticsearch 設置索引數據的存儲路徑,默認是elasticsearch根目錄下的data文件夾,這里自定義了兩個路徑,可以設置多個存儲路徑,用逗號隔開。 (6)path.logs: /usr/local/elasticsearch/logs 設置日志文件的存儲路徑,默認是elasticsearch根目錄下的logs文件夾 (7)bootstrap.memory_lock: true 此配置項一般設置為true用來鎖住物理內存。在linux下物理內存的執行效率要遠遠高於虛擬內存(swap)的執行效率,因此,當JVM開始使用swap內存時elasticsearch的執行效率會降低很多,所以要保證它不使用swap,保證機器有足夠的物理內存分配給elasticsearch。同時也要允許elasticsearch的進程可以鎖住物理內存,linux下可以通過“ulimit -l”命令查看最大鎖定內存地址空間(memlock)是不是unlimited,這個參數在之前系統調優的時候已經設置過了。 (8)network.host: 0.0.0.0 此配置項是network.publish_host和network.bind_host兩個配置項的集合,network.bind_host用來設置elasticsearch提供服務的IP地址,默認值為0.0.0.0,此默認配置不太安全,因為如果服務器有多塊網卡(可設置多個IP,可能有內網IP,也可能有外網IP),那么就可以通過外網IP來訪問elasticsearch提供的服務,顯然,elasticsearch集群在外網訪問的話將非常不安全,因此,建議將network.bind_host設置為內網IP地址比較好。 network.publish_host用來設置elasticsearch集群中該節點和其它節點間交互通信的IP地址,一般設置為該節點所在的內網IP地址即可。需要保證可以和集群中其它節點進行通信。 在elasticsearch新版本中,增加了network.host配置項,此配置項用來同時設置bind_host和publish_host上面兩個參數,根據上面的介紹,此值設置為服務器的內網IP地址即可。也就是設置bind_host和publish_host為同一個IP地址。 (9)http.port: 9200 設置elasticsearch對外提供服務的http端口,默認為9200。其實,還有一個端口配置選項transport.tcp.port,此配置項用來設置節點間交互通信的TCP端口,默認是9300 (10)discovery.zen.minimum_master_nodes: 1 配置當前集群中最少的master節點數,默認為1,也就是說,elasticsearch集群中master節點數不能低於此值,如果低於此值,elasticsearch集群將停止運行。在三個以上節點的集群環境中,建議配置大一點的值,推薦2至4個為好。 (11)discovery.zen.ping.unicast.hosts:[172.16.213.37:9300,172.16.213.78:9300] 設置集群中master節點的初始列表,可以通過這些節點來自動發現新加入集群的節點。這里需要注意,master節點初始列表中對應的端口是9300。即為集群交互通信端口。
------------------------------------------------------------更新----------------------------------------------------------------
1)啟動報錯:
$/usr/local/elasticsearch/bin/elasticsearch -d
Java HotSpot(TM) 64-Bit Server VM warning: Cannot open file logs/gc.log due to Permission denied
這是因為elasticsearch的logs目錄下的一個日志文件沒有權限所致,這個日志文件不知ES的訪問、錯誤配置,而是在ES的安裝目錄下的一個logs目錄,里面的gc.log沒有權限所致,默認應該是“-rw-rw-r--”,所以解決問題最簡單的方法是刪除了,然后重啟es集群即可,這個日志文件ES會自動創建。