Skip to content

Commit 1882c11

Browse files
committed
feat(rdb): add tutorial
1 parent a8bca8f commit 1882c11

File tree

2 files changed

+153
-66
lines changed

2 files changed

+153
-66
lines changed

menu/navigation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,7 @@
23802380
},
23812381
{
23822382
"label": "Connecting Managed Databases to Kubernetes clusters",
2383-
"slug": "conecting-managed-databases-to-kubernetes-clusters"
2383+
"slug": "connecting-managed-databases-to-kubernetes-clusters"
23842384
}
23852385
],
23862386
"label": "API/CLI",
Lines changed: 152 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,32 @@ Create a Private Network that both your Kubernetes cluster and database will use
5252
is-ha-cluster=true \
5353
user-name=admin \
5454
password=StrongP@ssw0rd123 \
55-
private-network-id=<private-network-id>
55+
region=fr-par
5656
```
5757

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.
6159

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-
```
7160
<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.
7462
</Message>
7563

7664
3. Add the Private Network endpoint to the database:
7765

7866
```
7967
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>
8281
```
8382

8483
### Creating a Kubernetes Kapsule cluster
@@ -103,8 +102,7 @@ Create a Private Network that both your Kubernetes cluster and database will use
103102
2. Wait for the cluster to be ready, then get the `kubeconfig`:
104103

105104
```
106-
scw k8s kubeconfig install \
107-
cluster-id=<cluster-id>
105+
scw k8s kubeconfig install <k8s-cluster-id> region=fr-par
108106
```
109107

110108
### Creating a Kubernetes secret for database credentials
@@ -125,9 +123,51 @@ Use `kubectl` to create a Kubernetes secret to store the database credentials:
125123
1. Create a Kubernetes deployment that will connect to the database. Save this as `db-app.yaml`:
126124

127125
```
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
129170
```
130-
131171
2. Apply it to your cluster:
132172

133173
```
@@ -227,43 +267,44 @@ Install Terraform and ensure the Scaleway Terraform provider is set up with `ter
227267
228268
# Create Managed PostgreSQL Database
229269
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+
236277
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
238280
}
239281
}
240282
241283
# Kubernetes Cluster (Kapsule)
242284
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
255290
}
256291
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"
266303
}
304+
305+
# Generate a random suffix for uniqueness
306+
resource "random_id" "suffix" {
307+
byte_length = 4
267308
}
268309
269310
# Output Database Connection Information
@@ -272,7 +313,7 @@ Install Terraform and ensure the Scaleway Terraform provider is set up with `ter
272313
}
273314
274315
output "db_port" {
275-
value = scaleway_rdb_instance.database.endpoint[0].port
316+
value = scaleway_rdb_instance.database.db_host_port
276317
}
277318
278319
output "kubeconfig" {
@@ -345,12 +386,13 @@ You need to create the necessary files for your Node.js application. Here’s a
345386
const { Pool } = require('pg');
346387
const app = express();
347388

389+
// Get DB credentials from environment variables
348390
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'
354396
});
355397

356398
app.get('/', async (req, res) => {
@@ -384,7 +426,28 @@ You need to create the necessary files for your Node.js application. Here’s a
384426

385427
### Creating Kubernetes manifests for the application
386428

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.
388451

389452
**`deployment.yaml`**:
390453
```yaml
@@ -403,24 +466,48 @@ You need to create two main Kubernetes manifests: one for the deployment and one
403466
app: node-postgres-app
404467
spec:
405468
containers:
406-
- name: node-postgres-app
469+
- name: node-postgres-app
407470
image: ${YOUR_DOCKER_REGISTRY}/node-postgres-app:latest
408471
ports:
409-
- containerPort: 8080
472+
- containerPort: 8080
410473
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
416499
---
417500
apiVersion: v1
418501
kind: Secret
419502
metadata:
420-
name: postgres-secret
503+
name: db-credentials
421504
type: Opaque
422505
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>
424511
```
425512
426513
**`service.yaml`**:

0 commit comments

Comments
 (0)