version: 1
id: kubernetes-service
title: Expose a Deployment with a Service
summary: Create a ClusterIP Service so other workloads can reach your API Deployment inside k3d.
difficulty: intermediate
estimatedMinutes: 35
prerequisites: [kubernetes-deploy]
image: alpine/k8s:1.31.4
shell: /bin/bash
runtime:
  type: k3d
setup:
  - "kubectl apply -f - <<'EOF'\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: api\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: api\n  template:\n    metadata:\n      labels:\n        app: api\n    spec:\n      containers:\n        - name: api\n          image: nginx:1.27-alpine\n          ports:\n            - containerPort: 80\nEOF"
tasks:
  - id: expose-api
    title: Publish the API
    description: Create a Service named api exposing port 80 to pods with label app=api. Save the manifest as /workspace/service.yaml and apply it.
    hints:
      - kind Service, selector app=api, port 80, targetPort 80.
      - kubectl apply -f /workspace/service.yaml
    checks:
      - type: file
        name: Service manifest saved
        path: /workspace/service.yaml
        value: "kind: Service"
      - type: kubernetes
        name: Service exists in cluster
        command: kubectl get svc api
      - type: kubernetes
        name: Endpoints populated
        command: kubectl get endpoints api -o yaml | grep -q app=api
limits: {cpus: "1.0", memory: 512m, pids: 256, timeout: 3600, network: true}
