Guestbook & MongoDB - The beginning
Guess what…
In this Lab we will deploy the guestbook application… again! Though this time we will also deploy a mongoDB within our cluster. The guestbook application should connect to the mongoDB to persistently save the entries.
What you will learn:
- Setting the current namespace in Kubeconfig
- Adding configuration with a Config Map
- Deployment of the mongoDB image
- Binding of credentials with a Secret
- Exposing mongoDB with Cluster IP
- Adding a persistence layer to the database
- Configuring Ingress for the Guestbook and MongoExpress
This task consists of different subtasks. We will start with deployment and modify it in every task to reach our goal.
1. Create new Namespace
Create a new namespace mongo.
To avoid mistakes when working in another namespace than default, we can consistently switch to another namespace in the kubeconfig.
After creating the namespace, set the current namespace to mongo in the kubeconfig.
HINT
1. Use the imperative command:
Solution
$ kubectl create namespace mongo
2. To switch namespaces permanently, get help from the Kubectl Reference Docs or within your terminal:
$ kubectl config --help
Solution
$ kubectl config set-context --current --namespace mongo
4. If there is no cluster, context and user configured in the config file, you could create your own context:
Solution
$ k config set-cluster [cluster-name]
$ k config set-context [context-name] --cluster [cluster-name] --user [user-name]
$ k config use-context [context-name]
$ k config set-context --current --namespace mongo
- You can find the kubeconfig file under $HOME/.kube/
With this configuration, you do not have to specify the namespace on commands meant for the mongo namespace.
2. Deploy the Guestbook Application
Like in the last Lab, you find manifest files for the start in the 4 - Namespace Mongo/setup directory.
If you want to, just inspect the files. Though, they are the same as in the last Lab setup.
Tip
You can deploy the setup resources, but keep in mind that we will edit them during the workshop. It is recommended to copy the file to your solution directory to work on it.
3. Create MongoDB Deployment
In order to persist our guestbook entries in a database, we will deploy a mongoDB within our cluster.
First write a Deployment definition file with the specs below and save it:
name: mongo-deploy
namespace: mongo
labels: app = mongo
replicas: 1
image: mongo
containerPort: 27017
You can create the deployment, but keep in mind, that we will edit this file during the workshop. You may have to recreate the deployment.
HINT
1. Use the imperative command:
Solution
$ kubectl create deployment mongo-deploy --image mongo --port 27017 --dry-run=client -o yaml > mongo-deploy.yaml
1. You can take the definition file 4 - Namespace Mongo/2-mongo-deploy.yaml as a reference.
3. Expose MongoDB Deployment
One thing that’s missing is a Service to make the mongoDB accessible to the guestbook application in our cluster.
Think about which kind of Service is needed to expose a Deployment internally to other Pods and Services.
Create the correct service type to expose our mongo-deployment and add the following specs:
name: mongo
selector: app = mongo
port: 27017
HINT
1. Use the imperative command:
Solution
$ kubectl expose deployment mongo --name mongo --port 27017
2. Alternatively you can take the definition file from 4 - Namespace Mongo/3-mongo-svc.yaml as a reference
When you are done, check the results. Display all Pods, Services and Endpoints.
HINT
$ kubectl get pod,service,endpoints
or
$ kubectl get po,svc,ep