简体   繁体   English

Kubernetes Pod 无法使用服务与另一个 Pod 通信

[英]Kubernetes Pod unable to communicate with another Pod using a Service

I have 2 Pods with 1 container each.我有 2 个 Pod,每个都有 1 个容器。 The container names are:容器名称是:

  1. mc1 mc1
  2. mc2 mc2

mc1 container hosts an asp.net core razor pages app while mc2 hosts a web api app. mc1容器托管一个 asp.net 核心 razor 页面应用程序,而mc2托管一个 web Z8A35DA52ED1205721A 应用程序。 Now mc1 has to communicate with mc2 ie razor page app has to call web api app.现在 mc1 必须与 mc2 通信,即 razor 页面应用程序必须调用 web api 应用程序。

I have tried to explain it in the below image:我试图在下图中解释它: 在此处输入图像描述

I created 2 deployments for these 2 pods:我为这 2 个 pod 创建了 2 个部署:

name: dep1
labels:
    app: mc-app1
spec:
  replicas: 1
  selector: 
    matchLabels:
      app: mc-app1
  template:
    metadata: 
      labels:
        app: mc-app1
    spec:
      containers:
        - name: mc1
          image: multiapp
          imagePullPolicy: Never
          ports:
            - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dep2
  labels:
    app: mc-app2
spec:
  replicas: 1
  selector: 
    matchLabels:
      app: mc-app2
  template:
    metadata: 
      labels:
        app: mc-app2
    spec:
      containers:
        - name: mc2
          image: multiapi
          imagePullPolicy: Never
          ports:
            - containerPort: 80

I also created a service for the POD containing the mc2 container (ie web api app).我还为包含 mc2 容器的 POD 创建了一个服务(即 web api 应用程序)。

apiVersion: v1
kind: Service
metadata:
  name: multi-container-service-2
spec:
  type: NodePort
  selector:
    app: mc-app2
  ports:
    - port: 8080
      targetPort: 80

The deployments and services are successfully applied to the k8s cluster.部署和服务已成功应用到 k8s 集群。

Next, I am entering the container "mc1" and trying to curl the service called multi-container-service-2 but this is not working.接下来,我进入容器“mc1”并尝试 curl 名为multi-container-service-2 的服务,但这不起作用。

I am getting error:我收到错误:

curl: (7) Failed to connect to multi-container-service-2 port 80: Connection refused curl:(7)无法连接到multi-container-service-2端口80:连接被拒绝

In the below image I am entering the shell of the container mc1 with the command:在下图中,我使用以下命令输入容器mc1的 shell:

kubectl exec -it dep1-5c78b8c889-tjzzr -c mc1 -- /bin/bash

在此处输入图像描述

In the next image I am doing the curl which is giving error:在下一张图片中,我正在执行 curl 错误:

在此处输入图像描述

Note that I have already installed curl using the 2 commands given below:请注意,我已经使用下面给出的 2 个命令安装了 curl:

apt-get update
apt-get install curl

Why can't the app in mc2 container be called using the service?为什么不能使用服务调用mc2容器中的应用程序? My Operating system is windows 10.我的操作系统是 windows 10。

I am taking the help of these 2 tutorials:我正在接受这两个教程的帮助:

  1. Build ASP.NET Core applications deployed as Linux containers into an AKS/Kubernetes orchestrator 将部署为 Linux 容器的 ASP.NET 核心应用程序构建到 AKS/Kubernetes 编排器中
  2. Communicate Between Containers in the Same Pod Using a Shared Volume 使用共享卷在同一 Pod 中的容器之间进行通信

You have set to service port to 8080 , but you are calling the service on port 80 (which is the container's port).您已将服务端口设置为8080 ,但您正在端口 80(这是容器的端口)上调用服务。

This should work:这应该有效:

curl http://multi-container-service-2:8080

As stated in the official kubernetes documentation:官方 kubernetes 文档中所述:

Kubernetes creates DNS records for services and pods. Kubernetes 为服务和 Pod 创建 DNS 记录。 You can contact services with consistent DNS names instead of IP addresses.您可以使用一致的 DNS 名称而不是 IP 地址联系服务。

In order to communicate Pod-to-Pod through a service in your cluster you have to use the following syntax:为了通过集群中的服务进行 Pod 到 Pod 的通信,您必须使用以下语法:

{service_name}.{namespace}.svc.cluster.local

So in your case, with curl it would be:因此,在您的情况下,使用 curl 将是:

curl multi-container-service-2.default.svc.cluster.local

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM