'How to create multiple containers in same pods which have separate deployment.yaml files?

tldr: in docker-compose, intercontainer communication is possible via localhost. I want to do the same in k8s, however, I have separate deployment.yaml files for each component. How to link them ?

I have a kubernetes helm package in which there are sub helm packages. 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.

However, in this architecture, I have separate deployment.yaml files for those components. I know that if I keep them as containers in a single deployment.yaml file, I can communicate via localhost.

Question: How do I put these containers in same pod, provided that they are present in separate deployment.yaml files ?



Solution 1:[1]

That's not possible. Pods are the smallest deployable unit in kubernetes that consist of one or more containers. All containers inside the pod share the same network namespace (beside others). The containers can only be reached via fqdn or ip. For each container outside a pod "localhost" means something completely different. Similar to running docker compose on different hosts, they can not connect using 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. Generally there is a dns record for every service with the schema <service>.<namespace>, e.g. component1.default for component1 running in the default namespace. If component2 where in a different namespace you would use http://component1.default:8080.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris