We have created Kubernetes configuration files with the jhipster kubernetes command. The next step is to build the artifacts and deploy them into Google Cloud.
Kubernetes will use the image from the Docker Registry. We configured the Docker username when we generated the application, so the first step will be to tag those images and then push them to our Docker repository.
To do so, we will do the following:
- Open the Terminal and go to the Kubernetes folder that we have generated. We will tag the images first:
> docker image tag store deepu105/store
- Next, we will push this image into the Docker repository:
> docker push deepu105/store
Alternatively, if we haven't built the Docker images already, we could do the build and push in a single step using the following command, which can be executed under each application folder (note the app name at the end):
> ./gradlew bootJar -Pprod jib -Djib.to.image=deepu105/store
- Similarly, push the invoice application to the Docker repository:
> docker image tag invoice deepu105/invoice
> docker push deepu105/invoice
- Do the same for notification:
> docker image tag notification deepu105/notification
> docker push deepu105/notification
This will push the store, invoice, and notification to the Docker repository. We can check this in the Docker Console:
- Now, we can connect to gcloud and deploy our containers with Kubernetes.
- First, we will log in to the gcloud CLI via the Terminal. In order to do that, open your Terminal:
> gcloud init // if this is the first time you are using gcloud (Ignore this step if you logged in already)
Then, gcloud will ask you to log in to your Google account. Once validated, this will list the projects that you might already have.
Here, we can choose [2] Create a new project by entering the number before creating a new project. Then, press Enter. It will ask you to enter the project information and then configure a few Google services for that project. Then, gcloud will list all the available regions and you can choose a region that suits you.
- If you have already logged in to the console and used it for other projects, then you can switch projects using the following command:
> gcloud config set project <project-name>
This will set the project, region, and the setting chosen as the default.
- Then, you have to enable Kubernetes in your application. We can do this by logging in to our Google Cloud Console via the browser. Then, select the project that we have just created and go to https://console.cloud.google.com/kubernetes/list to create a new cluster. Please choose the CPU type n1-standard-2 as we need a bit more CPU to deploy everything.
- This will create a cluster for your project. Alternatively, you can also create a cluster using the gcloud command:
> gcloud container clusters create online-store-app
--machine-type n1-standard-2
The following is the output of the preceding command:
Thus, the cluster is created with three nodes and the configuration is added to our kubectl config.
- Then, we can go to our Kubernetes folder and start deploying the services using kubectl:
> kubectl apply -f namespace.yml
> kubectl apply -f registry/
> kubectl apply -f invoice/
> kubectl apply -f notification/
> kubectl apply -f store/
> kubectl apply -f console/
Alternatively, you can also run the bash script, ./kubectl-apply.sh, which does what we mentioned previously
The output will be as follows:
This will deploy all the applications to the Google Cloud environment, under your project.
- You can check the pod's deployment process using the following command on bash:
> watch kubectl get pods -n jhipster
This will list the status of the pods that are spinning up in a watch loop so it is updated at specified intervals:
- Once the pods are in Running status, you can also get the logs of the pod using the following command:
> kubectl logs <name of the pod as shown above> -n jhipster
The following is the output:
You can stream the log by appending the -f flag to the command.
- You can get the application's external IP using this command:
> kubectl get svc store -n jhipster
This will list the application name, type, IP address, external address, ports, and uptime:
We can find the same information on the Google Cloud Console as well under the Services & Ingress section:
The application can be accessed at the external IP we found from the preceding command, for example, http://34.77.214.210:8080/:
- Similarly, you can find the JHipster Console external IP as well using kubectl get svc jhipster-console -n jhipster and access it on port 5601.
- You can also scale the application by using the following command:
> kubectl scale deployment/<app-name> --replicas <number-of-replicas> -n jhipster
For example, run kubectl scale deployment/invoice --replicas=2 -n jhipster. Now, if you run kubectl get pods -n jhipster, you will see that there are two pods for the invoice application. If you check the JHipster Registry, you will see two applications registered for INVOICE:
That is it, we have successfully deployed our entire microservice stack with monitoring and service discovery to Google Cloud using Kubernetes.
Next, we will see how we can use native Kubernetes features for service discovery, monitoring, and so on, using Istio service mesh.