Elasticsearch 部署文檔
搭建NFS server
目的:為集群snapshot備份提供共享文件系統。
服務端設置
- 安裝server軟件:
apt install nfs-kernel-server
,安裝后服務會自動啟動 - 默認配置文件:
/etc/default/nfs-kernel-server
和/etc/default/nfs-common
- 設置掛載目錄(由於NFSv4使用了一個全局根目錄,要想共享全局根目錄以外的目錄需要將其掛載至根目錄)
mount -bind /opt/backups /srv/nfs4/backups
- 添加至
/etc/fstab
開機啟動:/opt/backups /srv/nfs4/backups none bind 0 0
- 設置訪問權限:
vim /etc/exports
man exports
查看詳細的權限設置- 常見設置:
- rw 允許讀寫
- sync 同步寫入
- fsid=0 表示為全局根目錄
- root_squash/no_root_squash 是否將root視為匿名用戶
- all_squash 是否所有用戶是為匿名用戶
nobody/nogroup
- 生效配置:
exportfs -ar
- 查看配置:
exportfs -v
當用戶以匿名身份寫入時,請確保掛載目錄支持匿名身份寫入!
客戶端設置
- 安裝客戶端軟件:
apt install nfs-common
- 掛載文件:
mount -t nfs -o vers=4 172.16.60.11:/ /mnt/data/nfs
- 設置開機掛載
man nfs
查看具體掛載參數172.16.60.11:/ /mnt/data/nfs nfs defaults,timeo=900,retrans=5,_netdev 0 0
架構設計
graph LR H1[172.16.60.10] H2[172.16.60.11] H3[172.16.60.12] S[(NFS Server)] S --/mnt/data/nfs--> H2 H1 -./mnt/nfs > 172.16.60.11:/ .-> S H2 -./mnt/nfs > 172.16.60.11:/ .-> S H3 -./mnt/nfs > 172.16.60.11:/ .-> S
- 172.16.60.11是NFS 服務主機,提供
/mnt/data/nfs
用於服務根目錄 /mnt/nfs
為三台主機對NFS訪問的統一入口- 172.16.60.10-12通過NFS掛載至
/mnt/nfs
- 172.16.60.10-12通過NFS掛載至
Elasticsearch集群
- 官方教程
- 配置Elasticsearch
- 重要的Elasticsearch配置
- 重要的系統配置
- Elasticsearch安全設置
- 啟用Elasticsearch節點之間的TLS傳輸
- Elasticsearch設置密碼
- Docker安裝kibana
- 啟用reindex功能
graph LR N1[Node 172.16.60.10 從節點] N2[Node 172.16.60.11 從節點] N3[Node 172.16.60.12 主節點] C[Cluster] subgraph Elasticsearch Cluster C -.-> N1 C -.-> N2 C -.-> N3 end HTTP --> C
官方部署流程
- 修改
/etc/sysctl.conf
值vm.max_map_count=262144
- 設置目錄權限,保證
elasticsearch
用戶可以讀寫- mkdir esdatadir
- chmod g+rwx esdatadir
- chgrp 0 esdatadir
- docker run 添加
--group-add 0
確保docker里面elasticsearch用戶組為0
- 檢查
ulimits
限制- docker run --rm docker.elastic.co/elasticsearch/elasticsearch:{version} /bin/bash -c 'ulimit -Hn && ulimit -Sn && ulimit -Hu && ulimit -Su'
- 關閉交換分區
- 系統層面關閉
- swapoff -a # 關閉所有交換分區
- 修改
/etc/sysctl.conf
的vm.swappiness=1
減少內核的切換傾向 - 修改es配置,打開
bootstrap.memory_lock: true
,鎖定使用內存,可以使用GET _nodes?filter_path=**.mlockall
查看設置情況
- 僅設置docker關閉
- bootstrap.memory_lock: true #鎖住內存,避免交換
- memlock
- true(docker daemon)
-e "bootstrap.memory_lock=true" --ulimit memlock=-1:-1
- 系統層面關閉
- 隨機端口:
--publish-all
建議生產環境 - 手動設置堆大小:默認自動設置,無須手動干預
- 指定部署的鏡像版本
- 總是綁定數據卷:/usr/share/elasticsearch/data
- docker
storage-driver: devicemapper
避免使用loop-lvm
模式 - 集中化輸出日志
- 配置文件管理
- 綁定配置文件
-v /path/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- 設置環境變量
- 直接設置配置文件設置名
- 轉換配置文件設置名
- 通過docker run 鏡像命令行傳遞
- 綁定配置文件
- 修改系統設置
- TCP重傳超時: net.ipv4.tcp_retries2
- 確保JNA臨時目錄允許執行文件
- 線程數量
- 虛擬內存,es使用mmapfs存儲索引
- 文件描述符數量調整
GET _nodes/stats/process?filter_path=**.max_file_descriptors
檢查文件描述符數量
本機實際部署
系統調整
- 關閉交換分區: swapoff -a,禁用
/etc/fstab
交換分區掛載 - 調整系統設置
/etc/sysctl.conf
- vm.max_map_count=262144
- net.ipv4.tcp_retries2=5
- 創建es目錄(配置、數據等),修改目錄組權限為:
rwx
,組id為0
- /mnt/data/es es數據目錄
- /mnt/nfs/es_snapshot es快照目錄
- /home/tszh/code/ics_backup/elasticsearch
Elasticsearch配置
- 創建配置文件目錄
- 復制配置文件
elasticsearch.yaml
- 啟用密碼
- 打開
xpack.security.enabled: true
開關,表示啟用安全加密 7.17
版本要求啟用密碼后,節點傳輸需要開啟TLS加密- 生成TLS證書
- 任一es節點,使用
./bin/elasticsearch-certutil ca
命令生成CA - 利用CA生成證書,使用
./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
生成證書 - 拷貝生成的證書至
ES_PATH_CONF
環境變量指定的目錄 - 如果生成CA期間使用了密碼,需要更新
keystore
- ./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
- ./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
- 任一es節點,使用
- elasticsearch.yml配置項
- xpack.security.transport.ssl.enabled: true
- xpack.security.transport.ssl.verification_mode: certificate
- xpack.security.transport.ssl.client_authentication: required
- xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
- xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
- 生成TLS證書
- 設置es集群密碼
- 准備工作
- 確保集群已正常工作,設置密碼其實是通過調用
http
接口完成的 - 確保命令行可以正確地讀取elasticsearch的配置,建議使用運行狀態下集群bin目錄下的命令執行
- 確保集群已正常工作,設置密碼其實是通過調用
bin/elasticsearch-setup-passwords auto | interactive
生成密碼- auto是自動為保留的每個用戶(用戶名:apm,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user,elastic),自動生成密碼並輸出
- interactive是手動為每個用戶設置密碼
- 測試驗證集群密碼設置是否成功
- 准備工作
- 打開
- 啟用reindex功能,方便從其它集群復制index數據(可選)
- 添加
reindex.remote.whitelist: [172.16.60.12:9200]
設置
- 添加
Docker運行
-
Elasticsearch
docker run -d --name es_11 -p 9200:9200 -p 9300:9300 --group-add 0 -v /mnt/data/es:/usr/share/elasticsearch/data -v /mnt/nfs/es_snapshot:/mnt/nfs/es_snapshot -v /home/tszh/code/ics_backup/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/tszh/code/ics_backup/elasticsearch/config/elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12 docker.elastic.co/elasticsearch/elasticsearch:7.17.0
-p
指定運行端口--group-add
添加到root
用戶組0-v
掛載目錄- 數據目錄
- 快照目錄
- 配置目錄
-
Kibana
docker run -d --name kibana -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://elastic:xxx@172.16.60.12:9200" docker.elastic.co/kibana/kibana:7.17.1
-e
指定連接elasticsearch的信息,包括賬號和密碼
配置示例
- Elasticsearch
# All configs: https://www.elastic.co/guide/en/elasticsearch/reference/7.17/important-settings.html
cluster.name: es_cluster
node.name: es-11
# node.roles: ["master", "data"]
network.host: 0.0.0.0
network.publish_host: 172.16.60.11
http.port: 9200
transport.port: 9300
# Cluster Settings
# Cluster other hosts
discovery.seed_hosts:
- 172.16.60.10:9300
- 172.16.60.12:9300
cluster.initial_master_nodes:
- es-10
- es-11
- es-12
path.repo:
- /mnt/nfs/es_snapshot
# Reindex
reindex.remote.whitelist:
- 172.16.60.10:9200
- 172.16.60.11:9200
- 172.16.60.12:9200
- 172.16.60.130:9200
- 172.16.60.131:9200
- 172.16.60.133:9200
# Xpack
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.client_authentication: required
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
- /etc/sysctl.conf
# Max vm map count
vm.max_map_count=262144
# TCP retry count
net.ipv4.tcp_retries2=5
- /mnt/fstab
# Mount NFS
172.16.60.11:/ /mnt/nfs nfs defaults,timeo=900,retrans=5,_netdev 0 0
注意事項
- 由於掛載目錄的權限僅限
root和root組
進行訪問,所以確保docker elasticsearch用戶加入root用戶組
- completed handshake with [{es-11}{VXoHusQ9SuCFuYvGx77IcA}{blYzn_I0Qu2qLsc2YnR9qw}{172.17.0.2}{172.17.0.2:9300}{cdfhilmrstw}{ml.machine_memory=67381354496, ml.max_open_jobs=512, xpack.installed=true, ml.max_jvm_size=33285996544, transform.node=true}] but followup connection failed"
- docker部署時,需要指定
network.publish_host: xxx
,xxx
為當前主機IP地址,否則會造成無法找到節點ip
的問題
- docker部署時,需要指定