Services & Ingress

💤0
Lv 10 XP
← 📦 Containers & Kubernetes · Kubernetes

Services & Ingress

Intermediate ⭐ 80 XP ⏱ 18 min #kubernetes#services#ingress#networking

Give workloads stable networking and route external HTTP traffic into the cluster.

📖Theory

Pods are ephemeral with changing IPs, so you never target them directly. A Service gives a stable virtual IP and DNS name in front of a set of Pods selected by labels, load-balancing across them.

Service types:

  • ClusterIP (default) — reachable only inside the cluster
  • NodePort — opens a static port on every node
  • LoadBalancer — provisions an external cloud load balancer

For HTTP, an Ingress (with an ingress controller like nginx) routes by host and path to Services — so one external IP serves many apps with TLS termination.

🌍Real-World Example
apiVersion: v1
kind: Service
metadata: { name: web }
spec:
  selector: { app: web }
  ports: [{ port: 80, targetPort: 8080 }]
  type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata: { name: web }
spec:
  rules:
    - host: app.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend: { service: { name: web, port: { number: 80 } } }
✍️Hands-On Exercise
  1. Create a ClusterIP Service selecting Pods labeled app=web.
  2. Explain the difference between ClusterIP, NodePort, and LoadBalancer.
  3. Add an Ingress routing a hostname to that Service.
  4. Why can’t you just connect to a Pod’s IP directly?
🧾Cheat Sheet
Type / objectUse
ClusterIPInternal-only stable IP
NodePortStatic port on each node
LoadBalancerExternal cloud LB
IngressHTTP host/path routing + TLS
SelectorMatches Pod labels
kubectl exposeQuick Service creation
💬Common Interview Questions
Why do you need a Service in front of Pods?

Pods are ephemeral with changing IPs. A Service provides a stable virtual IP and DNS name plus load balancing, selecting Pods by label so clients have a constant endpoint.

What's the difference between a Service and an Ingress?

A Service provides L4 connectivity and load balancing to Pods. An Ingress is L7 HTTP routing (by host/path) that directs external traffic to Services, often with TLS.

What are the main Service types?

ClusterIP (internal), NodePort (a static port on each node), and LoadBalancer (an external cloud-provisioned load balancer).

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type