@@ -25,10 +25,10 @@ Download or copy the content of `env_variables.sh`:
2525[ env_variables.sh] ( env_variables.sh )
2626``` shell copy
2727# 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>"
2929
3030# the following namespace will be created if not exists
31- export MDB_NAMESPACE =" mongodb"
31+ export MDB_NS =" mongodb"
3232
3333# minimum required MongoDB version for running MongoDB Search is 8.0.10
3434export MDB_VERSION=" 8.0.10"
@@ -63,9 +63,9 @@ Next, install the MongoDB Kubernetes Operator from the Helm repository you just
6363
6464[ code_snippets/0100_install_operator.sh] ( code_snippets/0100_install_operator.sh )
6565``` shell copy
66- helm upgrade --install --debug --kube-context " ${K8S_CLUSTER_0_CONTEXT_NAME } " \
66+ helm upgrade --install --debug --kube-context " ${K8S_CTX } " \
6767 --create-namespace \
68- --namespace=" ${MDB_NAMESPACE } " \
68+ --namespace=" ${MDB_NS } " \
6969 mongodb-kubernetes \
7070 --set " ${OPERATOR_ADDITIONAL_HELM_VALUES:- " dummy=value" } " \
7171 " ${OPERATOR_HELM_CHART} "
@@ -82,15 +82,15 @@ MongoDB requires authentication for secure access. This step creates two Kuberne
8282
8383[ code_snippets/0305_create_mongodb_community_user_secrets.sh] ( code_snippets/0305_create_mongodb_community_user_secrets.sh )
8484``` shell copy
85- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " --namespace " ${MDB_NAMESPACE } " \
85+ kubectl --context " ${K8S_CTX } " --namespace " ${MDB_NS } " \
8686 create secret generic mdb-admin-user-password \
8787 --from-literal=password=" ${MDB_ADMIN_USER_PASSWORD} "
8888
89- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " --namespace " ${MDB_NAMESPACE } " \
89+ kubectl --context " ${K8S_CTX } " --namespace " ${MDB_NS } " \
9090 create secret generic mdbc-rs-search-sync-source-password \
9191 --from-literal=password=" ${MDB_SEARCH_SYNC_USER_PASSWORD} "
9292
93- kubectl --context " ${K8S_CLUSTER_0_CONTEXT_NAME } " --namespace " ${MDB_NAMESPACE } " \
93+ kubectl --context " ${K8S_CTX } " --namespace " ${MDB_NS } " \
9494 create secret generic mdb-user-password \
9595 --from-literal=password=" ${MDB_USER_PASSWORD} "
9696
@@ -106,7 +106,7 @@ Now, deploy MongoDB Community by creating a `MongoDBCommunity` custom resource n
106106
107107[ code_snippets/0310_create_mongodb_community_resource.sh] ( code_snippets/0310_create_mongodb_community_resource.sh )
108108``` 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
110110apiVersion : mongodbcommunity.mongodb.com/v1
111111kind : MongoDBCommunity
112112metadata :
@@ -147,7 +147,8 @@ spec:
147147 # admin user with root role
148148 - name : mdb-admin
149149 db : admin
150- passwordSecretRef : # a reference to the secret containing user password
150+ # a reference to the secret containing user password
151+ passwordSecretRef :
151152 name : mdb-admin-user-password
152153 scramCredentialsSecretName : mdb-admin-user
153154 roles :
@@ -156,20 +157,25 @@ spec:
156157 # user performing search queries
157158 - name : mdb-user
158159 db : admin
159- passwordSecretRef : # a reference to the secret containing user password
160+ # a reference to the secret containing user password
161+ passwordSecretRef :
160162 name : mdb-user-password
161163 scramCredentialsSecretName : mdb-user-scram
162164 roles :
163165 - name : restore
164166 db : sample_mflix
165167 - name : readWrite
166168 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.
170175 - name : search-sync-source
171176 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 :
173179 name : mdbc-rs-search-sync-source-password
174180 scramCredentialsSecretName : mdbc-rs-search-sync-source
175181 roles :
@@ -185,11 +191,12 @@ After applying the `MongoDBCommunity` custom resource, the operator begins deplo
185191[ code_snippets/0315_wait_for_community_resource.sh] ( code_snippets/0315_wait_for_community_resource.sh )
186192``` shell copy
187193echo " 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
189196echo ; 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
193200```
194201
195202### 7. Create MongoDB Search Resource
@@ -201,7 +208,7 @@ Note: Public Preview of MongoDB Community Search comes with some limitations, an
201208
202209[ code_snippets/0320_create_mongodb_search_resource.sh] ( code_snippets/0320_create_mongodb_search_resource.sh )
203210``` 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
205212apiVersion: mongodb.com/v1
206213kind: MongoDBSearch
207214metadata:
@@ -236,7 +243,8 @@ Similar to the MongoDB deployment, the Search deployment needs time to initializ
236243[code_snippets/0325_wait_for_search_resource.sh](code_snippets/0325_wait_for_search_resource.sh)
237244` ` ` shell copy
238245echo "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
240248` ` `
241249This command polls the status of the `MongoDBSearch` resource `mdbc-rs`.
242250
@@ -247,7 +255,8 @@ Double-check the status of your `MongoDBCommunity` resource to ensure it remains
247255[code_snippets/0330_wait_for_community_resource.sh](code_snippets/0330_wait_for_community_resource.sh)
248256` ` ` shell copy
249257echo "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
251260` ` `
252261This provides a final confirmation that the core database is operational.
253262
@@ -258,11 +267,11 @@ View all the running pods in your namespace. You should see pods for the MongoDB
258267[code_snippets/0335_show_running_pods.sh](code_snippets/0335_show_running_pods.sh)
259268` ` ` shell copy
260269echo; 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
262271echo; 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
266275` ` `
267276
268277# # Using MongoDB Search
@@ -275,9 +284,7 @@ To interact with your MongoDB deployment, this step deploys a utility pod named
275284
276285[code_snippets/0410_run_mongodb_tools_pod.sh](code_snippets/0410_run_mongodb_tools_pod.sh)
277286` ` ` 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
281288apiVersion: v1
282289kind: Pod
283290metadata:
@@ -294,7 +301,8 @@ spec:
294301EOF
295302
296303echo "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
298306` ` `
299307
300308# ## 12. Import Sample Data
@@ -303,14 +311,18 @@ To test the search functionality, this step imports the `sample_mflix.movies` co
303311
304312[code_snippets/0420_import_movies_mflix_database.sh](code_snippets/0420_import_movies_mflix_database.sh)
305313` ` ` 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
309316echo "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
311319echo "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"
314326EOF
315327)"
316328` ` `
@@ -322,10 +334,9 @@ Before performing search queries, create a search index. This step uses `kubectl
322334
323335[code_snippets/0430_create_search_index.sh](code_snippets/0430_create_search_index.sh)
324336` ` ` 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" \
329340 --eval "use sample_mflix" \
330341 --eval 'db.movies.createSearchIndex("default", { mappings: { dynamic: true } });'
331342` ` `
@@ -336,8 +347,6 @@ Creating a search index is an asynchronous operation. This script polls periodic
336347
337348[code_snippets/0440_wait_for_search_index_ready.sh](code_snippets/0440_wait_for_search_index_ready.sh)
338349` ` ` shell copy
339- #!/bin/bash
340-
341350# Currently it's not possible to check the status of search indexes, we need to just wait
342351echo "Sleeping to wait for search indexes to be created"
343352sleep 60
@@ -349,8 +358,6 @@ Once the search index is ready, execute search queries using the `$search` aggre
349358
350359[code_snippets/0450_execute_search_query.sh](code_snippets/0450_execute_search_query.sh)
351360` ` ` shell copy
352- #!/bin/bash
353-
354361mdb_script=$(cat <<'EOF'
355362use sample_mflix;
356363db.movies.aggregate([
@@ -391,9 +398,10 @@ db.movies.aggregate([
391398EOF
392399)
393400
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
395403echo '${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
397405EOF
398406)"
399- ` ` `
407+ ` ` `
0 commit comments