DEV Community 👩‍💻👨‍💻

Cover image for GCP Part -II (How to set up Kubernetes Engine)
Venkat
Venkat

Posted on • Updated on

GCP Part -II (How to set up Kubernetes Engine)

Hello there everyone!! Hope you're doing well and getting ready to read my yet another tech blog on Google Cloud Platform. This is continuation of part - I GCP Part -I (Regions, Zones and Compute Engine) and welcome to this blog and here you will get to know how to set up the Kubernetes Engine.

Kubernetes Engine

Let's quickly look into how to set up Kubernetes Engine in Google Cloud console as follows :

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Image description

Click Continue.

Image description

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID.

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

You can list the active account name with this command:

gcloud auth list
Enter fullscreen mode Exit fullscreen mode

You can list the project ID with this command:

gcloud config list project
Enter fullscreen mode Exit fullscreen mode

Set a default compute zone

Your compute zone is an approximate regional location in which your clusters and their resources live. For example, us-central1-a is a zone in the us-central1 region.

To set your default compute zone to us-central1-a, start a new session in Cloud Shell, and run the following command:

gcloud config set compute/zone us-central1-a
Enter fullscreen mode Exit fullscreen mode

Create a GKE cluster

A cluster consists of at least one cluster master machine and multiple worker machines called nodes. Nodes are Compute Engine virtual machine (VM) instances that run the Kubernetes processes necessary to make them part of the cluster.

Note: Cluster names must start with a letter and end with an alphanumeric, and cannot be longer than 40 characters.

To create a cluster, run the following command, replacing [CLUSTER-NAME] with the name you choose for the cluster (for example:my-cluster).

gcloud container clusters create [CLUSTER-NAME]
Enter fullscreen mode Exit fullscreen mode

You can ignore any warnings in the output. It might take several minutes to finish creating the cluster.

Expected output :

NAME        LOCATION       ...   NODE_VERSION  NUM_NODES  STATUS
my-cluster  us-central1-a  ...   1.16.13-gke.401  3        RUNNING
Enter fullscreen mode Exit fullscreen mode

Get authentication credentials for the cluster

After creating your cluster, you need authentication credentials to interact with it.

To authenticate the cluster, run the following command, replacing [CLUSTER-NAME] with the name of your cluster:

gcloud container clusters get-credentials [CLUSTER-NAME]
Enter fullscreen mode Exit fullscreen mode

Expected output :

Fetching cluster endpoint and auth data.
kubeconfig entry generated for my-cluster.
Enter fullscreen mode Exit fullscreen mode

Deploy an application to the cluster

You can now deploy a containerized application to the cluster.

GKE uses Kubernetes objects to create and manage your cluster's resources. Kubernetes provides the Deployment object for deploying stateless applications like web servers. Service objects define rules and load balancing for accessing your application from the internet.

To create a new Deployment hello-server from the hello-app container image, run the following kubectl create command:

kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0
Enter fullscreen mode Exit fullscreen mode

Expected output :

deployment.apps/hello-server created
Enter fullscreen mode Exit fullscreen mode

This Kubernetes command creates a Deployment object that represents hello-server. In this case, --image specifies a container image to deploy. The command pulls the example image from a Container Registry bucket. gcr.io/google-samples/hello-app:1.0 indicates the specific image version to pull. If a version is not specified, the latest version is used.

Note: You need to create hello-app image and should keep in the gcp bucket.

To create a Kubernetes Service, which is a Kubernetes resource that lets you expose your application to external traffic, run the following kubectl expose command:

kubectl expose deployment hello-server --type=LoadBalancer --port 8080
Enter fullscreen mode Exit fullscreen mode

In this command:

1. --port specifies the port that the container exposes.
2. type="LoadBalancer" creates a Compute Engine load balancer for your container.
Expected output :

service/hello-server exposed
Enter fullscreen mode Exit fullscreen mode

To inspect the hello-server Service, run kubectl get:

kubectl get service
Enter fullscreen mode Exit fullscreen mode

Expected output :

NAME              TYPE              CLUSTER-IP        EXTERNAL-IP      PORT(S)           AGE
hello-server      loadBalancer      10.39.244.36      35.202.234.26    8080:31991/TCP    65s
kubernetes        ClusterIP         10.39.240.1       <none>           433/TCP           5m13s
Enter fullscreen mode Exit fullscreen mode

Note: It might take a minute for an external IP address to be generated. Run the previous command again if the EXTERNAL-IP column status is pending.

To view the application from your web browser, open a new tab and enter the following address, replacing [EXTERNAL IP] with the EXTERNAL-IP for hello-server.

http://[EXTERNAL-IP]:8080
Enter fullscreen mode Exit fullscreen mode

Image description

Deleting the cluster

To delete the cluster, run the following command:

gcloud container clusters delete [CLUSTER-NAME]
Enter fullscreen mode Exit fullscreen mode

Congratulations!

You have just deployed 🚀 a containerized application to Kubernetes Engine! 🎉🎉🎉

Top comments (0)

12 Rarely Used Javascript APIs You Need

>> Check out this classic DEV post <<