In this tutorial, I will try to explain step by step, how you can set up Kubernetes, deploy your microservice on Kubernetes, and check the result via the Kubernetes dashboard. All other things will be “as simple as possible”. As a cloud platform gcp will be used. We will cover the following aspects of the problem:
Create a microservice to be deployed
Placed the application in your docker container
What is Kubernetes and how to install it?
Create a new Kubernetes project
Create new Cluster
Allow access from your local machine
Create service account
Activate service account
Connect to cluster
Gcloud initialization
Generate access token
Deploy and start the Kubernetes dashboard
Deploy microservice
STEP 1: CREATE MICROSERVICE TO BE DEPLOYED
Traditionally, in the programming world, everything starts with “Hello World”. So, as mentioned previously, to keep things simple, create a microservice that returns just “Hello World”. You can use start.spring.io for this goal. Create HelloController like this:
|
STEP 2: PLACED APPLICATION IN YOUR DOCKER CONTAINER
We have a microservice, need to put this microservice in a docker container and upload it on Kubernetes. From that point, Kubernetes will orchestrate the container according to your settings. Let’s create the first image from the microservice. Normally, as you might guess, it is called Dockerfile (without any extension), and the content is:
Dockerfile
FROM adoptopenjdk/openjdk11:jre-11.0.8_10-debianslim |
The next step is to create the docker-compose file. For that purpose, a call to Dockerfile will be made to build the image. You can do it manually, but the best way is from the docker-compose file, as you have a permanent track of the solution. This is a .yaml file. (picture below)
docker-compose.yaml
|
After starting docker, go to the folder where docker-compose is located and execute the command “docker-compose up”. The expectation is to reach this microservice on 8099 port. If everything is ok, in your docker will be something like this:
Check microservice docker installation with postman calling http://localhost:8099/api/v1/say-hello. In response, you have “Hello World”.
STEP 3: WHAT IS KUBERNETES AND HOW TO INSTALL IT?
What is Kubernetes?
Kubernetes is an open-source container orchestrator that automates many tasks involved in deploying, managing, and scaling containerized applications. What happens when you use Docker, and your container fails? Probably the first thing to do is to restart your container. You do that manually (if you don’t have Kubernetes). Here comes Kubernetes, observe that the container is down and start a new container automatically. This is just a basic use case. Please read more on the internet, there is a bunch of information about this.
How to install Kubernetes?
Ok, until now you are sure that Kubernetes is needed, but where to find it, what are the costs, and how to install it? First of all, try “download Kubernetes” on Google… Pick the site https://kubernetes.io/docs/tasks/tools/… Options for Windows, Mac, and Linux appear… Different installations like Kind, minikube, kubeadmin… So, is it worth spending so much time setting up properly this Kubernetes? You do not have to ask me, I agree with you, it is too much time. Fortunately, we can make a “go around” and skip all that: Go to Gcloud where Kubernetes is offered as a service and just use it. Somebody else takes care of this, we can focus just on the business logic in our microservice and use out-of-the-box Kubernetes installation from Gcloud. Sounds good, doesn’t it? The last and most important question; money. Is it for free? No, it is not. You have to pay for the Gcloud services and here is the price list: https://cloud.google.com/kubernetes-engine/pricing. But for ordinary people like you and me, Gcloud offers a free account for 3 months up to 300$ and it seems fair. It is enough time to learn about deploying microservices on Kubernetes. For any professional use in future, the company should stay behind this. Here is the link where you can create your free cloud account https://cloud.google.com/. One more thing, during the creation of a free account, Google will ask for your bank account, to automatically charge you. But do not worry, you are safe for the first three months and below 300$. And for any charging, you will be asked for permission before… So, until now my personal experience is positive, as Google is keeping its promise when you create an account. But the final decision is up to you.
STEP 4: CREATE NEW KUBERNETES PROJECT
Open up your Google account, sign in and go to the console.
Create a new project from the main dashboard; the name of the new project is “hello-world”. From now on, this is your active project.
STEP 5: CREATE NEW CLUSTER
Create a new cluster (named cluster2). Accept default values for other fields.
STEP 6: ALLOW ACCESS FROM YOUR LOCAL MACHINE
Now, we must allow access from our local machine to Kubernetes, via kubectl. For that purpose, we need to follow these steps:
Click on cluster2
Find your local IP address and add it here according to the CIDR standard in the Edit control plane authorized networks
STEP 7: CREATE SERVICE ACCOUNT
Give new account role “Owner”. Accept default values for other fields. After a service account is created, you should have something like this:
Generate keys for this service account with key type JSON. When the key is downloaded, it has some random name like hello-world-315318-ab0c74d58a70.json. Keep this file in a safe place, we will need it later.
Now, install Google Cloud SDK Shell on your machine according to your OS. Let’s do the configuration so kubectl can reach cluster2. Copy the file hello-world-315318-ab0c74d58a70.json and put it in the CLOUD SDK folder. For the Windows environment, it looks like this:
STEP 8: ACTIVATE SERVICE ACCOUNT
The first thing to do is to activate the service account with the command: gcp auth activate-service-account hello-world-service-account@hello-world-315.. –key-file=hello-world-315318-ab0c74d58a70.json
STEP 9: CONNECT TO CLUSTER
Now go to cluster2 again and find the connection string to connect to the new cluster
Execute this connection string in Google Cloud Shell: gcp container clusters get-credentials cluster2 –zone us-central1-c –project hello-world-315318
STEP 10: GCLOUD INITIALIZATION
The next command to execute is gcloud init, to initialize connection with the new project. Here is the complete code on how to do that from the Gcloud Shell:
|
STEP 11: GENERATE ACCESS TOKEN
Type kubectl get namespace, the access token is generated in .kube folder (in the home folder), in the config file:
If you open this config file, you will find your access token. You will need this later.
STEP 12: DEPLOY AND START KUBERNETES DASHBOARD
Now, deploy the Kubernetes dashboard with the next command: kubectl apply -f raw.githubusercontent.com/kubernetes/dashbo..
|
Start the dashboard with kubectl proxy command. Now open the dashboard from the link: http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/overview?namespace=default
In front of you, this screen will appear:
Now, you need the token from the config file that we spoke about a moment ago. Open the config file with Notepad (on Windows), find your access token, and copy from there and paste it in the Enter token* field. Be careful when you are copying token from the config file as there might be several tokens. You must choose yours (image below).
Finally, the stage is prepared to deploy microservice.
STEP 13: DEPLOY MICROSERVICE
Build the docker image from Dockerfile with the command: docker build -t docker2222/dimac:latest. docker2222/dimac is my public docker repository.
Push the image on docker hub with the command: docker image push docker2222/dimac:latest.
Execute kubectl apply -f k8s.yaml where k8s.yaml is the file below:
|
Last but not least, open the Kubernetes dashboard. If everything is OK, you should see your service.