Create an API Builder Multi-Container Application Using Kubernetes – Part 1

In this blog post, we’ll replicate the simple API Builder + MongoDB multi-container application described in the Create an API Builder Multi-Container Application Using Docker – Part 1 blog post. But this time, we’ll deploy to Kubernetes using minikube on our machine.

In this Part 1 of the series, we’ll make sure that kubectl and minikube are installed and working and then we’ll launch an API Builder Docker image using kubectl commands and then make API calls to API Builder.

In Part 2, we’ll add a local MongoDB container and connect the two. And, we’ll do this with deployment YAML files to automate the process.

Pre-requisites

Make sure kubectl and minikube are installed, running and working on your machine.

You can follow the instructions here to make sure minikube and kubectl are installed and working properly.

The commands are:

minikube start
kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
kubectl expose deployment hello-minikube --type=NodePort
kubectl get pod
curl $(minikube service hello-minikube --url)
kubectl delete deployment hello-minikube

The response should be:

CLIENT VALUES: client_address=172.17.0.1 command=GET real path=/ query=nil request_version=1.1 request_uri=http://192.168.99.100:8080/

SERVER VALUES: server_version=nginx: 1.10.0 – lua: 10001

HEADERS RECEIVED: accept=/ host=192.168.99.100:30073 user-agent=curl/7.43.0

BODY: -no body in request-

MongoDB Database

Make sure you have a MongoDB database with a collection with data in it. We’ll need the MongoDB connection URL. If you don’t have access to a MongoDB database, then head on over to MongoDB Atlas and create one so we don’t have to worry running MongoDB locally. We’ll run MongoDB locally in Part 2.

My MongoDB Atlas URL looks like the following:

mongodb://{username}:{password}@cluster0-shard-00-00-merks.mongodb.net:27017,cluster0-shard-00-01-merks.mongodb.net:27017,cluster0-shard-00-02-merks.mongodb.net:27017/test01?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true

My database is called test01.

Run your API Builder MongoDB Project

Use the following kubectl command from terminal to run your API Builder project in Minikube (Kubernetes).

kubectl run apibmongo --image=lbrenman/apibmongodb:1.0.1 --port=8080 --env="MONGO_URL=mongodb://{username}:{password}@cluster0-shard-00-00-merks.mongodb.net:27017,cluster0-shard-00-01-merks.mongodb.net:27017,cluster0-shard-00-02-merks.mongodb.net:27017/test01?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true"

From the command above, you can see the following:
* My deployment is called apimongo
* I am using my docker image from docker hub: lbrenman/apibmongodb:1.0.1
* I am exposing port 8080, which is the default port API Builder will listen for API calls on
* I am passing in the MongoDB URL I retrieved from the MongoDB Atlas portal

The response should be:

deployment.apps/apibmongo created

Check Your API Builder Pod

kubectl get pods

The response should be:

NAME                         READY   STATUS    RESTARTS   AGE
apibmongo-58ff6b6d67-l5npp   1/1     Running   0          50s

Check API Builder Logs

Let’s make sure the API Builder app is running properly and did not have any connectivity issues to your MongoDB instance as follows:

kubectl logs apibmongo-58ff6b6d67-l5npp

The response should be:

1551407745961 configuration applied in this order: /app/conf/default.js,/app/conf/mongo.default.js
1551407746811 API Builder/Kobe (4.5.8) apibmongodb/1.0.0
1551407746811 APIKey is: 3wEwROK+ZEyt4pr5k9lA5NQE9te8dG7p
1551407747326 No APIKey or APIKeyAuthPlugin set in your config.  Your server has no authentication strategy.
1551407747329 Registering upgrade handler for flow
1551407747337 waiting on server to finish loading
1551407748280 Registered plugin: @axway/api-builder-plugin-dc-mongo
1551407748281 Registered plugin: @axway/api-builder-plugin-fn-base64
1551407748281 Registered plugin: @axway/api-builder-plugin-fn-dot
1551407748281 Registered plugin: @axway/api-builder-plugin-fn-json
1551407748281 Registered plugin: @axway/api-builder-plugin-fn-restclient
1551407748281 Registered plugin: @axway/api-builder-plugin-fn-swagger
1551407748500 Starting connector/mongo@1.1.3
(node:1) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
1551407759341 Started connector/mongo
1551407759346 attempting to load model /app/models/dog.js
1551407759360 starting 8080
1551407759360 server is starting!
1551407759384 port 8080 available for use
1551407759384 loading schemas
1551407759385 loading 8 schema
...
...
...
1551407759485 binding api (delete) /api/dog/:id [model] sort:-2
1551407759486 binding api (get) /api/dog/:id [model] sort:-2
1551407759486 binding api (put) /api/dog/:id [model] sort:-2
1551407759487 binding api (delete) /api/mongo/dog/:id [model] sort:-2
1551407759488 binding api (put) /api/mongo/dog/:id [model] sort:-2
1551407759488 binding api (get) /api/mongo/dog/:id [model] sort:-2
1551407759489 binding api (get) /api/dog/distinct/:field [model] sort:-2
1551407759491 binding api (get) /api/mongo/dog/distinct/:field [model] sort:-2
1551407759493 Access the swagger API documentation at http://localhost:8080/apidoc/swagger.json
1551407759497 server started!
1551407759497 server started on port 8080

Expose API Builder Deployment

In order to access the API Builder container, we need to expose its URL as follows:

kubectl expose deployment apibmongo --type=NodePort

The response should be:

service/apibmongo exposed

Get URL

Retrieve the URL as follows:

minikube service apibmongo --url

The response should be:

http://192.168.99.100:32355

Call Your API

Test your API using Postman or Curl as follows:

curl http://192.168.99.100:32355/api/mongo/dog

The response should be:

{
  "success": true,
  "request-id": "44047fcb-c395-4b58-a824-090476e134f8",
  "key": "dogs",
  "dogs": [
    {
      "id": "5a24a9a67779e860d007b13e",
      "breed": "Poodle",
      "name": "Fido"
    },
    .
    .
    .
    {
      "id": "5b3aafcb9de9003840480fe7",
      "breed": "Doberman",
      "name": "Doobie"
    }
  ]
}

Common Commands

Try the following commands to learn more about your deployment:

kubectl get services

kubectl get deployments

kubectl describe deployment apibmongo

Teardown

When you are done, you can tear down your application using the following:

kubectl delete deployment apibmongo

kubectl delete service apibmongo

kubectl delete hpa apibmongo

Conclusion

In this blog post, we used kubectl and minikube to deploy an API Builder docker image to Kubernetes and learned some common kubectl commands.

In the next blog post, we’ll switch to using deployment YAML files to help automate our deployment and we’ll add a local MongoDB container to our app.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.