service.yaml#

# Title: Kubernetes Service Manifest for Weather MLOps Application
# Purpose: Exposes the weather forecasting FastAPI backend within the K8s cluster
# Owner: Platform Team / MLOps Engineer
# Source: k8s/service.yaml
# Last-Reviewed: 2025-08-24
# Depends-On: k8s/deployment.yaml (app: weather-ml-app labels must match)
# Change-Log: Added comprehensive header comments for portfolio documentation
# Links: https://kubernetes.io/docs/concepts/services-networking/service/

# Architecture Overview:
# This Service provides stable DNS-based service discovery and automatic load balancing
# for the weather forecasting FastAPI backend. Essential component of the MLOps pipeline.

# Design Rationale:
# WHY: ClusterIP enables secure internal-only access by default, implementing security
# by design. This microservices pattern allows seamless pod communication while
# maintaining isolation. External access is managed by separate Ingress resources for
# better control and security layering.

# Core Design Decisions:
# - ClusterIP type: Zero-trust security model with explicit external access
# - Port mapping (80→8000): Standard HTTP ports with clear service/target separation
# - Label selectors: Exact match with deployment.yaml ensures proper pod targeting
# - No session affinity: Enables true load balancing across all healthy pods

# API Resource Declaration
# WHY: apiVersion v1 is the stable core API for basic Kubernetes resources
apiVersion: v1
kind: Service

# Service Identity and Classification
# WHY: Metadata enables resource discovery, organization, and proper lifecycle management
metadata:
   # Service identity within the namespace - used by other resources for discovery
   name: weather-ml-service

   # Resource classification for management and selection by other Kubernetes objects
   labels:
     # Primary app label - CRITICAL: must match Deployment labels for proper operation
     app: weather-ml-app

# Service Behavior Specification
# WHY: Spec defines the operational parameters and routing behavior of the Service
spec:
   # Pod Selection Logic
   # WHY: Label-based selection enables dynamic pod discovery and load balancing
   # CRITICAL: Selector labels MUST exactly match the pod template labels in deployment.yaml
   selector:
     app: weather-ml-app

   # Network Endpoint Configuration
   # WHY: Defines the traffic routing from Service endpoints to container ports
   ports:
     - # Network transport protocol for HTTP-based communication
       protocol: TCP

       # Service endpoint port - cluster-internal access point for clients
       # WHY: Port 80 is the standard HTTP port, expected by Ingress and other clients
       port: 80

       # Container target port where the FastAPI application listens
       # WHY: Must exactly match containerPort in deployment.yaml pod spec
       # CRITICAL: Misalignment causes connection failures and service disruption
       targetPort: 8000

   # Service Exposure Type
   # WHY: Determines the security boundary and access pattern for the Service
   # Design Choice: ClusterIP prioritizes security over convenience for internal services
   # Alternatives:
   # - LoadBalancer: Use for production external access via cloud provider
   # - NodePort: Use for development/testing with direct node access
   type: ClusterIP