Elasticsearch 7.17 集群搭建


Elasticsearch 部署文檔

搭建NFS server

目的:為集群snapshot備份提供共享文件系統。

教程

服務端設置

  1. 安裝server軟件: apt install nfs-kernel-server,安裝后服務會自動啟動
  2. 默認配置文件:/etc/default/nfs-kernel-server/etc/default/nfs-common
  3. 設置掛載目錄(由於NFSv4使用了一個全局根目錄,要想共享全局根目錄以外的目錄需要將其掛載至根目錄)
    • mount -bind /opt/backups /srv/nfs4/backups
    • 添加至/etc/fstab開機啟動:/opt/backups /srv/nfs4/backups none bind 0 0
  4. 設置訪問權限:vim /etc/exports
    • man exports查看詳細的權限設置
    • 常見設置:
      • rw 允許讀寫
      • sync 同步寫入
      • fsid=0 表示為全局根目錄
      • root_squash/no_root_squash 是否將root視為匿名用戶
      • all_squash 是否所有用戶是為匿名用戶nobody/nogroup
  5. 生效配置:exportfs -ar
  6. 查看配置: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

Elasticsearch集群

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.confvm.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.confvm.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
  • dockerstorage-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
      • 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
    • 設置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: xxxxxx為當前主機IP地址,否則會造成無法找到節點ip的問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM