优秀的编程知识分享平台

网站首页 > 技术文章 正文

浅谈ElasticSearch 集群部署(elastic集群配置)

nanyue 2024-10-23 12:09:28 技术文章 2 ℃

我们之前介绍了在非容器环境部署EFK的步骤,对于其中的ElasticSearch集群我们采用All-In-One模式来部署,今天我们来认识一下ES Cluster

ElasticSearch Cluster


ElasticSearch使用多个节点构建集群,每个节点在集群中具有不同的角色和职责,不同版本的ES的节点类型会有些差异,但核心节点有下面三个:

  • Master Node
    Master节点是集群中唯一管理所有其他节点注册和配置的主节点。为了提高冗余性,它还可以有一个备份节点(master-eligible),能够随时提升为主节点
  • Data Node
    Data节点在集群中存储和操作数据
  • Coordinate Node
    Coordinate节点处理来自其他应用程序的HTTP请求,然后将请求负载均衡分发到数据节点

ElasticSearch Cluster on K8s


ES三个核心节点在Kubernetes中部署的说明如下:

  • Master Node
    Master节点在集群中是唯一的,其他节点需要向它注册。因此Master节点应该使用K8s中的Stateful来分配固定的DNS名称如es-master-1。同时为了保证inter-cluster服务发现,必须使用Headless Service模式分配DNS,该模式直接将DNS指向Pod IP地址。
    Master-eligible节点是主节点的备用节点,这意味着扩展Master节点的Stateful来分配es-master-2,es-master-3,而无需创建另外1个k8对象。
    当主节点没有相应时,在备份节点中会进行主节点选举,选举并提升1个节点作为主节点。
  • Data Node
    Data节点负责存储数据,如果有更大的数据量或者更多的查询请求,我们可以使用Stateful和PV进行水平扩展。此外它不需要DNS名称,所以不需要为此创建Service。
  • Coordinate Node
    Coordinate节点在ES中是负载均衡的角色,因此需要水平扩展以处理更多来自外部的HTTP流量,并且不需要对数据持久保存。我们可以使用Stateless Service对外暴露接口。

ElasticSearch Cluster on K8s 部署

接下来我们将使用ECK(Elastic Cloud on Kubernetes)来部署ES Cluster.

  1. 安装CRD 和ES operator
root@demo:~/es# kubectl apply -f https://download.elastic.co/downloads/eck/1.1.2/all-in-one.yaml
root@demo:~/es# kubectl -n elastic-system logs -f statefulset.apps/elastic-operator
  1. 创建本地PV并部署ES Cluster
root@demo:~/es# cat pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: elasticsearch-data-quickstart-es-default-0
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: elasticsearch-data-quickstart-es-default-1
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp1
---

root@demo:~/es# cat es-quick.yaml
apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
  name: quickstart
spec:
  version: 7.8.0
  nodeSets:
  - name: default
    count: 2
    config:
      node.master: true
      node.data: true
      node.ingest: true
      node.store.allow_mmap: false

3.检查ES Cluster状态

当我们看到Health状态为green时说明Cluser状态运行成功。

Note:

  • 当检查Pod时,需要等待2-3分钟才能看到Ready状态正常
  • ES部署需要在2台以上的Node上,如果k8s集群只有1台,我们只能看到其状态为yellow.
root@demo:~/es# kubectl get elasticsearch
NAME         HEALTH   NODES   VERSION   PHASE   AGE
quickstart   green    2       7.8.0     Ready   115m
root@demo:~/es# kubectl get statefulset
NAME                    READY   AGE
quickstart-es-default   2/2     116m
root@demo:~/es# kubectl get pod
NAME                      READY   STATUS    RESTARTS   AGE
quickstart-es-default-0   1/1     Running   0          116m
quickstart-es-default-1   1/1     Running   0          26m

通过检查Pod和Service我们也可以验证之前面提及的K8s部署ES的要求:

  • ES Cluster部署了2个Pod(quickstart-es-default-0),Service依照label来调用对应的Pod
  • Service(quickstart-es-http) 扮演了Coordinator Node,是通过ClusterIP来进行K8s内部调用
  • Service(quickstart-es-transport)扮演了Master Node,需要启用Headless
root@demo:~/es# kubectl get svc
NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes                ClusterIP   10.96.0.1       <none>        443/TCP          41d
quickstart-es-default     ClusterIP   None            <none>        <none>           25d
quickstart-es-http        ClusterIP   10.109.99.236   <none>        9200/TCP         25d
quickstart-es-transport   ClusterIP   None            <none>        9300/TCP         25d

ES操作初步

  1. 确认ES运行状态
#获得ES 密码
root@demo:~/es# PASSWORD=$(kubectl get secret quickstart-es-elastic-user -o go-template='{{.data.elastic | base64decode}}')
root@demo:~/es# echo $PASSWORD
28ug1YfVANH1pR1MLF6h6449
#创建Curl测试Pod
root@demo:~/es#  kubectl run curl --image=tutum/curl -- sleep 64000
root@demo:~/es#  kubectl exec -it curl sh
#  PASSWORD='28ug1YfVANH1pR1MLF6h6449'
# curl -u "elastic:$PASSWORD" -k "https://quickstart-es-http:9200/_cluster/health?pretty"
{
  "cluster_name" : "quickstart",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 6,
  "active_shards" : 12,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}
# curl -u "elastic:$PASSWORD" -k "https://quickstart-es-http:9200/_cluster/stats?pretty"    //  检查节点状态
  1. 添加Index信息
# curl -X POST -u "elastic:$PASSWORD" -k "https://quickstart-es-http:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index": {"_index": "pokemons"}}
{"id": 1, "Name": "Pickachu", "type": "electric"}
{"index": {"_index": "pokemons"}}
{"id": 2, "Name": "Charmendar", "type": "fire"}
{"index": {"_index": "pokemons"}}
{"id": 3, "Name": "Togepi", "type": "egg"}
{"index": {"_index": "pokemons"}}
{"id": 4, "Name": "Bulbasaur", "type": "grass"}
'
# curl -u "elastic:$PASSWORD" -k "https://quickstart-es-http:9200/_cat/indices?v"
health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   pokemons                 s8QJw-F7Svu3mZrXU7pUEw   1   1          6            0      9.9kb          4.9kb

Kibana on K8s

  1. 安装Kibana
root@demo:~/es#   cat kibana.yaml
apiVersion: kibana.k8s.elastic.co/v1
kind: Kibana
metadata:
  name: quickstart
spec:
  version: 7.8.0
  count: 1
  elasticsearchRef:
    name: quickstart
root@demo:~/es#  kubectl creat -f kibana.yaml
root@demo:~/es#  kubectl get pod
NAME                             READY   STATUS    RESTARTS   AGE
curl                             1/1     Running   0          76m
quickstart-es-default-0          1/1     Running   0          3h26m
quickstart-es-default-1          1/1     Running   0          116m
quickstart-kb-84dccf6d86-tld6b   1/1     Running   0          116m23s
  1. 查看ES
  • 修改SVC Kibana的Type为NodePort
root@demo:~/es#  kubectl get svc
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes                ClusterIP   10.96.0.1      <none>        443/TCP    5h18m
quickstart-es-default     ClusterIP   None           <none>        <none>     3h27m
quickstart-es-http        ClusterIP   10.96.82.242   <none>        9200/TCP   3h27m
quickstart-es-transport   ClusterIP   None           <none>        9300/TCP   3h27m
quickstart-kb-http        ClusterIP   10.97.4.138    <none>        5601/TCP   7m12s
root@demo:~/es#   kubectl edit svc quickstart-kb-http
spec:
  clusterIP: 10.97.4.138
  ports:
  - name: https
    port: 5601
    protocol: TCP
    targetPort: 5601
  selector:
    common.k8s.elastic.co/type: kibana
    kibana.k8s.elastic.co/name: quickstart
  sessionAffinity: None
  type: NodePort
root@demo:~/es#   kubectl get svc
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes                ClusterIP   10.96.0.1      <none>        443/TCP          5h21m
quickstart-es-default     ClusterIP   None           <none>        <none>           3h29m
quickstart-es-http        ClusterIP   10.96.82.242   <none>        9200/TCP         3h29m
quickstart-es-transport   ClusterIP   None           <none>        9300/TCP         3h29m
quickstart-kb-http        NodePort    10.97.4.138    <none>        5601:32096/TCP   9m13s
  • 在浏览器中输入http://<node-ip>:32096 访问Kibana,用户名和密码分别是elastic/28ug1YfVANH1pR1MLF6h6449, 内容显示如下:


Tags:

最近发表
标签列表