Practicing with Google Cloud Platform - Google Kubernetes Engine to deploy nginx
Tags: kubernetes, gcp, beginners, devops
Published: 2024-07-17 03:00:00

Introduction
------------

This article provides simple step-by-step instructions for those who are new to **Google Cloud Platform (GCP)** and **Google Kubernetes Engine (GKE)**.

I'll guide you through using **GKE** to create clusters and deploy **nginx**.

The instructions below will primarily use **gcloud** and **kubectl** to initialize the cluster, which is more convenient than manual management on the **Google Cloud** interface.

![Google Cloud Kubernetes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xhbp3xcgwfqnp8dw6z2.png)

Prerequisites
-------------

First, you need to prepare the following:

*   Have a **GCP** account with permission to use Cloud services. If you're new, you'll get a **$300 free trial** to use for **90 days**.
*   Create a new **GCP** project.
*   Enable **Compute Engine** and **Kubernetes Engine** services.

  

Install Google Cloud SDK and kubectl
------------------------------------

For this installation step, refer to the [**GCP** documentation](https://cloud.google.com/sdk/docs/install#installation_instructions) for instructions tailored to your operating system.

Once installed, execute the following commands to check if **gcloud** and **kubectl** are installed:

```sh
gcloud version
kubectl version
```

If you see the version result, we'll proceed to the next part.

![gcloud version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egwqupuq3ttsdzij6bnv.png)

Initialize **Google Cloud SDK** authentication
----------------------------------------------

Please execute the following command to configure **Google Cloud**:

```sh
gcloud init
```

Next, you'll follow the instructions to log in with your **Google Cloud** account. Then, you'll be prompted to configure the default **Compute Region and Zone**. It's important to note that different regions have different pricing for machine types. You can check the prices on the Create VM instance UI. However, you should still choose the region that best suits your needs to ensure the best network speed.

![Google Cloud Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o13xna17kxb3as07m2tp.png)

Creating GKE Cluster
--------------------

To create a **GKE Cluster**, simply execute the following command:

```sh
gcloud container clusters create {cluster name}
gcloud container clusters create k8s-cluster
```

If you want to specify specific details for the cluster, use the following command:

```sh
# gcloud container clusters create {cluster name} \
#   --project {project id} \
#   --zone {zone id} \
#   --machine-type {machine type id} \
#   --num-nodes {number of node}

gcloud container clusters create k8s-cluster \
  --project cluster-1 \
  --zone asia-southeast1-a \
  --machine-type e2-micro \
  --num-nodes 1
```

Here, I'm using the machine type **e2-micro**, which has a simple configuration and a relatively cheap price, sufficient for you to follow along with this article.

  

After successfully initializing the cluster, execute the following command to list the instances currently available:

```sh
gcloud compute instances list
```

Use kubectl to deploy nginx
---------------------------

Execute the following command to deploy **nginx** using a **Docker image**:

```sh
# kubectl create deployment {service name} --image={image name} --replicas={number}
kubectl create deployment service-name --image=nginx --replicas=1
```

![Deploy service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4dm2w9ssnrhv07yqo0w.png)

To check the pods currently available, execute the following command:

```sh
kubectl get pods
```

Next, I'll create a **LoadBalancer Service** to access the **Pod** from outside the **Cluster** as follows:

```sh
# kubectl expose deployment {service name} --name={load balance service name} --type=LoadBalancer --port={port load balancer service} --target-port={port pod}
kubectl expose deployment service-name --name=service-name-lb --type=LoadBalancer --port=80 --target-port=80
```

![Expose service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7v4tau4e3cdjna3qh0i.png)

To check the **LoadBalancer service**, use the following command:

```sh
kubectl get svc
# or
kubectl get svc service-name-lb
```

![Service info](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bwbsjggo9rwitwf0icf.png)

If you run this command too early, the **EXTERNAL-IP** column will be **\<pending\>**. Please wait a few minutes until you see an IP address in the **EXTERNAL-IP** column to access it. The result will be as follows:

![Nginx has been successfully deployed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/504pmlrshr9onf4pydx1.png)

Cleaning Up Resources
---------------------

After completing the steps outlined in the article to avoid any unwanted costs, delete the resources and services as follows:

```sh
# remove LoadBalancer service
kubectl delete service service-name-lb

# remove GKE cluster
gcloud container clusters delete k8s-cluster
```

![Result after deleted service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fwjjplgp8o5hq8ismap.png)

To check if the resources and services have been deleted, you can use the commands I provided earlier or directly check on the **Google Cloud Console**.

```sh
kubectl get svc
gcloud compute instances list
```

**_If you have any suggestions or questions regarding the content of the article, please don't hesitate to leave a comment below!_**

**_If you found this content helpful, please visit [the original article on my blog](https://howtodevez.blogspot.com/2024/04/practicing-with-google-cloud-platform-google-kubernetes-engine-to-deploy-nginx.html) to support the author and explore more interesting content._**

<a href="https://howtodevez.blogspot.com/2024/03/sitemap.html" target="_blank" rel="noreferrer"><img src="https://img.shields.io/badge/Blogger-FF5722?style=for-the-badge&logo=blogger&logoColor=white" width="36" height="36" alt="Blogspot" /></a><a href="https://dev.to/chauhoangminhnguyen" target="_blank" rel="noreferrer"><img src="https://img.shields.io/badge/dev.to-0A0A0A?style=for-the-badge&logo=dev.to&logoColor=white" width="36" height="36" alt="Dev.to" /></a><a href="https://www.facebook.com/profile.php?id=61557154776384" target="_blank" rel="noreferrer"><img src="https://img.shields.io/badge/Facebook-1877F2?style=for-the-badge&logo=facebook&logoColor=white" width="36" height="36" alt="Facebook" /></a><a href="https://x.com/DavidNguyenSE" target="_blank" rel="noreferrer"><img src="https://img.shields.io/badge/X-000000?style=for-the-badge&logo=x&logoColor=white" width="36" height="36" alt="X" /></a>