0. 前言
最近基本都是學一些環境配置,和一些中間件的安裝與配置。沒有實際編寫代碼。可能看起來有點水,我對自己的學習方式是,先要了解各個中間件的安裝配置以及簡單使用,理論應用場景,然后我在小項目中,逐步引入這些高大上的中間件,看實際效果怎樣,合不合適我自己用。一開始絕對不會花太多時間去了解技術系統,我覺得,我的工作是做應用開發,不是底層研發。開發就是以滿足實際需求為前提,怎么做到適合自己使用,怎么方便怎么來。
也有和一些人聊過,一般開發人員太愛自己造輪子了,實際項目,還是要用市場上普遍使用的開源軟件。只有自己平時學習與提高時,造一些輪子。但是造輪子對於我來說,目前還是太難了。能合理用好各個組件,快速實現業務需求,才是我應該注重的點。每個人對自己的要求是不一樣的。
1. 下載Image
如果不清楚最新版本的,可以到https://hub.docker.com/r/library/elasticsearch/ 這里查詢
docker pull elasticsearch:6.4.2
運行es
docker run -d --name es -p 9200:9200 -e http.port=9200 -e http.cors.allow-origin="*" -e http.cors.enabled=true -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true elasticsearch:6.4.2
運行elastichd
docker run -p 9800:9800 -d --link es:demo containerize/elastichd
這里的集群監控狀態為:Yellow,表示當前只有一個節點,沒有配置集群

運行dejavu
docker run -d -p 1358:1358 appbaseio/dejavu
這個要注意,如果要成功連接到ES的話,要先創建好索引(Index) 參考下面第3點,curl -X PUT -H "Content-Type: application/json" http://172.16.23.203:2101/wunaozai, 創建后,才能進行連接

2. 配置集群及測試
創建一個虛擬網絡
docker network create --driver bridge --subnet 172.22.17.0/24 --gateway 172.22.17.254 es_net
創建3個elasticsearch節點
1 # node1 2 docker run -d --name es_1 --net es_net --ip 172.22.17.1 \ 3 -p 2101:9200 -p 3101:9300 -v /etc/localtime:/etc/localtime \ 4 -e "cluster.name=wunaozai" \ 5 -e "node.name=node1" \ 6 -e "network.host=172.22.17.1" \ 7 -e "network.bind_host=0.0.0.0" \ 8 -e "discovery.zen.ping.unicast.hosts=172.22.17.1,172.22.17.2,172.22.17.3" \ 9 -e "discovery.zen.minimum_master_nodes=1" \ 10 -e "http.port=9200" \ 11 -e "http.cors.allow-origin=*" \ 12 -e "http.cors.enabled=true" \ 13 -e "http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization" \ 14 -e "http.cors.allow-credentials=true" \ 15 elasticsearch:6.4.2 16 # node2 17 docker run -d --name es_2 --net es_net --ip 172.22.17.2 \ 18 -p 2102:9200 -p 3102:9300 -v /etc/localtime:/etc/localtime \ 19 -e "cluster.name=wunaozai" \ 20 -e "node.name=node2" \ 21 -e "network.host=172.22.17.2" \ 22 -e "network.bind_host=0.0.0.0" \ 23 -e "discovery.zen.ping.unicast.hosts=172.22.17.1,172.22.17.2,172.22.17.3" \ 24 -e "discovery.zen.minimum_master_nodes=1" \ 25 -e "http.port=9200" \ 26 -e "http.cors.allow-origin=*" \ 27 -e "http.cors.enabled=true" \ 28 -e "http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization" \ 29 -e "http.cors.allow-credentials=true" \ 30 elasticsearch:6.4.2 31 # node3 32 docker run -d --name es_3 --net es_net --ip 172.22.17.3 \ 33 -p 2103:9200 -p 3103:9300 -v /etc/localtime:/etc/localtime \ 34 -e "cluster.name=wunaozai" \ 35 -e "node.name=node3" \ 36 -e "network.host=172.22.17.3" \ 37 -e "network.bind_host=0.0.0.0" \ 38 -e "discovery.zen.ping.unicast.hosts=172.22.17.1,172.22.17.2,172.22.17.3" \ 39 -e "discovery.zen.minimum_master_nodes=1" \ 40 -e "http.port=9200" \ 41 -e "http.cors.allow-origin=*" \ 42 -e "http.cors.enabled=true" \ 43 -e "http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization" \ 44 -e "http.cors.allow-credentials=true" \ 45 elasticsearch:6.4.2
運行,上面es_1,es_2,es_3是集群的3個幾點。下面那個es是上面運行的單節點。

訪問 ElisticHD http://172.16.23.203:9800/ 從下圖,可以看到集群的一些信息

集群節點如下,訪問任意一個節點,效果都是一樣的,都是操作同一份數據。
1 http://172.16.23.203:2101/ 2 http://172.16.23.203:2102/ 3 http://172.16.23.203:2103/
3. 數據測試
創建index
1 curl -X PUT -H "Content-Type: application/json" http://172.16.23.203:2101/wunaozai -d ' 2 { 3 "mappings":{ 4 "it": { 5 "properties": { 6 "bookId": {"type": "long"}, 7 "bookName": {"type": "text"}, 8 "publishDate": {"type": "date"} 9 } 10 } 11 } 12 } 13 '
查看index
curl -X GET http://172.16.23.203:2101/wunaozai
刪除index
curl -X DELETE http://172.16.23.203:2101/wunaozai
查詢type
curl -X GET -H "Content-Type: application/json" http://172.16.23.203:2101/wunaozai/_mapping
插入數據
1 curl -H "Content-Type: application/json" -X POST http://172.16.23.203:2101/wunaozai/it/001 -d '{"bookId": 1, "bookName":"<aa>", "publishDate":"2018-01-01"}' 2 curl -H "Content-Type: application/json" -X POST http://172.16.23.203:2102/wunaozai/it/002 -d '{"bookId": 2, "bookName":"<bb>", "publishDate":"2018-01-02"}' 3 curl -H "Content-Type: application/json" -X POST http://172.16.23.203:2103/wunaozai/it/003 -d '{"bookId": 3, "bookName":"<cc>", "publishDate":"2018-01-03"}'
更多操作就要參考官方文檔
4. 數據持久化
一開始無論是通過 -v 還是通過 --volumes-from 進行掛載,都會提示以下錯誤
后來查詢資料發現,es是不能在root權限下運行的,同理,在elasticsearch:6.4.2容器里,是以elasticsearch用戶運行的,因此只要把需要進行掛載的Volumn用戶組改為elasticsearch即可,這里需要用1000:1000 ,要用用戶ID和組ID,因為host里的用戶ID與Container里的用戶ID是不一樣的。
1 chown 1000:1000 -R data 2 3 docker run -it --name es -p 9200:9200 -v /etc/localtime:/etc/localtime -v /root/workspace/docker/es/data:/usr/share/elasticsearch/data -e http.port=9200 -e http.cors.allow-origin="*" -e http.cors.enabled=true -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true elasticsearch:6.4.2
通過上面命令運行后,插入數據,然后停止容器,刪除容器,再重新創建容器,上次創建的數據還是存在的,自此就實現了ES數據的host持久化。
參考資料:
https://github.com/appbaseio/dejavu/
https://hub.docker.com/_/elasticsearch/
http://www.codesheep.cn/2018/10/30/es-visualization/
http://www.codesheep.cn/2018/11/06/es-2-node-cluster/
https://github.com/elastic/elasticsearch-docker/issues/111
https://www.elastic.co/guide/en/elasticsearch/reference/5.5/docker.html#_b_bind_mounted_configuration
