kubernetes 如何在通过yml清单部署StatefulSet时编辑elasticsearch.yml?

idv4meu8  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(2)|浏览(113)

我想启用ElasticSearch的安全功能,根据this教程,我需要将xpack.security.enabled: true添加到elasticsearch.yml中。
我尝试通过添加以下命令来实现这一点:

command:
   - "sh"
   - "-c"
   - "echo 'xpack.security.enabled: true >> /usr/share/elasticsearch/config/elasticsearch.yml"

字符串
但这让吊舱进入了CrashLoopBackOff。一开始我以为这是因为elasticsearch.yml文件在这一点上不存在,但当我将命令改为:

command:
   - "sh"
   - "-c"
   - "cat /usr/share/elasticsearch/config/elasticsearch.yml"


kubectl logs <pod-name>中,我可以看到它确实存在,并包含以下几行:

cluster.name: "docker-cluster"
network.host: 0.0.0.0


奇怪的是,即使我使用一个非常简单的命令,比如ls,我总是得到CrashLoopBackOff。
这是ElasticSearch StatefulSet的完整清单文件:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: es-cluster
  namespace: efk-stack
spec:
  serviceName: elasticsearch
  replicas: 3
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.17.10
        command:
            - "sh"
            - "-c"
            - "cat /usr/share/elasticsearch/config/elasticsearch.yml"
        resources:
            limits:
              cpu: 1000m
            requests:
              cpu: 100m
        ports:
        - containerPort: 9200
          name: rest
          protocol: TCP
        - containerPort: 9300
          name: inter-node
          protocol: TCP
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
        env:
          - name: cluster.name
            value: k8s-logs
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: discovery.seed_hosts
            value: "es-cluster-0.elasticsearch,es-cluster-1.elasticsearch,es-cluster-2.elasticsearch"
          - name: cluster.initial_master_nodes
            value: "es-cluster-0,es-cluster-1,es-cluster-2"
          - name: ES_JAVA_OPTS
            value: "-Xms512m -Xmx512m"
      initContainers:
      - name: fix-permissions
        image: busybox
        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
        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
  volumeClaimTemplates:
  - metadata:
      name: data
      labels:
        app: elasticsearch
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 3Gi

ozxc1zmp

ozxc1zmp1#

如果我理解正确的话,你的主要目标是简单地编辑/usr/share/elasticsearch/config/elasticsearch.yml文件,然后让elastisearch正常启动?
在这种情况下,ConfigMap和VolumeMount是您的朋友。
TL;DR:创建一个ConfigMap,其中包含您在elasticsearch.yml中所需的 * 全部 * 内容(即而不仅仅是你想添加的部分),并将其挂载为/usr/share/elasticsearch/config/elasticsearch.yml的卷。这将在启动时覆盖该文件。
具体如下:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
  namespace: efk-stack
data:
  elasticsearch.yml: |-
    foo: bar
    baz: foo
    xpack.security.enabled: true    
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: es-cluster
  namespace: efk-stack
spec:
  serviceName: elasticsearch
  replicas: 3
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:

      ### added:
      volumes:
        - name: my-configmap
          configMap:
            name: my-configmap
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:7.17.10

        ## removed so that default startup command is used
        # command:
        #     - "sh"
        #     - "-c"
        #    - "cat /usr/share/elasticsearch/config/elasticsearch.yml"

        resources:
            limits:
              cpu: 1000m
            requests:
              cpu: 100m
        ports:
        - containerPort: 9200
          name: rest
          protocol: TCP
        - containerPort: 9300
          name: inter-node
          protocol: TCP
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data

        ## added
        - name: my-configmap
          subPath: elasticsearch.yml 
          readOnly: true
          mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
          
        env:
          - name: cluster.name
            value: k8s-logs
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: discovery.seed_hosts
            value: "es-cluster-0.elasticsearch,es-cluster-1.elasticsearch,es-cluster-2.elasticsearch"
          - name: cluster.initial_master_nodes
            value: "es-cluster-0,es-cluster-1,es-cluster-2"
          - name: ES_JAVA_OPTS
            value: "-Xms512m -Xmx512m"
      initContainers:
      - name: fix-permissions
        image: busybox
        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
        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
  volumeClaimTemplates:
  - metadata:
      name: data
      labels:
        app: elasticsearch
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 3Gi

字符串

9wbgstp7

9wbgstp72#

最简单的方法是使用图像的基本开始不要尝试使用特殊的exec cmd。当你的pod正常启动后,使用cat file_location_on_pod>>file_location_on_host和kubectl exec,在你的Host上有你需要修改的文件。
最后,您只需编辑它并将其用作Pod的挂载卷。

相关问题