Kubernetes on GCP: Setting up a cluster on Google Cloud Platform (with GKE)
Prerequisites
Before you begin, you will need the following:
- A Google Cloud Platform account with administrative access
- A basic understanding of Kubernetes concepts
- A local machine with the
gcloud
andkubectl
command-line tools installed
Step 1: Create a GKE Cluster
Google Kubernetes Engine (GKE) is a managed Kubernetes service that makes it easy to run Kubernetes on GCP without the need to manage your own Kubernetes control plane. To create a GKE cluster, follow these steps:
- Open the GCP Console and navigate to the GKE console.
- Click on “Create cluster”.
- Choose a name for your cluster and select the region and zone where you want to create it.
- Choose the number and type of nodes you want to create in your cluster.
- Choose the machine type and size for your nodes.
- Choose the networking options for your cluster.
- Review your settings and click on “Create”.
After you click “Create”, the GKE service will create your cluster and the necessary GCP resources, such as Compute Engine instances and firewalls. This process may take several minutes.
Step 2: Configure kubectl
Once your cluster is created, you need to configure kubectl
to access it. Follow these steps:
- Install the
gcloud
CLI tool if you haven’t already done so. - Run the following command to authenticate
kubectl
with your GCP account: gcloud auth login
- This command opens a web page and asks you to log in to your GCP account.
- Run the following command to configure
kubectl
to use your GKE cluster: gcloud container clusters get-credentials my-cluster --zone us-central1-a --project my-project
- Replace
my-cluster
with the name of your GKE cluster,us-central1-a
with the zone where your cluster is located, andmy-project
with your GCP project ID. - This command updates your
kubectl
configuration to use the GCP account that you used to create your cluster. It also sets the current context to your GKE cluster.
Step 3: Verify Your Cluster
To verify that your cluster is working correctly, run the following command:
kubectl get nodes
This command should return a list of nodes in your cluster. If you see a list of nodes, your cluster is working correctly.
Step 4: Deploy Applications to Your Cluster
Now that you have a working GKE cluster, you can deploy applications to it. You can use Kubernetes manifests to deploy applications to your cluster.
Here’s an example manifest that deploys a simple nginx application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
Save this manifest to a file named nginx.yaml
, then run the following command to deploy it to your cluster:
kubectl apply -f nginx.yaml
This command will create a deployment with three replicas of the nginx container and a Service to expose the nginx Deployment.
In this tutorial, we explored how to set up a Kubernetes cluster on Google Cloud Platform using Google Kubernetes Engine (GKE). By following these steps, you can quickly create a Kubernetes cluster on GCP and deploy your applications to it.
Running Kubernetes on GCP has many advantages, such as easy management of your cluster and integration with other GCP services. With GKE, you can focus on deploying and managing your applications, and let GCP handle the underlying infrastructure.
If you’re interested in learning more about Kubernetes, check out my book: Learning Kubernetes — A Comprehensive Guide from Beginner to Intermediate by Lyron Foster ( https://a.co/d/aLXDvsZ )
Lyron Foster is a Hawai’i based African American Author, Musician, Actor, Blogger, Philanthropist and Multinational Serial Tech Entrepreneur.