@@ -25,10 +25,10 @@ Download or copy the content of `env_variables.sh`:
25
25
[ env_variables.sh] ( env_variables.sh )
26
26
``` shell copy
27
27
# set it to the context name of the k8s cluster
28
- export K8S_CLUSTER_0_CONTEXT_NAME =" <local cluster context>"
28
+ export K8S_CTX =" <local cluster context>"
29
29
30
30
# the following namespace will be created if not exists
31
- export MDB_NAMESPACE =" mongodb"
31
+ export MDB_NS =" mongodb"
32
32
33
33
# minimum required MongoDB version for running MongoDB Search is 8.0.10
34
34
export MDB_VERSION=" 8.0.10"
@@ -63,9 +63,9 @@ Next, install the MongoDB Kubernetes Operator from the Helm repository you just
63
63
64
64
[ code_snippets/0100_install_operator.sh] ( code_snippets/0100_install_operator.sh )
65
65
``` shell copy
66
- helm upgrade --install --debug --kube-context " ${K8S_CLUSTER_0_CONTEXT_NAME } " \
66
+ helm upgrade --install --debug --kube-context " ${K8S_CTX } " \
67
67
--create-namespace \
68
- --namespace=" ${MDB_NAMESPACE } " \
68
+ --namespace=" ${MDB_NS } " \
69
69
mongodb-kubernetes \
70
70
--set " ${OPERATOR_ADDITIONAL_HELM_VALUES:- " dummy=value" } " \
71
71
" ${OPERATOR_HELM_CHART} "
@@ -82,15 +82,15 @@ MongoDB requires authentication for secure access. This step creates two Kuberne
82
82
83
83
[ code_snippets/0305_create_mongodb_community_user_secrets.sh] ( code_snippets/0305_create_mongodb_community_user_secrets.sh )
84
84
``` shell copy
85
- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " --namespace " ${MDB_NAMESPACE } " \
85
+ kubectl --context " ${K8S_CTX } " --namespace " ${MDB_NS } " \
86
86
create secret generic mdb-admin-user-password \
87
87
--from-literal=password=" ${MDB_ADMIN_USER_PASSWORD} "
88
88
89
- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " --namespace " ${MDB_NAMESPACE } " \
89
+ kubectl --context " ${K8S_CTX } " --namespace " ${MDB_NS } " \
90
90
create secret generic mdbc-rs-search-sync-source-password \
91
91
--from-literal=password=" ${MDB_SEARCH_SYNC_USER_PASSWORD} "
92
92
93
- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " --namespace " ${MDB_NAMESPACE } " \
93
+ kubectl --context " ${K8S_CTX } " --namespace " ${MDB_NS } " \
94
94
create secret generic mdb-user-password \
95
95
--from-literal=password=" ${MDB_USER_PASSWORD} "
96
96
@@ -106,7 +106,7 @@ Now, deploy MongoDB Community by creating a `MongoDBCommunity` custom resource n
106
106
107
107
[ code_snippets/0310_create_mongodb_community_resource.sh] ( code_snippets/0310_create_mongodb_community_resource.sh )
108
108
``` yaml copy
109
- kubectl apply --context "${K8S_CLUSTER_0_CONTEXT_NAME }" -n "${MDB_NAMESPACE }" -f - <<EOF
109
+ kubectl apply --context "${K8S_CTX }" -n "${MDB_NS }" -f - <<EOF
110
110
apiVersion : mongodbcommunity.mongodb.com/v1
111
111
kind : MongoDBCommunity
112
112
metadata :
@@ -147,7 +147,8 @@ spec:
147
147
# admin user with root role
148
148
- name : mdb-admin
149
149
db : admin
150
- passwordSecretRef : # a reference to the secret containing user password
150
+ # a reference to the secret containing user password
151
+ passwordSecretRef :
151
152
name : mdb-admin-user-password
152
153
scramCredentialsSecretName : mdb-admin-user
153
154
roles :
@@ -156,20 +157,25 @@ spec:
156
157
# user performing search queries
157
158
- name : mdb-user
158
159
db : admin
159
- passwordSecretRef : # a reference to the secret containing user password
160
+ # a reference to the secret containing user password
161
+ passwordSecretRef :
160
162
name : mdb-user-password
161
163
scramCredentialsSecretName : mdb-user-scram
162
164
roles :
163
165
- name : restore
164
166
db : sample_mflix
165
167
- name : readWrite
166
168
db : sample_mflix
167
- # user used by MongoDB Search to connect to MongoDB database to synchronize data from
168
- # For MongoDB <8.2, the operator will be creating the searchCoordinator custom role automatically
169
- # From MongoDB 8.2, searchCoordinator role will be a built-in role.
169
+ # user used by MongoDB Search to connect to MongoDB database to
170
+ # synchronize data from.
171
+ # For MongoDB <8.2, the operator will be creating the
172
+ # searchCoordinator custom role automatically.
173
+ # From MongoDB 8.2, searchCoordinator role will be a
174
+ # built-in role.
170
175
- name : search-sync-source
171
176
db : admin
172
- passwordSecretRef : # a reference to the secret that will be used to generate the user's password
177
+ # a reference to the secret that will be used to generate the user's password
178
+ passwordSecretRef :
173
179
name : mdbc-rs-search-sync-source-password
174
180
scramCredentialsSecretName : mdbc-rs-search-sync-source
175
181
roles :
@@ -185,11 +191,12 @@ After applying the `MongoDBCommunity` custom resource, the operator begins deplo
185
191
[ code_snippets/0315_wait_for_community_resource.sh] ( code_snippets/0315_wait_for_community_resource.sh )
186
192
``` shell copy
187
193
echo " Waiting for MongoDBCommunity resource to reach Running phase..."
188
- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME} " -n " ${MDB_NAMESPACE} " wait --for=jsonpath=' {.status.phase}' =Running mdbc/mdbc-rs --timeout=400s
194
+ kubectl --context " ${K8S_CTX} " -n " ${MDB_NS} " wait \
195
+ --for=jsonpath=' {.status.phase}' =Running mdbc/mdbc-rs --timeout=400s
189
196
echo ; echo " MongoDBCommunity resource"
190
- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " -n " ${MDB_NAMESPACE } " get mdbc/mdbc-rs
191
- echo ; echo " Pods running in cluster ${K8S_CLUSTER_0_CONTEXT_NAME } "
192
- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " -n " ${MDB_NAMESPACE } " get pods
197
+ kubectl --context " ${K8S_CTX } " -n " ${MDB_NS } " get mdbc/mdbc-rs
198
+ echo ; echo " Pods running in cluster ${K8S_CTX } "
199
+ kubectl --context " ${K8S_CTX } " -n " ${MDB_NS } " get pods
193
200
```
194
201
195
202
### 7. Create MongoDB Search Resource
@@ -201,7 +208,7 @@ Note: Public Preview of MongoDB Community Search comes with some limitations, an
201
208
202
209
[ code_snippets/0320_create_mongodb_search_resource.sh] ( code_snippets/0320_create_mongodb_search_resource.sh )
203
210
``` shell copy
204
- kubectl apply --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " -n " ${MDB_NAMESPACE } " -f - << EOF
211
+ kubectl apply --context " ${K8S_CTX } " -n " ${MDB_NS } " -f - << EOF
205
212
apiVersion: mongodb.com/v1
206
213
kind: MongoDBSearch
207
214
metadata:
@@ -236,7 +243,8 @@ Similar to the MongoDB deployment, the Search deployment needs time to initializ
236
243
[code_snippets/0325_wait_for_search_resource.sh](code_snippets/0325_wait_for_search_resource.sh)
237
244
` ` ` shell copy
238
245
echo "Waiting for MongoDBSearch resource to reach Running phase..."
239
- kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" wait --for=jsonpath='{.status.phase}'=Running mdbs/mdbc-rs --timeout=300s
246
+ kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait \
247
+ --for=jsonpath='{.status.phase}'=Running mdbs/mdbc-rs --timeout=300s
240
248
` ` `
241
249
This command polls the status of the `MongoDBSearch` resource `mdbc-rs`.
242
250
@@ -247,7 +255,8 @@ Double-check the status of your `MongoDBCommunity` resource to ensure it remains
247
255
[code_snippets/0330_wait_for_community_resource.sh](code_snippets/0330_wait_for_community_resource.sh)
248
256
` ` ` shell copy
249
257
echo "Waiting for MongoDBCommunity resource to reach Running phase..."
250
- kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" wait --for=jsonpath='{.status.phase}'=Running mdbc/mdbc-rs --timeout=400s
258
+ kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait \
259
+ --for=jsonpath='{.status.phase}'=Running mdbc/mdbc-rs --timeout=400s
251
260
` ` `
252
261
This provides a final confirmation that the core database is operational.
253
262
@@ -258,11 +267,11 @@ View all the running pods in your namespace. You should see pods for the MongoDB
258
267
[code_snippets/0335_show_running_pods.sh](code_snippets/0335_show_running_pods.sh)
259
268
` ` ` shell copy
260
269
echo; echo "MongoDBCommunity resource"
261
- kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME }" -n "${MDB_NAMESPACE }" get mdbc/mdbc-rs
270
+ kubectl --context "${K8S_CTX }" -n "${MDB_NS }" get mdbc/mdbc-rs
262
271
echo; echo "MongoDBSearch resource"
263
- kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME }" -n "${MDB_NAMESPACE }" get mdbs/mdbc-rs
264
- echo; echo "Pods running in cluster ${K8S_CLUSTER_0_CONTEXT_NAME }"
265
- kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME }" -n "${MDB_NAMESPACE }" get pods
272
+ kubectl --context "${K8S_CTX }" -n "${MDB_NS }" get mdbs/mdbc-rs
273
+ echo; echo "Pods running in cluster ${K8S_CTX }"
274
+ kubectl --context "${K8S_CTX }" -n "${MDB_NS }" get pods
266
275
` ` `
267
276
268
277
# # Using MongoDB Search
@@ -275,9 +284,7 @@ To interact with your MongoDB deployment, this step deploys a utility pod named
275
284
276
285
[code_snippets/0410_run_mongodb_tools_pod.sh](code_snippets/0410_run_mongodb_tools_pod.sh)
277
286
` ` ` shell copy
278
- #!/bin/bash
279
-
280
- kubectl apply -n "${MDB_NAMESPACE}" --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -f - <<EOF
287
+ kubectl apply -n "${MDB_NS}" --context "${K8S_CTX}" -f - <<EOF
281
288
apiVersion: v1
282
289
kind: Pod
283
290
metadata:
@@ -294,7 +301,8 @@ spec:
294
301
EOF
295
302
296
303
echo "Waiting for the mongodb-tools to be ready..."
297
- kubectl wait --for=condition=Ready pod/mongodb-tools-pod -n "${MDB_NAMESPACE}" --context "${K8S_CLUSTER_0_CONTEXT_NAME}" --timeout=60s
304
+ kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait \
305
+ --for=condition=Ready pod/mongodb-tools-pod --timeout=60s
298
306
` ` `
299
307
300
308
# ## 12. Import Sample Data
@@ -303,14 +311,18 @@ To test the search functionality, this step imports the `sample_mflix.movies` co
303
311
304
312
[code_snippets/0420_import_movies_mflix_database.sh](code_snippets/0420_import_movies_mflix_database.sh)
305
313
` ` ` shell copy
306
- #!/bin/bash
307
-
308
- kubectl exec -n "${MDB_NAMESPACE}" --context "${K8S_CLUSTER_0_CONTEXT_NAME}" mongodb-tools-pod -- /bin/bash -eu -c "$(cat <<EOF
314
+ kubectl exec -n "${MDB_NS}" --context "${K8S_CTX}" \
315
+ mongodb-tools-pod -- /bin/bash -eu -c "$(cat <<EOF
309
316
echo "Downloading sample database archive..."
310
- curl https://atlas-education.s3.amazonaws.com/sample_mflix.archive -o /tmp/sample_mflix.archive
317
+ curl https://atlas-education.s3.amazonaws.com/sample_mflix.archive \
318
+ -o /tmp/sample_mflix.archive
311
319
echo "Restoring sample database"
312
- mongorestore --archive=/tmp/sample_mflix.archive --verbose=1 --drop --nsInclude 'sample_mflix.*' \
313
- --uri="mongodb://mdb-user:${MDB_USER_PASSWORD}@mdbc-rs-0.mdbc-rs-svc.${MDB_NAMESPACE}.svc.cluster.local:27017/?replicaSet=mdbc-rs"
320
+ mongorestore \
321
+ --archive=/tmp/sample_mflix.archive \
322
+ --verbose=1 \
323
+ --drop \
324
+ --nsInclude 'sample_mflix.*' \
325
+ --uri="mongodb://mdb-user:${MDB_USER_PASSWORD}@mdbc-rs-0.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017/?replicaSet=mdbc-rs"
314
326
EOF
315
327
)"
316
328
` ` `
@@ -322,10 +334,9 @@ Before performing search queries, create a search index. This step uses `kubectl
322
334
323
335
[code_snippets/0430_create_search_index.sh](code_snippets/0430_create_search_index.sh)
324
336
` ` ` shell copy
325
- #!/bin/bash
326
-
327
- kubectl exec --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" mongodb-tools-pod -- \
328
- mongosh --quiet "mongodb://mdb-user:${MDB_USER_PASSWORD}@mdbc-rs-0.mdbc-rs-svc.${MDB_NAMESPACE}.svc.cluster.local:27017/?replicaSet=mdbc-rs" \
337
+ kubectl exec --context "${K8S_CTX}" -n "${MDB_NS}" mongodb-tools-pod -- \
338
+ mongosh --quiet \
339
+ "mongodb://mdb-user:${MDB_USER_PASSWORD}@mdbc-rs-0.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017/?replicaSet=mdbc-rs" \
329
340
--eval "use sample_mflix" \
330
341
--eval 'db.movies.createSearchIndex("default", { mappings: { dynamic: true } });'
331
342
` ` `
@@ -336,8 +347,6 @@ Creating a search index is an asynchronous operation. This script polls periodic
336
347
337
348
[code_snippets/0440_wait_for_search_index_ready.sh](code_snippets/0440_wait_for_search_index_ready.sh)
338
349
` ` ` shell copy
339
- #!/bin/bash
340
-
341
350
# Currently it's not possible to check the status of search indexes, we need to just wait
342
351
echo "Sleeping to wait for search indexes to be created"
343
352
sleep 60
@@ -349,8 +358,6 @@ Once the search index is ready, execute search queries using the `$search` aggre
349
358
350
359
[code_snippets/0450_execute_search_query.sh](code_snippets/0450_execute_search_query.sh)
351
360
` ` ` shell copy
352
- #!/bin/bash
353
-
354
361
mdb_script=$(cat <<'EOF'
355
362
use sample_mflix;
356
363
db.movies.aggregate([
@@ -391,9 +398,10 @@ db.movies.aggregate([
391
398
EOF
392
399
)
393
400
394
- kubectl exec --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" mongodb-tools-pod -- /bin/bash -eu -c "$(cat <<EOF
401
+ kubectl exec --context "${K8S_CTX}" -n "${MDB_NS}" \
402
+ mongodb-tools-pod -- /bin/bash -eu -c "$(cat <<EOF
395
403
echo '${mdb_script}' > /tmp/mdb_script.js
396
- mongosh --quiet "mongodb://mdb-user:${MDB_USER_PASSWORD}@mdbc-rs-0.mdbc-rs-svc.${MDB_NAMESPACE }.svc.cluster.local:27017/?replicaSet=mdbc-rs" < /tmp/mdb_script.js
404
+ mongosh --quiet "mongodb://mdb-user:${MDB_USER_PASSWORD}@mdbc-rs-0.mdbc-rs-svc.${MDB_NS }.svc.cluster.local:27017/?replicaSet=mdbc-rs" < /tmp/mdb_script.js
397
405
EOF
398
406
)"
399
- ` ` `
407
+ ` ` `
0 commit comments