Skip to content
advanced Phase 8 · Containers & Orchestration

Kubernetes Networking

Configure Kubernetes networking with Services (ClusterIP, NodePort, LoadBalancer), Ingress controllers, and NetworkPolicies.

1h
0 problems
Topic Progress 0%

Kubernetes Service Types

Kubernetes Services provide stable networking for pods, which are ephemeral and receive dynamic IP addresses. A Service selects pods by labels and routes traffic to them.

ClusterIP (default) exposes the Service on an internal IP only accessible within the cluster. Use this for internal microservice communication:

apiVersion: v1
kind: Service
metadata:
  name: api-service
spec:
  selector:
    app: api-server
  ports:
  - port: 80
    targetPort: 8080

NodePort exposes the Service on a static port (30000-32767) on every node. External clients connect via <NodeIP>:<NodePort>. Useful for development or when you don't have a load balancer.

LoadBalancer provisions a cloud load balancer (ALB, NLB) that routes external traffic to the Service. This is the standard way to expose services to the internet on cloud providers:

apiVersion: v1
kind: Service
metadata:
  name: api-external
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: nlb
spec:
  type: LoadBalancer
  selector:
    app: api-server
  ports:
  - port: 443
    targetPort: 8080

ExternalName maps a Service to a DNS CNAME record, allowing pods to reference external services by a stable internal name. For example, mapping an external database to db.internal so application code doesn't change if the database moves.

Ingress Controllers and Resources

Ingress provides HTTP/HTTPS routing to Services based on hostnames and URL paths. An Ingress Controller (like NGINX, ALB Ingress, or Traefik) implements the routing logic.

An Ingress resource defines routing rules:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: "10m"
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - api.example.com
    secretName: api-tls-secret
  rules:
  - host: api.example.com
    http:
      paths:
      - path: /v1
        pathType: Prefix
        backend:
          service:
            name: api-v1
            port:
              number: 80
      - path: /v2
        pathType: Prefix
        backend:
          service:
            name: api-v2
            port:
              number: 80

TLS termination happens at the Ingress Controller. The TLS secret contains the certificate and key. Use cert-manager to automate Let's Encrypt certificate provisioning and renewal.

The AWS ALB Ingress Controller creates an Application Load Balancer for each Ingress resource. It supports path-based routing, host-based routing, authentication via Cognito, and WAF integration. For TCP/UDP workloads, use a Network Load Balancer with a MetalLB or NLB controller.

NetworkPolicies for Traffic Control

NetworkPolicies are the Kubernetes firewall. By default, all pods can communicate with all other pods. NetworkPolicies restrict this, enabling zero-trust networking within the cluster.

A basic NetworkPolicy that allows traffic only to pods with label role: api:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-allow
  namespace: production
spec:
  podSelector:
    matchLabels:
      role: api
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: frontend
    ports:
    - protocol: TCP
      port: 8080

This policy only allows pods labeled role: frontend to reach pods labeled role: api on port 8080. All other ingress traffic is blocked.

Restrict egress to prevent compromised pods from exfiltrating data:

spec:
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          name: database
    ports:
    - protocol: TCP
      port: 5432
  - to: []
    ports:
    - protocol: UDP
      port: 53

Always allow DNS egress (port 53) or your pods will fail to resolve service names. NetworkPolicies require a CNI plugin that supports them—Calico, Cilium, and Weave all provide this.

CNI Plugins and DNS Resolution

Container Network Interface (CNI) plugins provide pod networking. Each pod gets a unique IP address, and all pods can communicate directly without NAT.

Popular CNI plugins include:

  • Calico: Network policy enforcement with BGP routing, supports NetworkPolicy
  • Cilium: eBPF-based networking with advanced observability and security
  • Flannel: Simple overlay network, good for small clusters
  • AWS VPC CNI: Assigns VPC IP addresses directly to pods, enabling security group integration

The AWS VPC CNI is unique because pods get real VPC IPs instead of overlay IPs. This means security groups, NACLs, and VPC flow logs work at the pod level. The trade-off is IP address exhaustion in large clusters—use VPC CNI with prefix delegation to allocate /28 prefixes instead of individual IPs.

Kubernetes DNS (CoreDNS) provides automatic service discovery. Every Service gets a DNS entry:

# Fully qualified
api-service.production.svc.cluster.local

# Within same namespace
api-service

# Cross-namespace
database.database-namespace

CoreDNS resolves these names and supports custom DNS policies for forwarding to external DNS servers or stub domains. Pod DNS configuration can be customized to use specific nameservers or search domains.

Quiz

1. Which Kubernetes Service type provisions a cloud load balancer?

Question 1 options

2. Why should you always allow DNS egress (port 53) in NetworkPolicies?

Question 2 options

3. What is the advantage of AWS VPC CNI over overlay-based CNI plugins?

Question 3 options

Flashcards

Question

What is the difference between ClusterIP and LoadBalancer Services?

Answer

ClusterIP is internal-only, accessible within the cluster. LoadBalancer provisions a cloud load balancer for external access.

Question

What does an Ingress Controller do?

Answer

Implements HTTP/HTTPS routing rules (host, path) defined in Ingress resources, forwarding traffic to the appropriate backend Services. Often handles TLS termination.

Question

What is a CNI plugin?

Answer

Container Network Interface plugin that provides pod networking—assigning IPs, managing routes, and enabling pod-to-pod communication across nodes.

Question

What is the default DNS format for a Kubernetes Service?

Answer

<service-name>.<namespace>.svc.cluster.local — within the same namespace, just <service-name> works.

Revision Notes

Key Takeaways

  • 1. ClusterIP for internal services, LoadBalancer for external access, NodePort for development
  • 2. Ingress provides HTTP routing with TLS termination; choose NGINX, ALB, or Traefik controllers
  • 3. NetworkPolicies enforce zero-trust—always allow DNS egress to prevent service discovery failures
  • 4. AWS VPC CNI assigns real VPC IPs to pods, enabling native security group integration
  • 5. CoreDNS provides automatic service discovery via <service>.<namespace>.svc.cluster.local

Interview Tips

  • Explain the difference between ClusterIP, NodePort, and LoadBalancer with use cases
  • Describe how Ingress routing works with path-based and host-based rules
  • Explain why NetworkPolicies need to allow DNS traffic and how to structure them
  • Discuss the trade-offs between overlay CNIs (Calico) and AWS VPC CNI

Cheat Sheet

Service types: ClusterIP (internal), NodePort (static port on nodes), LoadBalancer (cloud LB), ExternalName (CNAME). Ingress: HTTP routing + TLS via controllers (NGINX, ALB). NetworkPolicies: default deny-all, explicitly allow ingress/egress by pod selector and port. CNI: Calico (policy+BGP), Cilium (eBPF), VPC CNI (real VPC IPs). DNS: ..svc.cluster.local.