In my last article here I did detail out steps required for setting up kubernetes cluster using Google Cloud web interface. But as programmer I do prefer working on things using Command Line / CLI. Here are the few advantage using CLI over web interface

  • If you know the right commands it is really fast & easy
  • Less memory consuming.
  • Command remains more or less on any OS
  • Writing a small task / hooking up everything to CD / CI pipeline becomes super simple

Assumption

You have already setup Google cloud SDK on your local environment. Here is my article which should help you setting up environment. Also to run a kubernetes cluster you will need valid google cloud account. If you are new to google cloud, on signup you will get 300$ credit to get started.

Let's get started.

Before we get started, please make sure you verify gcloud SDK installation by executing following command in your command line interface. This command should return installed sdk version.

gcloud --version
Verify gCloud on local system

If you see any error executing above command, here is documentation page which should help you resolve issues related to installation.

First thing before we create a kubernetes cluster, we will have to authenticate our command prompt interface with google cloud account. For this get back to command prompt and type

gcloud auth login --no-launch-browser
Authenticate google cloud CLI

This will display a URL which you can copy and paste in browser address bar of your choose. You will be navigate to google cloud login screen. After successful authentication you will be navigated to authorization screen.  

Once you click on Allow, you will see token text which needs to be copied back for authorization of command prompt. Paste this text in command prompt and hit enter. You should now see your registered email Id. --no-launch-browser comes handy when you setting up things with CLI on remote server or system which does not have GUI.

If you are using command prompt on OS which does have GUI like windows etc, please remove --no-launch-browser from above command. Above command without this parameter will directly open browser for authentication. On successful authentication and authorization you will automatically login in your command prompt.

Once you successfully authorize your account you will have to set project information in your command prompt. If you are having only one project assigned to your account, gcloud cli would automatically set it. If you want to set it manual you using command

gcloud config set project [project-ID]
Command for setting project manually

Project-ID should be in gcloud web interface, Top right next to "Google Cloud Platform".

Project Listing on Google Cloud Platform

Click on "My First Project" would open grid with Project Name & ID, Please refer to ID text for setting project on CLI. Just to confirm if you have set right project, you can use command

gcloud config list
GCloud config list command

Project ID mentioned next to Project in result should match with the project ID you set. Now to create a cluster we will need few details mentioned below

  • Project ID (Project-ID)
  • No of Nodes (No-Of-Nodes), Since this is sample application we will keep it to 1
  • Machine Type (Machine-Type) : g1-small, this should be enough for running sample application. But based on your need you can pick any from here
  • Zone (Zone-Name) : asia-south1-a, Since I am from Mumbai, IND asia-south1 -a is the nearest available zone. You can chose any zone based on your requirement. Detail listing is available here
  • Cluster Name (Cluster-Name) : sample-app, you can changes this name based on your choice
  • Network (Network): default, To keep it simple we will go with default network setting for now
  • Disk Size (Disk-Size): Default is 100 GB, but if we want to save on cost you can set it to 10 GB which is the minimum size

Here is how you command should look like with place holder

gcloud container --project "Project-ID" clusters create "Cluster-Name" --zone "Zone-Name" --machine-type "Machine-Type" --num-nodes "No-Of-Nodes" --network "Network"
GCloud Cluster creation template

Here is how the command will look with required parameters,

gcloud container --project "Project-XXXNNNN" clusters create "sample-app" --zone "asia-south1" --machine-type "asia-south1-a" --num-nodes "1" --network "default"

I don't want to share my project-id, that is the reason project id is marked with XXXNNNN. After successfully execution of above command your kubernetes cluster should be up and running. To verify if you are connected to your newly created cluster please execute following command

kubectl get pods
Command to list running container in cluster

Since this is a new cluster, you will see message "No resources found". Once you start deploying containers you should see list of container running in container.

More commands required while creating a cluster is available here

In our next article we will how we can deploy a Node.js docker container in kubernetes cluster and access it via internet.