Kubernetes cluster in GKE – first steps


Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/srv33763/domains/emilwypych.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

I use MacBook Pro on a daily basis, so I have no problem with Kubernetes cluster. I can just enable it in Docker for Mac configuration and then I can do almost whatever I want. If you use Linux or Windows, you can choose for example Minikube for testing purposes. But sometimes it’s not enough. There are some cases when you need something more – maybe you want to check how your application will behave in a more complex environment or just your workstation is not strong enough to run particular deployments. In such cases, Kubernetes cluster in GKE can be very helpful! And – what’s very important – you can deploy it in a very simple way. And more than one way. 🙂

What is GKE?

GKE is an abbreviation of Google Kubernetes Engine. It’s a Kubernetes as a Service provided by Google in their Google Cloud Platform. GKE allows us to the simple and quick provision of the Kubernetes cluster. We don’t need to think about stuff like zookeeper, etcd, DNS services and so on. Google takes all responsibility for provisioning and maintaining the whole cluster. Sound great, right? And it’s great at all. Of course, it has some limitation in comparison with our own Kubernetes cluster built on our own infrastructure, but in many cases, it’s enough. Especially if we just want to test some deployments and building the whole cluster is the last thing we are thinking about. In such cases, we can just run a Kubernetes cluster in GKE.

Kubernetes cluster in GKE – two ways

There are many ways in which you can run your own Kubernetes cluster in GKE, but I want to focus on two of them:

  • gcloud tool
  • terraform tool

The first one is an official tool from Google that provides the command line interface access to the whole Google Cloud Platform. If you work with GCP, I’m sure that you are familiar with this tool. The second one is a tool from Hashicorp and is used in Infrastructure as a Code approach. To be honest, it doesn’t matter which one you choose. Gcloud would be enough, but I just want to show you how to use an alternative solution – maybe will be better for you.

Let’s use gcloud!

In this scenario, I assume that you already have:

  • Cloud SDK and gcloud installed and configured
  • GCP project configured (e.g. GCP Free Tier)
  • kubectl installed (with Cloud SDK)

So if you have everything mentioned above done, you can start deploying Kubernetes cluster in GKE. Google Cloud Platform has great documentation, so you can take a look at the section on gcloud containers command. As you can see, there are many parameters you can pass to the command. You can feel giddy watching this, I know. But hey! You have this post! So to cut the long story short, you can use the following command to run your test cluster:

BEWARE! Do not use this command to run a PRODUCTION cluster! It’s just an example!

The command above will create a cluster places in the europe-west3 regions with the data nodes in two zones across that region. Also, it will use n1-standard-1 machine type (feel free to put any other type to minimize costs). It will create one data node per zone (by default this command creates three nodes per zone, so it’s good to use –num-nodes flag).

Check whether our Kubernetes cluster in GKE is ready

We can use gcloud again to do that.

should show our cluster, like:

Now, we can show more information about our cluster with the following command:

It will print yaml with multiple data like the number of nodes, oAuth Scopes, node pools, network configuration and so on.

Now let’s use Terraform!

Terraform in this scenario can look more complex and hard, but trust me – it’s a fantastic tool when you want to manage the whole infrastructure. And such little things like deploying your test cluster is a great way to learn how to use it. Of course, I’m going to leave backend configuration and syncing the state of the infrastructure, because I just want to to show you how to deploy Kubernetes cluster in GKE in an easy way. There are many fantastic articles on the Internet, where you can find more details about daily work with the Terraform.

Ok, so in this case, I assume that you have Terraform installed and you’ve made a preliminary configuration like Provider installation and so on. If not, please see the documentation.

I’ve never used Terraform – what should I do?!

Ok, I understand. You’ve never used Terraform before. So arguably you have no idea how to prepare your project, you don’t know that you should enable API in the GCP and generate a Service Account. Don’t worry! Please see the official Terraform Documentation:

Terraform Init

And then visit this fantastic tutorial on Elastisys:

HERE

You can find there a detailed tutorial of how to install, configure Terraform and what you should do on your GCP Project side to have everything needed for running Terraform.

Run your Kubernetes cluster in GKE with Terraform!

Ok, so you need a Terraform manifests, where you can store your configuration. It’s a very simple config, as we don’t want to create a complex cluster with all features enabled and configured. Just example like for gcloud way – simple cluster with one, small node pool. There you have three files you should create in the separate folder:

 

You should change necessary variables and names according to your needs – of course you can use more variables (e.g. you may want to have a possibility to change the type of the machines or something like that). I’ve created just a simple example.

When you have your Terraform manifests, you can just type the following commands:

As a result, you will see the progress of the creation. It makes take a few minutes until everything is ready – in my case it took:

After that, you can check if everything went well in a similar way as you could do that via gcloud.

But how can I access my Kubernetes cluster in GKE?!

Ok, yes, cluster without possibility to access it is useless. But don’t worry, Kubernetes cluster in GKE has a very simple way how you can solve this issue! Do you remember gcloud? Yeah, it’s the solution.

First, you need your cluster name. I assume that you remember that, but if not, you can list your GKE clusters:

Now you can just issue the following command:

And it’ll fetch and save configuration to the ~/.kube/config. Now you can just try to list all pods to check if everything works fine:

If you see any output (or just doesn’t see any errors…) it means that you have your own GKE cluster up and ready!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.