Deploying the application to Google Cloud with Kubernetes

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.

It is also possible to deploy to other Kubernetes services such as Azure Kubernetes Service or Amazon Elastic Kubernetes Service using this configuration. Just follow the cloud provider's documentation to create a Kubernetes cluster and apply the generated configuration using the kubectl apply commands, as mentioned later in this section.

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:

  1. 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
  1. 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
Note: You have to log in to Docker Hub before pushing the image. You can log in to Docker using the docker login command, followed by your username and password. If you don't have an account, you can create one at the following link: https://cloud.docker.com/.
  1. Similarly, push the invoice application to the Docker repository:
> docker image tag invoice deepu105/invoice
> docker push deepu105/invoice
  1. 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:

  1. Now, we can connect to gcloud and deploy our containers with Kubernetes. 
This assumes that you have set up the gcloud SDK and kubectl on your machine.
  1. 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.

  1. 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. 

  1. 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.
  2. 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.

  1. 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.

  1. 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:

  1. 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.

  1. 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/:

  1. 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.
The JHipster Registry is deployed in headless mode. In order to check the JHipster Registry, we can explicitly expose the service by using this command: kubectl expose service jhipster-registry --type=LoadBalancer --name=exposed-registry -n jhipster. Then, we can access the application via the external IP of exposed-registry on port 8761.
  1. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.107.241