ElasticSearch 8(3),2024年最新关于大数据开发性能优化的几点建议
jvm.options: |
-Xms4g
-Xmx4g
-XX:+UseG1GC ## JVM temporary directory -Djava.io.tmpdir=${ES\_TMPDIR} # Leverages accelerated vector hardware instructions; removing this may # result in less optimal vector performance 20-:--add-modules=jdk.incubator.vector # REMOVE once bumped to a JDK greater than 21.0.1, https://github.com/elastic/elasticsearch/issues/103004 19-21:-XX:CompileCommand=exclude,org.apache.lucene.util.MSBRadixSorter::computeCommonPrefixLengthAndBuildHistogram 19-21:-XX:CompileCommand=exclude,org.apache.lucene.util.RadixSelector::computeCommonPrefixLengthAndBuildHistogram ## heap dumps # generate a heap dump when an allocation from the Java heap fails; heap dumps # are created in the working directory of the JVM unless an alternative path is # specified -XX:+HeapDumpOnOutOfMemoryError # exit right after heap dump on out of memory error -XX:+ExitOnOutOfMemoryError # specify an alternative path for heap dumps; ensure the directory exists and # has sufficient space -XX:HeapDumpPath=/elasticsearch/data # specify an alternative path for JVM fatal error logs -XX:ErrorFile=/elasticsearch/logs/hs_err_pid%p.log ## GC logging -Xlog:gc*,gc+age=trace,safepoint:file=/elasticsearch/logs/gc.log:utctime,level,pid,tags:filecount=32,filesize=64m${ES_HOME}/bin/elasticsearch-certutil ca
这里可以选择添加证书密码,如果添加密码的话,后续使用CA证书去生成其他证书都需要先校验密码 默认会在${ES\_HOME}目录下生成 elastic-stack-ca.p12 这个证书文件,在实际操作中根据自己的实际情况进行调整 **使用CA证书生成 transport证书** 123456789${ES_HOME}/bin/elasticsearch-certutil cert --ca ${ES_HOME}/elastic-stack-ca.p12
最终会生成1个elastic-certificates.p12的证书文件 **使用CA证书生成http证书** 12345678/usr/share/elasticsearch/bin/elasticsearch-certutil http
执行后需要按提示输入一些选项 12345需要设置一些选项
: 否 {选是的话,需要使用自己的CA根证书}
使用已创建CA:是
输入CA文件路径:/elastic-stack-ca.p12 ${ES_HOME}/elastic-stack-ca.p12
设置证书过期时间:可以按年月日计算,例如10y为10年,10d为10天,10m为10个月。
录入需要使用证书的主机名 即可以通过https访问的主机名或域名,域名可以设置通配符如:*.es.example.com | 这里我用的是 es-01-svc.es 也就是{ES集群serviceAPI的name}.{service所在的命名空间},同个集群内都可以使用,k8s集群内部访问都是通过service的域名来做请求的,可以多个,每个换行。
录入需要使用证书的IP 需要设置证书适用集群节点的IP,否则后期增加节点会不可用,还得重新生成证书 : 输入ES集群节点将要绑定的IP地址,可以多个,每个换行
设置证书密码:建议为空,省点麻烦,这么多证书认证已经够够的了 以上完成后将在${elasticsearch\_home}下生成一个zIP压缩文件。 解压文件,生成一个文件夹,里面包含两个文件夹: elasticsearch文件夹包含http.p12及elasticsearch.yml的配置参考; kibana文件夹包含elasticsearch-ca.pem及kibana.yml的配置参考;(注意:kibana仅有这个证书是不够的) **用java/bin/keytool http.p12证书中注入CA证书** 这步很重要,否则生成的http.p12根本用不了,会提示异常,也是最让我想吐槽的地方,官方文档的安装步骤里根本没提,只是因为问题反馈有人说,才找到解答说这步是在哪哪哪,这也是我觉得ES官方文档很碎片化的原因,要说没有的吧,在某个地方也说了,但是从安装说明那开始走,找不到!! 1234567891011ERROR: Unable to create an enrollment token. Elasticsearch node HTTP layer SSL configuration Keystore doesn’t contain any PrivateKey entries where the associated certificate is a CA certificate, with exit code 73
12这里需要使用java/bin/keytool 给http.p12证书中注入CA证书
如果http.p12证书没有设置密码,只需要在命令中添加 -storepass “” 参数
keytool -importkeystore -destkeystore -srckeystore
discovery.seed_hosts: [“10.244.220.10:9300”,“10.244.220.11:9300”]
2. 注释单节点初始化 12345首次启动单点
#cluster.initial_master_nodes: [“10.244.220.10”]
3. 删除 commond中的 sleep 3600; **其他节点配置** 从节点1的k8s部署配置如下,有更多节点2,3,4,5 可以参考复制该配置,并调整name,IP属性值 **声明单独的存储PVC** 123456789101112#声明存储使用
#local存储 storage_class:local-path-provisioner(rancher)
APIVersion: v1
kind: PersistentVolumeClaim
metadata:
name: es-011-pvc
namespace: es
spec:
accessModes:
- ReadWriteOnce #本地存储只支持ReadWriteOnce
storageClassName: local-path #local-path: 容器删除,存储local-path动态删除,local-path-retain: 容器删除,存储local-path保留,local-path|local-path-retain为手动安装的storageclass
resources:
requests:
storage: 50Gi #声明最少要使用存储空间,不足则无法创建 Gi=G Mi=M
#persistentVolumeReclaimPolicy: Delete # PVC 回收策略 Retain 保留| Delete 清除 | PV: local-path-provisioner(rancher) 不支持设置该属性 **pod部署说明** * es-cluster: es-01 不要动这是为了让集群service能匹配到集群内的所有pod,集群内节点需要保持一致 12345678APIVersion: apps/v1
kind: StatefulSet # Deployment | StatefulSet | DaemonSet | JobSet
metadata:
name: es-011
namespace: es
spec:
replicas: 1 #运行副本数
selector:
matchLabels:
k8s-app: es-011 #与下方template节点中的 labels 保持一致
revisionHistoryLimit: 10 #设定保留最近的几个revision 用于回滚,默认10
#serviceName: “nginx-headless” #设置绑定的service,以支持内部dns访问 ...svc.cluster.local
updateStrategy: #更新策略 [Statefulset]
type: RollingUpdate # RollingUpdate (滚动更新) | OnDelete (删除时更新)
rollingUpdate:
#maxSurge: 1 #[Deployment]支持-升级过程中可以启动超过原先设置的POD数量的上限:数量 或 百分比 1 | 20%
#maxUnavailable: 1 #[Deployment]支持-升级过程中无法提供服务的POD数量的上限:数量 或 百分比 1 | 20%,最好与maxSurge保持一致,这样能确保更新过程中的服务能力不会下降
partition: 0 #[Statefulset] 灰度发布控制器,每次只更新部署的pod序号 >= partition的pod,如果有5个pod[0-4],0=更新所有,4=更新1pod,3=更新2pod
template:
metadata:
labels:
k8s-app: es-011
es-cluster: es-01
annotations:
“cni.projectcalico.org/IPAddrs”: “[“10.244.220.11”]” #pod绑定固定IP,依赖于calico IPam插件,必须使用calico 3.24.1以上的版本才可以
spec:
restartPolicy: Always
securityContext:
runAsUser: 0 #以root运行
containers:
- name: es-011
image: docker.elastic.co/elasticsearch/elasticsearch:8.12.0
imagePullPolicy: IfNotPresent # IfNotPresent | Always | Never
securityContext: ##开启特权,因为要调整系统内核
privileged: true
resources:
requests:
memory: “4Gi” #Gi=G Mi=M 只支持整数
cpu: “2000m” #1000m=1cpu (cpu物理线程)
limits:
memory: “6Gi” #Gi=G Mi=M 只支持整数
cpu: “4000m” #1000m=1cpu (cpu物理线程)
#securityContext: ###添加参数启用容器root权限
# privileged: true
ports:
- containerPort: 9200
protocol: TCP
- containerPort: 9300
protocol: TCP
command: [“/bin/sh”,“-c”]
args: #可以设置多行命令,不过启动后初始化还是推荐使用postStart钩子函数来执行,不能有#注释符
#将挂载的配置文件同步到默认的ES配置文件中,因为elastic的安全机制,软连接无法生效
#将${POD_NAME}.es.ndcto.com添加到本机hosts中,以便于与http.p12中的授信主机名适配
- |
cat /config/elasticsearch.yml > /usr/share/elasticsearch/config/elasticsearch.yml;
cat /config/jvm.options > /usr/share/elasticsearch/config/jvm.options;
chown -R elasticsearch:elasticsearch /usr/share/elasticsearch;
chown -R elasticsearch:elasticsearch /elasticsearch; swapoff -a echo elasticsearch - nproc 4096 >> /etc/security/limits.conf; echo elasticsearch - nofile 65535 >> /etc/security/limits.conf; echo elasticsearch soft memlock unlimited >> /etc/security/limits.conf; echo elasticsearch hard memlock unlimited >> /etc/security/limits.conf; echo vm.max\_map\_count=262144 >> /tmp/sysctl.conf; echo vm.swappiness=0 >> /tmp/sysctl.conf; echo vm.overcommit\_memory=1 >> /tmp/sysctl.conf; echo vm.zone\_reclaim\_mode=0 >> /tmp/sysctl.conf; echo net.IPv4.tcp\_retries2=5 >> /tmp/sysctl.conf; sysctl -p; echo ${POD\_IP} es01.es.ndcto.com >> /etc/hosts; echo ${POD\_IP} es-01-svc >> /etc/hosts; su - elasticsearch -c "/usr/share/elasticsearch/bin/elasticsearch -p /elasticsearch/elasticsearch.pid"; env: #环境变量配置 - name: POD_NAME valueFrom: fieldRef: APIVersion: v1 fieldPath: metadata.name - name: POD_IP valueFrom: fieldRef: fieldPath: status.podIP volumeMounts: - name: es-volume #挂载部署目录 mountPath: /elasticsearch/data subPathExpr: $(POD\_NAME)/elasticsearch/data - name: es-volume #挂载部署目录 mountPath: /elasticsearch/logs subPathExpr: $(POD\_NAME)/elasticsearch/logs - name: es-volume #挂载部署目录 mountPath: /usr/share/elasticsearch/.cache subPathExpr: $(POD\_NAME)/elasticsearch/cache - name: es-volume #挂载部署目录 mountPath: /usr/share/elasticsearch/plugins subPathExpr: $(POD\_NAME)/elasticsearch/plugins - name: es-011-cert-file #挂载存储目录 mountPath: /usr/share/elasticsearch/config/local-certs - name: es-011-config #挂载配置文件 mountPath: /config #readOnly: true - name: host-time #挂载本地时区 mountPath: /etc/localtime readOnly: true volumes: - name: es-volume #使用pvc persistentVolumeClaim: claimName: es-011-pvc - name: es-011-config #使用pvc configMap: #使用configMap name: es-011-config defaultMode: 420 #420-644 493-755 - name: es-011-cert-file hostPath: #挂载主机的目录 path: /data/deploy/k8s/elasticsearch/certs type: "" - name: host-time hostPath: #挂载本地时区 path: /etc/localtime type: ""APIVersion: v1
kind: ConfigMap #配置信息
metadata:
name: es-011-config #es-010配置
namespace: es
data:
elasticsearch.yml: |
cluster.name: “es-01”
node:
name: “es-011”
#指定节点角色
#roles: [ data, master]
# 为HTTP 和传输流量设置此节点的地址。 elastic将监听该地址的所有请求,0.0.0.0 代表监听本机所有网络地址的请求,指定地址则仅监听该地址的请求(接受IP、主机名或特殊值)。
network.host: 10.244.220.11 # 默认不开启 # 开启是为了能够在内网与其他节点通讯,使得新节点可以加入集群,0.0.0.0 代表监听本机所有网络地址的请求,指定地址则仅监听该地址的请求(接受IP、主机名或特殊值)。 transport.host: 10.244.220.11 # 用于节点发现 discovery.seed_hosts: ["10.244.220.10:9300","10.244.220.11:9300"] # 初始主节点配置,集群形成后,从每个节点的配置中删除此设置。 # 单点首次启动 #cluster.initial\_master\_nodes: ["10.244.220.11"] # 初始集群配置至少3台,集群形成后,从每个节点的配置中删除此设置,需要按顺序启动 #cluster.initial\_master\_nodes: ["10.244.220.10","10.244.220.11","10.244.220.12"] #配置存储路径 path.data: /elasticsearch/data path.logs: /elasticsearch/logs # 开启es跨域与head插件 http.cors.allow-origin: "\*" http.cors.enabled: true http.cors.allow-headers: Authorization http.max_content_length: 200mb #linux在使用内存锁时仍会交换堆外内存。要防止堆外内存交换,请禁用所有交换文件。 #k8s容器无法执行 bootstrap.memory_lock: true #限制高成本查询 search.default_search_timeout: "50s" #必须set为true,否则kibana报错 search.allow_expensive_queries: true #禁用通配符模糊匹配删除索引 action.destructive_requires_name: true # 设置自动创建索引(可选) # 一些商业功能会自动在 Elasticsearch 中创建索引。 默认情况下,Elasticsearch 配置为允许自动创建索引,不需要额外的步骤 #action.auto\_create\_index: .monitoring\*,.watches,.triggered\_watches,.watcher-history\*,.ml\* #----------------------- BEGIN SECURITY AUTO CONFIGURATION ----------------------- # # The following settings, TLS certificates, and keys have been automatically # generated to configure Elasticsearch security features on 07-02-2024 12:57:00 # # -------------------------------------------------------------------------------- # Enable security features xpack.security: enabled: true autoconfiguration: enabled: true xpack.security.enrollment.enabled: true # Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents xpack.security.http.ssl: enabled: true # pem证书配置方式 #key: local-certs/elastic-http.key #certificate: local-certs/elastic-http.crt #certificate\_authorities: [ "local-certs/elastic-http.crt" ] keystore.path: local-certs/http.p12 # Enable encryption and mutual authentication between cluster nodes xpack.security.transport.ssl: enabled: true verification_mode: certificate # pem证书配置方式 #key: local-certs/ca.key #certificate: local-certs/ca.crt #certificate\_authorities: [ "local-certs/ca.crt" ] # pks12证书配置方式 keystore.path: local-certs/elastic-certificates.p12 truststore.path: local-certs/elastic-certificates.p12 #----------------------- END SECURITY AUTO CONFIGURATION -------------------------jvm.options: |
-Xms4g
-Xmx4g
-XX:+UseG1GC ## JVM temporary directory -Djava.io.tmpdir=${ES\_TMPDIR} # Leverages accelerated vector hardware instructions; removing this may # result in less optimal vector performance 20-:--add-modules=jdk.incubator.vector # REMOVE once bumped to a JDK greater than 21.0.1, https://github.com/elastic/elasticsearch/issues/103004 19-21:-XX:CompileCommand=exclude,org.apache.lucene.util.MSBRadixSorter::computeCommonPrefixLengthAndBuildHistogram 19-21:-XX:CompileCommand=exclude,org.apache.lucene.util.RadixSelector::computeCommonPrefixLengthAndBuildHistogram ## heap dumps # generate a heap dump when an allocation from the Java heap fails; heap dumps # are created in the working directory of the JVM unless an alternative path is # specified -XX:+HeapDumpOnOutOfMemoryError # exit right after heap dump on out of memory error -XX:+ExitOnOutOfMemoryError # specify an alternative path for heap dumps; ensure the directory exists and # has sufficient space -XX:HeapDumpPath=/elasticsearch/data # specify an alternative path for JVM fatal error logs -XX:ErrorFile=/elasticsearch/logs/hs_err_pid%p.log ## GC logging -Xlog:gc*,gc+age=trace,safepoint:file=/elasticsearch/logs/gc.log:utctime,level,pid,tags:filecount=32,filesize=64m${ES_HOME}/bin/elasticsearch-reset-password -u elastic -i
${ES_HOME}/bin/elasticsearch-reset-password -u kibana_system -i 通过service 的 cluster IP 查看一下集群状态,需要输出刚才重置的elastic账号的密码 12345https://${svc_cluster_IP}:9200/_cat/nodes?v
正常输出![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/958d72e9cc5f4de1811f351fea9e2aab.png) 至此elasticsearch集群部署完成,后续可以根据需要安装插件,比如分词器elasticsearch-analysis-ik 只需要下载插件的压缩包,然后在所有ES节点的 ${ES\_HOME}//plugins/中创建该插件的目录,比如针对 elasticsearch-analysis-ik 我建了个analysis-ik的目录,然后将压缩包通过 kubectl cp 复制进去再解压缩,重启ES节点,ES节点启动后会自动扫描安装插件。 **注意插件的适配版本一定要与当前ES集群的版本一致,否则会报错 !!!** **生成kibana使用的证书** 除了之前生成http证书时生成的 elasticsearch-ca.pem 之外还有3个文件 kibana.crt,kibana.key,kibana.csr 下面先生成 kibana.csr,kibana.key生成kibana证书,在es节点中执行
-dns 证书适配的域名(kibana访问的es集群时使用的域名),多个可用,分隔 ,也可以使用 -IP 设置证书适配的IP
/usr/share/elasticsearch/bin/elasticsearch-certutil csr -name kibana -dns es-01-svc
这里我依然是通过域名方式来处理,使用的是es集群的service 域名,注意因为我的kibana是跟我的ES部署在k8s集群中的同一个namespace下,因此才可以通过https|http://service-name:port 直接访问。否则还需要按照k8s规则拼完整的service域名,这里就先不展开,反正只要kiibana跟es 部署在1个ns下就没问题。 执行后默认会生成 **csr-bundle.zIP** 解压缩后得到kibana.csr ,kibana.key,用它2生成 kibana.crt 123456789生成crt文件
openssl x509 -req -in kibana.csr -signkey kibana.key -out kibana.crt
将3个文件kibana.csr、kibana.key、kibana.crt打包下载到本地,包括elasticsearch-ca.pem 一并挂载到到 kibana pod的目录中,这里我使用/config/local\_certs,实际kibana配置文件中并没有配kibana.csr,为了以后便于维护我也放了进去。 ### kibana k8s部署文件 **照例先声明存储PVC** 1234567891011#声明存储使用
#local存储 storage_class:local-path-provisioner(rancher)
APIVersion: v1
kind: PersistentVolumeClaim
metadata:
name: kibana-010-pvc
namespace: es
spec:
accessModes:
- ReadWriteOnce #本地存储只支持ReadWriteOnce
storageClassName: local-path #local-path: 容器删除,存储local-path动态删除,local-path-retain: 容器删除,存储local-path保留,local-path|local-path-retain为手动安装的storageclass
resources:
requests:
storage: 20Gi #声明最少要使用存储空间,不足则无法创建 Gi=G Mi=M
#persistentVolumeReclaimPolicy: Delete # PVC 回收策略 Retain 保留| Delete 清除 | PV: local-path-provisioner(rancher) 不支持设置该属性 **声明service** 12345API service
APIVersion: v1
kind: Service
metadata:
name: kibana-01-svc
namespace: es
annotations:
desc : elastic 01 集群 kibana的服务访问入口
spec:
selector:
kibana-cluster: kibana-01
type: ClusterIP #type: ClusterIP【默认】 | NodePort | LoadBalancer(外部负载均衡) | ExternalName (外部DNS解析)
clusterIP: 10.106.220.221
ports:
- port: 5601
targetPort: 5601
name: httpport **声明StatefulSet ,这里不要绑定固定IP** 12345APIVersion: apps/v1
kind: StatefulSet # Deployment | StatefulSet | DaemonSet | JobSet
metadata:
name: kibana-010
namespace: es
spec:
replicas: 1 #运行副本数
selector:
matchLabels:
k8s-app: kibana-010 #与下方template节点中的 labels 保持一致
revisionHistoryLimit: 10 #设定保留最近的几个revision 用于回滚,默认10
#serviceName: “nginx-headless” #设置绑定的service,以支持内部dns访问 ...svc.cluster.local
updateStrategy: #更新策略 [Statefulset]
type: RollingUpdate # RollingUpdate (滚动更新) | OnDelete (删除时更新)
rollingUpdate:
#maxSurge: 1 #[Deployment]支持-升级过程中可以启动超过原先设置的POD数量的上限:数量 或 百分比 1 | 20%
#maxUnavailable: 1 #[Deployment]支持-升级过程中无法提供服务的POD数量的上限:数量 或 百分比 1 | 20%,最好与maxSurge保持一致,这样能确保更新过程中的服务能力不会下降
partition: 0 #[Statefulset] 灰度发布控制器,每次只更新部署的pod序号 >= partition的pod,如果有5个pod[0-4],0=更新所有,4=更新1pod,3=更新2pod
template:
metadata:
labels:
k8s-app: kibana-010
kibana-cluster: kibana-01
annotations:
#“cni.projectcalico.org/IPAddrs”: “[“10.244.220.10”]” #pod绑定固定IP,依赖于calico IPam插件,必须使用calico 3.24.1以上的版本才可以
spec:
restartPolicy: Always
securityContext:
runAsUser: 0 #以root运行
containers:
- name: kibana-010
image: docker.elastic.co/kibana/kibana:8.12.0
imagePullPolicy: IfNotPresent # IfNotPresent | Always | Never
#securityContext: ##要调整系统内核参数才开启特权容器
# privileged: true
resources:
requests:
memory: “1Gi” #Gi=G Mi=M 只支持整数
cpu: “1000m” #1000m=1cpu (cpu物理线程)
limits:
memory: “2Gi” #Gi=G Mi=M 只支持整数
cpu: “2000m” #1000m=1cpu (cpu物理线程)
#securityContext: ###添加参数启用容器root权限
# privileged: true
ports:
- containerPort: 8601
protocol: TCP
command: [“/bin/sh”,“-c”]
args: #可以设置多行命令,不过启动后初始化还是推荐使用postStart钩子函数来执行,不能有#注释符
- |
cat /config/kibana.yml > /usr/share/kibana/config/kibana.yml;
echo ‘’${POD_IP}’ kibana01.ndcto.com’ >> /etc/hosts;
su - kibana -c “/usr/share/kibana/bin/kibana”; env: #环境变量配置 - name: POD_NAME valueFrom: fieldRef: APIVersion: v1 fieldPath: metadata.name - name: POD_IP valueFrom: fieldRef: fieldPath: status.podIP volumeMounts: - name: kibana-volume #挂载部署目录 mountPath: /usr/share/kibana/data subPathExpr: $(POD\_NAME)/data - name: kibana-volume #挂载部署目录 mountPath: /usr/share/kibana/logs subPathExpr: $(POD\_NAME)/logs - name: kibana-010-config #挂载配置文件 mountPath: /config - name: kibana-010-cert-file #挂载ssl证书目录 mountPath: /config/local-certs/ - name: host-time #挂载本地时区 mountPath: /etc/localtime readOnly: true volumes: - name: kibana-volume #使用pvc persistentVolumeClaim: claimName: kibana-010-pvc - name: kibana-010-config #使用pvc configMap: #使用configMap name: kibana-010-config defaultMode: 420 #420-644 493-755 - name: kibana-010-cert-file hostPath: #挂载主机的目录 path: /data/deploy/k8s/kibana/certs type: "" - name: host-time hostPath: #挂载本地时区 path: /etc/localtime type: ""Ongwu博客 版权声明:以上内容未经允许不得转载!授权事宜或对内容有异议或投诉,请联系站长,将尽快回复您,谢谢合作!