In this guide, let’s create a Kubernetes cluster using Google Cloud Platform (GCP) account and connect to this cluster using GCP Cloud SDK.
Table of contents
- Create a Google Cloud Platform (GCP) account
- Create a new Kubernetes cluster on GCP
- Download and configure Google Cloud SDK on local PC
- Connect to GCP Kubernetes cluster using Google Cloud SDK
- Kubernetes commandline setup
- Try out basic kubernetes commands
Create a Google Cloud Platform (GCP) account
Sing up for Google Cloud Platform.
The first step is to create an account in Google Cloud Platform. Log into Google Cloud Platform using your Gmail/Google account credentials here.
Create a new project
Traverse to Kubernetes Engine > Clusters and create a new Project.
Create a new Project
Here, I have created a new project called codeaches-project.
I have opted for a free trial to start using Kubernetes Engine.
Create a new Kubernetes cluster on GCP
Once, you subscribe to free trial, traverse to Kubernetes Engine > Clusters and click on Create Cluster to create a new kubernetes cluster.
I have opted for default values where we get 3 nodes in a cluster.
Now we have a kubernetes cluster ready for us!
At this point, we have a cluster codeaches-cluster-1 in our project codeaches-project on Google Cloud Platform. Let’s move on to downloading the Google Cloud SDK on our local PC which will help us with connecting to the above created cluster.
Download and configure Google Cloud SDK on local PC
Download the Google Cloud SDK installer using the link here.
Once the installation is complete, open the command prompt and vefiry that the Cloud SDK is setup.
> gcloud version
Google Cloud SDK 275.0.0 bq 2.0.51 core 2020.01.03 gsutil 4.46
Connect to GCP Kubernetes cluster using Google Cloud SDK
Use the gcloud init command to perform several common SDK setup tasks. These include authorizing the SDK tools to access Google Cloud Platform using your user account credentials and setting up the default SDK configuration.
> gcloud init
Welcome! This command will take you through the configuration of gcloud. Your current configuration has been set to: [default] You can skip diagnostics next time by using the following flag: gcloud init --skip-diagnostics Network diagnostic detects and fixes local network connection issues. Checking network connection...done. Reachability Check passed. Network diagnostic passed (1/1 checks passed). You must log in to continue. Would you like to log in (Y/n)? y Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?.................... You are logged in as: [email@example.com]. Pick cloud project to use:  secret-argon-123456  Create a new project Please enter numeric choice or text value (must exactly match list item): 1 Your current project has been set to: [secret-argon-123456].
Note that the project ID
secret-argon-123456 corresponds to the earlier created project with name
secret-argon-123456 is a random ID assigned by Google Cloud Platform.
> gcloud projects list
PROJECT_ID NAME PROJECT_NUMBER secret-argon-123456 codeaches-project 654321123456
Kubernetes commandline setup
Earlier, we had created a kubernetes cluster named codeaches-cluster-1 using the Google Cloud Platform UI.
To access the Kubernetes Engine cluster, we need kubernetes commandline. Let’s install the
> gcloud components install kubectl
Once, kubectl is installed successfully, configure the local
kubectl to point to codeaches-cluster-1 on GCP.
> gcloud container clusters get-credentials codeaches-cluster-1 --zone us-central1-a --project secret-argon-123456
Fetching cluster endpoint and auth data. kubeconfig entry generated for codeaches-cluster-1.
Now you are all set to use the locally installed Cloud SDK to interact with Kubernetes Cluster on GCP
Try out basic kubernetes commands
Get cluster details
> kubectl cluster-info
Kubernetes master is running at https://184.108.40.206 GLBCDefaultBackend is running at https://220.127.116.11/api/v1/namespaces/kube-system/services/default-http-backend:http/proxy Heapster is running at https://18.104.22.168/api/v1/namespaces/kube-system/services/heapster/proxy KubeDNS is running at https://22.214.171.124/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy Metrics-server is running at https://126.96.36.199/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Get node details
> kubectl get nodes
NAME STATUS ROLES AGE VERSION gke-codeaches-cluster-1-default-pool-38905352-080c Ready <none> 8m31s v1.12.8-gke.10 gke-codeaches-cluster-1-default-pool-38905352-km5p Ready <none> 8m31s v1.12.8-gke.10 gke-codeaches-cluster-1-default-pool-38905352-mk4c Ready <none> 8m31s v1.12.8-gke.10
This concludes our guide to creating creating Kubernetes cluster on GCP and connecting to it through Cloud SDK running on our local machine!
Your feedback is always appreciated. Happy coding!