Deployment, Service, Ingress

The classic trio: a Deployment runs Pods, a Service groups them, and an Ingress exposes them.

Most web apps on Kubernetes are built from three resources working together.

  1. Deployment — keeps a set of identical Pods running and handles rolling updates.
  2. Service — gives those Pods one stable internal address and load-balances between them.
  3. Ingress — exposes the Service to the outside world via Traefik.

On k3s you can apply all three at once because Traefik and ServiceLB are already installed. The example puts them in a single file separated by ---.

Example

Example · yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: nginx:1.27
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web
spec:
  rules:
    - host: web.localhost
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 80

When to use it

  • A team deploys a Node.js API with a Deployment for replicas, a ClusterIP Service for internal access, and an Ingress for HTTPS routing to api.example.com.
  • An operator uses a single YAML file containing Deployment, Service, and Ingress to GitOps-deploy an entire microservice to k3s.
  • A developer scales a Deployment from 1 to 3 replicas and the existing Service automatically load-balances traffic across all pods.

More examples

Deployment manifest

Defines a Deployment that keeps two nginx pods running, labeled so a Service can select them.

Example · yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: nginx:alpine
        ports:
        - containerPort: 80

Service and Ingress manifest

Creates a ClusterIP Service to front the pods and an Ingress to route external HTTP traffic by hostname.

Example · yaml
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
  - port: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web
spec:
  rules:
  - host: web.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web
            port:
              number: 80

Apply and verify the stack

Applies the combined manifest, lists all three resource types, and waits for the Deployment to finish rolling out.

Example · bash
kubectl apply -f web-stack.yaml
kubectl get deploy,svc,ingress
kubectl rollout status deployment/web

Discussion

  • Be the first to comment on this lesson.