Port mapping consists of binding or mapping a port on the host to an application port on the container. It is done through the run command where we publish the ports using the argument -p (publishes the container's port to the host).
To display the containers:
Now the application inside the container could be reached through the host_port and the host IP.
Shortcomings of containers:
If we are running a lot of containers on a host, we would soon run out of free ports on the host to bind to the growing number of containers.
Solution: the pods:
Pods are the smallest unit of kubernetes, they wrap around containers and they have a unique IP address that is reachable from all the other pods in the Kubernetes cluster. Pods are like a virtual machines inside the host node, with their own IP addresses, names and a range of ports to allocate to the applications running inside its containers. Pods usually have one container running inside them, but they could have more, for an example a container running a Web Server and another container running a log application for monitoring). After its creation, a pod gets a namespace and a virtual Ethernet interface. We can run multiple applications on a host inside different pods on the same port since the applications will be on different pods, they will have different IP addresses.
Pods and container engines:
We could use different container engines like Docker,LXC or Rkt without making any changes to Kubernetes because of the abstraction layer offered by the pod around the container. Kubernetes is not linked to any specific container engine.
Communication between multiple containers inside a Pod:
In case we have more than one container inside a pod, they can communicate using their localhost address (loopback address).
We use a YAML file (pod1.yaml) that contains all the information needed to create a pod.
The container port is the port the application is listening on.
We then can create the pod using the below command:
Containers inside the same Pod:
Containers inside the same pod share the same namespace and communicate through the local loopback(localhost) IP address or lo_IP in the diagram below.