Skip to content

Commit 0c91296

Browse files
committed
k8s updated
1 parent bcd9154 commit 0c91296

6 files changed

Lines changed: 220 additions & 0 deletions

File tree

k8s/deployment.yaml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
4+
metadata:
5+
name: metadata-frontend
6+
labels:
7+
app: metadata-frontend
8+
9+
spec:
10+
replicas: 2
11+
12+
revisionHistoryLimit: 5 # Keep last 5 ReplicaSets for rollback
13+
14+
strategy:
15+
type: RollingUpdate
16+
rollingUpdate:
17+
maxUnavailable: 1 # At most 1 pod can be down during update
18+
maxSurge: 1 # Create 1 extra pod during update
19+
20+
selector:
21+
matchLabels:
22+
app: metadata-frontend
23+
24+
template:
25+
metadata:
26+
labels:
27+
app: metadata-frontend
28+
29+
spec:
30+
31+
terminationGracePeriodSeconds: 30 # It tells Kubernetes to wait up to 30 seconds for the pod to shut down gracefully before forcefully killing it.
32+
33+
containers:
34+
- name: metadatafrontend
35+
image: deepdiv/metadatafrontend:v1.0
36+
imagePullPolicy: Always
37+
ports:
38+
- containerPort: 4000
39+
40+
# Run container as non-root user for better security; Makes filesystem read-only
41+
securityContext:
42+
runAsNonRoot: true
43+
runAsUser: 1000 # Forces container to run as Linux user with UID 1000; Normal users = usually 1000+
44+
allowPrivilegeEscalation: false
45+
readOnlyRootFilesystem: true
46+
47+
## kubectl top pod
48+
# Extremly low for test purposes, to test HPA, Base i saw is 20Mi
49+
resources:
50+
requests: # minimum guaranteed resources or guranteed space
51+
cpu: "20m" # Required for HPA calculation, horizontal scaling #
52+
memory: "30Mi"
53+
limits: # maximum allowed usage
54+
cpu: "100m" # CPU limit = speed cap # Not a kill switch.
55+
memory: "50Mi" # Yes — it crashes if crosses
56+
57+
# Delays container shutdown by 10 seconds so traffic can drain and ongoing requests can finish safely before the pod stops.
58+
lifecycle:
59+
preStop:
60+
exec:
61+
command: ["sh", "-c", "sleep 10"]
62+
63+
startupProbe:
64+
httpGet:
65+
path: /health
66+
port: 4000
67+
periodSeconds: 5
68+
failureThreshold: 8 # 5 × 8 = 40 seconds allowed for startup
69+
70+
livenessProbe: # Auto Restart if Broken # Active Health Monitoring
71+
httpGet:
72+
path: /health
73+
port: 4000
74+
periodSeconds: 10 # Kubernetes runs the health check every 10 seconds.
75+
timeoutSeconds: 3 # Kubernetes waits up to 3 seconds for the app to respond before marking the check as failed.
76+
failureThreshold: 3 # If the health check fails 3 consecutive times, Kubernetes considers the probe failed and takes action (restart for liveness, mark NotReady for readiness).
77+
78+
79+
readinessProbe: # Determines whether the pod is ready to receive traffic from the Service
80+
httpGet:
81+
path: /health # Kubernetes sends an HTTP GET request to /health
82+
port: 4000 # The request is sent to container port 4000
83+
initialDelaySeconds: 5 # Wait 5 seconds after container starts before performing the first readiness check
84+
periodSeconds: 5 # Run the readiness check every 5 seconds
85+
timeoutSeconds: 2 # Wait up to 2 seconds for a response before marking the check as failed
86+
failureThreshold: 3 # If the check fails 3 consecutive times, mark the pod as NotReady (removed from Service traffic)
87+

k8s/hpa.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
apiVersion: autoscaling/v2 # Required for advanced metric-based autoscaling
2+
kind: HorizontalPodAutoscaler # Defines automatic scaling rules
3+
4+
metadata:
5+
name: metadata-frontend-hpa # Name of the HPA resource
6+
labels:
7+
app: metadata-frontend # Label for identification
8+
9+
spec:
10+
scaleTargetRef:
11+
apiVersion: apps/v1 # Target API version
12+
kind: Deployment # We are scaling a Deployment
13+
name: metadata-frontend # Must match Deployment name exactly
14+
15+
minReplicas: 2 # Never scale below 2 pods
16+
maxReplicas: 6 # Never scale above 6 pods
17+
18+
metrics:
19+
# CPU METRIC
20+
- type: Resource
21+
resource:
22+
name: cpu # Monitor CPU usage
23+
target:
24+
type: Utilization # Percentage of requested CPU
25+
averageUtilization: 80 # Scale if average CPU > 70% of request
26+
27+
# MEMORY METRIC
28+
- type: Resource
29+
resource:
30+
name: memory # Monitor Memory usage
31+
target:
32+
type: Utilization # Percentage of requested Memory
33+
averageUtilization: 80 # Scale if average Memory > 80% of request
34+
35+
behavior:
36+
# SCALE UP BEHAVIOR
37+
scaleUp:
38+
stabilizationWindowSeconds: 30 # Wait 30s before scaling up again
39+
policies:
40+
- type: Percent
41+
value: 100 # Can increase replicas by 100% (double)
42+
periodSeconds: 60 # Within a 60-second window
43+
44+
# SCALE DOWN BEHAVIOR
45+
scaleDown:
46+
stabilizationWindowSeconds: 300 # Wait 5 minutes before scaling down
47+
policies:
48+
- type: Percent
49+
value: 50 # Reduce at most 50% of pods at once
50+
periodSeconds: 60 # Within a 60-second window

k8s/ingress.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
4+
metadata:
5+
name: metadata-frontend-ingress
6+
annotations:
7+
nginx.ingress.kubernetes.io/load-balance: "round_robin" # Load balancing strategy
8+
nginx.ingress.kubernetes.io/ssl-redirect: "true"
9+
10+
spec:
11+
ingressClassName: nginx # Must match installed ingress controller
12+
13+
# TLS configuration (HTTPS - Port 443)
14+
# Only enabled for public domain
15+
tls:
16+
- hosts:
17+
- coldemailgenerator.online # Public domain secured with HTTPS
18+
secretName: coldemailgenerator-tls # TLS secret containing certificate & key
19+
20+
rules:
21+
22+
# Localhost
23+
- host: localhost
24+
http:
25+
paths:
26+
- path: /
27+
pathType: Prefix
28+
backend:
29+
service:
30+
name: metadata-frontend-service
31+
port:
32+
number: 4000
33+
34+
# Public Domain
35+
- host: coldemailgenerator.online
36+
http:
37+
paths:
38+
- path: /
39+
pathType: Prefix
40+
backend:
41+
service:
42+
name: metadata-frontend-service
43+
port:
44+
number: 4000

k8s/pdb.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: policy/v1 # API version for PodDisruptionBudget
2+
kind: PodDisruptionBudget # Ensures minimum pod availability
3+
4+
metadata:
5+
name: metadata-frontend-pdb # Name of the PDB
6+
labels:
7+
app: metadata-frontend # Label for grouping
8+
9+
spec:
10+
minAvailable: 1 # At least 1 pod must always remain running
11+
# Even during voluntary disruptions (node drain, upgrades)
12+
13+
selector:
14+
matchLabels:
15+
app: metadata-frontend # Must match Deployment pod labels

k8s/service.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: v1 # Core Kubernetes API version for Service resources
2+
kind: Service # Defines a stable network endpoint for a set of Pods
3+
4+
metadata:
5+
name: metadata-frontend-service # Service name (used for internal DNS)
6+
labels:
7+
app: metadata-frontend # Label for identifying this Service
8+
9+
spec:
10+
type: ClusterIP # Exposes service internally within the cluster only
11+
# (Default type. Not accessible from outside)
12+
13+
selector:
14+
app: metadata-frontend # Selects Pods with this label
15+
# Must exactly match Deployment pod template labels
16+
17+
ports:
18+
- name: http # Logical name for the port (useful for clarity and future extensions)
19+
protocol: TCP # Protocol used for communication
20+
port: 4000 # Port exposed by the Service inside the cluster
21+
# Other pods will call this port
22+
targetPort: 4000 # Port on the container (matches containerPort in Deployment)
23+
# Traffic received on port 80 is forwarded to 4000

rough/Lecture-Summarize

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit e4d58716840a4474b373cb3c112bea71d734a228

0 commit comments

Comments
 (0)