【发布时间】:2020-08-24 15:24:13
【问题描述】:
我正在尝试使用 minikube 在本地更近地运行 kubernetes。这是我第一次尝试 Kubernetes。所以 我不熟悉它的所有方面。 我正在尝试部署一个连接到弹性搜索服务器的 Spring Boot 应用程序。
springboot 部署.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
labels:
app: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp1:latest
imagePullPolicy: Never
弹性搜索服务器 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
暴露弹性搜索服务如下
apiVersion: v1
kind: Service
metadata:
name: elasticsearch
labels:
service: elasticsearch
spec:
ports:
- name: client
port: 9200
protocol: TCP
targetPort: 9200
- name: nodes
port: 9300
protocol: TCP
targetPort: 9300
type: NodePort
selector:
run: elasticsearch
同样,我也暴露了 springboot app 的服务。 现在我想知道如何从 springboot 服务连接到弹性搜索服务。
当 springbbot 和弹性搜索在同一台机器上正常部署时(不在 kubernetes 中),我使用 as 连接
RestClient.builder(new HttpHost("localhost", 9200))
.build();
在 kubernetes 中从 springboot 连接到弹性搜索的最佳方式是什么?
将elasticsearch服务的ip保存在环境变量中,在springboot中使用还是使用elasticsearch服务的服务名?
请指教
【问题讨论】:
-
你能正确粘贴
Elastic search sever deployment.yaml吗? -
问题中提供的deployment.yaml就是那个。我没有别的了
-
那为什么你的“部署”是
kind: Service?为什么你的 elasticsearch 部署和 elasticsearch 服务一样? -
@HelloWorld 抱歉。我没有注意到它。更正了它。谢谢
-
所有 yaml 文件看起来都很好。尝试使用完整的域名:
elasticsearch.default.svc.cluster.local.另外,如果您收到Connection refused,请尝试找出您的 springboot 应用程序在哪里尝试连接。 (例如,将 HttpHost ip 地址打印到日志或运行 tcpdump 并查看目标 ip)。
标签: spring-boot elasticsearch kubernetes