elasticsearch.yaml


Overview

The elasticsearch.yaml file is a Kubernetes manifest template used to deploy and manage an Elasticsearch instance within a Kubernetes cluster. It is designed specifically to be rendered by Helm, a Kubernetes package manager, and is conditionally included only when the DOC_ENGINE environment variable is set to "elasticsearch" in the Helm values.

This file provisions the following Kubernetes resources essential for running Elasticsearch:

The manifest utilizes Helm templating syntax to dynamically configure resource names, labels, storage classes, resource requests, container images, and environment variables based on user-provided Helm values.


Detailed Explanation of Resources

1. PersistentVolumeClaim (PVC)

Purpose

Defines persistent storage for Elasticsearch data to ensure data durability across pod restarts or rescheduling.

Key Fields

Usage Example

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myapp-es-data
spec:
  storageClassName: fast-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi

2. StatefulSet

Purpose

Manages Elasticsearch pods, providing stable network IDs and persistent storage, critical for Elasticsearch clustering and data integrity.

Key Fields

Usage Example Snippet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: myapp-es
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
      component: elasticsearch
  template:
    metadata:
      labels:
        app: myapp
        component: elasticsearch
    spec:
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0
        ports:
          - containerPort: 9200
          - containerPort: 9300
        volumeMounts:
          - name: es-data
            mountPath: /usr/share/elasticsearch/data
      volumes:
        - name: es-data
          persistentVolumeClaim:
            claimName: myapp-es-data

3. Service

Purpose

Exposes Elasticsearch pods to other services or external clients for HTTP and transport communication.

Key Fields

Usage Example

apiVersion: v1
kind: Service
metadata:
  name: myapp-es
spec:
  selector:
    app: myapp
    component: elasticsearch
  ports:
    - protocol: TCP
      port: 9200
      targetPort: http
  type: ClusterIP

Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

flowchart TD
    subgraph PVC
        PVC[PersistentVolumeClaim: es-data]
    end

    subgraph StatefulSet
        STS[StatefulSet: elasticsearch]
        IC1[InitContainer: fix-data-volume-permissions]
        IC2[InitContainer: sysctl]
        C[Container: elasticsearch]
        VolumeMount[VolumeMount: es-data]
        Env[EnvFrom: secret & configMap]
        Ports[Ports: 9200 (http), 9300 (transport)]
    end

    subgraph Service
        SVC[Service: elasticsearch]
    end

    PVC -->|Provides Storage| VolumeMount
    VolumeMount --> C
    IC1 --> STS
    IC2 --> STS
    Env --> C
    Ports --> C
    STS --> SVC
    SVC -->|Routes Traffic| C

Summary

The elasticsearch.yaml file is a Helm-templated Kubernetes manifest that deploys an Elasticsearch cluster with persistent storage, proper initialization, and secure runtime configuration. It integrates tightly with Helm's templating system and external configuration sources, ensuring a flexible, consistent, and production-ready Elasticsearch deployment within a Kubernetes environment. The file defines key Kubernetes resources (PVC, StatefulSet, Service) and includes best practices such as init containers for environment setup and security context restrictions.