Skip to content

Commit 87f94b0

Browse files
authored
Update blog-deployment.yaml
1 parent 51afe17 commit 87f94b0

1 file changed

Lines changed: 105 additions & 154 deletions

File tree

K8s/blog-deployment.yaml

Lines changed: 105 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,241 +1,192 @@
1-
# 👇 Kubernetes version declaration (optional but helpful for editors)
1+
# ============================================
2+
# 1) NAMESPACE
3+
# ============================================
24
apiVersion: v1
35
kind: Namespace
46
metadata:
57
name: blog-namespace
6-
# 👆 Create a namespace to logically separate our blog app
7-
88
---
9-
10-
# ========================
11-
# 1️⃣ MYSQL DATABASE
12-
# ========================
9+
# ============================================
10+
# 2) MYSQL - HEADLESS SERVICE
11+
# ============================================
12+
apiVersion: v1
13+
kind: Service
14+
metadata:
15+
name: mysql.blog
16+
namespace: blog-namespace
17+
spec:
18+
clusterIP: None # Headless for StatefulSet
19+
selector:
20+
app: mysql
21+
ports:
22+
- port: 3306
23+
---
24+
# ============================================
25+
# 3) MYSQL - STATEFULSET + PVC
26+
# ============================================
1327
apiVersion: apps/v1
14-
kind: Deployment
28+
kind: StatefulSet
1529
metadata:
16-
name: mysql-blog
17-
namespace: blog-namespace # 👈 deploy inside the same namespace
30+
name: mysql
31+
namespace: blog-namespace
1832
spec:
19-
replicas: 1 # one MySQL pod is enough
33+
serviceName: mysql.blog # <-- IMPORTANT (matches service)
34+
replicas: 1
2035
selector:
2136
matchLabels:
22-
app: mysql-blog
37+
app: mysql
38+
2339
template:
2440
metadata:
2541
labels:
26-
app: mysql-blog
42+
app: mysql
2743
spec:
2844
containers:
2945
- name: mysql
30-
image: mysql:8 # 👈 same image used in Docker Compose
31-
env: # 👇 environment variables for MySQL
46+
image: mysql:8
47+
env:
3248
- name: MYSQL_ROOT_PASSWORD
3349
value: "password"
3450
- name: MYSQL_DATABASE
3551
value: "blog"
3652
ports:
37-
- containerPort: 3306 # expose internal MySQL port
53+
- containerPort: 3306
3854
volumeMounts:
39-
- name: mysql-storage
40-
mountPath: /var/lib/mysql # same as docker volume path
41-
volumes:
42-
- name: mysql-storage
43-
persistentVolumeClaim:
44-
claimName: mysql-pvc # 👈 attach a persistent storage claim
45-
46-
---
47-
48-
# 🗄️ Persistent Volume Claim for MySQL data
49-
apiVersion: v1
50-
kind: PersistentVolumeClaim
51-
metadata:
52-
name: mysql-pvc
53-
namespace: blog-namespace
54-
spec:
55-
accessModes:
56-
- ReadWriteOnce
57-
resources:
58-
requests:
59-
storage: 1Gi # 👈 1GB storage for MySQL data
60-
55+
- name: mysql-data
56+
mountPath: /var/lib/mysql
57+
58+
volumeClaimTemplates:
59+
- metadata:
60+
name: mysql-data
61+
spec:
62+
accessModes: ["ReadWriteOnce"]
63+
resources:
64+
requests:
65+
storage: 2Gi
6166
---
62-
63-
# 💡 Service to expose MySQL internally to other pods
64-
apiVersion: v1
65-
kind: Service
66-
metadata:
67-
name: mysql-blog-service
68-
namespace: blog-namespace
69-
spec:
70-
selector:
71-
app: mysql-blog
72-
ports:
73-
- port: 3306 # service port
74-
targetPort: 3306 # container port
75-
clusterIP: None # �� optional for direct DNS resolution (headless service)
76-
77-
---
78-
79-
# ========================
80-
# 2️⃣ BACKEND (Django)
81-
# ========================
67+
# ============================================
68+
# 4) BACKEND (DJANGO) - DEPLOYMENT
69+
# ============================================
8270
apiVersion: apps/v1
8371
kind: Deployment
8472
metadata:
85-
name: backend-blog
73+
name: backend
8674
namespace: blog-namespace
8775
spec:
8876
replicas: 1
8977
selector:
9078
matchLabels:
91-
app: backend-blog
79+
app: backend
9280
template:
9381
metadata:
9482
labels:
95-
app: backend-blog
83+
app: backend
9684
spec:
9785
containers:
9886
- name: backend
99-
image: krishna2808/blog_backend:V1 # 👈 use same backend image
87+
image: krishna2808/backend-blog:V1
10088
env:
10189
- name: DEBUG
10290
value: "True"
103-
- name: DJANGO_DB
104-
value: "blog"
10591
- name: MYSQL_HOST
106-
value: "mysql-blog-service" # 👈 use service name for DB host
92+
value: mysql.blog
10793
- name: MYSQL_NAME
108-
value: "mysql"
94+
value: blog
10995
- name: MYSQL_USER
110-
value: "root"
96+
value: root
11197
- name: MYSQL_PASSWORD
112-
value: "password"
98+
value: password
11399
- name: MYSQL_PORT
114100
value: "3306"
115101
ports:
116102
- containerPort: 8000
117-
volumeMounts:
118-
- name: backend-data
119-
mountPath: /app/backend/ # same as Docker Compose volume
120-
volumes:
121-
- name: backend-data
122-
emptyDir: {} # temporary storage (for learning)
123-
124103
---
125-
126-
# 🔄 Service for backend — exposes Django API to cluster
104+
# ============================================
105+
# 5) BACKEND SERVICE (ClusterIP)
106+
# ============================================
127107
apiVersion: v1
128108
kind: Service
129109
metadata:
130-
name: backend-blog-service
110+
name: backend-service
131111
namespace: blog-namespace
132112
spec:
113+
type: ClusterIP
133114
selector:
134-
app: backend-blog
115+
app: backend
135116
ports:
136-
- port: 8000 # cluster port
137-
targetPort: 8000 # Django port in container
138-
type: ClusterIP # accessible only inside Kubernetes network
139-
117+
- port: 8000
118+
targetPort: 8000
140119
---
141-
142-
# ========================
143-
# 3️⃣ FRONTEND (React)
144-
# ========================
120+
# ============================================
121+
# 6) FRONTEND (REACT) - DEPLOYMENT
122+
# ============================================
145123
apiVersion: apps/v1
146124
kind: Deployment
147125
metadata:
148-
name: frontend-blog
126+
name: frontend
149127
namespace: blog-namespace
150128
spec:
151129
replicas: 1
152130
selector:
153131
matchLabels:
154-
app: frontend-blog
132+
app: frontend
155133
template:
156134
metadata:
157135
labels:
158-
app: frontend-blog
136+
app: frontend
159137
spec:
160138
containers:
161139
- name: frontend
162-
image: krishna2808/blog_frontend:V1 # 👈 replace with your frontend image
140+
image: krishna2808/frontend-blog:V1
163141
ports:
164142
- containerPort: 3000
165-
volumeMounts:
166-
- name: frontend-data
167-
mountPath: /app/frontend/
168-
volumes:
169-
- name: frontend-data
170-
emptyDir: {} # demo volume (no persistence)
171-
172143
---
173-
174-
# 🌐 Service for frontend — to expose React app to browser
144+
# ============================================
145+
# 7) FRONTEND SERVICE (ClusterIP)
146+
# ============================================
175147
apiVersion: v1
176148
kind: Service
177149
metadata:
178-
name: frontend-blog-service
150+
name: frontend-service
179151
namespace: blog-namespace
180152
spec:
153+
type: ClusterIP
181154
selector:
182-
app: frontend-blog
155+
app: frontend
183156
ports:
184157
- port: 3000
185158
targetPort: 3000
186-
type: NodePort # 👈 for external access from your machine
187-
188159
---
189-
190-
# ========================
191-
# 4️⃣ NGINX (Reverse Proxy)
192-
# ========================
193-
apiVersion: apps/v1
194-
kind: Deployment
160+
# ============================================
161+
# 8) INGRESS (Frontend + Backend Routing)
162+
# ============================================
163+
apiVersion: networking.k8s.io/v1
164+
kind: Ingress
195165
metadata:
196-
name: nginx-blog
166+
name: blog-ingress
197167
namespace: blog-namespace
168+
annotations:
169+
nginx.ingress.kubernetes.io/rewrite-target: /$1
170+
nginx.ingress.kubernetes.io/use-regex: "true"
198171
spec:
199-
replicas: 1
200-
selector:
201-
matchLabels:
202-
app: nginx-blog
203-
template:
204-
metadata:
205-
labels:
206-
app: nginx-blog
207-
spec:
208-
containers:
209-
- name: nginx
210-
image: nginx:alpine # same image from compose
211-
ports:
212-
- containerPort: 80
213-
- containerPort: 443
214-
# You can mount nginx.conf here if needed
215-
# volumeMounts:
216-
# - name: nginx-conf
217-
# mountPath: /etc/nginx/conf.d/
218-
# volumes:
219-
# - name: nginx-conf
220-
# configMap:
221-
# name: nginx-config
222-
223-
---
224-
225-
# 🚪 Service to expose Nginx externally
226-
apiVersion: v1
227-
kind: Service
228-
metadata:
229-
name: nginx-blog-service
230-
namespace: blog-namespace
231-
spec:
232-
selector:
233-
app: nginx-blog
234-
ports:
235-
- port: 80
236-
targetPort: 80
237-
nodePort: 30080 # fixed port on your host (optional)
238-
- port: 443
239-
targetPort: 443
240-
nodePort: 30443
241-
type: NodePort # 👈 expose outside Kubernetes cluster
172+
rules:
173+
- host: blog.local
174+
http:
175+
paths:
176+
# API FIRST
177+
- path: /api/?(.*)
178+
pathType: Prefix
179+
backend:
180+
service:
181+
name: backend-service
182+
port:
183+
number: 8000
184+
185+
# FRONTEND FALLBACK
186+
- path: /?(.*)
187+
pathType: Prefix
188+
backend:
189+
service:
190+
name: frontend-service
191+
port:
192+
number: 3000

0 commit comments

Comments
 (0)