简体   繁体   English

2 个容器在 Kubernetes pod 中使用相同的端口

[英]2 containers using the same port in Kubernetes pod

I have the same problem as the following: Dual nginx in one Kubernetes pod我有以下相同的问题: 一个 Kubernetes 吊舱中的双 nginx

In my Kubernetes Deployment template, I have 2 containers that are using the same port 80. I understand that containers within a Pod are actually under the same network namespace, which enables accessing another container in the Pod with localhost or 127.0.0.1 .在我的 Kubernetes Deployment模板中,我有 2 个使用相同端口 80 的容器。我知道Pod中的容器实际上位于同一个网络命名空间下,这使得可以使用localhost127.0.0.1访问Pod中的另一个容器。 It means containers can't use the same port.这意味着容器不能使用相同的端口。

It's very easy to achieve this with the help of docker run or docker-compose , by using 8001:80 for the first container and 8002:80 for the second container.借助docker rundocker-compose很容易实现这一点,第一个容器使用8001:80 ,第二个容器使用8002:80

Is there any similar or better solution to do this in Kubernetes Pod?在 Kubernetes Pod 中是否有类似或更好的解决方案? Without separating these 2 containers into different Pods.无需将这 2 个容器分成不同的 Pod。

I believe what you need to do is specify a different Container Port for each container in the pod.我相信您需要做的是为 pod 中的每个容器指定不同的容器端口 Kubernetes allows you specify the port each container exposes using this parameter in the pod definition file. Kubernetes 允许您在 pod 定义文件中使用此参数指定每个容器公开的端口。 You can then create services pointing to same pods but different ports.然后,您可以创建指向相同 pod 但不同端口的服务。

Basically I totally agree with @David's and @Patric's comments but I decided to add to it a few more things expanding it into an answer.基本上我完全同意@David@Patric 的评论,但我决定在其中添加更多内容,将其扩展为答案。

I have the same problem as the following: Dual nginx in one Kubernetes pod我有以下相同的问题: 一个 Kubernetes 吊舱中的双 nginx

And there is already a pretty good answer for that problem in a mentioned thread.并且在提到的线程中已经有一个很好的答案来解决这个问题。 From the technical point of view it provides ready solution to your particular use-case however it doesn't question the idea itself.从技术角度来看,它为您的特定用例提供了现成的解决方案,但它并不质疑这个想法本身。

It's very easy to achieve this with the help of docker run or docker-compose, by using 8001:80 for the first container and 8002:80 for the second container.在 docker run 或 docker-compose 的帮助下,第一个容器使用 8001:80,第二个容器使用 8002:80,很容易实现这一点。

It's also very easy to achieve in Kubernetes .Kubernetes中也很容易实现。 Simply put both containers in different Pods and you will not have to manipulate with nginx config to make it listen on a port different than 80 .只需将两个容器放在不同的 Pod 中,您就不必使用Pods配置来使其侦听不同于80的端口。 Note that those two docker containers that you mentioned don't share a single network namespace and that's why they can both listen on ports 80 which are mapped to different ports on host system ( 8001 and 8002 ).请注意,您提到的这两个 docker 容器不共享单个网络命名空间,这就是为什么它们都可以侦听映射到主机系统上不同端口( 80018002 )的端口80的原因。 This is not the case with Kubernetes Pods . Kubernetes Pods不是这种情况。 Read more about microservices architecture and especially how it is implemented on k8s and you'll notice that placing a few containers in a single Pod is really rare use case and definitely should not be applied in a case like yours.阅读更多关于微服务架构的信息,特别是它是如何在k8s上实现的,你会注意到在一个Pod中放置几个容器是非常罕见的用例,绝对不应该在像你这样的情况下应用。 There should be a good reason to put 2 or more containers in a single Pod .应该有充分的理由将 2 个或更多容器放在一个Pod中。 Usually the second container has some complimentary function to the main one.通常第二个容器与主容器有一些互补的 function。

There are 3 design patterns for multi-container Pods, commonly used in Kubernetes : sidecar, ambassador and adapter. 多容器 Pod 有 3 种设计模式,常用在Kubernetes:sidecar、ambassador 和 adapter。 Very often all of them are simply referred to as sidecar containers .很多时候,它们都被简单地称为边车容器

Note that 2 or more containers coupled together in a single Pod in all above mentioned use cases have totally different function .请注意,在上述所有用例中,在单个Pod中耦合在一起的 2 个或多个容器具有完全不同的 function Even if you put more than just one container in a single Pod (which is most common), in practice it is never a container of the same type (like two nginx servers listening on different ports in your case).即使您在单个Pod中放置了多个容器(这是最常见的),实际上它也绝不会是同一类型的容器(例如在您的情况下侦听不同端口的两个 nginx 服务器)。 They should be complimentary and there should be a good reason why they are put together, why they should start and shut down at the same time and share same network namespace.它们应该是互补的,并且应该有充分的理由将它们放在一起,为什么它们应该同时启动和关闭并共享相同的网络命名空间。 Sidecar container with a monitoring agent running in it has complimentary function to the main container which can be eg nginx webserver. Sidecar 容器中运行着一个监控代理,它对主容器有免费的 function,例如 nginx 网络服务器。 You can read more about container design patterns in general in this article.您可以在本文中阅读有关容器设计模式的更多信息。

I don't have a very firm use case, because I'm still very new to Kubernetes and the concept of a cluster.我没有一个非常确定的用例,因为我对 Kubernetes 和集群的概念还很陌生。

So definitely don't go this way if you don't have particular reason for such architecture.因此,如果您对这种架构没有特别的理由,那么绝对不要以这种方式使用 go。

My initial planning of the cluster is putting all my containers of the system into a pod.我对集群的初步规划是将系统的所有容器放入一个 pod。 So that I can replicate this pod as many as I want.这样我就可以根据需要复制这个 pod。

You don't need a single Pod to replicate it.您不需要单个Pod来复制它。 You can have in your cluster a lot of replicaSets (usually managed by Deployments ), each of them taking care of running declared number of replicas of a Pod of a certain kind.您的集群中可以有很多replicaSets (通常由Deployments管理),每个副本都负责运行某种Pod的声明数量的副本。

But according to all the feedback that I have now, it seems like I going in the wrong direction.但根据我现在得到的所有反馈,似乎我走错了方向。

Yes, this is definitely wrong direction, but it was actually already said.是的,这绝对是错误的方向,但实际上已经说过了。 I'd like only to highlight why namely this direction is wrong.我只想强调为什么这个方向是错误的。 Such approach is totally against the idea of microservices architecture and this is what Kubernetes is designed for.这种方法完全违背了微服务架构的理念,而这正是Kubernetes的设计目的。 Putting all your infrastructure in a single huge Pod and binding all your containers tightly together makes no sense.将所有基础设施放在一个巨大的Pod中并将所有容器紧密绑定在一起是没有意义的。 Remember that a Pod is the smallest deployable unit in Kubernetes and when one of its containers crashes, the whole Pod crashes.请记住, PodKubernetes中最小的可部署单元,当其中一个容器崩溃时,整个Pod都会崩溃。 There is no way you can manually restart just one container in a Pod .您无法手动重启Pod中的一个容器。

I'll review my structure and try with the suggests you all provided.我将审查我的结构并尝试使用你们提供的建议。 Thank you, everyone!谢谢大家! =) =)

This is a good idea:)这是一个好主意:)

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

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