Dealing with Kubernetes’son: Minikube.

Dealing with Kubernetes’son: Minikube.

If you are approaching on working with Docker and Containers, it should be probably interesting to understand more about Minikube, and in particular on how to work with Minikube using your own docker images deployed locally, so avoiding to use a docker registry (e.g. Docker Hub, Google Container Registry).

I suppose that you already know what Kubernetes and Minikube are, and besides that you are familiar with Docker and its main commands.

The first thing that we need is an app that will be "containerized" through its docker image. You can find the source code related with this article here.

The app made for this article, it is a very easy NodeJS app that, using ExpressJS, serves an API that returns information about the user balance (Note: this is just a REST API made for this article and its code is not intended as a code to use in a real scenario).

You can test the app micros pointing to its folder with your shell, and running the command:

  • npm install
  • node server

The node command will start a node app listening on the port 9090, so if you type on your browser the address https://localhost:9090/api/account/balance you should be able to see a JSON message like this one:

If you take a look into the server.js file, you will see that the app has been clustered. This is a good practice when you work with node, because you will take advantage of multi-core systems (here a good tutorial about the node clustering https://stackabuse.com/setting-up-a-node-js-cluster/ and here the official documentation https://nodejs.org/api/cluster.html).

Before creating the image of our app, we need to start our Minikube executing the command:

minikube start

Now, to be able to work with the docker daemon on your mac/linux host we need to use the command:

minikube docker-env

Now it’s time to build a docker image of our, app and to do that we will use a Docker file as follow:

FROM node:6.11.3-onbuild
EXPOSE 9090

and running the following command we will build our image:

docker build -t micros:v1 .

If everything went well, listing the docker images with the command:

docker images   

you should be able to see your micros image.

Now that we have a docker image we can, using kubectl, run the container and expose it as a service. Run the command:

kubectl run micros-test --image=micros:v1 --port=9090

and to check if the Pod has been created:

kubectl get pods   

Now, we need to expose the deployment to the external traffic:

kubectl expose deployment micros-test --type="LoadBalancer"

and to check if the service has been created correctly:

kubectl get services

Finally we need to get the generated URL using the command:

minikube service micros-test --url

Opening the browser to the URL that you got with the previous command, and pointing to the balance API endpoint (/api/account/balance), you should be able to see your app up and running.

If you don’t like to deal with your shell, you can also make a deployment, using the Kubernetes Dashboard (https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/) that moreover it is a very powerful tool to monitor activities on your Pods.

I hope that this article will help to understand how to reuse the Docker daemon inside the VM, so avoiding to push your images into a docker registry.

Domenico Vacchiano (CTO at We Are Casino)

要查看或添加评论,请登录

Domenico Vacchiano的更多文章

社区洞察

其他会员也浏览了