GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing piotrminkowski.com — Microservices with Kubernetes and Docker - Piotr's TechBlog
In one of my previous posts, I described an example of a continuous delivery configuration for building microservices with Docker and Jenkins. It was a simple configuration where I decided to use only Docker Pipeline Plugin for building and running containers with microservices. That solution had one big disadvantage – we had to link all containers between each other to provide communication between microservices deployed inside those containers. Today I’m going to show you one the smart solution which helps us to avoid that problem – Kubernetes.
Kubernetes is an open-source platform for automating deployment, scaling, and operations of application containers across clusters of hosts, providing container-centric infrastructure. It was originally designed by Google. It has many features especially useful for applications running in production like service naming and discovery, load balancing, application health checking, horizontal auto-scaling or rolling updates. There are several important concepts around Kubernetes we should know before going into the sample. Pod – this is basic unit in Kubernetes. It can consist of one or more containers that are guaranteed to be co-located on the host machine and share the same resources. All containers deployed inside the pod can see other containers via the localhost. Each pod has a unique IP address within the cluster Service – is a set of pods that work together. By default, a service is exposed inside a cluster but it can also be exposed onto an external IP address outside your cluster. We can expose it using one of four available behaviors: Replication Controller – it is specific type of Kubernetes controllers. It handles replication and scaling by running a specified number of copies of a pod across the cluster. It is also responsible for pods replacement if the underlying node fails. MinikubeConfiguration of highly available Kubernetes cluster is rather not easy task to perform. Fortunately, there is a tool that makes it easy to run Kubernetes locally – Minikube. It can run a single-node cluster inside a VM, which is really important for developers who want to try it out. The beginning is really easy. For example on Windows, you have to download minikube.exe and kubectl.exe and add them to PATH environment variable. Then you can start it from command line using
ApplicationThe concept of microservices architecture for our sample is pretty similar to the concept from my article about continuous delivery with Docker and Jenkins which I mentioned at the beginning of that article. We also have accounts and customer microservices. Customer service is interacting with account service while searching for customer accounts. We do not use the gateway (Zuul) and discovery (Eureka) Spring Boot services, because we have such mechanisms available on Kubernetes out of the box. Here’s the picture illustrating the architecture of the presented solution. Each microservice’s pod consists of two containers: first with a microservice application and second with a Mongo database. Account and customer microservices have their own database where all data is stored. Each pod is exposed as a service and can be searched by name on Kubernetes. We also configure Kubernetes Ingress which acts as a gateway for our microservices.
Sample application source code is available on GitHub. It consists of two modules account-service and customer-service. It is based on the Spring Boot framework but doesn’t use any of Spring Cloud projects except the Feign client. Here’s dockerfile from account service. We use small openjdk image – alpine. Thanks to that our result image will have about ~120MB instead of ~650MB when using standard openjdk as a base image.
To enable
In customer service, we are going to call the API method from the
The docker image of our microservices can be built with the command visible below. After the build, you should push that image to the official docker hub or your private registry. In the next section, I’ll describe how to use them on Kubernetes. Docker images of the described microservices are also available on my Docker Hub public repository as piomin/account-service and piomin/customer-service.
Kubernetes deploymentYou can create deployment on Kubernetes using
We are creating a new deployment by running the command below. The same command is used for creating services and ingress. Only a JSON file format is different.
Now, let’s take o look at service configuration file. We have already created deployment. As you could see in the dashboard image has been pulled from Docker Hub, pod and replica set has been created. Now, we would like to expose our microservice outside. That’s why service is needed. We are also exposing the Mongo database on its default port, to be able to connect the database and create collections from a MongoDB client.
After creating a similar configuration for customer service we have our microservices exposed. Inside Kubernetes they are visible on default ports (2222 and 3333) and service name. That’s why inside customer service REST client (
Ok, we have our two microservices available under two different ports. It is not exactly what we need. We need some kind of gateway available under on IP which proxies our requests to exact service by matching the request path. Fortunately, such an option is also available on Kubernetes. This solution is
The last thing that needs to be done to make the gateway working is to add following entry to system hosts file (/etc/hosts for Linux and C:\Windows\System32\drivers\etc\hosts for windows). Now, you could try to call from your web browser http://micro.all/accounts or http://micro.all/customers/{id}, which also calls account service in the background. [MINIKUBE_IP] micro.all ConclusionKubernetes is a great tool for microservices clustering and orchestration. It is still a relatively new solution under active development. It can be used together with Spring Boot stack or as an alternative for Spring Cloud Netflix OSS, which seems to be the most popular solution for microservices now. It has also a UI dashboard where you can manage and monitor all resources. Production grade configuration is probably more complicated than a single host development configuration with Minikube, but I don’t that it is a solid argument against Kubernetes. Like this:Like Loading… Related |
| You are visitor 01402860 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |