通过在kubernetes上运行的rest api调用连接后端和前端
我有一个两层的应用程序。 前端通过简单的http rest调用调用webapi层 下面是我的docker编写代码,应用程序可以正常工作通过在kubernetes上运行的rest api调用连接后端和前端,kubernetes,alibaba-cloud,Kubernetes,Alibaba Cloud,我有一个两层的应用程序。 前端通过简单的http rest调用调用webapi层 下面是我的docker编写代码,应用程序可以正常工作 version: '3' services: webfrontend: image: webfrontend build: ./nodeexpress-alibaba-ci-tutorial ports: - "3000:3000" networks: - my-shared-network
version: '3'
services:
webfrontend:
image: webfrontend
build: ./nodeexpress-alibaba-ci-tutorial
ports:
- "3000:3000"
networks:
- my-shared-network
mywebapi:
image: mywebapi
build: ./dotnetcorewebapi-alibaba-ci-tutorial
ports:
- "5000:5000"
networks:
- my-shared-network
networks:
my-shared-network: {}
现在我正试着让这个在库伯内特斯身上起作用。
我为webfrontend创建了两个部署和两个服务loadbalancer,为mywebapi创建了clusterip
但是,部署之后,我发现来自mywebapi的数据没有到达前端。我可以通过负载均衡器公共ip在浏览器上查看前端
Mywebapi部署yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: '3'
creationTimestamp: '2019-09-28T13:31:32Z'
generation: 3
labels:
app: mywebapi
tier: backend
name: mywebapi
namespace: default
resourceVersion: '1047268388'
selfLink: /apis/apps/v1beta2/namespaces/default/deployments/mywebapi
uid: 493ab5e0-e1f4-11e9-9a64-d63fe9981162
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: mywebapi
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
annotations:
aliyun.kubernetes.io/deploy-timestamp: '2019-09-28T14:36:01Z'
labels:
app: mywebapi
spec:
containers:
- image: >-
registry-intl-vpc.ap-southeast-1.aliyuncs.com/devopsci-t/mywebapi:1.0
imagePullPolicy: Always
name: mywebapi
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 2
conditions:
- lastTransitionTime: '2019-09-28T14:51:18Z'
lastUpdateTime: '2019-09-28T14:51:18Z'
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: 'True'
type: Available
- lastTransitionTime: '2019-09-28T14:49:55Z'
lastUpdateTime: '2019-09-28T14:51:19Z'
message: ReplicaSet "mywebapi-84cf98fb4f" has successfully progressed.
reason: NewReplicaSetAvailable
status: 'True'
type: Progressing
observedGeneration: 3
readyReplicas: 2
replicas: 2
updatedReplicas: 2
Mywebapi服务yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: '2019-09-28T13:31:33Z'
name: mywebapi-svc
namespace: default
resourceVersion: '1047557879'
selfLink: /api/v1/namespaces/default/services/mywebapi-svc
uid: 49e21207-e1f4-11e9-9a64-d63fe9981162
spec:
clusterIP: None
ports:
- name: mywebapiport
port: 5000
protocol: TCP
targetPort: 5000
selector:
app: mywebapi
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
我甚至尝试将http rest调用url更新为,但仍然不起作用。
在webfrontend pod日志中,我发现了以下错误
Got error: getaddrinfo ENOTFOUND mywebapi-svc mywebapi-svc:5000
衷心感谢所有的帮助
谢谢
"
更新
将mywebapi svc更改为禁用headless。当前YAML如下所示。问题还是一样
apiVersion: v1
kind: Service
metadata:
creationTimestamp: '2019-09-29T15:21:44Z'
name: mywebapi-svc
namespace: default
resourceVersion: '667545270'
selfLink: /api/v1/namespaces/default/services/mywebapi-svc
uid: d84503ee-e2cc-11e9-93ec-a65f0b53b1fa
spec:
clusterIP: 172.19.0.74
ports:
- name: mywebapiport
port: 5000
protocol: TCP
targetPort: 5000
selector:
app: mywebapi-default
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
那是因为你在使用 我想你不需要为这份工作提供无头服务。因为如果标签选择器有多个pod匹配,DNS服务器将不会返回单个IP地址。从您的服务中删除
spec.ClusterIP
字段
apiVersion: v1
kind: Service
metadata:
name: mywebapi-svc
namespace: default
spec:
# clusterIP: None <-- remove
ports:
- name: mywebapiport
port: 5000
protocol: TCP
targetPort: 5000
selector:
app: mywebapi
type: ClusterIP
apiVersion:v1
种类:服务
元数据:
名称:mywebapi svc
名称空间:默认值
规格:
#clusterIP:没有,我不是从集群外打电话。webfrontend和mywebapi是同一集群的一部分。@Arnab我编辑了我的答案,请尝试这样做。这可能会解决你的问题。那么,这将是我的端点地址??你能告诉我任何说明这一点的文档吗,TXV同一文档中有“假设Kubernetes命名空间栏中有一个名为foo的服务。命名空间栏中运行的Pod只需对foo进行DNS查询就可以查找该服务。”你还有问题吗?没有你的评论,是的,我有