0
0
Kubernetesdevops~5 mins

Pod-to-Pod communication in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Pods in Kubernetes are groups of containers that share network and storage. Pod-to-Pod communication means letting these pods talk to each other inside the cluster. This helps apps work together smoothly.
When you want a frontend pod to get data from a backend pod inside the cluster
When multiple pods need to share information or coordinate tasks
When you want to test if two pods can connect before deploying a full app
When you want to expose a pod's service to other pods without external access
When you want to debug network issues between pods
Config File - pod-communication.yaml
pod-communication.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-a
  labels:
    app: pod-a
spec:
  containers:
  - name: container-a
    image: busybox
    command: ["sleep", "3600"]
    ports:
    - containerPort: 80
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-b
  labels:
    app: pod-b
spec:
  containers:
  - name: container-b
    image: busybox
    command: ["sleep", "3600"]
    ports:
    - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: pod-b-service
spec:
  selector:
    app: pod-b
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: ClusterIP

This file creates two pods named pod-a and pod-b, each running a simple container that sleeps to stay alive. It also creates a Service named pod-b-service that selects pod-b by its label. This service gives pod-b a stable network name and IP inside the cluster, so pod-a can reach it easily.

Commands
This command creates the two pods and the service in the Kubernetes cluster. It sets up the environment for pod-to-pod communication.
Terminal
kubectl apply -f pod-communication.yaml
Expected OutputExpected
pod/pod-a created pod/pod-b created service/pod-b-service created
This command checks that both pods are running and ready to communicate.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE pod-a 1/1 Running 0 10s pod-b 1/1 Running 0 10s
This command runs a simple HTTP request from pod-a to pod-b using the service name. It tests if pod-a can reach pod-b inside the cluster.
Terminal
kubectl exec pod-a -- wget -qO- http://pod-b-service
Expected OutputExpected
No output (command runs silently)
-- - Separates kubectl exec options from the command to run inside the pod
This command shows details about the service that connects to pod-b, including its cluster IP and ports.
Terminal
kubectl describe service pod-b-service
Expected OutputExpected
Name: pod-b-service Namespace: default Labels: <none> Annotations: <none> Selector: app=pod-b Type: ClusterIP IP: 10.96.0.1 Port: 80/TCP TargetPort: 80/TCP Endpoints: 10.244.1.5:80 Session Affinity: None Events: <none>
Key Concept

Pods can communicate inside a Kubernetes cluster using Services that provide stable network names and IPs.

Common Mistakes
Trying to connect to a pod directly by its IP address
Pod IPs can change when pods restart, so connections break.
Use a Service to give pods a stable network name and IP.
Not labeling pods correctly to match the Service selector
The Service won't find the pods to route traffic to, so communication fails.
Make sure pod labels match the Service selector exactly.
Running commands without the '--' flag in kubectl exec
kubectl treats the command as its own option, causing errors.
Always use '--' before the command to run inside the pod.
Summary
Create pods and a Service to enable stable network communication between pods.
Use kubectl exec with the '--' flag to run commands inside pods for testing connectivity.
Verify pods are running and the Service is correctly routing traffic.