Skip to content

Commit 7d5bb95

Browse files
committed
add k8s
1 parent ebabe46 commit 7d5bb95

File tree

6 files changed

+827
-0
lines changed

6 files changed

+827
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: backend-deployment
5+
namespace: default
6+
labels:
7+
app: backend
8+
version: v1
9+
spec:
10+
replicas: 2
11+
strategy:
12+
type: RollingUpdate
13+
rollingUpdate:
14+
maxSurge: 1
15+
maxUnavailable: 0
16+
selector:
17+
matchLabels:
18+
app: backend
19+
template:
20+
metadata:
21+
labels:
22+
app: backend
23+
spec:
24+
serviceAccountName: backend-sa
25+
affinity:
26+
podAntiAffinity:
27+
preferredDuringSchedulingIgnoredDuringExecution:
28+
- weight: 100
29+
podAffinityTerm:
30+
labelSelector:
31+
matchExpressions:
32+
- key: app
33+
operator: In
34+
values:
35+
- backend
36+
topologyKey: kubernetes.io/hostname
37+
containers:
38+
- name: backend
39+
image: backend-service:latest
40+
imagePullPolicy: Never
41+
ports:
42+
- name: http
43+
containerPort: 5000
44+
protocol: TCP
45+
- name: metrics
46+
containerPort: 8000
47+
protocol: TCP
48+
env:
49+
- name: LOG_LEVEL
50+
value: "INFO"
51+
- name: JAEGER_ENDPOINT
52+
value: "http://jaeger.monitoring.svc.cluster.local:4318/v1/traces"
53+
resources:
54+
requests:
55+
cpu: "100m"
56+
memory: "128Mi"
57+
limits:
58+
cpu: "500m"
59+
memory: "512Mi"
60+
livenessProbe:
61+
httpGet:
62+
path: /api
63+
port: http
64+
initialDelaySeconds: 30
65+
periodSeconds: 10
66+
timeoutSeconds: 5
67+
failureThreshold: 3
68+
readinessProbe:
69+
httpGet:
70+
path: /api
71+
port: http
72+
initialDelaySeconds: 10
73+
periodSeconds: 5
74+
timeoutSeconds: 3
75+
failureThreshold: 2
76+
securityContext:
77+
runAsNonRoot: true
78+
runAsUser: 1000
79+
readOnlyRootFilesystem: true
80+
allowPrivilegeEscalation: false
81+
capabilities:
82+
drop:
83+
- ALL
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: frontend-deployment
5+
namespace: default
6+
labels:
7+
app: frontend
8+
version: v1
9+
spec:
10+
replicas: 2
11+
strategy:
12+
type: RollingUpdate
13+
rollingUpdate:
14+
maxSurge: 1
15+
maxUnavailable: 0
16+
selector:
17+
matchLabels:
18+
app: frontend
19+
template:
20+
metadata:
21+
labels:
22+
app: frontend
23+
spec:
24+
serviceAccountName: frontend-sa
25+
affinity:
26+
podAntiAffinity:
27+
preferredDuringSchedulingIgnoredDuringExecution:
28+
- weight: 100
29+
podAffinityTerm:
30+
labelSelector:
31+
matchExpressions:
32+
- key: app
33+
operator: In
34+
values:
35+
- frontend
36+
topologyKey: kubernetes.io/hostname
37+
containers:
38+
- name: frontend
39+
image: frontend-service:latest
40+
imagePullPolicy: Never
41+
ports:
42+
- name: http
43+
containerPort: 5000
44+
protocol: TCP
45+
- name: metrics
46+
containerPort: 8000
47+
protocol: TCP
48+
env:
49+
- name: LOG_LEVEL
50+
value: "INFO"
51+
- name: JAEGER_ENDPOINT
52+
value: "http://jaeger.monitoring.svc.cluster.local:4318/v1/traces"
53+
- name: BACKEND_URL
54+
value: "http://backend-service:5000"
55+
resources:
56+
requests:
57+
cpu: "100m"
58+
memory: "128Mi"
59+
limits:
60+
cpu: "500m"
61+
memory: "512Mi"
62+
livenessProbe:
63+
httpGet:
64+
path: /
65+
port: http
66+
initialDelaySeconds: 30
67+
periodSeconds: 10
68+
timeoutSeconds: 5
69+
failureThreshold: 3
70+
readinessProbe:
71+
httpGet:
72+
path: /
73+
port: http
74+
initialDelaySeconds: 10
75+
periodSeconds: 5
76+
timeoutSeconds: 3
77+
failureThreshold: 2
78+
securityContext:
79+
runAsNonRoot: true
80+
runAsUser: 1000
81+
readOnlyRootFilesystem: true
82+
allowPrivilegeEscalation: false
83+
capabilities:
84+
drop:
85+
- ALL

k8s-reliability/hpa-and-pdb.yaml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Kubernetes Horizontal Pod Autoscaler (HPA) and Pod Disruption Budget (PDB)
2+
# Demonstrates high availability and resilience patterns
3+
4+
---
5+
# Horizontal Pod Autoscaler for backend service
6+
apiVersion: autoscaling/v2
7+
kind: HorizontalPodAutoscaler
8+
metadata:
9+
name: backend-hpa
10+
namespace: default
11+
spec:
12+
scaleTargetRef:
13+
apiVersion: apps/v1
14+
kind: Deployment
15+
name: backend
16+
minReplicas: 2
17+
maxReplicas: 10
18+
metrics:
19+
# Scale based on CPU utilization
20+
- type: Resource
21+
resource:
22+
name: cpu
23+
target:
24+
type: Utilization
25+
averageUtilization: 70
26+
# Scale based on memory utilization
27+
- type: Resource
28+
resource:
29+
name: memory
30+
target:
31+
type: Utilization
32+
averageUtilization: 80
33+
behavior:
34+
scaleDown:
35+
stabilizationWindowSeconds: 300
36+
policies:
37+
- type: Percent
38+
value: 50
39+
periodSeconds: 60
40+
scaleUp:
41+
stabilizationWindowSeconds: 60
42+
policies:
43+
- type: Percent
44+
value: 100
45+
periodSeconds: 60
46+
47+
---
48+
# Horizontal Pod Autoscaler for frontend service
49+
apiVersion: autoscaling/v2
50+
kind: HorizontalPodAutoscaler
51+
metadata:
52+
name: frontend-hpa
53+
namespace: default
54+
spec:
55+
scaleTargetRef:
56+
apiVersion: apps/v1
57+
kind: Deployment
58+
name: frontend
59+
minReplicas: 2
60+
maxReplicas: 10
61+
metrics:
62+
- type: Resource
63+
resource:
64+
name: cpu
65+
target:
66+
type: Utilization
67+
averageUtilization: 75
68+
- type: Resource
69+
resource:
70+
name: memory
71+
target:
72+
type: Utilization
73+
averageUtilization: 80
74+
behavior:
75+
scaleDown:
76+
stabilizationWindowSeconds: 300
77+
policies:
78+
- type: Percent
79+
value: 50
80+
periodSeconds: 60
81+
scaleUp:
82+
stabilizationWindowSeconds: 60
83+
policies:
84+
- type: Percent
85+
value: 100
86+
periodSeconds: 60
87+
88+
---
89+
# Pod Disruption Budget for backend service
90+
# Ensures at least 1 pod is always available during voluntary disruptions
91+
apiVersion: policy/v1
92+
kind: PodDisruptionBudget
93+
metadata:
94+
name: backend-pdb
95+
namespace: default
96+
spec:
97+
minAvailable: 1
98+
selector:
99+
matchLabels:
100+
app: backend
101+
102+
---
103+
# Pod Disruption Budget for frontend service
104+
apiVersion: policy/v1
105+
kind: PodDisruptionBudget
106+
metadata:
107+
name: frontend-pdb
108+
namespace: default
109+
spec:
110+
minAvailable: 1
111+
selector:
112+
matchLabels:
113+
app: frontend
114+
115+
---
116+
# Pod Disruption Budget for Prometheus
117+
apiVersion: policy/v1
118+
kind: PodDisruptionBudget
119+
metadata:
120+
name: prometheus-pdb
121+
namespace: monitoring
122+
spec:
123+
minAvailable: 1
124+
selector:
125+
matchLabels:
126+
app.kubernetes.io/name: prometheus
127+
128+
---
129+
# Pod Disruption Budget for Grafana
130+
apiVersion: policy/v1
131+
kind: PodDisruptionBudget
132+
metadata:
133+
name: grafana-pdb
134+
namespace: monitoring
135+
spec:
136+
minAvailable: 1
137+
selector:
138+
matchLabels:
139+
app.kubernetes.io/name: grafana

0 commit comments

Comments
 (0)