Pod and Network
Normally, we want for one container to access the program running on another container. Two scenarios are possible: containers run on the same pod or containers run on different pods.
Containers on the same Pod
flowchart
subgraph pod[Pod]
flask
postgres
end
subgraph network[Network]
pod
end
subgraph host[Host]
browser
network
end
browser[Web browser] --> flask[Flask];
flask .-> postgres[PostgreSQL];
classDef classPod fill:lightblue,stroke:blue
class pod classPod;
classDef classNetwork stroke:black,stroke-dasharray: 5 5
class network classNetwork;
Diagram of containers running on the same pod.
Containers in the same pod share the IP addresses, MAC addresses and port mappings. This allows the container to be reached by another container using localhost or the name of the container.
Example
Given the following Kubernetes manifest file
apiVersion: v1
kind: ConfigMap
metadata:
name: client-entrypoint
data:
entrypoint.sh: |
valkey-cli -h server set counter 0
while [ -z "" ]
do
valkey-cli -h server incr counter
sleep 5s
done
---
apiVersion: v1
kind: Pod
metadata:
labels:
app: valkey
name: valkey0
spec:
containers:
- name: server
image: docker.io/valkey/valkey:8.0.2-alpine
- name: client
image: docker.io/valkey/valkey:8.0.2-alpine
command:
- /bin/sh
- /opt/entrypoint.sh
volumeMounts:
- mountPath: /opt
name: client-entrypoint-volume
volumes:
- configMap:
name: client-entrypoint
items:
- key: "entrypoint.sh"
path: "entrypoint.sh"
name: client-entrypoint-volume
running
podman kube play play.yml
creates a pod named valkey0 with a container named server running the Valkey server and another container named client running a small Bash script that increases a counter every 5 seconds.
Containers on different pods
flowchart
subgraph pod1[Pod 1]
flask
end
subgraph pod2[Pod 2]
postgres
end
subgraph network[Network]
pod1
pod2
end
subgraph host[Host]
browser
network
pod1
pod2
end
browser[Web browser] --> flask[Flask];
flask .-> postgres[PostgreSQL];
classDef classPod fill:lightblue,stroke:blue
class pod1,pod2 classPod;
classDef classNetwork stroke:black,stroke-dasharray: 5 5
class network classNetwork;
Diagram of containers running on different pods.
The container can be reached by another container from a different pod if both pods are part of the same network. This allows the container to be reached by another container using the name of the pod.
Example
Important
When no network option is specified and host network mode is not configured in the YAML file, a new network stack is created and pods are attached to it making possible pod to pod communication.
First, create a new network by running
podman network valkey1
Given the following Kubernetes manifest file
apiVersion: v1
kind: ConfigMap
metadata:
name: client-entrypoint
data:
entrypoint.sh: |
valkey-cli -h valkey1-server set counter 0
while [ -z "" ]
do
valkey-cli -h valkey1-server incr counter
sleep 5s
done
---
apiVersion: v1
kind: Pod
metadata:
labels:
app: valkey
name: valkey1-server
spec:
containers:
- name: server
image: docker.io/valkey/valkey:8.0.2-alpine
---
apiVersion: v1
kind: Pod
metadata:
labels:
app: valkey
name: valkey1-client
spec:
containers:
- name: client
image: docker.io/valkey/valkey:8.0.2-alpine
command:
- /bin/sh
- /opt/entrypoint.sh
volumeMounts:
- mountPath: /opt
name: client-entrypoint-volume
volumes:
- configMap:
name: client-entrypoint
items:
- key: "entrypoint.sh"
path: "entrypoint.sh"
name: client-entrypoint-volume
running
podman kube play --net valkey1 play.yml
creates a pod named valkey1-server connected to the network valkey1 with a container named server running the Valkey server and another pod named valkey1-client connected to the network valkey1 with a container named client running a small Bash script that increases a counter every 5 seconds.