Deep Dive into Networking & Service Discovery
Hands-On Lab Guide
In this lab, we'll deploy a full-stack MERN application to Kubernetes. The application consists of three microservices:
First, verify you can access the Kubernetes cluster:
kubectl get nodes
NAME STATUS ROLES AGE VERSION ip-10-1-1-10 Ready control-plane 1d v1.28.0 ip-10-1-1-20 Ready worker 1d v1.28.0
kubectl get pods -A
Clone the demo application repository:
git clone git@github.com:sasidatta/docker_demo.git
cd docker_demo/k8s
ssh-keygen and add it to your GitHub account.
Namespaces provide a virtual cluster within a physical cluster. They're useful for:
kubectl create namespace <your-name>
kubectl config set-context --current --namespace=<your-name>
--namespace with every command!
kubectl get namespaces
Let's get our application running so we can explore Pods, Services, and Networking.
kubectl apply -f frontend.yaml
kubectl apply -f backend.yaml
kubectl scale deployment backend --replicas=2
NAME READY STATUS RESTARTS AGE backend-6b5f7b8889-abc12 1/1 Running 0 20s backend-6b5f7b8889-def34 1/1 Running 0 10s frontend-7c9d4s8881-xyz78 1/1 Running 0 30s
A Pod is the smallest deployable unit in Kubernetes. Key concepts:
kubectl apply -f busybox.yaml
kubectl run nginx --image=nginx:latest --restart=Never
kubectl get pod busybox-pod -o yaml | grep -i restart
restartPolicy: Always restartCount: 1
Always - Always restart the container (default)OnFailure - Restart only if container exits with errorNever - Never restart the containerkubectl get pods -o wide
NAME READY STATUS IP NODE frontend-abc 1/1 Running 10.244.1.30 ip-10-1-1-20
kubectl delete pod busybox-pod nginx
| Feature | Pod | Deployment |
|---|---|---|
| Self-healing | ❌ No | ✅ Yes |
| Scaling | ❌ Manual | ✅ Automatic |
| Rolling updates | ❌ No | ✅ Yes |
| Use case | Testing, debugging | Production workloads |
Pods are ephemeral and their IPs change. Services provide:
Purpose: Internal cluster communication
Access: Only within cluster
Use case: Backend ↔ Database
Purpose: External access via node IP
Access: <NodeIP>:<NodePort>
Use case: Exposing frontend to users
Purpose: Cloud load balancer integration
Access: External IP from cloud provider
Use case: Production external access
kubectl apply -f backend-service.yaml
kubectl apply -f frontend-service.yaml
kubectl get services -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE backend ClusterIP 10.103.45.67 <none> 5000/TCP 2m frontend NodePort 10.107.12.34 <none> 80:30678/TCP 2m
http://<node-ip>:30678Example:
http://44.204.96.183:30678
All pods can communicate with all other pods on any other node without NAT.
Services provide stable IPs and DNS names for sets of pods.
Let's verify that pods can talk to each other directly using their IPs, bypassing services.
First, find the IP address of your backend or frontend pod.
kubectl get pods -o wide
NAME READY IP NODE backend 1/1 10.244.1.45 ip-10-1-1-20
Create a temporary busybox pod to run network tests.
kubectl run debug --image=busybox --restart=Never -it -- sh
Once inside the debug shell, use nc (netcat) to check if the port is open.
# Syntax: nc -zv <POD_IP> <PORT>
nc -zv 10.244.1.45 5000
10.244.1.45 (10.244.1.45:5000) open
Fetch the HTML/JSON response from the application manually.
# Syntax: wget -qO- <POD_IP>:<PORT>
wget -qO- 10.244.1.45:5000
-q: Quiet (no loading bar)-O-: Output to standard out (console)Network Policies control traffic flow between pods. By default, all traffic is allowed.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-backend-only
spec:
podSelector:
matchLabels:
app: database
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: backend
Ingress manages external access to the services in a cluster, typically HTTP/HTTPS.
kubectl apply -f ingress.yaml
k8slab.local to the
Ingress Controller IP given by:
kubectl get ingress
A DaemonSet ensures that all (or some) Nodes run a copy of a Pod.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: kube-system
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
containers:
- name: fluentd-elasticsearch
image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
kubectl get daemonsets -n kube-system
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE fluentd-elasticsearch 2 2 2 2 2
kubectl logs <pod-name>
kubectl describe pod <pod-name>
kubectl exec <pod-name> -- /bin/sh
kubectl exec -it <pod-name> -- /bin/sh
/bin/bash, use
/bin/sh instead!
On worker nodes, use crictl instead of docker:
crictl ps
crictl stop <container-id>
Cause: Cannot pull container image
Solution: Check image name, registry access, and credentials
Cause: Container keeps crashing
Solution: Check logs with kubectl logs
Cause: Cannot schedule pod (resources, node selector, etc.)
Solution: Use kubectl describe pod to see events
By default, all pods can communicate with each other. Use Network Policies to restrict traffic: