本文介绍了如何使多个Pod在kubernetes上相互通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kubernetes的新手,我正尝试通过microk8s将应用程序部署到kubernetes.该应用程序包含python flask flask后端,Angular前端,redis和mysql数据库.我将映像部署在多个吊舱中,状态显示为正在运行",但吊舱彼此之间未通信.

I am new to Kubernetes and I'm trying to deploy an application to kubernetes via microk8s. The application contains python flask backend, angular frontend, redis and mysql database. I deployed the images in multiple pods and the status is showing "running" but the pods are not communicating with each other.

然后将应用程序完全泊坞化,并在docker级别运行.在部署到kubernetes之前,我的烧瓶主机是docker-compose.yaml中的0.0.0.0,而mysql主机是服务名",但是目前我用kubernetes yml文件的服务名替换了它.

Then app is completely dockerized and its functioning in the docker level.Before deploying into kubernetes my flask host was 0.0.0.0 and mysql host was "service name" in the docker-compose.yaml but currently I replaced it with service names of kubernetes yml file.

此外,在有角度的前端中,我已将要连接的URL更改为 http://localhost:5000 http://backend-service ,其中backend-service是backend-service.yml中提供的名称(dns).文件.但这也没有任何改变.有人可以告诉我如何使这些吊舱进行交流吗?

Also, in angular frontend I have changed the url to connect to backed as http://localhost:5000 to http://backend-service, where backend-service is the name(dns) given in the backend-service.yml file. But this also didn't make any change. Can someone tell me how can I make these pods communicate?

在未部署其余部分的情况下,我只能访问前端.

I am able to access only the frontend after deploying rest is not connected.

列出角度的后端服务和部署文件.

Listing down the service and deployment files of angular, backend.

 apiVersion: v1
 kind: Service
 metadata:
   name: angular-service
 spec:
   type: NodePort
   selector:
     name: angular
   ports:
     - protocol: TCP
       nodePort: 30042
       targetPort: 4200
       port: 4200


 apiVersion: v1
 kind: Service
 metadata:
   name: backend-service
 spec:
   type: ClusterIP
   selector:
     name: backend
   ports:
     - protocol: TCP
       targetPort: 5000
       port: 5000

提前谢谢!

(修改后的服务文件)

推荐答案

对于 Kubernetes 中不同微服务之间的内部通信,应使用类型的Service href ="https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#creating-a-service" rel ="nofollow noreferrer"> ClusterIP .实际上,它是默认类型,因此即使您未在Service yaml定义文件中指定它, Kubernetes 也会假设您要创建ClusterIP.它创建虚拟内部IP(可在您的Kubernetes群集中访问),并将群集组件(微服务)公开为单个入口点,即使它由许多Pod备份也是如此.

For internal communication between different microservices in Kubernetes you should use Service of a type ClusterIP. It is actually the default type so even if you don't specify it in your Service yaml definition file, Kubernetes assumes you want to create ClusterIP.It creates virtual internal IP (accessible within your Kubernetes cluster) and exposes your cluster component (microservice) as a single entry point even if it is backed up by many pods.

让我们假设您有一个前端应用程序,该应用程序需要与在3个不同的pod中运行的后端组件进行通信. ClusterIP服务提供单个入口点,并处理不同Pod之间的负载平衡,从而在它们之间平均分配请求.

Let's assume you have front-end app which needs to communicate with back-end component which is run in 3 different pods. ClusterIP service provides single entry point and handles load-balancing between different pods, distributing requests evenly among them.

您可以通过提供ClusterIP服务的IP地址和端口来访问您的应用程序组件.请注意,您可以为Service定义一个不同的端口(在Service定义中称为port),以监听应用程序使用的实际端口(在Service定义中称为targetPort). ).尽管可以使用其ClusterIP地址访问Service,但与在其内部公开的Pod通信的所有组件都应使用其DNS名称.如果所有应用程序组件都放在同一命名空间中,则只是您创建的Service名称.如果某些组件位于不同的命名空间中,则需要使用完全限定的域名,以便它们可以在命名空间之间进行通信.

You can access your ClusterIP service by providing its IP address and port that your application component is exposed on. Note that you may define a different port (referred to as port in Service definition) for the Service to listen on than the actual port used by your application (referred to as targetPort in your Service definition). Although it is possible to access the Service using its ClusterIP address, all components that communicate with pods internally exposed by it should use its DNS name. It is simply a Service name that you created if all application components are placed in the same namespace. If some components are in a different namespaces you need to use fully qualified domain name so they can communicate across the namespaces.

您的Service定义文件可能如下所示:

Your Service definition files may look like this:

apiVersion: v1
kind: Service
metadata:
  name: angular-service
spec:
  type: ClusterIP ### may be ommited as it is a default type
  selector:
    name: angular ### should match your labels defined for your angular pods
  ports:
  - protocol: TCP
    targetPort: 4200 ### port your angular app listens on
    port: 4200 ### port on which you want to expose it within your cluster

apiVersion: v1
kind: Service
metadata:
  name: backend-service
spec:
  type: ClusterIP ### may be ommited as it is a default type
  selector:
    name: backend ### should match your labels defined for your backend pods
  ports:
  - protocol: TCP
    targetPort: 5000 ### port your backend app listens on
    port: 5000 ### port on which you want to expose it within your cluster

您可以在官方 Kubernetes 中找到有关此主题的详细说明,文档.

You can find a detailed description of this topic in official Kubernetes documentation.

NodePort具有完全不同的功能.例如,可以使用它.将您的前端应用程序暴露在节点IP的特定端口上.请注意,如果您有由许多节点组成的Kubernetes集群,并且您的前端Pod位于不同的节点上,则要访问您的应用程序,您需要使用3个不同的IP地址.在这种情况下,您需要一个额外的负载平衡器.如果您使用某种云平台解决方案,并且想要将应用程序的前端部分暴露给外部世界,则服务类型 LoadBalancer 是可行的方法(而不是使用NodePort).

NodePort has totally different function. It may be used e.g. to expose your front-end app on a specific port on your node's IP. Note that if you have Kubernetes cluster consisting of many nodes and your front-end pods are placed on different nodes, in order to access your application you need to use 3 different IP addresses. In such case you need an additional load balancer. If you use some cloud platform solution and you want to expose front-end part of your application to external world, Service type LoadBalancer is the way to go (instead of using NodePort).

这篇关于如何使多个Pod在kubernetes上相互通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 11:29