You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Deployment YAML在GKE的Elasticsearch Pod中创建synonyms文件夹

给Elasticsearch Deployment添加synonyms文件夹的修改方案

要在GKE环境下的Elasticsearch Pod中创建/usr/share/elasticsearch/synonyms文件夹,你需要对现有Deployment做以下修改:

方案1:直接在初始化容器中创建文件夹(无持久化)

修改initContainers里的fix-permissions容器命令,让它在修正data目录权限的同时,创建synonyms文件夹并设置正确权限(Elasticsearch容器以UID 1000运行):

initContainers:
- name: fix-permissions
  image: busybox
  command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data && mkdir -p /usr/share/elasticsearch/synonyms && chown -R 1000:1000 /usr/share/elasticsearch/synonyms"]
  securityContext:
    privileged: true
  volumeMounts:
  - name: data-volume
    mountPath: /usr/share/elasticsearch/data

方案2:给synonyms文件夹添加持久化存储(可选)

如果需要持久化synonyms目录下的文件(比如同义词词典),可以添加新的Volume和VolumeMount:

  1. 在spec.volumes中新增一个卷(以PVC为例,需提前创建对应PVC):
volumes:
- name: data-volume
  persistentVolumeClaim:
    claimName: elasticsearch-pvc
- name: synonyms-volume
  persistentVolumeClaim:
    claimName: elasticsearch-synonyms-pvc  # 需提前创建该PVC
  1. 在Elasticsearch容器的volumeMounts中添加挂载:
volumeMounts:
- name: data-volume
  mountPath: /usr/share/elasticsearch/data
- name: synonyms-volume
  mountPath: /usr/share/elasticsearch/synonyms
  1. 修改fix-permissions容器的命令和volumeMounts,确保synonyms目录权限正确:
initContainers:
- name: fix-permissions
  image: busybox
  command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data && chown -R 1000:1000 /usr/share/elasticsearch/synonyms"]
  securityContext:
    privileged: true
  volumeMounts:
  - name: data-volume
    mountPath: /usr/share/elasticsearch/data
  - name: synonyms-volume
    mountPath: /usr/share/elasticsearch/synonyms

修改后的完整Deployment YAML(方案1)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: elasticsearch
  namespace: elk
spec:
  replicas: 1
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: app-elk
                operator: In
                values:
                - "elastic-tomcat"
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.17.5
        imagePullPolicy: Always
        resources:
            requests:
              memory: 1Gi
        ports:
        - containerPort: 9200
          protocol: TCP
          name: rest
        - containerPort: 9300
          protocol: TCP
          name: inter-node
        volumeMounts:
        - name: data-volume
          mountPath: /usr/share/elasticsearch/data  
        env:
          - name: cluster.name
            value: elasticsearch-cluster
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: discovery.seed_hosts
            value: "elasticsearch-cluster-0.elasticsearch"
          - name: cluster.initial_master_nodes
            value: "elasticsearch-cluster-0"
          - name: ES_JAVA_OPTS
            value: "-Xms512m -Xmx512m"
          - name: network.host
            value: "0.0.0.0"
      initContainers:
      - name: fix-permissions
        image: busybox
        command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data && mkdir -p /usr/share/elasticsearch/synonyms && chown -R 1000:1000 /usr/share/elasticsearch/synonyms"]
        securityContext:
          privileged: true
        volumeMounts:
        - name: data-volume
          mountPath: /usr/share/elasticsearch/data
      - name: increase-vm-max-map
        image: busybox
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      - name: increase-fd-ulimit
        image: busybox
        command: ["sh", "-c", "ulimit -n 65536"]
        securityContext:
          privileged: true
      volumes:
        - name: data-volume
          persistentVolumeClaim:
            claimName: elasticsearch-pvc  # Create a PVC for data storage

内容的提问来源于stack exchange,提问作者Nishit Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 19:16:05