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的问题
posted @ 2022-03-03 16:00  冬天之歌  阅读(1892)  评论(0编辑  收藏  举报