K8S StatefulSet方式部署elasticsearch集群 —— 筑梦之路

直接上yaml内容

# es-cluster.yaml
apiVersion: apps/v1 
# 设置控制器
kind: StatefulSet
metadata:
  name: es-cluster
  namespace: merry
spec:
  # 必须设置
  serviceName: es-cluster-svc
  # 设置副本数
  replicas: 3
  # 设置选择器
  selector:
    # 设置标签
    matchLabels:
      app: es-net-data
  template:
    metadata:
      # 此处必须要与上面的matchLabels相同
      labels: 
        app: es-net-data
    spec:
      # 初始化容器
      # 初始化容器的作用是在应用容器启动之前做准备工作,每个init容器都必须在下一个启动之前成功完成
      initContainers:
        - name: increase-vm-max-map
          image: busybox:1.32
          command: ["sysctl", "-w", "vm.max_map_count=262144"]
          securityContext:
            privileged: true
        - name: increase-fd-ulimit
          image: busybox:1.32
          command: ["sh", "-c", "ulimit -n 65536"]
          securityContext:
            privileged: true
      # 初始化容器结束后,才能继续创建下面的容器
      containers:
        - name: es-container
          image: elasticsearch:7.6.2
          ports:
            # 容器内端口
            - name: rest
              containerPort: 9200
              protocol: TCP
          # 限制CPU数量
          resources:
            limits:
              cpu: 1000m
            requests:
              cpu: 100m 
          # 设置挂载目录
          volumeMounts:
            - name: es-data
              mountPath: /usr/share/elasticsearch/data
          # 设置环境变量
          env:
            # 自定义集群名
            - name: cluster.name
              value: k8s-es
            # 定义节点名,使用metadata.name名称
            - name: node.name
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            # 初始化集群时,ES从中选出master节点
            - name: cluster.initial_master_nodes
              # 对应metadata.name名称加编号,编号从0开始
              value: "es-cluster-0,es-cluster-1,es-cluster-2"
            - name: discovery.zen.minimum_master_nodes
              value: "2" 
            # 发现节点的地址,discovery.seed_hosts的值应包括所有master候选节点
            # 如果discovery.seed_hosts的值是一个域名,且该域名解析到多个IP地址,那么es将处理其所有解析的IP地址。
            - name: discovery.seed_hosts
              value: "es-cluster-svc"
             # 配置内存
            - name: ES_JAVA_OPTS
              value: "-Xms1g -Xmx1g"
            - name: network.host
              value: "0.0.0.0" 
  volumeClaimTemplates:
    - metadata:
        # 对应容器中volumeMounts.name
        name: es-data
        labels:
          app: es-volume
      spec:
        # 存储卷可以被单个节点读写
        accessModes: [ "ReadWriteOnce" ]
        # 对应es-nfs-storage-class.yaml中的metadata.name
        storageClassName: es-nfs-storage 
        # 申请资源的大小
        resources:
          requests:
            storage: 10Gi
---
# es-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: es-cluster-svc
  namespace: merry
spec:
  selector:
    # 注意一定要与"es-cluster.yaml"中spec.selector.matchLabels相同
    app: es-net-data
  # 设置服务类型
  type: NodePort
  ports:
    - name: rest
      # 服务端口
      port: 9200
      # 应用端口(Pod端口)
      targetPort: 9200
      # 映射到主机的端口,端口范围是30000~32767
      nodePort: 32000
    - name: socket9300
      port: 9300
      targetPort: 9300
      nodePort: 39300
---

能看懂的人都能明白!

------------------------------------------------改进版本如下----------------------------------------------------------

主要改进地方:

1. 添加时区的环境变量,修改cluster.name值为docker-cluster

2.初始化容器指定数据目录权限

3.添加无头服务headless

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: merry
  name: es-cluster
spec:
  serviceName: elasticsearch-svc-headless
  replicas: 3
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      initContainers:
      - name: permissions
        image: busybox:latest
        command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
        securityContext:
          privileged: true
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
      - name: increase-vm-max-map
        image: busybox:latest
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      - name: increase-fd-ulimit
        image: busybox:latest
        command: ["sh", "-c", "ulimit -n 65536"]
        securityContext:
          privileged: true
      containers:
      - name: elasticsearch
        image: elasticsearch:7.10.2-analysis-hanlp
        resources:
            limits:
              cpu: 1000m
            requests:
              cpu: 100m
        ports:
        - containerPort: 9200
          name: rest
          protocol: TCP
        - containerPort: 9300
          name: socket
          protocol: TCP
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
        env:
          - name: TZ
            value: Asia/Shanghai
          - name: cluster.name
            value: docker-cluster
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: discovery.seed_hosts
            value: "es-cluster-0.elasticsearch-svc-headless,es-cluster-1.elasticsearch-svc-headless,es-cluster-2.elasticsearch-svc-headless"
          - name: cluster.initial_master_nodes
            value: "es-cluster-0,es-cluster-1,es-cluster-2"
          - name: ES_JAVA_OPTS
            value: "-Xms512m -Xmx512m"
          - name: discovery.zen.minimum_master_nodes
            value: "2"
          - name: network.host
            value: "0.0.0.0"
  volumeClaimTemplates:
  - metadata:
      name: data
      labels:
        app: elasticsearch
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: managed-nfs-storage
      resources:
        requests:
          storage: 1000Gi
---
kind: Service
apiVersion: v1
metadata:
  namespace: merry
  name: elasticsearch-svc-headless
  labels:
    app: elasticsearch
spec:
  selector:
    app: elasticsearch
  clusterIP: None
  ports:
    - port: 9200
      name: rest
    - port: 9300
      name: socket
---
kind: Service
apiVersion: v1
metadata:
  namespace: merry
  name: elasticsearch-svc
  labels:
    app: elasticsearch
spec:
  selector:
    app: elasticsearch
  type: NodePort
  ports:
    - port: 9200
      name: rest
    - port: 9300
      name: socket

你可能感兴趣的:(云计算,linux系统运维,elasticsearch,搜索引擎,k8s)