【问题标题】:Mount local directory having Elasticsearch indices to Minikube将具有 Elasticsearch 索引的本地目录挂载到 Minikube
【发布时间】:2020-05-17 22:13:58
【问题描述】:

我正在尝试将我的 Springboot App with Elasticsearch 移动到 minikube 上运行。 但我想在 Kubernetes 上运行的弹性搜索中重用我使用 local elastic search 创建的现有索引。

我可以看到一些关于 Persistent Volume 的文档,但我找不到任何关于重用本地目录上现有索引的信息。

有人可以建议一些有关如何在 Kubernetes 上挂载包含 Elasticsearch 索引的现有本地目录的信息。

我正在尝试使用 minikube 在本地运行 Kubernetes 集群。这是我第一次尝试 Kubernetes。因此,我并不熟悉它的各个方面。我正在尝试部署一个连接到 Elasticsearch 服务器的 Spring Boot 应用程序。

Elasticsearch 服务器 - deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: elasticsearch
    spec:
      selector:
        matchLabels:
          run: elasticsearch
      replicas: 1
      template:
        metadata:
          labels:
            run: elasticsearch
        spec:
          containers:
            - image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
              name: elasticsearch
              imagePullPolicy: IfNotPresent
              env:
                - name: discovery.type
                  value: single-node
                - name: cluster.name
                  value: elasticsearch
              ports:
              - containerPort: 9300
                name: nodes
              - containerPort: 9200
                name: client

【问题讨论】:

  • 如果我的回答对您有帮助,请点赞。
  • @Harshit 我还没有检查它。我一测试就会接受你的回答。

标签: elasticsearch kubernetes minikube


【解决方案1】:

尝试为 Elasticsearch 创建一个StatefulSet,它将用作持久卷声明 (PVC)

PVC 参考链接:https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/

在有状态集配置中,您需要像这样提及volumeMounts

volumeMounts:
    - name: elasticsearch
      mountPath: /path/to/local_directory/local_indexes

这可以帮助您发现旧索引(本地创建)以及新索引!

要同时访问这些索引,我建议创建 Service,这也是 Elasticsearch 的一种负载均衡器。它将生成一个外部 IP 来访问 Kubernetes 集群中的 Elasticsearch 配置和索引。生成的外部IP可以通过curl访问。

【讨论】:

  • 我没有完全遵循您的解决方案。我尝试将 mountpath 与 hotspath 结合使用,但最后我没有完全成功。我在stackoverflow.com/questions/62224475/… 中发布了一个后续问题。如果你可以看看那将是有帮助的
  • 我最终使用了 StatefulSet,但实际的问题是我必须在 minikube 中显式挂载,即使部署可能有工作。但是当我根据您的建议阅读 StatefulSet 时,我发现它更合适。谢谢
  • 我赞成你的回答,因为它对我有帮助,但我不接受,因为问题的解决方案不同。感谢您的帮助
【解决方案2】:

我已经在这里发布了答案Minikube - Not able to get any result from elastic search to if it uses existing indices 为了完整起见,我也在此处粘贴该答案。挂载后,我没有重用现有索引,而是使用 kubernet 部署的弹性搜索 pod 创建了一个新索引。我想,现有的索引可能也可以工作,但我已经更改了我的代码,因为我正在尝试不同的策略来使其工作。

HostPath with minikube - Kubernetes 中的解决方案对我有用。 要将本地目录挂载到 minikube(版本 - v1.9.2)中的 pod,您必须将该本地目录挂载到 minikube,然后在 hostpath 中使用 minikube 挂载路径 (https://minikube.sigs.k8s.io/docs/handbook/mount/)。

 minikube mount ~/esData:/indexdata
?  Mounting host path /esData into VM as /indexdata ...
    ▪ Mount type:   <no value>
    ▪ User ID:      docker
    ▪ Group ID:     docker
    ▪ Version:      9p2000.L
    ▪ Message Size: 262144
    ▪ Permissions:  755 (-rwxr-xr-x)


       ▪ Options:      map[]
        ▪ Bind Address: 192.168.5.6:55230
    ?  Userspace file server: ufs starting
    ✅  Successfully mounted ~/esData to /indexdata

?  NOTE: This process must stay alive for the mount to be accessible ...

您必须在单独的终端中运行 minikube mount,因为它会启动一个进程并停留在那里直到您卸载。

现在我将其作为 Statefulset 进行,而不是像在原始问题中那样作为 Deployment 进行,但同样的解决方案也适用于 Deployment。

我在安装过程中遇到的另一个问题是弹性搜索服务器 pod 正在抛出 java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes 。然后我看到here 我必须使用 initContainers 在 /usr/share/elasticsearch/data/nodes 中设置完全权限。

请查看我的最终 yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: elasticsearch
spec:
  serviceName: "elasticsearch"
  replicas: 1
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      initContainers:
      - name: set-permissions
        image: registry.hub.docker.com/library/busybox:latest
        command: ['sh', '-c', 'mkdir -p /usr/share/elasticsearch/data && chown 1000:1000 /usr/share/elasticsearch/data' ]
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
        env:
        - name: discovery.type
          value: single-node
        ports:
        - containerPort: 9200
          name: client
        - containerPort: 9300
          name: nodes
        volumeMounts:
        - name: data
          mountPath: /usr/share/elasticsearch/data
      volumes:
      - name: data
        hostPath:
          path: /indexdata
---
apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
  labels:
    service: elasticsearch
spec:
  ports:
  - port: 9200
    name: client
  - port: 9300
    name: nodes
  type: NodePort  
  selector:
    app: elasticsearch

【讨论】:

    猜你喜欢
    • 2018-07-10
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2017-08-08
    • 2020-03-08
    • 2022-10-20
    • 2013-06-25
    相关资源
    最近更新 更多