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:
1 2 3 4 5 |
gcloud container clusters create my-cluster --region europe-west3 \ --node-locations europe-west3-a,europe-west3-b \ --machine-type=n1-standard-1 \ --num-nodes 1 \ --project bearit-test |
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.
1 |
$ gcloud container clusters list |
should show our cluster, like:
1 2 3 |
$ gcloud container clusters list NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS my-cluster europe-west3 1.12.8-gke.10 35.198.186.219 n1-standard-1 1.12.8-gke.10 2 RUNNING |
Now, we can show more information about our cluster with the following command:
1 |
$ gcloud container clusters describe my-cluster --region europe-west3 |
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:
And then visit this fantastic tutorial on Elastisys:
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:
1 2 3 4 5 6 |
provider "google" { credentials = "${file("YOUR_KEY_FILE.json")}" project = "${var.project_id}" region = "europe-west3" zone = "europe-west3-a" } |
1 2 3 4 5 6 |
variable "project_id" { default = "YOUR_PROJECT" } variable "default_region"{ default = "europe-west3" } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
resource "google_container_cluster" "primary" { name = "my-cluster" location = "${var.default_region}" project = "${var.project_id}" # From Terraform Provider documentation: # We can't create a cluster with no node pool defined, but we want to only use # separately managed node pools. So we create the smallest possible default # node pool and immediately delete it. initial_node_count = 1 remove_default_node_pool = true node_locations = [ "europe-west3-a", "europe-west3-b", ] } resource "google_container_node_pool" "gke_first_pool" { name = "gke-node-pool" location = "${var.default_region}" cluster = "${google_container_cluster.primary.name}" node_count = 1 node_config { preemptible = true machine_type = "n1-standard-1" metadata = { disable-legacy-endpoints = "true" } oauth_scopes = [ "https://www.googleapis.com/auth/logging.write", "https://www.googleapis.com/auth/monitoring", ] } } |
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:
1 2 |
$ terraform plan -out=./k8s-plan $ terraform apply ./k8s-plan |
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:
1 2 |
google_container_cluster.primary: Creation complete after 4m35s [id=my-cluster] google_container_node_pool.gke_first_pool: Creation complete after 46s [id=europe-west3/my-cluster/gke-node-pool] |
After that, you can check if everything went well in a similar way as you could do that via gcloud.
1 2 3 |
$ gcloud container clusters list NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS my-cluster europe-west3 1.12.8-gke.10 35.246.144.253 n1-standard-1 1.12.8-gke.10 2 RUNNING |
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:
1 |
$ gcloud container clusters list |
Now you can just issue the following command:
1 |
$ gcloud container clusters get-credentials CLUSTER_NAME |
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:
1 |
$ kubectl get pods --all-namespaces |
If you see any output (or just doesn’t see any errors…) it means that you have your own GKE cluster up and ready!