Kubernetes and MEAN Stack for Microservices Development
Learn how to locally create and run containerized microservices with Kubernetes, Docker, and the MEAN stack, plus a troubleshooting guide.
Join the DZone community and get the full member experience.
Join For FreeThis article focuses on an end to end solution using Angular4/Material for UI, NodeJS/Express for middleware, MongoDB for storage, and Docker/Kubernetes for container-based deployment. While the solution provided is not so much a 12-factor app nor follows microservices guidelines, it lays out a foundation and gets you a quick start on an end to end containerized sample.
With fully functional end-to-end solution it helps to see all moving parts to quickly get a hold on concepts. This article focuses on critical and helpful details only, to complement already available wealth of documentation on the web.
Q: What is the value added in this article than bringing in hello-world samples in different areas together?
A: It's important to view the details below to understand the difference:
The focus is on Kubernetes, Docker, and troubleshooting details
Docker Compose is used to link containers together, and taking it further, to link Pods in Kubernetes as we move to a minikube env.
You will get over a few stumbling blocks with local minkube/pods/services working that are easy to miss.
It is also MEAN stack with Angular4, Express, Mongo, and a NodeJS sample, and is fully containerized that you can test with both Docker only or with Kubernetes. However, it is more than just MEAN and you can replace Angular with React in no time.
With fully working sample code, it gives you a good hold on an early start and appreciation of Kubernetes and Microservices direction.
Finally, these concepts and focus help even if you are enjoying a higher abstraction using OpenStack or many other cloud platforms that use Kubenetes.
Fig.1 Below is a quick end to end view of the solution:
The goal is to get a local setup and run everything locally. So any code, samples, and approach shared are focused around local development and not targeted with a production environment in mind.
How an Angular website is hosted in a container using “ng serve” is not how we normally deploy websites in prod. To get a fully working end to end sample and keep it simple, some details might be left out. Please keep this in mind while you take the sample code or read the contents of this article.
Pre-Requisites
- Install NodeJS (Version 8):
- Install Angular (Version 4):
- Install Docker (Version >= 17.x):
- Install minikube (Version >= v0.20.x):
Must-read concepts: Below are wonderful documentation links that you don't want to miss. More than anything, the Kubernetes team did an amazing job of providing great documentation that is easy to follow and something you'll enjoy reading.
Docker:
https://docs.docker.com/get-started/#containers-vs-virtual-machines
https://docs.docker.com/machine/overview/#why-should-i-use-it
Kubernetes:
https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/
https://kubernetes.io/docs/tutorials/kubernetes-basics/cluster-interactive/
Please see the “Concepts, Topics, Tutorials, Tasks” under the documentation for a wealth of information.
Because this article's focus is more on Kubernetes and Docker, details on Angular, NodeJS, and MongoDB are skipped, but these deserve a great deal of attention otherwise. It is important to understand and appreciate how well decoupling is achieved regardless of technology and tools for your application. Your application can choose the tools and technology that fits best for your needs that are container friendly. Docker will build container images and Kubernetes complements Docker in managing/maintaining Docker containers and much more.
Phase 1: Docker
STEP 1: Get the full source code by executing the below commands in the terminal window:
- Open the command prompt/terminal in the projects root folder you use for projects.
- Git clone https://github.com/tjayram/kube-docker-mean-angular4.
- Run
cd kube-docker-mean-angular4
- The rest of the steps below assume the above folder as "ProjectRoot folder."
STEP 2: Build
docker-compose build
STEP 3: Run
docker-compose up
STEP 4: Test
Click on the "submit" button in the UI to view the product list, to see the UI as shown below with the product list:
STEP 5: Clean Up
docker-compose down
STEP 6: Troubleshooting
If things are not working as expected, or if you'd like to get more insight, please look at the “Troubleshooting Docker-Related Issues” section later in this article.
Phase 2: Kubernetes With Minikube
STEP 1 (Optional) - Required only if Phase 1 steps are skipped…
- Open the command prompt/terminal in the projects root folder you use for projects.
- Git clone https://github.com/tjayram/kube-docker-mean-angular4.
- Run
cd kube-docker-mean-angular4
. - The rest of steps below assume the above folder as "ProjectRoot folder."
STEP 2: Build
Run ./meankube_build.sh
.
STEP 3: Run
Run ./meankube_create.sh
.
The above command should bring up three browser windows:
1. Minikube Dashboard page
2. MEANKube REST API page. This browser window opened to easily provide the URL address for the REST API. Please copy the base URL; it is useful for the next step. We are not testing the REST API too much but you are free to type in the "/Products" path to view the list of products.
3. MEANKube Web UI
STEP 4: Test
Now, in the MEANKube Web UI, copy and paste the HTTP address of what you see in the “MEANKube REST API” browser as below (NOTE: This step is different than Phase 1).
You should now see the UI with the product list, as below:
STEP 5: Clean-Up: If you need to clean up, run the following command:
./meankube_delete.sh
STEP 6: Troubleshooting: Please see the section “Troubleshooting Minikube and Kubernetes."
Troubleshooting Docker-Related Issues
What if stuff doesn't work? This is a more interesting part, and now you need to look at each component separately in a process of elimination to find and fix the issue:
1. Is Docker set up correctly and looks good on your machine?
Try:docker run hello-world
.
If the response is “Hello from Docker!,” you are good.
If the response is“docker: Cannot connect to the Docker daemon,” this means the Docker daemon not running on your machine. Just double-click the famous blue whale icon from LaunchPad or if you see it on the taskbar.
If you can get past this stage, don’t go further without getting this fixed. You need to dig more into Docker and your specific environment (Windows/Mac) to see why Docker is not working or if there is a network connectivity issue that Docker is not able to pull images from the Docker registry, etc.
2. Is the UI not showing data?
First, see if your API is returning data: http://localhost:10010/products. If you see data, that is great, but if you get an empty [], then see the next step.
Is MongoDB running and you have the data imported correctly?
Install any MongoDB client tools (MongoChef or Robo Mongo).
Try connecting to mongoldb://localhost:27017.
If the connection fails, your MongoDB Docker container did not run. This usually means Docker is failing to pull the Mongo image. See below for how to check individual Docker builds, images, and containers.
Able to connect to MongoDB but don’t see data:
As a quick workaround: try following in MongoDB Client tool:
Create database “meankube”
Import “db/products.json” into products collection
Now just refresh below API page: http://localhost:10010
Now if you refresh UI: http://localhost:4200 you should see data
3. How do I build Docker images directly (without docker-compose
)?
docker-compose
is used to make it easy to build/run multiple containers and link their dependencies, but you can just build Docker images separately and troubleshoot as follows:
To build a Docker image: docker build -t <tagname>
.
Example:
To build a web image from ProjectRoot, try
cd meankube-web
docker build -t meankube-web .
(Note the . in the above command - it is also part of the command.)
To build an API image from ProjectRoot, try
cd meankube-api
docker build -t meankube-web .
(Note the . in the above command - it is also part of the command.)
4. How do I look at the contents of a Docker image?
docker run -it docker_image_name bash
For example: running “ docker run -it meankube-web sh
” will open up a command prompt that lets you view contents of an image with ls, etc.
5. How do I view contents of a specific Docker container while it is running?
docker exec -it docker_container_id bash
Tip: You can see a list of running containers and docker_container_ids with the docker ps
command.
Troubleshooting Minikube and Kunernetes Issues
1. What is the IP of my Minikube?
The command minikube ip
returns the IP of your minikube, if running.
If minikube is not running, start minkube with minikube start
.
2. How do I set my local environment to the specific Docker local registry that Minikube uses?
Run eval $(minikube docker-env)
.
Now when you try “docker images,” it shows contents from the same Docker instance that Minikube uses.
When you are done, you can try eval $(minikube docker-env -u)
to get back to your local Docker env.
3. How do I bring up the Kubernetes dashboard to view running Pods, services, and other details?
Run minikube dashboard
.
4. How do I bring up service details of a specific port service running in minikube?
Run minikube service server-name
.
5. How do I connect the API to specific port the database is running from?
Once a deployment is exposed through a service, minikube provides below environment variables:
SERVICE_NAME_SERVICE_HOST
SERVICE_NAME_SERVICE_PORT
Example: for meankube-db, below are the environment variables:
- MEANKUBE_DB_SERVICE_HOST
- MEANKUBE_DB_SERVICE_PORT
Published at DZone with permission of Jay Tallamraju. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments