【问题标题】:SpringBoot could not access ElasticSearch on kubernetesSpringBoot 无法访问 kubernetes 上的 ElasticSearch
【发布时间】:2019-10-27 09:15:15
【问题描述】:

Spring Boot 访问 ElasticSearch 关于 kubernetes 的问题

如果 ElasticSearch 安装在本地,那么 Spring Boot Application 可以通过 spring.data.elasticsearch.cluster-nodes=localhost:9300 访问它

但是当它们都安装在 K8s 上时,SpringBoot 无法访问它了

spring boot的部署yaml如下

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springbootelastic
spec:
  selector:
    matchLabels:
      run: springbootelastic
  replicas: 1
  template:
    metadata:
      labels:
        run: springbootelastic
    spec:
      containers:
        - name: springbootelastic
          image: wangyan100/springbootelastic:latest
          imagePullPolicy: Always
          env:
            - name: CLUSTER_NODES
              value: elasticsearch:9300
            - name: CLUSTER_HOST
              value: elasticsearch
      #imagePullSecrets:
      #  - name: regcred


您可以在以下位置找到所有 yaml 文件和代码 https://github.com/wangyan100/springbootexamples/tree/master/spring-boot-elasticsearch/src

我在 spring boot 的 pod 日志中得到的异常如下所示

:添加传输节点:10.108.175.123:9300 不是集群集群 [elasticsearch] 的一部分,忽略...

:无法加载弹性搜索节点:org.elasticsearch.client.transport.NoNodeAvailableException:配置的节点都不可用:[{#transport#-1}{RwIDGzHOQFqU1Lrl0yULkQ}{elasticsearch}{10.108.175.123:9300}]

elasticsearch 在 pod 上运行,我可以通过 http://192.168.99.113:31183 浏览它(31183 是 nodeport)

{
  "name" : "c97xU38",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "3FQq0XXeQjuzDRqXO2wY6w",
  "version" : {
    "number" : "6.4.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "fe40335",
    "build_date" : "2018-10-30T23:17:19.084789Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

【问题讨论】:

  • 您似乎没有在代码 CLUSTER_NODES 的任何地方访问此环境变量? localhost 可以工作,因为这就是你的 props 文件中的内容。
  • 你能发布elasticsearch:9200 的输出吗,你应该看到类似这样的内容 { "name": "5M3kvCf", "cluster_name": "docker-cluster", "cluster_uuid": "tbbXSaIIQhearMZckxpEQQ" ,“版本”:{“编号”:“6.2.4”,“build_hash”:“ccec39f”,“build_date”:“2018-04-12T20:37:28.497551Z”,“build_snapshot”:假,“lucene_version” :“7.2.1”,“minimum_wire_compatibility_version”:“5.6.0”,“minimum_index_compatibility_version”:“5.0.0”},“标语”:“你知道,搜索”}
  • 使用yaml文件,CLUSTER_NODES的值为elasticsearch:9300 elasticsearch是elasticsearch在k8s pod上运行的servicename,在k8s上,pod之间可以使用service:port而不是ip:port进行通信跨度>

标签: spring-boot kubernetes


【解决方案1】:

问题解决了。我写了at this link

在 Kubernetes 上部署 SpringBootApplication 和 ElasticSearch

本演示将向您展示如何在 Kubernetes 上部署 SpringBootApplication 和 ElasticSearch。

演示将使用来自this link 的 SpringBootApplication。

构建 docker 镜像

  • gradle clean build

  • docker build -t wangyan100/springbootelastic .

将 docker 镜像(wangyan100/springbootelastic)推送到 docker hub

  • 在 Docker Hub 网站上创建 wangyan100/springbootelastic 作为存储库。当然,如果你没有,你需要在 Docker Hub 网站上注册一个帐户。

  • docker 登录

  • 输入 docker hub 的用户名和密码。

  • docker 标签 wangyan100/springbootelastic:latest

  • docker push wangyan100/springbootelastic:latest

编写部署和服务 yaml 文件

  • 对于 SpringBootApplication,您可以找到如下所示的示例。

  • 对于elasticsearch,您可以找到如下所示的示例。

在本地机器上安装 Kubernetes

  • 关注 the link 在您的本地 PC 上安装 Kubernetes。

在 Kubernetes 上部署应用程序

  • minikube start --memory 4096(elasticsearch 需要 4GB 内存)。

  • 部署弹性搜索。

    cd spring-boot-elasticsearch/src/elastick8s
    kubectl apply -f . 
    
  • 等到 elasticsearch 启动并运行。

    kubectl get pods
    NAME                                 READY   STATUS    RESTARTS   
    elasticsearch-5b74bbdd86-wgw2n       1/1     Running   0        
    
  • 部署 SpringBootApplication。

    cd spring-boot-elasticsearch/src/springbootk8s
    kubectl apply -f . 
    
  • 获取 springboot 和 elasticsearch 的暴露 URL。

    minikube service springbootelastic --url
    http://192.168.99.113:31742
    
    minikube service elasticsearch --url
    http://192.168.99.113:30765
    http://192.168.99.113:32663
    
  • 使用邮递员向SpringBootApplication发送请求,它会在elasticsearch创建一个入口。

  • 检查结果,它有效。

【讨论】:

    【解决方案2】:

    您能否验证http://elasticsearch:9200 的输出,您应该会看到如下内容:

    {
    
        "name": "5M3kvCf",
        "cluster_name": "docker-cluster",
        "cluster_uuid": "tbbXSaIIQhearMZckxpEQQ",
        "version": {
            "number": "6.2.4",
            "build_hash": "ccec39f",
            "build_date": "2018-04-12T20:37:28.497551Z",
            "build_snapshot": false,
            "lucene_version": "7.2.1",
            "minimum_wire_compatibility_version": "5.6.0",
            "minimum_index_compatibility_version": "5.0.0"
        },
        "tagline": "You Know, for Search"
    }
    

    集群名称应与:

    spring.data.elasticsearch.cluster-name
    

    在你的 spring 配置中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2020-07-06
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多