Saturday, October 2, 2021

Run Kubernetes on Google Kubernetes Engine

Introduction

I was interested to learn Kubernetes. I tried to run the K8s on Minikube as well on macOS. There are some limitations in minikube on macOS so I tried it on GKE.

Account Setup on GKE

I followed below steps to signup on GCP. 

Open the link https://console.cloud.google.com/ on browser. 



Sign in using your email and password.



You will have to agree the terms.


Click on Activate button top right corner.


Choose the country and organization needs. 

There will be 2 more steps to Verify by phone number  and  Add credit card details . There will be charges if you don't stop after trial period or you exhaust $300 credit. 

After signup you will land on the home page like below.



Google Kubernetes Engine

You can access GKE from left hand navigation by click on Kubernetes Engine >> Clusters. 


If you are using it first time then you will have to enable it for you.



Click on create and it will open below page


Click on Configure  against GKE Standard. It will open below. 


Click on Create  button on the bottom of the page. It will open cluster page and will start the cluster for you. 

In few minutes cluster will be running state. 


The green dot o on the status shows the cluster is up and running. You can check the cluster details by clicking on cluster-1  link under name. Refer below. 


The cluster has 3 nodes.


Google Cloud Shell 

We will cloud shell to launch k8. 

Click on icon top right corner Cloud Shell - Spring Boot on GCP 

Refer below screenshot.


It will take few minutes to provision cloud instance and then provide a shell in bottom of the page.


You can now start typing your commands on the shell.

gcloud container clusters get-credentials cluster-1 --zone "us-central1-c"

This will open a pop-up and ask you to  authorize . You can click on authorize  button. 

The cloud shell will now have access to connect with GKE cluster. The output on shell will look like below:

aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$ gcloud container clusters get-credentials cluster-1 --zone "us-central1-c"
Fetching cluster endpoint and auth data.
kubeconfig entry generated for cluster-1.
aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$

Run below command to launch a hello-server using docker image from google container registry.

kubectl create deployment hello-server --image=us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0

Output will be like below:

aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$ kubectl create deployment hello-server --image=us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
deployment.apps/hello-server created
aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$
Expose the deployment via service.

kubectl expose deployment hello-server --type LoadBalancer --port 80 --target-port 8080
Output will be like below:

aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$ kubectl expose deployment hello-server --type LoadBalancer --port 80 --target-port 8080
service/hello-server exposed
aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$

Inspect and view the application

Inspect the running Pods by using kubectl get pods :

aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$ kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
hello-server-5bd6b6875f-p2z64   1/1     Running   0          6m46s
aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$

You should see one hello-server Pod running on your cluster.

Inspect the hello-server Service by using  kubectl get service :

aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$ kubectl get service
NAME           TYPE           CLUSTER-IP   EXTERNAL-IP    PORT(S)        AGE
hello-server   LoadBalancer   10.8.13.54   34.72.114.79   80:30778/TCP   6m
kubernetes     ClusterIP      10.8.0.1     <none>         443/TCP        33m
aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$
From this command's output, copy the hello-server Service's external IP address from the EXTERNAL-IP column.
Note: You might need to wait several minutes before the Service's external IP address populates. If the application's external IP is <pending>, run kubectl get again.

View the application from your web browser by using the external IP address with the exposed port:
http://EXTERNAL_IP
As the external ip is 34.72.114.79.

http://34.72.114.79
You have just deployed a containerized web application to GKE.


Clean up 

To avoid incurring charges to your Google Cloud account for the resources used in this page, follow these steps.

Delete the application's Service by running using  kubectl delete :
aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$ kubectl delete service hello-server
service "hello-server" deleted
aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$
This command deletes the Compute Engine load balancer that you created when you exposed the Deployment.

Delete your cluster by running  gcloud container clusters delete:

aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$ gcloud container clusters delete cluster-1 --zone "us-central1-c"
The following clusters will be deleted.
 - [cluster-1] in [us-central1-c]

Do you want to continue (Y/n)?  Y

Deleting cluster cluster-1...done.     
Deleted [https://container.googleapis.com/v1/projects/jovial-honor-327604/zones/us-central1-c/clusters/cluster-1].
aagarwal_jobs@cloudshell:~ (jovial-honor-327604)$
The cluster will be deleted once you run the delete command.



Happy Coding !!!



No comments:

Post a Comment