es 7.6.2 部署
2022-08-02
准备环境
# 下载安装包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-linux-x86_64.tar.gz
Dockerfile
编写
# cat Dockerfile
FROM mrliulei/jdk:13.0.2
RUN rm -f /etc/localtime \
&& ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
ENV LANG en_US.UTF-8
ADD elasticsearch-7.6.2-linux-x86_64.tar.gz /opt
RUN mkdir -p /opt/elasticsearch-7.6.2/data \
&& mkdir -p /opt/elasticsearch-7.6.2/logs \
&& useradd elasticsearch \
&& chown -R elasticsearch:elasticsearch /opt \
&& chmod -R 777 /opt \
&& setfacl -R -m u:elasticsearch:rwx /opt \
&& setfacl -R -m u:elasticsearch:rwx /opt \
&& rm -f /opt/elasticsearch-7.6.2/config/elasticsearch.yml
COPY elasticsearch.yml /opt/elasticsearch-7.6.2/config/
USER elasticsearch
EXPOSE 9200 9300
CMD ["/opt/elasticsearch-7.6.2/bin/elasticsearch"]
elasticsearch.yml
文件准备
# cat elasticsearch.yml
cluster.name: es-cluster
# 这里的 elasticsearch 是 svc 的名称;也可以写为:pod名称.service名称.namespace名称.svc.cluster.local
node.name: ${MY_POD_NAME}.elasticsearch
# path.data: /opt/elasticsearch-7.6.2/data
# path.logs: /opt/elasticsearch-7.6.2/logs
network.host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
# ES7.0+新版废弃了原先discovery.zen.ping.unicast.hosts及discovery.zen.minimum_master_nodes的探测方式,而是改为了discovery.seed_hosts及cluster.initial_master_nodes。
# 这里的 es-x.elasticsearch 为 es 节点名称+ svc 名称;这样写可以准确找到 es 的各个节点IP地址;
discovery.seed_hosts: ["es-0.elasticsearch","es-1.elasticsearch","es-2.elasticsearch"]
cluster.initial_master_nodes: ["es-0.elasticsearch","es-1.elasticsearch","es-2.elasticsearch"]
构建docker镜像
# ls
Dockerfile elasticsearch-7.6.2-linux-x86_64.tar.gz elasticsearch.yml
#
docker build -t mrliulei/elasticsearch:7.6.2 .
docker push mrliulei/elasticsearch:7.6.2
编写 StatefulSet 文件
cat es-7.6.2.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
addonmanager.kubernetes.io/mode: Reconcile
k8s-app: es
version: v7.6.2
name: es
namespace: monitoring
spec:
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: es
version: v7.6.2
serviceName: elasticsearch
template:
metadata:
labels:
k8s-app: es
version: v7.6.2
spec:
nodeSelector:
log: es # 指定部署在哪个节点。需根据环境来修改
affinity:
# 指定调度podAntiAffinity,确保每个节点只运行一个es 的 pod
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: k8s-app
operator: In
values:
- es
topologyKey: "kubernetes.io/hostname"
containers:
- env:
- name: NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
# 因为在es配置文件中指定了,所以这里注释掉
# - name: cluster.name
# value: es-cluster
# - name: discovery.seed_hosts
# value: es-0.elasticsearch,es-1.elasticsearch,es-2.elasticsearch
# - name: cluster.initial_master_nodes
# value: es-0.elasticsearch,es-1.elasticsearch,es-2.elasticsearch
# - name: node.master
# value: "true"
# - name: node.data
# - name: network.host
# value: "0.0.0.0"
- name: ES_JAVA_OPTS
value: "-Xms5g -Xmx5g"
name: es
image: mrliulei/elasticsearch:7.6.2
imagePullPolicy: Always
ports:
- containerPort: 9200
name: db
protocol: TCP
- containerPort: 9300
name: transport
protocol: TCP
volumeMounts:
# - mountPath: /usr/share/elasticsearch/data
- mountPath: /opt/elasticsearch-7.6.2/data
name: elasticsearch-logging
dnsConfig:
options:
- name: single-request-reopen
initContainers:
- name: increase-fd-ulimit
image: busybox
command: ["sh", "-c", "ulimit -n 65536"]
securityContext:
privileged: true
# terminationGracePeriodSeconds: 60
- command:
- /sbin/sysctl
- -w
- vm.max_map_count=262144
image: alpine:3.6
imagePullPolicy: IfNotPresent
name: elasticsearch-logging-init
resources: {}
securityContext:
privileged: true
- name: fix-permissions
image: alpine:3.6
command: ["sh", "-c", "chown -R 1000:1000 /opt/elasticsearch-7.6.2/data"]
securityContext:
privileged: true
volumeMounts:
- name: elasticsearch-logging
mountPath: /opt/elasticsearch-7.6.2/data
volumes:
- name: elasticsearch-logging
hostPath:
path: /esdata
---
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: es
name: elasticsearch
namespace: monitoring
spec:
ports:
- port: 9200
protocol: TCP
name: db
- port: 9300
protocol: TCP
name: transport
selector:
k8s-app: es
type: ClusterIP
# clusterIP: None
kubectl aplace -f es-7.6.2.yaml
检查 es 集群状态
# 这里解释一下为什么可以直接用 elasticsearch 就可以访问 es 集群;
1. es 的 svc 名称为 elasticsearch
es 的全名 为 elasticsearch.monitoring.svc.cluster.local
2. es 节点中的 dns 搜索域中有完全的 svc 名称,如下:
$ cat /etc/resolv.conf
nameserver 172.20.0.10
search monitoring.svc.cluster.local svc.cluster.local cluster.local us-west-2.compute.internal
options ndots:5 single-request-reopen
# 其中的 search 行代表 dns 的搜索域,也就是说 我们 ping elasticsearch ,它会自动补全
# elasticsearch.monitoring.svc.cluster.local, 如果这个svc不存在,它会继续向后补全
# elasticsearch.svc.cluster.local
# 单节点状态检查
curl http://elasticsearch:9200
curl http://localhost:9200
# 查看集群状态
curl -XGET "http://elasticsearch:9200/_cat/nodes"
10.20.162.100 3 89 3 0.10 0.10 0.09 dilm - es-1.elasticsearch
10.20.145.96 2 99 4 0.20 0.15 0.13 dilm * es-0.elasticsearch
10.20.111.19 2 59 4 0.00 0.04 0.13 dilm - es-2.elasticsearch
# 查看集群详细信息,后面添加"?v"
# 注意:带*符号的表示是当前的master主节点
curl -XGET 'http://elasticsearch:9200/_cat/nodes?v'
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
10.20.145.96 2 99 3 0.07 0.12 0.12 dilm * es-0.elasticsearch
10.20.162.100 3 89 3 0.03 0.08 0.08 dilm - es-1.elasticsearch
10.20.111.19 3 59 5 0.07 0.05 0.12 dilm - es-2.elasticsearch
# 查看集群状态方法
curl -XGET 'http://elasticsearch:9200/_cluster/state/nodes?pretty'
# 查询集群中的master(下面两个命令都可以)
curl -XGET 'http://elasticsearch:9200/_cluster/state/master_node?pretty'
curl -XGET 'http://elasticsearch:9200/_cat/master?v'
# 查询集群的健康状态(一共三种状态:green、yellow,red;其中green表示健康)
# 下面两个命令都可以
curl -XGET 'http://elasticsearch:9200/_cat/health?v'
curl -XGET 'http://elasticsearch:9200/_cluster/health?pretty'
#
参考
连接地址: