|
2 | 2 |
|
3 | 3 | This recipe provisions a MongoDB instance on Kubernetes using Terraform. |
4 | 4 |
|
| 5 | +--- |
| 6 | + |
5 | 7 | ## Inputs |
6 | 8 |
|
7 | | -- `name` (string, required): MongoDB instance name |
8 | | -- `version` (string, default: 6.0): MongoDB version |
9 | | -- `replicas` (int, default: 1): Number of replicas |
10 | | -- `storage_size` (string, default: 10Gi): PVC size |
11 | | -- `storage_class` (string, default: standard): Storage class |
12 | | -- `username` (string, default: admin): Admin username |
13 | | -- `password` (string, required): Admin password |
14 | | -- `persistence` (bool, default: true): Enable persistence |
15 | | -- `backup_enabled` (bool, default: false): Enable backups |
16 | | -- `backup_schedule` (string): Cron schedule for backups |
17 | | -- `resources` (object): CPU/memory requests/limits |
| 9 | +| Variable | Type | Default | Description | |
| 10 | +|-------------------|--------|-------------|------------------------------------| |
| 11 | +| `name` | string | required | MongoDB instance name | |
| 12 | +| `version` | string | 6.0 | MongoDB version | |
| 13 | +| `replicas` | int | 1 | Number of replicas | |
| 14 | +| `storage_size` | string | 10Gi | PVC size | |
| 15 | +| `storage_class` | string | standard | Kubernetes storage class | |
| 16 | +| `username` | string | admin | Admin username | |
| 17 | +| `password` | string | required | Admin password | |
| 18 | +| `persistence` | bool | true | Enable persistence | |
| 19 | +| `backup_enabled` | bool | false | Enable backups | |
| 20 | +| `backup_schedule` | string | "" | Cron schedule for backups | |
| 21 | +| `resources` | object | {} | CPU/memory requests and limits | |
| 22 | + |
| 23 | +> **Note:** For CI or ephemeral testing, `persistence` should be set to `false` to avoid PVC-related delays in Kind clusters. |
| 24 | +
|
| 25 | +--- |
18 | 26 |
|
19 | 27 | ## Outputs |
20 | 28 |
|
21 | 29 | - `mongodb_service_name`: Kubernetes service name |
22 | 30 | - `mongodb_credentials_secret`: Secret containing credentials |
23 | 31 |
|
| 32 | +--- |
| 33 | + |
24 | 34 | ## Manual Testing |
25 | 35 |
|
26 | 36 | 1. Apply Terraform: |
27 | 37 | ```bash |
28 | 38 | terraform init |
29 | | - terraform apply -var="name=mydb" -var="password=MySecretPass123" |
| 39 | + terraform apply \ |
| 40 | + -var="name=mydb" \ |
| 41 | + -var="password=MySecretPass123" \ |
| 42 | + -auto-approve |
30 | 43 | ``` |
31 | 44 |
|
32 | | -2. Connect to MongoDB: |
| 45 | +2. Check pod status: |
| 46 | + ```bash |
| 47 | + kubectl get pods |
| 48 | + # Ensure test-mongodb-0 is READY=1/1 |
| 49 | + ``` |
| 50 | + |
| 51 | +3. Connect to MongoDB: |
33 | 52 | ```bash |
34 | 53 | kubectl run mongo-client --rm -it --image=mongo -- \ |
35 | 54 | mongo "mongodb://$(kubectl get svc mydb-svc -o jsonpath='{.spec.clusterIP}'):27017" \ |
36 | 55 | -u admin -p MySecretPass123 |
37 | 56 | ``` |
38 | 57 |
|
39 | | -3. Verify database operations. |
| 58 | +4. Verify database operations. |
| 59 | + |
| 60 | +5. Clean up (optional): |
| 61 | + ```bash |
| 62 | + terraform destroy -var="name=mydb" -var="password=MySecretPass123" -auto-approve |
| 63 | + ``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +## CI / GitHub Actions Testing |
| 68 | + |
| 69 | +This recipe is automatically tested in GitHub Actions for pull requests and branch pushes. |
| 70 | + |
| 71 | +- A temporary Kind cluster is created. |
| 72 | +- Terraform applies the recipe with `persistence=false` for fast ephemeral testing. |
| 73 | +- The workflow waits for MongoDB pods to become ready. |
| 74 | +- Terraform destroys the resources and deletes the Kind cluster after the test. |
| 75 | + |
| 76 | +Workflow file: `.github/workflows/test-mongodb-recipe.yml` |
| 77 | + |
| 78 | +Example ephemeral test run: |
| 79 | +```bash |
| 80 | +terraform apply -var="name=test-mongodb" -var="password=MySecretPass123" -var="persistence=false" -auto-approve |
| 81 | +kubectl get pods |
| 82 | +terraform destroy -var="name=test-mongodb" -var="password=MySecretPass123" -var="persistence=false" -auto-approve |
| 83 | +``` |
| 84 | + |
| 85 | +> Make sure the MongoDB pod shows `READY=1/1` before connecting. |
| 86 | +
|
| 87 | +--- |
| 88 | + |
| 89 | +## References |
| 90 | + |
| 91 | +- Radius MongoDB Resource Schema: https://docs.radapp.io/reference/resource-schema/databases/mongodb/ |
| 92 | +- Example Kubernetes Recipe: https://github.com/radius-project/recipes/blob/main/local-dev/mongodatabases.bicep |
| 93 | + |
0 commit comments