我创建了一个简单的 scala 应用程序,它使用 akkaHttp 将用户数据添加到 Elasticsearch 中,akkaHttp 在“0.0.0.0”上运行,端口为“8083”。
Http().newServerAt("0.0.0.0", 8080).bind(route)
虽然 Elasticserach 在“0.0.0.0”上运行并且有一个端口“9200”
val http = new HttpHost("0.0.0.0", 9200)
val esClient: RestClient = RestClient.builder(http).build();
之后我创建了下面给出的“部署”和“服务”:
部署.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: elastic-deployment
spec:
replicas: 1
selector:
matchLabels:
app: elastic-label
template:
metadata:
name: elastic-pod
labels:
app: elastic-label
spec:
containers:
- name: elastic-container
image: elasticdeployment:0.1.0-SNAPSHOT
ports:
- containerPort: 8083
env:
- name: ELASTICSEARCH_URL
value: "http://localhost:9200"
服务.yaml
apiVersion: v1
kind: Service
metadata:
name: elastic-service
spec:
type: LoadBalancer
selector:
app: elastic-label
ports:
- port: 8080
targetPort: 8083
不使用 k8 的部署我的应用程序工作正常,即我能够将用户数据添加到 ES 并从 ES 获取数据。但是通过添加k8的部署,并尝试使用Postman通过负载均衡器添加数据也得到了错误
发生错误:连接被拒绝
我哪里做错了?
我相信您需要将
targetPort
添加到您的服务定义中,因此它看起来如下所示:
apiVersion: v1
kind: Service
metadata:
name: elastic-service
spec:
type: LoadBalancer
selector:
app: elastic-label
ports:
- port: 8080
targetPort: 8083
默认情况下
service
会假定为port
分配的LB
与应用程序端口相同。
在您的情况下,LB
端口为 8080
,应用程序端口为 8083
,因此您需要明确定义它。