[英]How to create multiple containers in same pods which have separate deployment.yaml files?
tldr: in docker-compose, intercontainer communication is possible via localhost. tldr:在 docker-compose 中,可以通过本地主机进行容器间通信。 I want to do the same in k8s, however, I have separate deployment.yaml files for each component.
我想在 k8s 中做同样的事情,但是,每个组件都有单独的 deployment.yaml 文件。 How to link them?
如何链接它们?
I have a kube.netes helm package in which there are sub helm packages.我有一个 kube.netes helm package,其中有子 helm 包。 The folder structure is as follows::
文件夹结构如下:
A
├── Chart.yaml
├── values.yaml
├── charts
│ ├── component1
│ │ ├── Chart.yaml
│ │ ├── templates
│ │ │ ├── configmap.yaml
│ │ │ ├── deployment.yaml
│ │ │ ├── hpa.yaml
│ │ │ ├── ingress.yaml
│ │ │ ├── service.yaml
│ │ │ ├── serviceaccount.yaml
│ │ └── values.yaml
│ ├── component2
│ │ ├── Chart.yaml
│ │ ├── templates
│ │ │ ├── certs.yaml
│ │ │ ├── configmap.yaml
│ │ │ ├── pdb.yaml
│ │ │ ├── role.yaml
│ │ │ ├── statefulset.yaml
│ │ │ ├── pvc.yaml
│ │ │ └── svc.yaml
│ │ ├── values-production.yaml
│ │ └── values.yaml
In docker-compose, I was able to communicate between component1 and component2 via ports using localhost.在 docker-compose 中,我能够使用本地主机通过端口在 component1 和 component2 之间进行通信。
However, in this architecture, I have separate deployment.yaml files for those components.但是,在此架构中,我为这些组件提供了单独的 deployment.yaml 文件。 I know that if I keep them as containers in a single deployment.yaml file, I can communicate via localhost.
我知道如果我将它们作为容器保存在单个 deployment.yaml 文件中,我可以通过本地主机进行通信。
Question: How do I put these containers in same pod, provided that they are present in separate deployment.yaml files?问题:如果这些容器存在于单独的 deployment.yaml 文件中,我如何将它们放在同一个 pod 中?
That's not possible.那是不可能的。 Pods are the smallest deployable unit in kube.netes that consist of one or more containers.
Pod 是 kube.netes 中最小的可部署单元,由一个或多个容器组成。 All containers inside the pod share the same.network namespace (beside others).
pod 内的所有容器共享相同的 .network 命名空间(除了其他容器)。 The containers can only be reached via fqdn or ip. For each container outside a pod "localhost" means something completely different.
容器只能通过 fqdn 或 ip 访问。对于 pod 外的每个容器,“localhost”意味着完全不同的东西。 Similar to running docker compose on different hosts, they can not connect using localhost.
类似于在不同主机上运行 docker compose,它们无法使用 localhost 连接。
You can use the service's name to have a similar behaviour.您可以使用该服务的名称来实现类似的行为。 Instead of calling
http://localhost:8080
you can simple use http://component1:8080
to reach component1 from component2, supposing the service in component1/templates/service.yaml
is named component1 and both are in the same namespace.您可以简单地使用 http
http://localhost:8080
http://component1:8080
从 component2 到达 component1,假设component1/templates/service.yaml
中的服务被命名为 component1 并且两者都在同一个命名空间中。 Generally there is a dns record for every service with the schema <service>.<namespace>
, eg component1.default
for component1 running in the default namespace.通常,每个具有架构
<service>.<namespace>
的服务都有一个 dns 记录,例如component1.default
用于在默认命名空间中运行的 component1。 If component2 where in a different namespace you would use http://component1.default:8080
.如果 component2 在不同的命名空间中,您将使用
http://component1.default:8080
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.