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.
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.
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.
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.
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.
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.
// 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.
You can see the repository in the console as well.
Container Registry
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 \ --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.
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.
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
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.
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 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.