1# Kubernetes Service definitions for Flask application
2# Services provide stable networking and load balancing for pods
3
4---
5# ClusterIP Service (internal access only)
6# This is the default service type for internal communication
7apiVersion: v1
8kind: Service
9metadata:
10 name: flask-app
11 labels:
12 app: flask-app
13 annotations:
14 description: "Internal service for Flask application"
15spec:
16 type: ClusterIP
17
18 # Session affinity (optional): route same client to same pod
19 # sessionAffinity: ClientIP
20 # sessionAffinityConfig:
21 # clientIP:
22 # timeoutSeconds: 10800
23
24 ports:
25 - port: 80 # Service port
26 targetPort: 5000 # Container port
27 protocol: TCP
28 name: http
29
30 selector:
31 app: flask-app # Match pods with this label
32
33 # IP allocation (optional)
34 # clusterIP: 10.96.0.100
35
36---
37# NodePort Service (external access via node IP)
38# Useful for development or when LoadBalancer is not available
39# Uncomment to use:
40#
41# apiVersion: v1
42# kind: Service
43# metadata:
44# name: flask-app-nodeport
45# labels:
46# app: flask-app
47# spec:
48# type: NodePort
49# ports:
50# - port: 80
51# targetPort: 5000
52# nodePort: 30080 # Port on each node (30000-32767)
53# protocol: TCP
54# name: http
55# selector:
56# app: flask-app
57
58---
59# LoadBalancer Service (external access with cloud LB)
60# This creates a cloud provider load balancer (AWS ELB, GCP LB, etc.)
61# Uncomment to use:
62#
63# apiVersion: v1
64# kind: Service
65# metadata:
66# name: flask-app-lb
67# labels:
68# app: flask-app
69# annotations:
70# # AWS specific annotations
71# service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
72# service.beta.kubernetes.io/aws-load-balancer-backend-protocol: "http"
73# service.beta.kubernetes.io/aws-load-balancer-ssl-ports: "443"
74# service.beta.kubernetes.io/aws-load-balancer-ssl-cert: "arn:aws:acm:region:account-id:certificate/cert-id"
75#
76# # GCP specific annotations
77# # cloud.google.com/load-balancer-type: "Internal"
78#
79# # Azure specific annotations
80# # service.beta.kubernetes.io/azure-load-balancer-internal: "true"
81# spec:
82# type: LoadBalancer
83# ports:
84# - port: 80
85# targetPort: 5000
86# protocol: TCP
87# name: http
88# - port: 443
89# targetPort: 5000
90# protocol: TCP
91# name: https
92# selector:
93# app: flask-app
94# # External traffic policy
95# # externalTrafficPolicy: Local # Preserves client IP but may cause imbalance
96
97---
98# Headless Service (for direct pod access)
99# Returns pod IPs directly instead of load balancing
100# Useful for StatefulSets or service discovery
101apiVersion: v1
102kind: Service
103metadata:
104 name: flask-app-headless
105 labels:
106 app: flask-app
107spec:
108 clusterIP: None # Makes it headless
109 ports:
110 - port: 5000
111 targetPort: 5000
112 protocol: TCP
113 name: http
114 selector:
115 app: flask-app
116
117---
118# Usage Examples:
119#
120# 1. Get service details:
121# kubectl get svc flask-app
122# kubectl describe svc flask-app
123#
124# 2. Test internal connectivity:
125# kubectl run -it --rm debug --image=curlimages/curl --restart=Never -- \
126# curl http://flask-app/health
127#
128# 3. Port forward for local testing:
129# kubectl port-forward svc/flask-app 8080:80
130# curl http://localhost:8080
131#
132# 4. Get service endpoints (actual pod IPs):
133# kubectl get endpoints flask-app
134#
135# 5. DNS resolution inside cluster:
136# - Same namespace: flask-app
137# - Other namespace: flask-app.default.svc.cluster.local