Skip to content

Commit d529969

Browse files
Merge pull request #18 from amanmallsops/feature/primary_services
created primary services in mongodb, added resources limit and request and affinity on all module
2 parents 01bd842 + 9539896 commit d529969

File tree

9 files changed

+155
-47
lines changed

9 files changed

+155
-47
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,27 @@ module "mongodb" {
9898

9999
## IAM Permissions
100100
The required IAM permissions to create resources from this module can be found [here](https://github.com/squareops/terraform-kubernetes-mongodb/blob/main/IAM.md)
101-
101+
## Mongo Backup and Restore
102+
This module provides functionality to automate the backup and restore process for mongo databases using AWS S3 buckets. It allows users to easily schedule backups, restore databases from backups stored in S3, and manage access permissions using AWS IAM roles.
103+
Features
104+
### Backup
105+
- Users can schedule full backups.
106+
- Backups are stored in specified S3 buckets.
107+
### Restore
108+
- Users can restore Mongo databases from backups stored in S3 buckets.
109+
- Supports specifying the backup file to restore from and the target S3 bucket region.
110+
### IAM Role for Permissions
111+
- Users need to provide an IAM role for the module to access the specified S3 bucket and perform backup and restore operations.
112+
## Module Inputs
113+
### Backup Configuration
114+
- bucket_uri: The URI of the S3 bucket where backups will be stored.
115+
- s3_bucket_region: The region of the S3 bucket.
116+
- cron_for_full_backup: The cron expression for scheduling full backups.
117+
### Restore Configuration
118+
- mongodb_restore_config: Configuration for restoring databases.
119+
- bucket_uri: The URI of the S3 bucket containing the backup file.
120+
- file_name: The name of the backup file to restore.
121+
- s3_bucket_region: The region of the S3 bucket containing the backup file.
102122
## Important Notes
103123
1. In order to enable the exporter, it is required to deploy Prometheus/Grafana first.
104124
2. The exporter is a tool that extracts metrics data from an application or system and makes it available to be scraped by Prometheus.

examples/complete/aws/helm/exporter.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ resources:
1313
memory: 200Mi
1414
requests:
1515
cpu: 50m
16-
memory: 100Mi
16+
memory: 100Mi

examples/complete/aws/helm/values.yaml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,44 @@ affinity:
77
operator: In
88
values:
99
- "true"
10+
11+
resources:
12+
limits:
13+
cpu: 900m
14+
memory: 800Mi
15+
requests:
16+
cpu: 600m
17+
memory: 500Mi
18+
19+
metrics:
20+
enabled: true
21+
resources:
22+
limits:
23+
cpu: 200m
24+
memory: 528Mi
25+
requests:
26+
cpu: 120m
27+
memory: 228Mi
28+
29+
arbiter:
30+
resources:
31+
limits:
32+
cpu: 200m
33+
memory: 400Mi
34+
requests:
35+
cpu: 100m
36+
memory: 250Mi
37+
38+
affinity:
39+
nodeAffinity:
40+
requiredDuringSchedulingIgnoredDuringExecution:
41+
nodeSelectorTerms:
42+
- matchExpressions:
43+
- key: "Addons-Services"
44+
operator: In
45+
values:
46+
- "true"
47+
1048
backupjob:
1149
resources:
1250
requests:
@@ -23,4 +61,4 @@ restorejob:
2361
cpu: 50m
2462
limits:
2563
memory: 200Mi
26-
cpu: 100m
64+
cpu: 100m

helm/values/exporter/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ livenessProbe:
2424

2525
# [mongodb[+srv]://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
2626
mongodb:
27-
uri: "mongodb://mongodb_exporter:${mongodb_exporter_password}@mongodb-0.mongodb-headless.{namespace}.svc.cluster.local:27017/admin?authSource=admin"
27+
uri: "mongodb://mongodb_exporter:${mongodb_exporter_password}@mongodb-primary.{namespace}.svc.cluster.local:27017/admin?authSource=admin"
2828

2929
# Name of an externally managed secret (in the same namespace) containing the connection uri as key `mongodb-uri`.
3030
# If this is provided, the value mongodb.uri is ignored.

helm/values/mongodb/values.yaml

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,29 @@ clusterDomain: cluster.local
6767
## app.kubernetes.io/name: mongodb
6868
## primary: "true"
6969
##
70-
extraDeploy: []
70+
extraDeploy:
71+
- apiVersion: v1
72+
kind: Service
73+
metadata:
74+
name: mongodb-primary
75+
namespace: ${namespace}
76+
labels:
77+
app.kubernetes.io/component: mongodb
78+
app.kubernetes.io/instance: mongodb
79+
app.kubernetes.io/managed-by: Helm
80+
app.kubernetes.io/name: mongodb
81+
spec:
82+
type: ClusterIP
83+
ports:
84+
- name: mongodb-primary
85+
port: 27017
86+
protocol: TCP
87+
targetPort: mongodb
88+
selector:
89+
app.kubernetes.io/component: mongodb
90+
app.kubernetes.io/instance: mongodb
91+
app.kubernetes.io/name: mongodb
92+
primary: "true"
7193
## @param commonLabels Add labels to all the deployed resources (sub-charts are not considered). Evaluated as a template
7294
##
7395
commonLabels: {}
@@ -502,23 +524,23 @@ containerSecurityContext:
502524
## @param resources.limits The resources limits for MongoDB(®) containers
503525
## @param resources.requests The requested resources for MongoDB(®) containers
504526
##
505-
resources:
527+
resources: {}
506528
## Example:
507529
## limits:
508530
## cpu: 100m
509531
## memory: 128Mi
510532
##
511-
limits:
512-
cpu: 1000m
513-
memory: 1000Mi
533+
# limits:
534+
# cpu: 1000m
535+
# memory: 1000Mi
514536
## Examples:
515537
## requests:
516538
## cpu: 100m
517539
## memory: 128Mi
518540
##
519-
requests:
520-
cpu: 500m
521-
memory: 500Mi
541+
# requests:
542+
# cpu: 500m
543+
# memory: 500Mi
522544
## @param containerPorts.mongodb MongoDB(®) container port
523545
containerPorts:
524546
mongodb: 27017
@@ -620,7 +642,24 @@ initContainers: []
620642
## - name: DEBUG
621643
## value: "true"
622644
##
623-
sidecars: []
645+
sidecars:
646+
- name: mongo-labeler
647+
image: korenlev/k8s-mongo-labeler-sidecar
648+
imagePullPolicy: Always
649+
env:
650+
- name: LABEL_SELECTOR
651+
value: "app.kubernetes.io/component=mongodb,app.kubernetes.io/instance=mongodb,app.kubernetes.io/name=mongodb"
652+
- name: NAMESPACE
653+
value: ${namespace}
654+
- name: DEBUG
655+
value: "true"
656+
resources:
657+
limits:
658+
cpu: 100m
659+
memory: 200Mi
660+
requests:
661+
cpu: 50m
662+
memory: 100Mi
624663
## @param extraVolumeMounts Optionally specify extra list of additional volumeMounts for the MongoDB(®) container(s)
625664
## Examples:
626665
## extraVolumeMounts:
@@ -996,7 +1035,7 @@ rbac:
9961035
## that allows MongoDB(®) pods querying the K8s API
9971036
## this needs to be set to 'true' to enable the mongo-labeler sidecar primary mongodb discovery
9981037
##
999-
create: false
1038+
create: true
10001039
## @param rbac.rules Custom rules to create following the role specification
10011040
## The example below needs to be uncommented to use the 'mongo-labeler' sidecar for dynamic discovery of the primary mongodb pod:
10021041
## rules:
@@ -1010,7 +1049,16 @@ rbac:
10101049
## - watch
10111050
## - update
10121051
##
1013-
rules: []
1052+
rules:
1053+
- apiGroups:
1054+
- ""
1055+
resources:
1056+
- pods
1057+
verbs:
1058+
- get
1059+
- list
1060+
- watch
1061+
- update
10141062
## PodSecurityPolicy configuration
10151063
## Be sure to also set rbac.create to true, otherwise Role and RoleBinding won't be created.
10161064
## ref: https://kubernetes.io/docs/concepts/policy/pod-security-policy/
@@ -1304,23 +1352,23 @@ arbiter:
13041352
## @param arbiter.resources.limits The resources limits for Arbiter containers
13051353
## @param arbiter.resources.requests The requested resources for Arbiter containers
13061354
##
1307-
resources:
1355+
resources: {}
13081356
## Example:
13091357
## limits:
13101358
## cpu: 100m
13111359
## memory: 128Mi
13121360
##
1313-
limits:
1314-
cpu: 200m
1315-
memory: 500Mi
1361+
# limits:
1362+
# cpu: 200m
1363+
# memory: 500Mi
13161364
## Examples:
13171365
## requests:
13181366
## cpu: 100m
13191367
## memory: 128Mi
13201368
##
1321-
requests:
1322-
cpu: 100m
1323-
memory: 250Mi
1369+
# requests:
1370+
# cpu: 100m
1371+
# memory: 250Mi
13241372
## @param arbiter.containerPorts.mongodb MongoDB(®) arbiter container port
13251373
##
13261374
containerPorts:
@@ -1866,19 +1914,14 @@ metrics:
18661914
## @param metrics.resources.limits The resources limits for Prometheus exporter containers
18671915
## @param metrics.resources.requests The requested resources for Prometheus exporter containers
18681916
##
1869-
resources:
1870-
## Example:
1871-
## limits:
1872-
## cpu: 100m
1873-
## memory: 128Mi
1874-
##
1875-
limits: {}
1876-
## Examples:
1877-
## requests:
1878-
## cpu: 100m
1879-
## memory: 128Mi
1880-
##
1881-
requests: {}
1917+
resources: {}
1918+
# limits:
1919+
# cpu: 100m
1920+
# memory: 128Mi
1921+
# requests:
1922+
# cpu: 100m
1923+
# memory: 128Mi
1924+
##
18821925
## @param metrics.containerPort Port of the Prometheus metrics container
18831926
##
18841927
containerPort: 9216
@@ -1929,9 +1972,9 @@ metrics:
19291972
##
19301973
readinessProbe:
19311974
enabled: true
1932-
initialDelaySeconds: 5
1975+
initialDelaySeconds: 30
19331976
periodSeconds: 5
1934-
timeoutSeconds: 1
1977+
timeoutSeconds: 15
19351978
failureThreshold: 3
19361979
successThreshold: 1
19371980
## Slow starting containers can be protected through startup probes

main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ resource "helm_release" "mongodb_backup" {
6767
azure_storage_account_key = var.bucket_provider_type == "azure" ? var.azure_storage_account_key : ""
6868
azure_container_name = var.bucket_provider_type == "azure" ? var.azure_container_name : ""
6969
annotations = var.bucket_provider_type == "s3" ? "eks.amazonaws.com/role-arn : ${var.iam_role_arn_backup}" : var.bucket_provider_type == "gcs" ? "iam.gke.io/gcp-service-account: ${var.service_account_backup}" : var.bucket_provider_type == "azure" ? "azure.workload.identity/client-id: ${var.az_account_backup}" : ""
70-
})
70+
}),
71+
var.mongodb_config.values_yaml
7172
]
7273
}
7374

@@ -90,7 +91,8 @@ resource "helm_release" "mongodb_restore" {
9091
azure_storage_account_key = var.bucket_provider_type == "azure" ? var.azure_storage_account_key : ""
9192
azure_container_name = var.bucket_provider_type == "azure" ? var.azure_container_name : ""
9293
annotations = var.bucket_provider_type == "s3" ? "eks.amazonaws.com/role-arn : ${var.iam_role_arn_restore}" : var.bucket_provider_type == "gcs" ? "iam.gke.io/gcp-service-account: ${var.service_account_restore}" : var.bucket_provider_type == "azure" ? "azure.workload.identity/client-id: ${var.az_account_restore}" : ""
93-
})
94+
}),
95+
var.mongodb_config.values_yaml
9496
]
9597
}
9698

modules/backup/templates/fullbackup-cronjob.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ spec:
1313
spec:
1414
template:
1515
spec:
16-
affinity: {{ .Values.affinity | toYaml | nindent 10 }}
16+
affinity:
17+
{{- toYaml .Values.affinity | nindent 12 }}
1718
restartPolicy: OnFailure
1819
serviceAccountName: sa-mongo-backup
1920
containers:
2021
- name: backup-mongodb
2122
image: squareops/mongodb-backup:v7
2223
env:
2324
- name: MONGODB_URI
24-
value: mongodb://{{ .Values.auth.rootUser }}:{{ .Values.auth.rootPassword }}@mongodb-0.mongodb-headless.{{ .Release.Namespace }}.svc.cluster.local:27017
25+
value: mongodb://{{ .Values.auth.rootUser }}:{{ .Values.auth.rootPassword }}@mongodb-headless.{{ .Release.Namespace }}.svc.cluster.local:27017
2526
- name: MONGO_OPLOG
2627
value: "true"
2728
- name: MONGO_BUCKET_URI
@@ -33,4 +34,5 @@ spec:
3334
value: {{ .Values.bucket_provider_type }}
3435
- name: AWS_DEFAULT_REGION
3536
value: {{ .Values.backup.aws_default_region }}
36-
resources: {{ .Values.backupjob.resources | toYaml | nindent 12 }}
37+
resources:
38+
{{- toYaml .Values.backupjob.resources | nindent 14 }}

modules/restore/templates/job.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ metadata:
55
spec:
66
template:
77
spec:
8-
affinity: {{ .Values.affinity | toYaml | nindent 6 }}
8+
affinity:
9+
{{- toYaml .Values.affinity | nindent 8 }}
910
serviceAccountName: sa-mongo-restore
1011
containers:
1112
- name: restore-mongodb
1213
image: squareops/mongodb-restore:v6
1314
env:
1415
- name: MONGO_URI
15-
value: mongodb://{{ .Values.auth.rootUser }}:{{ .Values.auth.rootPassword }}@mongodb-0.mongodb-headless.{{ .Release.Namespace }}.svc.cluster.local:27017
16+
value: mongodb://{{ .Values.auth.rootUser }}:{{ .Values.auth.rootPassword }}@mongodb-primary.{{ .Release.Namespace }}.svc.cluster.local:27017
1617
- name: MONGO_OPLOG
1718
value: "false"
1819
- name: AWS_DEFAULT_REGION
@@ -26,6 +27,7 @@ spec:
2627
value: {{ .Values.restore.file_name | quote }}
2728
- name: RESTORE_FROM
2829
value: {{ .Values.bucket_provider_type}}
29-
resources: {{ .Values.restorejob.resources | toYaml | nindent 12 }}
30+
resources:
31+
{{- toYaml .Values.restorejob.resources | nindent 14 }}
3032
restartPolicy: Never
3133
backoffLimit: 4

output.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
output "mongodb_endpoints" {
22
description = "MongoDB endpoints in the Kubernetes cluster."
33
value = {
4-
mongoport = "27017",
5-
mongodb_endpoint = "mongodb-headless.${var.namespace}.svc.cluster.local"
4+
mongoport = "27017",
5+
mongodb_headless_endpoint = "mongodb-headless.${var.namespace}.svc.cluster.local"
6+
mongodb_primary_endpoint = "mongodb-primary.${var.namespace}.svc.cluster.local"
67
}
78
}
89

0 commit comments

Comments
 (0)