Deploying web app on GKE Autopilot

Deploying web app on GKE Autopilot
by u/bhargavbachina in googlecloud

Example Angular project on GCP GKE Autopilot https://medium.com/bb-tutorials-and-thoughts/gcp-deploying-angular-app-with-net-web-api-on-gke-autopilot-b87cd2db022 submitted by /u/bhargavbachina [link] [comments]

GCP introduces GKE Autopilot which is a new mode of operation in Google Kubernetes Engine (GKE) that is designed to reduce the operational cost of managing clusters, optimize your clusters for production, and yield higher workload availability. The mode of operation refers to the level of flexibility, responsibility, and control that you have over your cluster. It still offers the standard version as well.

In this post, we are going to deploy an Angular application with a .NET web API. First, we dockerize our app and push that image to the Google container registry and run that app on Google GKE Autopilot. We will see how we can build the Kubernetes cluster on Google GKE, Accessing clusters from outside, configuring kubectl to work with the GKE cluster, and many more.

  • Example Project
  • Prerequisites
  • Install gcloud CLI and Configure
  • Dockerize the Project
  • Pushing Docker Image To Container Registry
  • Creating GKE Autopilot Cluster
  • Configure Kuebctl With GKE Autopilot Cluster
  • Deploy Kubernetes Objects on GKE Autopilot Cluster
  • Access the WebApp from the browser
  • Summary
  • Conclusion
Advertisements

Example Project

This is a simple project which demonstrates developing and running an Angular application with the .NET. We have a simple app in which we can add users, count, and display them at the side, and retrieve them whenever you want.

Example Project

If you want to practice your own here is a Github link to this project. You can clone it and run it on your machine as well.

// clone the project
git clone https://github.com/bbachi/angular-dotnet-example.git// run the project
cd angular-dotnet-example
dotnet run

Prerequisites

  • If you are not familiar with the process or you want to know before studying this guide, I would recommend you going through it.

How To Develop and Build Angular App With .NET Core Backend

The other prerequisites to this post are Docker essentials and Kubernests essentials. We are not going to discuss the basics such as what is a container or what is Kubernetes, rather, we will see how to build a Kubernetes cluster on GCP GKE. Below are the prerequisites you should know before going through this article

Docker Essentials

You need to understand Docker concepts such as creating images, container management, etc. Below are some of the links that you can understand about Docker if you are new.

Kubernetes Essentials

You need to understand Kubernetes’ essentials as well along with Docker essentials. Here are some of the docs to help you understand the concepts of Kubernetes.

GCP Prerequisites

  • Create a New project
  • You need to create a Billing Account
  • Link Billing Account With this project
  • Enable All the APIs that we need to run the dataflow on GCP
  • Download the Google SDK

Service Account

Need to create a service account so that when you run the application from your local machine it can invoke the GCP dataflow pipeline with owner permissions.

Service Account

You have to generate the key and download and set the environment variable called GOOGLE_APPLICATION_CREDENTIALS.

export GOOGLE_APPLICATION_CREDENTIALS="/Users/bhargavbachina/gcp-credentials/gcp-dataflow-service-account.json"

Finally, you can run the following command to log in to your GCP account.

gcloud auth login
Advertisements

Install gcloud CLI and Configure

Once you have the GCP Account you can install the gcloud command-line tool. You can go to the below documentation and install gcloud CLI based on your operation system. You can configure gcloud CLI with your project.

The gcloud CLI is a part of the Google Cloud SDK. You must download and install the SDK on your system and initialize it before you can use the gcloud command-line tool

// initializing
gcloud init// auth login
gcloud auth login

Once you run the above command, it opens up a browser for you to login into your GCP and you get the response as below.

gcloud auth login

You can list the projects with the following command.

gcloud projects list

You can set the current project with the following command.

gcloud config set project staticweb-test
Configuring the project

Dockerize the Project

Google GKE is a managed service that makes it easy for you to run Kubernetes on the Google Cloud Platform. The first thing you need to do is to dockerize your project.

Here is the Dockerfile and it is using multi-stage builds to reduce the image size and surface attacks.https://medium.com/media/918f07f566ca176d29bf0590113bf963Dockerfile

Here are the commands to build the image and run it on the Docker engine on your local machine. If you are new to Docker and check this detailed post on this topic.

// build the image
docker build -t ang-dotnet-img .// run the image
docker run -d -p 5000:80 --name ang-dotnet-ui ang-dotnet-img// list the image you just built
docker images// list the container
docker ps

Pushing Docker Image To Container Registry

Container Registry is a single place for your team to manage Docker images, perform vulnerability analysis, and decide who can access what with fine-grained access control. Existing CI/CD integrations let you set up fully automated Docker pipelines to get fast feedback. You can check the whole documentation here.

GCP GKE works with any Docker registry such as Docker Hub, etc. But, in this post, we see how we can use a GCP container registry to store our Docker images. Make sure you enable the relevant API on GCP. If you log in and go to the Container Registry you can see the empty registry.

Container Registry Console

The first thing we need to do is to enable the API with the following command.

gcloud services enable containerregistry.googleapis.com

You need to configure docker with the following command. You can see the full documentation here.

gcloud auth configure-docker
Configuring Docker

As we have seen in the Example Project section, let’s build the Docker image with the following command.

docker build -t ang-dotnet-img .

Let’s tag the local docker image with the registry name by using the command:

docker tag [SOURCE_IMAGE] [HOSTNAME]/[PROJECT-ID]/[IMAGE]:[TAG]// run this command
docker tag ang-dotnet-img gcr.io/staticweb-test/angular-webapp:v1

Finally, push the image into the GCP container registry

docker push gcr.io/staticweb-test/angular-webapp:v1
docker push

You can see the repository in the console as well.

Container Registry

Creating GKE Autopilot Cluster

Bestseller No. 1
Mybaby Automatic Soap Dispenser, 1.1 Pound
  • Works With All Liquid Hand Soaps
  • Plays 20 Second Song That Teaches Children How To...
  • Motion Sensor Technology Provides Sanitary,...
  • Magnetically Attached Drip Tray Removes Easily For...
  • Self-cleaning Function Prevents Clogs And Mess
SaleBestseller No. 2
Asterom Walking Cane - Handmade Wolf Cane - Cool Walking Canes for Men and Women - Wooden, Carved, Unique - Walking Sticks for Men & Seniors (36 Inch)
  • STYLISH CANE FROM THE RED CARPET TO YOUR HOME! Our...
  • 100% SATISFACTION GUARANTEED! Buy with confidence,...
  • EXCEPTIONAL COMFORT DUE TO ERGONOMIC HANDLE. The...
  • CHOOSE THE CORRECT LENGTH TO AVOID SPINE PAIN....
  • WIDE QUAD CANE TIP COMPATIBLE. Need extra...

We have pushed the Docker image into the container registry and it’s time to create a GKE Cluster. You can create the cluster with the following command.

gcloud container clusters create-auto CLUSTER_NAME \
--region REGION \
--project=PROJECT_ID

But, we will see how we can create Cluster through the console. Go to the GKE dashboard in the GCP console. Click the button to create a cluster. You will see the popup that allows you to select the autopilot or standard version as below.

Autopilot

It asks you some basic questions such as name, region, and you can have some other advanced options as well.

Creating an Autopilot cluster

Once you click on the create button on the above screen it creates the cluster in Autopilot Mode.

GKE Autopilot Cluster Created

You can click on the cluster and go to the details section.

Details

You can list the clusters with the following command.

gcloud container clusters list
Cluster is in running state

Configure Kuebctl With GKE Autopilot Cluster

Kubectl is the command-line utility for the Kubernetes. You need to install kubectl before you configure it. Click on the connect button on the console so that it gives you a command to configure kubectl with the GKE Cluster.

Cluster

Just copy the below command and run it on your terminal.

Connect to the cluster
gcloud container clusters get-credentials frontend-cluster-autopilot --region us-central1 --project staticweb-test

Once you run this command, kubectl is configured to use GKE Cluster and you can even get the nodes from the cluster. The one thing we need to remember here is that we didn’t create any nodes but, we can see the two nodes in the running state. That's the Autopilot in action.

kubectl is configured with GKE cluster

Deploy Kubernetes Objects on GKE Autopilot Cluster

Now we have configured kubectl to use GCP GKE from our own machine. Let’s create deployment and service objects and use the image from the GCP Container Registry.

First, you need to get the repository URL from the details section of Container Registry where our repositor resides.

Repository URL

Here is the manifest file which contains these objects. Notice we are using the same repository from the Container Registry.https://medium.com/media/d37271a64771b42fdd49ce21e6459092manifest.yml

If you cloned the above example project and you are at the root folder just use this command to create objects kubectl create -f manifest.yml

kubectl create -f manifest.yml
New
Rose Quartz Agate | Serving Tray with Brass Handles | Circular (Gold - Finish), Diameter(12 inch)
  • The natural look of agate stone creates a unique...
  • The edges of each piece are electroplated (NOT...
  • Handles are solid brass.
  • Available in 12", 14" and 16" diameter
  • Can do silver, rose gold, or gold plating for...
New
AANTHROPOLOGY By Rhea White Crystal Agate Cheese Platter/Tray | Circular (Gold - Finish), Diameter(12 inches)
  • The natural look of agate stone creates a unique...
  • The edges of each piece are electroplated (NOT...
  • Handles are solid brass.
  • Available in 12", 14" and 16" diameter
  • Can do silver, rose gold, or gold plating for...
New
Magenta Agate Aventurine Set of 4 Large Coasters/Sign Boards (Rose Gold - Finish)
  • Sold as a set of 4 pieces
  • 4" x 4"
  • 10 - 12 mm thick
  • Natural stone will vary in size, color, and...
  • Each piece has 4 rubber pads to avoid scratching...

You can use the following commands to verify all the objects are in the desired state.

// list the deployment
kubectl get deploy// list the pods
kubectl get po// list the service
kubectl get svc

The other difference we can notice in the Autopilot mode is that the number of pods in the running state. Even though we have declared 5 replicas in the manifest file we can only see 2pods running because of the Autopilot feature of the Cluster.

Kubernetes Objects Running on GCP GKE

Access the WebApp from the browser

We have created a service with the LoadBalancer type. You can get the external IP from the service and access the entire from the browser.

service

You can access the web app with the following URL

Original Postre>



Accessing from the browser

Delete the Cluster

You can just delete the cluster with the following command. Make sure you delete if you don’t want to incur charges.

gcloud container clusters delete [CLUSTER_NAME]
Advertisements

Summary

  • GCP introduces GKE Autopilot which is a new mode of operation in Google Kubernetes Engine (GKE) that is designed to reduce the operational cost of managing clusters, optimize your clusters for production, and yield higher workload availability.
  • The mode of operation refers to the level of flexibility, responsibility, and control that you have over your cluster. It still offers the standard version as well.
  • GKE is Google’s managed Kubernetes solution that lets you run and manage containerized applications in the cloud.
  • Before starting this, you need to have docker and Kubernetes essentials. If you don’t have these essentials please go through these with the links provided.
  • You need to create a GCP Account here.
  • You need a billing account to be associated with your project so that all the cost is billed to this billing account.
  • You can create the GKE cluster through a console, Gcloud CLI, REST API as well.
  • You can install gcloud CLI and configure it to use with your GKE Cluster.
  • Configure kubectl to use the GKE cluster.
  • Create a deployment and service with Loadbalancer so that you can access it from outside.
  • You can access the dashboard with either Kubeconfig or Token.
  • Make sure you delete if you don’t want to incur charges.

Conclusion

We have deployed a simple Angular application with .NET Core web API on Google GKE Cluster in Autopilot mode and access it from the browser. In future posts, we can see more advanced use cases.