我在kubernetes上运行了前端和后端-说前端和后端。它们每个都可以在localhost上单独访问。但是当我把这些服务放在豆荚里时。然后,这些Pod无法相互通信。我已经通过Loadbalancer公开了前端和后端。
就像这样,我可以访问http:// localhost:8081 / ping和http:// localhost:8083 / doWork,但是调用http:// localhost:8083 / doWork的http:// localhost:8081 / doMainWork连接被拒绝错误。没有容器,如果我只对它们进行泊坞处理,那就可以了。
frontend.yaml
apiVersion: v1
kind: Service
metadata:
name: frontend
spec:
selector:
app: frontend
ports:
- protocol: "TCP"
port: 8081
targetPort: 5000
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: frontend
imagePullPolicy: Never
image: frontend:latest
ports:
- name: http
containerPort: 5000
backend.yaml
apiVersion: v1
kind: Service
metadata:
name: backend
spec:
selector:
app: backend
ports:
- protocol: "TCP"
port: 8083
targetPort: 80
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend
spec:
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: backend
imagePullPolicy: Never
image: backend:latest
ports:
- name: http
containerPort: 80
C#中的HTTPClient
static HttpClient client = new HttpClient()
{
BaseAddress = new Uri( "http://host.docker.internal:8083/" ),
//BaseAddress = new Uri( "http://localhost:8083/" ),
Timeout = TimeSpan.FromSeconds( 5 )
};
如果我使用localhost或host.docker.internal
,错误仍然相同答案 0 :(得分:0)
根据k8s文档:
Kubernetes中的服务是一个REST对象,类似于Pod。像所有REST对象一样,您可以向API服务器
POST
进行服务定义以创建新实例。服务对象的名称必须为有效的DNS label name。例如,假设您有一组Pod,每个Pod都侦听TCP端口9376并带有标签
app=MyApp
:apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: MyApp ports: - protocol: TCP port: 80 targetPort: 9376
此规范会创建一个名为“ my-service”的新服务对象,该服务对象的目标是带有
app=MyApp
标签的任何Pod上的TCP端口9376。
您应该调用localhost
名称,而不是调用service
。