Google Cloud Platform - Using the Kubernetes API
Last Updated :
09 Jun, 2022
In this article, we will look at the Kubernetes API and how it makes modeling the application lifecycle easier. Let's go over the concepts that make Kubernetes usable, scalable, and just downright awesome.
When it comes to what scalable applications need, we've talked about containers and nodes, but that's just the tip of the iceberg. There's plenty more we need in order to run a fully scalable application. That's where the Kubernetes API comes in. It offers some really convenient primitives that make managing cloud-native applications a lot easier. A few API objects are pods and nodes, deployments, secrets, and many more.
Let's take an example application. We established that containers are the first step to using Kubernetes, so let's start with the container that we built to run a Hello app. This app is really simple. It just returns "Hello" whenever someone pings it on its local IP in port 8080.
The first thing we have to do is to create some machines, or nodes, to run our application on. Here we'll use Google Kubernetes Engine to quickly get started. We can just use the Gcloud command-line tool to provision a Kubernetes cluster using the below command:
$ gcloud container cluster create hello-cluster
After a few minutes, we'll have a cluster. By default, it comes with three nodes.

This is a great starting point, but now we need to actually run our app. Since we're using the command line, we can use a handy tool called kubectl to help interact with the Kubernetes API using the below command:
$ kubectl create deployment dbd --image \
mydb/example-db:1.0.0 --record
This command is actually going to create a Kubernetes API object called a deployment. A deployment is an abstraction that manages the lifecycle of an application. We can set the desired number of app instances for the deployment to manage, and then it will make sure the correct number of instances, or replicas, are running.

If we increase the number of replicas that we want, the deployment will see that there are currently not enough replicas and spin up another one.

That even works when a node crashes. If the node goes down, the current state is once again different from the desired state, and Kubernetes will schedule another replica for us.

Now we know our app is running on these nodes. To access it we'll have to create a service using the below command:
$ kubectl expose deployment dbd \
--port 80 --type Loadbalancer
This creates an endpoint that we can use to access the running app instances. In this case, we have multiple app instances. So this service will load-balance incoming requests between the two running pods. For any container inside of the cluster, they can connect to our service using the service's name. Either way, the service keeps track of wherever the pod is running.

That is another example of how Kubernetes removes the need to manually keep track of where your containers are running. Even if a pod were to go down, once a new one comes back online, the service will automatically update its list of endpoints to target the new pod.
Kubernetes objects, like deployments and services, automatically ensure that we have the right number of app instances running through pods and that we can always reach them. Features that used to have to be manually coded just become afterthoughts when using Kubernetes. For example, deployments make things like rolling updates really simple. We can edit the deployment and watch the version of the applications gradually change. So let's say we have three replicas, and we want to put out a new version of our application that returns "Bye" instead of "Hello. We can update our application container, watch as the new version is gradually rolled out, and the deployment will bring up new application instances and start rerouting traffic to them. Then once the desired number of new instances are online, the old application instances are taken offline.

This would mean this approach has zero downtime. Since Kubernetes is incrementally updating old pod instances with new ones. And a feature like that, plus the ability to quickly roll back if necessary, and keep track of deployment histories and more, all of that is just built into Kubernetes.
That's a large part of what makes it such a great tool to build other systems and applications on top of. The Kubernetes API really does make it easy to manage the application lifecycle. The basic primitives' pods, service, deployments, plus a few more allow sysadmins and developers to focus on the app without having to worry about managing it at scale. We used an imperative approach. We made manual commands, instead of using a declarative approach, which is one of the big appeals of Kubernetes.
Similar Reads
Google Cloud Platform - Overview of G Suite APIs
Many of you know how to use Gmail, Google Drive, and Docs, and that's great. But can you code them too? In this article we aim to learn about G Suite as another tool, giving you the ability to code those apps you know so well. Productivity tools like a word processor, spreadsheets, and presentation
5 min read
Google Cloud Platform - Concept of Nodes in Kubernetes
In this article, we will figure out how Kubernetes actually runs containers on nodes. Let's go over the concepts that make Kubernetes usable, scalable, and just downright awesome. A node is a virtual or physical machine that you run your workloads on. In Kubernetes, we operate on clusters, which are
3 min read
Why Google Cloud Platform?
Pre-requisite: GCP Google Cloud Platform(GCP) is a collection of calculating services that run on the same structure that Google uses internally for its end-stoner products, similar to Google Search and YouTube. GCP offers a wide range of services, including storehouse, networking, big data, machine
4 min read
Google Cloud Platform - Setting Up a Game Server
In this article, we will set up a server in a public cloud to serve a game application by using infrastructure as a service (IaaS) provided by the google cloud platform. However, the process will be nearly the same for any other public cloud platforms. Overview of steps:Set up the account and create
4 min read
Introduction to Google Cloud Platform
Google Cloud Platform (GCP) is an initiative by Google to provide cloud computing services to customers. These services run on the same infrastructure and platform on which Google services such as Gmail, YouTube, etc run. GCP was launched on April 7, 2008, and the complete set of services and the pl
5 min read
What is Google Cloud Platform (GCP)?
Google Cloud Platform (GCP) is a cloud computing service by Google that helps businesses, developers, and enterprises run applications, store data, and manage workloads on a secure, scalable, and high-performance infrastructure. Whether you're building a website, handling large datasets, or running
15+ min read
Google Cloud Platform - Cloud Storage
Google Cloud Storage is unified object storage. In reality, the GCS is the place where you can store and serve static binary assets either for your app to use or directly to your users. But as straightforward, as it sounds, there is a lot going under the hood. Google Cloud Storage The GCP has Bucket
2 min read
Google Cloud Platform - Ways of Uploading Data to GCS
Before you can harness the power of the cloud, to serve your content, you have to get your data into it. In this article, we will look into all the different ways you can upload data. We all know that Google Cloud Storage (GCS) can serve your binary assets to users worldwide at high speed and low pr
3 min read
How To View Request Logs On Google Cloud Platform ?
Google Cloud Platform Logs Explorer is a tool that enables users to retrieve view and analyze log data helping them troubleshoot problems and monitor their resources effectively. Logs are records of event ed through Google Cloud Console and log entries can be read through the Logging API or Google C
4 min read
Google Cloud Run - Working with Python
Python is one of the most popular programming languages and growing. It's well-suited for a number of use cases, including web applications, machine learning, and big data. Cloud Run lets you use any runtime you want, making it easy to deploy Python in a serverless way. Cloud Run is also fully manag
3 min read