@@ -52,33 +52,32 @@ Create a Private Network that both your Kubernetes cluster and database will use
52
52
is-ha-cluster=true \
53
53
user-name=admin \
54
54
password=StrongP@ssw0rd123 \
55
- private-network-id=<private-network-id>
55
+ region=fr-par
56
56
```
57
57
58
- This creates a high-availability PostgreSQL 15 database attached to the Private Network. The database is only accessible within the Private Network.
59
-
60
- 2 . ** Optional** If you prefer a public endpoint as well:
58
+ This creates a high-availability PostgreSQL 15 database with a public Endpoint.
61
59
62
- ```
63
- scw rdb instance create \
64
- name=my-kube-database \
65
- node-type=db-dev-s \
66
- engine=PostgreSQL-15 \
67
- is-ha-cluster=true \
68
- user-name=admin \
69
- password=StrongP@ssw0rd123
70
- ```
71
60
<Message type = " important" >
72
- Adding a public endpoint is less secure, but can be useful for management purposes in some cases.
73
- ** Ensure to choose a strong password for your database user.**
61
+ At this point the database is exposed to the Internet.
74
62
</Message >
75
63
76
64
3 . Add the Private Network endpoint to the database:
77
65
78
66
```
79
67
scw rdb endpoint create \
80
- instance-id=<database-instance-id> \
81
- private-network-id=<private-network-id>
68
+ <database-instance-id> \
69
+ private-network.private-network-id=<private-network-id> \
70
+ private-network.enable-ipam=true region-fr-par
71
+ ```
72
+
73
+ 4 . Get the Insance details and look for the public endpoint ID under the "Endpoints" section.
74
+ ```
75
+ scw rdb instance get <database-instance-id>
76
+ ```
77
+
78
+ 4 . Remove the public endpoint to ensure the database is only reachable from the Private Network and no longer exposed to the public Ineternet.
79
+ ```
80
+ scw rdb endpoint delete instance-id=<database-instance-id> <public-endpoint-id>
82
81
```
83
82
84
83
### Creating a Kubernetes Kapsule cluster
@@ -103,8 +102,7 @@ Create a Private Network that both your Kubernetes cluster and database will use
103
102
2 . Wait for the cluster to be ready, then get the ` kubeconfig ` :
104
103
105
104
```
106
- scw k8s kubeconfig install \
107
- cluster-id=<cluster-id>
105
+ scw k8s kubeconfig install <k8s-cluster-id> region=fr-par
108
106
```
109
107
110
108
### Creating a Kubernetes secret for database credentials
@@ -125,9 +123,51 @@ Use `kubectl` to create a Kubernetes secret to store the database credentials:
125
123
1 . Create a Kubernetes deployment that will connect to the database. Save this as ` db-app.yaml ` :
126
124
127
125
```
128
- ADD DB APP YAML FILE
126
+ apiVersion: apps/v1
127
+ kind: Deployment
128
+ metadata:
129
+ name: postgres-client
130
+ spec:
131
+ replicas: 1
132
+ selector:
133
+ matchLabels:
134
+ app: postgres-client
135
+ template:
136
+ metadata:
137
+ labels:
138
+ app: postgres-client
139
+ spec:
140
+ containers:
141
+ - name: postgres-client
142
+ image: postgres:latest
143
+ command: ["sleep", "infinity"]
144
+ env:
145
+ - name: DB_HOST
146
+ valueFrom:
147
+ secretKeyRef:
148
+ name: db-credentials
149
+ key: DB_HOST
150
+ - name: DB_PORT
151
+ valueFrom:
152
+ secretKeyRef:
153
+ name: db-credentials
154
+ key: DB_PORT
155
+ - name: DB_NAME
156
+ valueFrom:
157
+ secretKeyRef:
158
+ name: db-credentials
159
+ key: DB_NAME
160
+ - name: DB_USER
161
+ valueFrom:
162
+ secretKeyRef:
163
+ name: db-credentials
164
+ key: DB_USER
165
+ - name: DB_PASSWORD
166
+ valueFrom:
167
+ secretKeyRef:
168
+ name: db-credentials
169
+ key: DB_PASSWORD
129
170
```
130
-
131
171
2 . Apply it to your cluster:
132
172
133
173
```
@@ -227,43 +267,44 @@ Install Terraform and ensure the Scaleway Terraform provider is set up with `ter
227
267
228
268
# Create Managed PostgreSQL Database
229
269
resource "scaleway_rdb_instance" "database" {
230
- name = "my-kube-database"
231
- node_type = "db-dev-s"
232
- engine = "PostgreSQL-15"
233
- is_ha_cluster = true
234
- user_name = var.db_user
235
- password = var.db_password
270
+ name = "my-kube-database"
271
+ node_type = "db-dev-s"
272
+ engine = "PostgreSQL-15"
273
+ is_ha_cluster = true
274
+ user_name = var.db_user
275
+ password = var.db_password
276
+
236
277
private_network {
237
- pn_id = scaleway_vpc_private_network.private_net.id
278
+ pn_id = scaleway_vpc_private_network.private_net.id
279
+ enable_ipam = true
238
280
}
239
281
}
240
282
241
283
# Kubernetes Cluster (Kapsule)
242
284
resource "scaleway_k8s_cluster" "kapsule" {
243
- name = "my-kube-cluster"
244
- version = "1.28.2"
245
- cni = "cilium"
246
- private_network_id = scaleway_vpc_private_network.private_net.id
247
-
248
- autoscaler_config {
249
- disable_scale_down = false
250
- scale_down_delay_after_add = "10m"
251
- scale_down_unneeded_time = "10m"
252
- estimator = "binpacking"
253
- expander = "random"
254
- ignore_daemonsets_utilization = true
285
+ name = "my-kube-cluster-${random_id.suffix.hex}" # Make the name unique
286
+ version = "1.28.2"
287
+ cni = "cilium"
288
+ private_network_id = scaleway_vpc_private_network.private_net.id
289
+ delete_additional_resources = true
255
290
}
256
291
257
- pool {
258
- name = "default-pool"
259
- node_type = "DEV1-M"
260
- size = 2
261
- autoscaling = true
262
- min_size = 2
263
- max_size = 5
264
- autohealing = true
265
- container_runtime = "containerd"
292
+ # Kubernetes Node Pool
293
+ resource "scaleway_k8s_pool" "default_pool" {
294
+ cluster_id = scaleway_k8s_cluster.kapsule.id
295
+ name = "default-pool"
296
+ node_type = "DEV1-M"
297
+ size = 2
298
+ autoscaling = true
299
+ min_size = 2
300
+ max_size = 5
301
+ autohealing = true
302
+ container_runtime = "containerd"
266
303
}
304
+
305
+ # Generate a random suffix for uniqueness
306
+ resource "random_id" "suffix" {
307
+ byte_length = 4
267
308
}
268
309
269
310
# Output Database Connection Information
@@ -272,7 +313,7 @@ Install Terraform and ensure the Scaleway Terraform provider is set up with `ter
272
313
}
273
314
274
315
output "db_port" {
275
- value = scaleway_rdb_instance.database.endpoint[0].port
316
+ value = scaleway_rdb_instance.database.db_host_port
276
317
}
277
318
278
319
output "kubeconfig" {
@@ -345,12 +386,13 @@ You need to create the necessary files for your Node.js application. Here’s a
345
386
const { Pool } = require('pg');
346
387
const app = express();
347
388
389
+ // Get DB credentials from environment variables
348
390
const pool = new Pool({
349
- user : ' postgres ' ,
350
- host : ' node-postgres-db ' , // This matches the service name in Kubernetes
351
- database : ' postgres ' ,
352
- password : ' password ' , // Ensure this matches the password set in the Kubernetes secret
353
- port : 5432 ,
391
+ user : process . env . DB_USER , // 'admin'
392
+ host : process . env . DB_HOST , // '<private-network-db-hostname>'
393
+ database : process . env . DB_NAME , // 'rdb'
394
+ password : process . env . DB_PASSWORD ,
395
+ port : process . env . DB_PORT , // '5432'
354
396
} );
355
397
356
398
app.get('/', async (req, res) => {
@@ -384,7 +426,28 @@ You need to create the necessary files for your Node.js application. Here’s a
384
426
385
427
### Creating Kubernetes manifests for the application
386
428
387
- You need to create two main Kubernetes manifests: one for the deployment and one for the service.
429
+ 1 . Ensure the previously created secret is cleared:
430
+ ```
431
+ kubectl delete secret db-credentials
432
+ ```
433
+
434
+ 2 . Recreate the Secret Using ` kubectl create secret ` . Run the following command without any base64 encoding:
435
+ ```
436
+ kubectl create secret generic db-credentials \
437
+ --from-literal=DB_HOST=<private-network-db-hostname> \
438
+ --from-literal=DB_PORT=5432 \
439
+ --from-literal=DB_NAME=rdb \
440
+ --from-literal=DB_USER=admin \
441
+ --from-literal=DB_PASSWORD=StrongP@ssw0rd123
442
+ ```
443
+ Kubernetes will automatically handle the base64 encoding for you.
444
+
445
+ 3 . Get the secret details:
446
+ ```
447
+ kubectl get secret db-credentials -o yaml
448
+ ```
449
+
450
+ 4 . Create two main Kubernetes manifests: one for the deployment and one for the service.
388
451
389
452
** ` deployment.yaml ` ** :
390
453
``` yaml
@@ -403,24 +466,48 @@ You need to create two main Kubernetes manifests: one for the deployment and one
403
466
app : node-postgres-app
404
467
spec :
405
468
containers :
406
- - name: node-postgres-app
469
+ - name : node-postgres-app
407
470
image : ${YOUR_DOCKER_REGISTRY}/node-postgres-app:latest
408
471
ports :
409
- - containerPort: 8080
472
+ - containerPort : 8080
410
473
env :
411
- - name: POSTGRES_PASSWORD
412
- valueFrom:
413
- secretKeyRef:
414
- name: postgres-secret
415
- key: password
474
+ - name : DB_HOST
475
+ valueFrom :
476
+ secretKeyRef :
477
+ name : db-credentials
478
+ key : DB_HOST
479
+ - name : DB_PORT
480
+ valueFrom :
481
+ secretKeyRef :
482
+ name : db-credentials
483
+ key : DB_PORT
484
+ - name : DB_NAME
485
+ valueFrom :
486
+ secretKeyRef :
487
+ name : db-credentials
488
+ key : DB_NAME
489
+ - name : DB_USER
490
+ valueFrom :
491
+ secretKeyRef :
492
+ name : db-credentials
493
+ key : DB_USER
494
+ - name : DB_PASSWORD
495
+ valueFrom :
496
+ secretKeyRef :
497
+ name : db-credentials
498
+ key : DB_PASSWORD
416
499
---
417
500
apiVersion : v1
418
501
kind : Secret
419
502
metadata :
420
- name: postgres-secret
503
+ name : db-credentials
421
504
type : Opaque
422
505
data :
423
- password: cGFzc3dvcmQ= # base64 encoded password, 'password' in this case
506
+ DB_HOST : <base64-encoded-db-host>
507
+ DB_PORT : <base64-encoded-db-port>
508
+ DB_NAME : <base64-encoded-db-name>
509
+ DB_USER : <base64-encoded-db-user>
510
+ DB_PASSWORD : <base64-encoded-db-password>
424
511
` ` `
425
512
426
513
**` service.yaml`**:
0 commit comments