文章目錄
目錄準備
-
準備用戶
ES不能以root身份啓動。所以要提前新建一個用戶,以完成後續操作。
useradd wuyujin
添加新用戶
su wuyujin
切換到新用戶。以下操作皆由用戶wuyujin
執行。 -
準備文件夾
我習慣把自己安裝的環境放在/opt
下。
關於ElasticSearch有一個成熟的技術棧ELK
(ElasticSearch + Logstash + Kibana),爲了後續可能會有的擴展:
新建文件夾sudo mkdir /opt/elk/
,
修改父文件夾權限sudo chown wuyujin -R /opt/elk/
後續的ELK相關擴展我都會放在/opt/elk/
下 -
下載
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.1.1.tar.gz
更多版本見ElasticSearch 官網下載頁 -
解壓到指定目錄
tar -zxvf elasticsearch-6.1.1.tar.gz -C /opt/elk/
-
進入解壓好的目錄
cd /opt/elk/elasticsearch-6.1.1/
,並查看目錄結構ll
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$ ll
total 240
drwxr-xr-x 7 wuyujin wuyujin 4096 12月 17 2017 ./
drwxr-xr-x 3 wuyujin root 4096 2月 7 19:36 ../
drwxr-xr-x 2 wuyujin wuyujin 4096 12月 17 2017 bin/
drwxr-xr-x 2 wuyujin wuyujin 4096 12月 17 2017 config/
drwxr-xr-x 2 wuyujin wuyujin 4096 12月 17 2017 lib/
-rw-r--r-- 1 wuyujin wuyujin 11358 12月 17 2017 LICENSE.txt
drwxr-xr-x 15 wuyujin wuyujin 4096 12月 17 2017 modules/
-rw-rw-r-- 1 wuyujin wuyujin 191887 12月 17 2017 NOTICE.txt
drwxr-xr-x 2 wuyujin wuyujin 4096 12月 17 2017 plugins/
-rw-r--r-- 1 wuyujin wuyujin 9326 12月 17 2017 README.textile
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$
啓動ES /opt/elk/elasticsearch-6.1.1/bin/elasticsearch
若有報錯,解決方案見:ES啓動常見錯誤
正常啓動如下:
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$ /opt/elk/elasticsearch-6.1.1/bin/elasticsearch
[2020-02-07T11:46:37,669][INFO ][o.e.n.Node ] [] initializing ...
[2020-02-07T11:46:37,854][INFO ][o.e.e.NodeEnvironment ] [YlLXEJ_] using [1] data paths, mounts [[/ (/dev/sda1)]], net usable_space [388.8gb], net total_space [457.4gb], types [ext4]
[2020-02-07T11:46:37,859][INFO ][o.e.e.NodeEnvironment ] [YlLXEJ_] heap size [990.7mb], compressed ordinary object pointers [true]
[2020-02-07T11:46:37,861][INFO ][o.e.n.Node ] node name [YlLXEJ_] derived from node ID [YlLXEJ_MSG-k5JPgHt1flw]; set [node.name] to override
[2020-02-07T11:46:37,861][INFO ][o.e.n.Node ] version[6.1.1], pid[7109], build[bd92e7f/2017-12-17T20:23:25.338Z], OS[Linux/5.3.0-28-generic/amd64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_241/25.241-b07]
[2020-02-07T11:46:37,861][INFO ][o.e.n.Node ] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/opt/elk/elasticsearch-6.1.1, -Des.path.conf=/opt/elk/elasticsearch-6.1.1/config]
[2020-02-07T11:46:40,330][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [aggs-matrix-stats]
[2020-02-07T11:46:40,331][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [analysis-common]
[2020-02-07T11:46:40,332][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [ingest-common]
[2020-02-07T11:46:40,332][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [lang-expression]
[2020-02-07T11:46:40,332][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [lang-mustache]
[2020-02-07T11:46:40,333][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [lang-painless]
[2020-02-07T11:46:40,333][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [mapper-extras]
[2020-02-07T11:46:40,333][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [parent-join]
[2020-02-07T11:46:40,333][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [percolator]
[2020-02-07T11:46:40,333][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [reindex]
[2020-02-07T11:46:40,333][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [repository-url]
[2020-02-07T11:46:40,334][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [transport-netty4]
[2020-02-07T11:46:40,334][INFO ][o.e.p.PluginsService ] [YlLXEJ_] loaded module [tribe]
[2020-02-07T11:46:40,334][INFO ][o.e.p.PluginsService ] [YlLXEJ_] no plugins loaded
[2020-02-07T11:46:44,343][INFO ][o.e.d.DiscoveryModule ] [YlLXEJ_] using discovery type [zen]
[2020-02-07T11:46:45,108][INFO ][o.e.n.Node ] initialized
[2020-02-07T11:46:45,109][INFO ][o.e.n.Node ] [YlLXEJ_] starting ...
[2020-02-07T11:46:45,307][INFO ][o.e.t.TransportService ] [YlLXEJ_] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2020-02-07T11:46:45,325][WARN ][o.e.b.BootstrapChecks ] [YlLXEJ_] max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
[2020-02-07T11:46:45,326][WARN ][o.e.b.BootstrapChecks ] [YlLXEJ_] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2020-02-07T11:46:48,459][INFO ][o.e.c.s.MasterService ] [YlLXEJ_] zen-disco-elected-as-master ([0] nodes joined), reason: new_master {YlLXEJ_}{YlLXEJ_MSG-k5JPgHt1flw}{gj17a7gsSpCN9fzEIo8v_g}{127.0.0.1}{127.0.0.1:9300}
[2020-02-07T11:46:48,468][INFO ][o.e.c.s.ClusterApplierService] [YlLXEJ_] new_master {YlLXEJ_}{YlLXEJ_MSG-k5JPgHt1flw}{gj17a7gsSpCN9fzEIo8v_g}{127.0.0.1}{127.0.0.1:9300}, reason: apply cluster state (from master [master {YlLXEJ_}{YlLXEJ_MSG-k5JPgHt1flw}{gj17a7gsSpCN9fzEIo8v_g}{127.0.0.1}{127.0.0.1:9300} committed version [1] source [zen-disco-elected-as-master ([0] nodes joined)]])
[2020-02-07T11:46:48,550][INFO ][o.e.h.n.Netty4HttpServerTransport] [YlLXEJ_] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2020-02-07T11:46:48,551][INFO ][o.e.n.Node ] [YlLXEJ_] started
[2020-02-07T11:46:48,559][INFO ][o.e.g.GatewayService ] [YlLXEJ_] recovered [0] indices into cluster_state
這是沒有修改配置文件直接運行的結果,其中:
YlLXEJ_
是隨機生成的節點名node.name
。
[o.e.c.s.MasterService ]
該行後面的127.0.0.1:9300
是集羣內多個ES節點通訊的端口。
[o.e.h.n.Netty4HttpServerTransport]
該行後面的{127.0.0.1:9200}
是本ES節點監聽的HTTP地址。
NOTE 運行ES後,在ES根目錄下會多出兩個文件夾:data/
和logs/
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$ ll
total 248
drwxr-xr-x 9 wuyujin wuyujin 4096 2月 7 19:38 ./
drwxr-xr-x 3 wuyujin root 4096 2月 7 19:36 ../
drwxr-xr-x 2 wuyujin wuyujin 4096 12月 17 2017 bin/
drwxr-xr-x 2 wuyujin wuyujin 4096 12月 17 2017 config/
drwxr-xr-x 3 wuyujin wuyujin 4096 2月 7 19:38 data/
drwxr-xr-x 2 wuyujin wuyujin 4096 12月 17 2017 lib/
-rw-r--r-- 1 wuyujin wuyujin 11358 12月 17 2017 LICENSE.txt
drwxr-xr-x 2 wuyujin wuyujin 4096 2月 7 19:38 logs/
drwxr-xr-x 15 wuyujin wuyujin 4096 12月 17 2017 modules/
-rw-rw-r-- 1 wuyujin wuyujin 191887 12月 17 2017 NOTICE.txt
drwxr-xr-x 2 wuyujin wuyujin 4096 12月 17 2017 plugins/
-rw-r--r-- 1 wuyujin wuyujin 9326 12月 17 2017 README.textile
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$ tree -h logs/
logs/
├── [ 0] elasticsearch_deprecation.log
├── [ 0] elasticsearch_index_indexing_slowlog.log
├── [ 0] elasticsearch_index_search_slowlog.log
└── [4.4K] elasticsearch.log
0 directories, 4 files
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$ tree -h data/
data/
└── [4.0K] nodes
└── [4.0K] 0
├── [ 0] node.lock
└── [4.0K] _state
├── [ 140] global-0.st
└── [ 71] node-0.st
3 directories, 3 files
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$
要注意data/
下新增的數據(其中包含接集羣名稱、節點名稱等)。
- 測試
兩種方式測試:通過瀏覽器或命令行。- 瀏覽器訪問
127.0.0.1:9200
這個地址是Netty4HttpServerTransport
那一行後面的地址publish_address
。
若正常如下圖:
如果是在服務器上調配,沒有圖形化界面,就只能通過命令行,如下: - 命令行
curl -X GET "http://localhost:9200"
可以得到響應。
wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$ curl -X GET "http://localhost:9200" { "name" : "YlLXEJ_", "cluster_name" : "elasticsearch", "cluster_uuid" : "td3DLE0iRMaWILGLuGveDw", "version" : { "number" : "6.1.1", "build_hash" : "bd92e7f", "build_date" : "2017-12-17T20:23:25.338Z", "build_snapshot" : false, "lucene_version" : "7.1.0", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" } wuyujin@ubuntu18:/opt/elk/elasticsearch-6.1.1$
- 瀏覽器訪問
若有以上響應,則啓動成功。
ES配置(單節點)
-
查詢本機IP
ifconfig
ES默認監聽的IP地址是本機的127.0.0.1
,啓動後而只可以在本機通過127.0.0.1
或localhost
加端口號訪問。
如果想要被其他的機器訪問,則需要配置爲本機的IP。
我的機器IP是192.168.1.6
,是一個局域網IP。
這個IP會在後面的配置中用到。 -
修改配置文件(單節點配置)
gedit ./config/elasticsearch.yml
ES配置 官網文檔
# 集羣名稱
cluster.name: cluster-wuyujin
# 節點名稱
node.name: node-liubei
# 監聽的地址
network.host: 192.168.1.6
http.port: 9200
# 發現集羣內的其他節點
# discovery.zen.minimum_master_nodes: 2
# discovery.zen.ping.unicase.hosts: ["192.168.1.6"]
# 本節點是否存儲數據
node.data: true
# 本節點是否可被選舉爲主節點
node.master: true
# 允許跨域資源訪問(ES-head插件)
http.cors.enabled: true
http.cors.allow-origin: "*"
# 日誌和數據的存儲路徑(注意權限)
# path.logs: /path/to/logs
# path.data: /path/to/data
然後啓動ES,訪問curl -X GET "192.168.1.6:9200"
。並在局域網內別的機器訪問192.168.1.6:9200
進行測試。
NOTE 有的時候會報錯。註釋掉network.host: 192.168.1.6
配置行就可以正常啓動。
- 關於
network.host
127.0.0.1
或localhost
默認值。只能在本機訪問。- 如
192.168.1.6
,別的機器可以訪問到的IP,則在本機部署啓動之後,在別的機器上也可以訪問。
多節點配置
ES的本質是一個分佈式數據庫
,其底層調用了使用倒排索引存儲數據的Lucene,所以查詢關鍵詞所在位置的執行速度明顯優於關係型數據庫。
準備三個節點,暫時部署在同一臺機器上(因爲我暫時只有一臺測試機),機器IP爲192.168.1.6
。
三臺機器的節點名稱分別爲node-liubei, node-guanyu, node-zhangfei
。
依次監聽的HTTP端口號爲:9201, 9202, 9203
。
- 解壓
tar -zxvf elasticsearch-6.1.1.zip -C /opt/elk/cluster-wuyujin
進入該文件夾cd /opt/elk/cluster-wuyujin/
查看內容ll
wuyujin@ubuntu18:~$ cd /opt/elk/cluster-wuyujin/
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ # 查看文件夾內容
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ ll
total 12
drwxr-xr-x 3 wuyujin wuyujin 4096 2月 7 21:25 ./
drwxr-xr-x 4 wuyujin root 4096 2月 7 21:21 ../
drwxr-xr-x 7 wuyujin wuyujin 4096 12月 17 2017 elasticsearch-6.1.1/
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ # 複製三份目錄,名稱非別爲劉關張(可自定義)
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ cp elasticsearch-6.1.1/ es-liubei -r
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ cp elasticsearch-6.1.1/ es-guanyu -r
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ cp elasticsearch-6.1.1/ es-zhangfei -r
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ # 刪除不需要的文件夾
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ rm elasticsearch-6.1.1/ -rf
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$ ll
total 20
drwxr-xr-x 5 wuyujin wuyujin 4096 2月 7 21:26 ./
drwxr-xr-x 4 wuyujin root 4096 2月 7 21:21 ../
drwxr-xr-x 7 wuyujin wuyujin 4096 2月 7 21:25 es-guanyu/
drwxr-xr-x 7 wuyujin wuyujin 4096 2月 7 21:25 es-liubei/
drwxr-xr-x 7 wuyujin wuyujin 4096 2月 7 21:25 es-zhangfei/
wuyujin@ubuntu18:/opt/elk/cluster-wuyujin$
以上,文件夾名稱和之後配置文件中的集羣/節點名稱並無關係。只要一一對應即可 。
- 配置文件
分別修改三個實例中的./config/elasticsearch.yml
文件。配置分別爲:- 編輯liubei的配置文件
gedit es-liubei/config/elasticsearch.yml
- 編輯liubei的配置文件
cluster.name: cluster-wuyujin
node.name: node-liubei
network.host: 192.168.1.6
http.port: 9201
discovery.zen.minimum_master_nodes: 2
discovery.zen.ping.unicase.hosts: ["192.168.1.6"]
node.data: true
node.master: true
http.cors.enabled: true
http.cors.allow-origin: "*"
# path.logs: /path/to/logs
# path.data: /path/to/data
- 編輯guanyu `gedit es-guanyu/config/elasticsearch.yml`
cluster.name: cluster-wuyujin
node.name: node-guanyu
network.host: 192.168.1.6
http.port: 9202
discovery.zen.minimum_master_nodes: 2
discovery.zen.ping.unicase.hosts: ["192.168.1.6"]
node.data: true
node.master: true
http.cors.enabled: true
http.cors.allow-origin: "*"
# path.logs: /path/to/logs
# path.data: /path/to/data
- 編輯zhangfei `gedit es-zhangfei/config/elasticsearch.yml`
cluster.name: cluster-wuyujin
node.name: node-zhangfei
network.host: 192.168.1.6
http.port: 9203
discovery.zen.minimum_master_nodes: 2
discovery.zen.ping.unicase.hosts: ["192.168.1.6"]
node.data: true
node.master: true
http.cors.enabled: true
http.cors.allow-origin: "*"
# path.logs: /path/to/logs
# path.data: /path/to/data
- 啓動
之前一直是直接啓動ES,在shell上打印消息。缺點是關閉該shell窗口也會造成ES的停止。
ES後臺啓動:elasticsearch --daemonize
daemon是守護進程的意思。
執行以下命令,將三個節點都啓動(如果想先調試啓動無誤的話,可以依次在多個shell中啓動):
./es-liubei/bin/elasticsearch --daemonize
./es-guanyu/bin/elasticsearch --daemonize
./es-zhangfei/bin/elasticsearch --daemonize
- 訪問測試
curl -X GET "192.168.1.6:9201"
curl -X GET "192.168.1.6:9202"
curl -X GET "192.168.1.6:9203"
應該依次可以看到節點的信息(包括節點所屬的集羣名稱,節點的名稱等)
- 停止
ps -aux | grep elastic # 查找相關進程
kill -9 進程號 # 根據pid殺死對應的進程
插件elasticsearch-head
的使用
假設讀者已經知道ES-head的用途。以下爲環境搭建過程:
- 下載
wget https://codeload.github.com/mobz/elasticsearch-head/zip/master
github 下載頁面 - 解壓
unzip elasticsearch-head-master.zip
- 進入文件夾
cd elasticsearch-head-master/
- 安裝node依賴
npm install
- ES-head是一個node應用,啓動它
npm run start
。啓動時讀取的配置信息在package.json
中,可自行修改。 - 瀏覽器訪問ES-head localhost:9100
- 使用ES-head插件要訪問哪個節點呢?填寫其地址即可(訪問一個集羣中的任一節點效果相同)
如:192.168.1.6:9201
,點擊連接按鈕,即可看到各個節點的信息(需要在ES中提前設置好跨域訪問)。
之前在各個節點的配置文件已經設置過了跨域訪問(否則不能使用ES-head讀寫訪問ES的數據):
http.cors.enabled: true
http.cors.allow-origin: "*"
三個端口:9100, 9200, 9300
- 9100 ES-head插件默認監聽的端口,訪問ES-head:ip:9100,若ES-head插件運行於本地,則ip爲localhost或127.0.0.1.
- 9300 同一個ES集羣中,多個節點之間通信的端口,一般不修改。
- 9200 每一個ES節點都會監聽該機器上的一個端口,用於接受HTTP請求。
相當於tomcat的8080。
當一臺機器上運行好幾個服務器的時候,這個端口不能重複。否則報錯port already bind。
ES啓動常見錯誤
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
最大文件打開個數:4096太少,需要增加到至少爲65535。
修改文件:/etc/security/limits.conf
添加配置:
# max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
* soft nofile 65536
* hard nofile 65536
測試 ulimit -Sn; ulimit -Hn
。(也可以使用命令行sudo ulimit -n 65535
設置參數值)
max number of threads [3818] for user [es] is too low, increase to at least [4096]
配置文件同上,/etc/security/limits.conf
用戶的最大線程數:3818太少,需要增加到至少爲4096。
# max number of threads [3818] for user [es] is too low, increase to at least [4096]
* soft nproc 4096
* hard nproc 4096
測試: ulimit -Su; ulimit -Hu
。也可以使用命令行sudo ulimit -u 4096
設置參數值。
-
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
最大的虛擬內存區域vm.max_map_count
參數的值:65530太少了,增加到至少262144。
依舊有兩種修改方式:- 修改配置文件
vi /etc/sysctl.conf
增加vm.max_map_count=262144
使配置生效sysctl -p
- 直接命令行修改,執行:
sudo sysctl -w vm.max_map_count=262144
- 修改配置文件
-
權限問題
ES不能用root權限啓動,否則會報錯Caused by: java.lang.RuntimeException: can not run elasticsearch as root
所以用普通用戶執行即可。
用普通用戶啓動ES需要保證前提:ES的文件夾權限必須授權給該普通用戶chown 用戶名 -R 目錄名
,否則會出現沒有訪問權限的錯誤。