Skip to content

ElasticSearch in Kubernetes

namespace.yaml

yaml
apiVersion: v1
kind: Namespace
metadata:
  name: common

elasticsearch-deployment.yaml

yaml
# Elasticsearch Service
apiVersion: v1
kind: Service
metadata:
  name: elasticsearch-service
  namespace: common
spec:
  type: ClusterIP
  selector:
    component: elasticsearch
  ports:
  - port: 80
    targetPort: 9200
---
# Elasticsearch Data PV Claim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: elasticsearch-data-pv-claim
  namespace: common
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
# Elasticsearch Plugin PV Claim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: elasticsearch-plugin-pv-claim
  namespace: common
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
# Elasticsearch Config PV Claim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: elasticsearch-config-pv-claim
  namespace: common
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
# Elasticsearch Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: elasticsearch-deployment
  namespace: common
spec:
  replicas: 1
  selector:
    matchLabels:
      component: elasticsearch
  template:
    metadata:
      labels:
        component: elasticsearch
    spec:
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: elasticsearch-data-pv-claim
        - name: plugin
          persistentVolumeClaim:
            claimName: elasticsearch-plugin-pv-claim
        - name: config
          persistentVolumeClaim:
            claimName: elasticsearch-config-pv-claim
        - name: logs
          emptyDir: {}
      containers:
        - name: elasticsearch
          image: elasticsearch:8.16.1
          ports:
            - containerPort: 9200
          volumeMounts:
            - name: data
              mountPath: /usr/share/elasticsearch/data
            - name: plugin
              mountPath: /usr/share/elasticsearch/plugins
            - name: config
              mountPath: /usr/share/elasticsearch/config
            - name: logs
              mountPath: /usr/share/elasticsearch/logs
          env:
            - name: discovery.type
              value: single-node
            - name: bootstrap.memory_lock
              value: 'true'
            - name: ES_JAVA_OPTS
              value: '-server -Xss1024K -Xmx2G'
            - name: TAKE_FILE_OWNERSHIP
              value: 'true'
            - name: xpack.security.enabled
              value: 'false'
      initContainers:
        - name: install-ik-plugin
          image: elasticsearch:8.16.1
          command:
            - sh
            - -c
            - |
              # 安装插件
              bin/elasticsearch-plugin install --batch https://get.infini.cloud/elasticsearch/analysis-ik/8.16.1
              if [ $? -ne 0 ]; then
                echo "Failed to install IK plugin."
                exit 1
              fi

              # 检查并复制默认配置
              if [ ! -d /usr/share/elasticsearch/config-copy ]; then
                mkdir -p /usr/share/elasticsearch/config-copy
              fi
              cp -R /usr/share/elasticsearch/config/* /usr/share/elasticsearch/config-copy/
              echo "Configuration files copied successfully."

          volumeMounts:
            - name: plugin
              mountPath: /usr/share/elasticsearch/plugins
            - name: config
              mountPath: /usr/share/elasticsearch/config-copy