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
- 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
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.
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
- If you are not familiar with the process or you want to know before studying this guide, I would recommend you going through it.
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
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.
- Docker Docs
- Docker — A Beginner’s guide to Dockerfile with a sample project
- Docker — Image creation and Management
- Docker — Container Management With Examples
- Understanding Docker Volumes with an example
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.
- 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
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.
You have to generate the key and download and set the environment variable called GOOGLE_APPLICATION_CREDENTIALS.
Finally, you can run the following command to log in to your GCP account.
gcloud auth login
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.
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.
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
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
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.
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
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
You can see the repository in the console as well.
Creating GKE Autopilot Cluster
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 \
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.
It asks you some basic questions such as name, region, and you can have some other advanced options as well.
Once you click on the create button on the above screen it creates the cluster in Autopilot Mode.
You can click on the cluster and go to the details section.
You can list the clusters with the following command.
gcloud container clusters list
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.
Just copy the below command and run it on your terminal.
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.
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.
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
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.
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.
You can access the web app with the following URL
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
- 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.
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.