【发布时间】: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进行通信跨度>