|
1 | | -# 👇 Kubernetes version declaration (optional but helpful for editors) |
| 1 | +# ============================================ |
| 2 | +# 1) NAMESPACE |
| 3 | +# ============================================ |
2 | 4 | apiVersion: v1 |
3 | 5 | kind: Namespace |
4 | 6 | metadata: |
5 | 7 | name: blog-namespace |
6 | | - # 👆 Create a namespace to logically separate our blog app |
7 | | - |
8 | 8 | --- |
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 | +# ============================================ |
13 | 27 | apiVersion: apps/v1 |
14 | | -kind: Deployment |
| 28 | +kind: StatefulSet |
15 | 29 | metadata: |
16 | | - name: mysql-blog |
17 | | - namespace: blog-namespace # 👈 deploy inside the same namespace |
| 30 | + name: mysql |
| 31 | + namespace: blog-namespace |
18 | 32 | spec: |
19 | | - replicas: 1 # one MySQL pod is enough |
| 33 | + serviceName: mysql.blog # <-- IMPORTANT (matches service) |
| 34 | + replicas: 1 |
20 | 35 | selector: |
21 | 36 | matchLabels: |
22 | | - app: mysql-blog |
| 37 | + app: mysql |
| 38 | + |
23 | 39 | template: |
24 | 40 | metadata: |
25 | 41 | labels: |
26 | | - app: mysql-blog |
| 42 | + app: mysql |
27 | 43 | spec: |
28 | 44 | containers: |
29 | 45 | - name: mysql |
30 | | - image: mysql:8 # 👈 same image used in Docker Compose |
31 | | - env: # 👇 environment variables for MySQL |
| 46 | + image: mysql:8 |
| 47 | + env: |
32 | 48 | - name: MYSQL_ROOT_PASSWORD |
33 | 49 | value: "password" |
34 | 50 | - name: MYSQL_DATABASE |
35 | 51 | value: "blog" |
36 | 52 | ports: |
37 | | - - containerPort: 3306 # expose internal MySQL port |
| 53 | + - containerPort: 3306 |
38 | 54 | 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 |
61 | 66 | --- |
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 | +# ============================================ |
82 | 70 | apiVersion: apps/v1 |
83 | 71 | kind: Deployment |
84 | 72 | metadata: |
85 | | - name: backend-blog |
| 73 | + name: backend |
86 | 74 | namespace: blog-namespace |
87 | 75 | spec: |
88 | 76 | replicas: 1 |
89 | 77 | selector: |
90 | 78 | matchLabels: |
91 | | - app: backend-blog |
| 79 | + app: backend |
92 | 80 | template: |
93 | 81 | metadata: |
94 | 82 | labels: |
95 | | - app: backend-blog |
| 83 | + app: backend |
96 | 84 | spec: |
97 | 85 | containers: |
98 | 86 | - name: backend |
99 | | - image: krishna2808/blog_backend:V1 # 👈 use same backend image |
| 87 | + image: krishna2808/backend-blog:V1 |
100 | 88 | env: |
101 | 89 | - name: DEBUG |
102 | 90 | value: "True" |
103 | | - - name: DJANGO_DB |
104 | | - value: "blog" |
105 | 91 | - name: MYSQL_HOST |
106 | | - value: "mysql-blog-service" # 👈 use service name for DB host |
| 92 | + value: mysql.blog |
107 | 93 | - name: MYSQL_NAME |
108 | | - value: "mysql" |
| 94 | + value: blog |
109 | 95 | - name: MYSQL_USER |
110 | | - value: "root" |
| 96 | + value: root |
111 | 97 | - name: MYSQL_PASSWORD |
112 | | - value: "password" |
| 98 | + value: password |
113 | 99 | - name: MYSQL_PORT |
114 | 100 | value: "3306" |
115 | 101 | ports: |
116 | 102 | - 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 | | - |
124 | 103 | --- |
125 | | - |
126 | | -# 🔄 Service for backend — exposes Django API to cluster |
| 104 | +# ============================================ |
| 105 | +# 5) BACKEND SERVICE (ClusterIP) |
| 106 | +# ============================================ |
127 | 107 | apiVersion: v1 |
128 | 108 | kind: Service |
129 | 109 | metadata: |
130 | | - name: backend-blog-service |
| 110 | + name: backend-service |
131 | 111 | namespace: blog-namespace |
132 | 112 | spec: |
| 113 | + type: ClusterIP |
133 | 114 | selector: |
134 | | - app: backend-blog |
| 115 | + app: backend |
135 | 116 | 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 |
140 | 119 | --- |
141 | | - |
142 | | -# ======================== |
143 | | -# 3️⃣ FRONTEND (React) |
144 | | -# ======================== |
| 120 | +# ============================================ |
| 121 | +# 6) FRONTEND (REACT) - DEPLOYMENT |
| 122 | +# ============================================ |
145 | 123 | apiVersion: apps/v1 |
146 | 124 | kind: Deployment |
147 | 125 | metadata: |
148 | | - name: frontend-blog |
| 126 | + name: frontend |
149 | 127 | namespace: blog-namespace |
150 | 128 | spec: |
151 | 129 | replicas: 1 |
152 | 130 | selector: |
153 | 131 | matchLabels: |
154 | | - app: frontend-blog |
| 132 | + app: frontend |
155 | 133 | template: |
156 | 134 | metadata: |
157 | 135 | labels: |
158 | | - app: frontend-blog |
| 136 | + app: frontend |
159 | 137 | spec: |
160 | 138 | containers: |
161 | 139 | - name: frontend |
162 | | - image: krishna2808/blog_frontend:V1 # 👈 replace with your frontend image |
| 140 | + image: krishna2808/frontend-blog:V1 |
163 | 141 | ports: |
164 | 142 | - 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 | | - |
172 | 143 | --- |
173 | | - |
174 | | -# 🌐 Service for frontend — to expose React app to browser |
| 144 | +# ============================================ |
| 145 | +# 7) FRONTEND SERVICE (ClusterIP) |
| 146 | +# ============================================ |
175 | 147 | apiVersion: v1 |
176 | 148 | kind: Service |
177 | 149 | metadata: |
178 | | - name: frontend-blog-service |
| 150 | + name: frontend-service |
179 | 151 | namespace: blog-namespace |
180 | 152 | spec: |
| 153 | + type: ClusterIP |
181 | 154 | selector: |
182 | | - app: frontend-blog |
| 155 | + app: frontend |
183 | 156 | ports: |
184 | 157 | - port: 3000 |
185 | 158 | targetPort: 3000 |
186 | | - type: NodePort # 👈 for external access from your machine |
187 | | - |
188 | 159 | --- |
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 |
195 | 165 | metadata: |
196 | | - name: nginx-blog |
| 166 | + name: blog-ingress |
197 | 167 | namespace: blog-namespace |
| 168 | + annotations: |
| 169 | + nginx.ingress.kubernetes.io/rewrite-target: /$1 |
| 170 | + nginx.ingress.kubernetes.io/use-regex: "true" |
198 | 171 | 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