{"id":7619,"date":"2017-12-12T06:51:59","date_gmt":"2017-12-12T11:51:59","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=7619"},"modified":"2021-02-16T06:14:54","modified_gmt":"2021-02-16T11:14:54","slug":"kubernetes-on-google-cloud-platform","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/kubernetes-on-google-cloud-platform\/","title":{"rendered":"Kubernetes on Google Cloud Platform"},"content":{"rendered":"<p>This is the second part in a series of posts about on getting started with Kubernetes. In my <a href=\"https:\/\/qxf2.com\/blog\/getting-started-with-kubernetes-setup\/\">previous post<\/a>, I covered how to setup single node of Kubernetes Cluster locally using Minikube and discussed a few issues which I came across during its setup. Since Kubernetes comes with its own toolset, we can pretty much configure and run the software on multiple cloud providers, on-premise servers and even our local machine. This post is a step by step guide on setting up a highly available multi-zone Kubernetes cluster using <a href=\"https:\/\/cloud.google.com\/\">Google Cloud Platform(GCP)<\/a>. Google Cloud Platform has excellent support for Kubernetes through the <a href=\"https:\/\/cloud.google.com\/kubernetes-engine\/\">Google Container Engine(GKE)<\/a> that provides a Container Engine service built on top of Kubernetes. This makes managing a Kubernetes cluster a breeze. In this post, I am going to detail on how to spin up multi-node Kubernetes cluster on GKE (Google Container Engine).<\/p>\n<p><a href=\"https:\/\/kubernetes.io\/\">Kubernetes<\/a> is a container orchestration software that started at Google. A cluster has a set of nodes (think computers) on which we have pods, where each pod is a unit that contains several containers. In simple terms, it&#8217;s about managing a cluster (a set of machines) on which we have containers running. To follow this post, you should have a minimal understanding of Kubernetes and Google Container Engine.<\/p>\n<hr \/>\n<h3>Setup And Configure Google Cloud Platform<\/h3>\n<p>In this section, I will be talking about pre-requisites to configure Google Cloud Platform and different ways to communicate to Google Cloud platform. <em><strong>Note<\/strong><\/em>:-Users of Google Cloud Platform are eligible for a $300 free trial.<\/p>\n<p><strong>Prerequisites<\/strong> &#8211; To follow this post,<\/p>\n<ol>\n<li>You should have a <a href=\"https:\/\/accounts.google.com\/SignUp\">Google Cloud account<\/a><\/li>\n<li>Create a Google Cloud Project\n<ol>i) Go to <a href=\"https:\/\/console.cloud.google.com\/home\/\">Google Cloud Platform console<\/a><\/ol>\n<ol>ii) Create a project. My project is &#8220;kubernetes-project&#8221;. Every project has an id that you&#8217;re mostly working with. My PROJECT ID is &#8220;kubernetes-project-184510&#8221;. This is a unique name across all Google Cloud projects<\/ol>\n<\/li>\n<\/ol>\n<p><strong>Ways to communicate to Google Cloud platform. <\/strong><\/p>\n<li> Once you are done with the basic setup, now we can create a cluster. One way of doing it is you can initialize and configure from a console window on the local window using <a href=\"https:\/\/cloud.google.com\/sdk\/docs\/quickstarts\">Google Cloud SDK<\/a>(the Google command line interface for Google Cloud platform). If you don&#8217;t have Google Cloud SDK installed, you can follow this tutorial for the setup <a href=\"https:\/\/cloud.google.com\/sdk\/docs\/quickstarts\">Google Cloud SDK<\/a>. It explains how to install the Google Cloud SDK, initialize it and run core gcloud commands from the command-line. Once you have installed the Google Cloud SDK, initialize and configure the <a href=\"https:\/\/cloud.google.com\/compute\/docs\/gcloud-compute\/\">gcloud CLI<\/a>. Once this set up is done your local system is communicating with your Google Cloud Platform account and project. After completing this step you will be able to create a Kubernetes cluster.<\/li>\n<li> The second way of communicating with Google Cloud Platform is by using <a href=\"https:\/\/cloud.google.com\/shell\/\">Google Cloud Shell<\/a>. Google Cloud interactive shell, is a browser-based command line environment running in the cloud. This Debian-based Docker container is loaded with all the development tools like docker, gcloud, kubectl. Google Cloud Shell comes pre-installed with the Google Cloud SDK. You can easily manage your projects and resources without having to install the Google Cloud SDK or other tools on your system. I have used Google Cloud interactive shell for executing the Kubernetes cluster creation commands in the below sections.<\/li>\n<hr\/>\n<h3>Creating Kubernetes Cluster On GKE<\/h3>\n<p> GKE Cluster can be created in two ways: via the gcloud Interactive Shell or via the <a href=\"https:\/\/console.cloud.google.com\/home\/\">Cloud Platform Console<\/a>. Both the methods use Google Container Engine (GKE) for creating clusters. In below sections, I will provide you with the steps for setting up a single and multi-zone Kubernetes cluster using both the methods.   <\/p>\n<h4>Cluster creation via Google Cloud Shell<\/h4>\n<p> You can create GKE cluster by following simple commands on Google Shell.<\/p>\n<li><strong>Enable the Container and Compute Engine APIs<\/strong>  <a href=\"https:\/\/console.cloud.google.com\/apis\/library\/container.googleapis.com\/\">Container Engine API<\/a> and <a href=\"https:\/\/console.cloud.google.com\/apis\/library\/compute.googleapis.com\/\">Compute Engine API<\/a>.<\/li>\n<li><strong>Activate Google Cloud Shell<\/strong> &#8211; Activate Google Cloud Shell by selecting the project(in<br \/>\nmy case &#8216;kubernetes-project&#8217;) created from the Google Cloud Platform dashboard. Then select the Activate Google Cloud Shell button in the top nav. Once connected, you are already authenticated and the PROJECT_ID environment variable is already set by default. <strong><em>Note<\/em><\/strong>: As mentioned earlier, Google Cloud Shell comes pre-installed with the Google Cloud SDK and built-in authorization for access to projects and resources hosted on Google Cloud Platform.<\/li>\n<figure id=\"attachment_7661\" aria-describedby=\"caption-attachment-7661\" style=\"width: 1287px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Google-Cloud-Shell.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Google-Cloud-Shell.png\" alt=\"\" width=\"1287\" height=\"523\" class=\"size-full wp-image-7661\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Google-Cloud-Shell.png 1287w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Google-Cloud-Shell-300x122.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Google-Cloud-Shell-768x312.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Google-Cloud-Shell-1024x416.png 1024w\" sizes=\"auto, (max-width: 1287px) 100vw, 1287px\" \/><\/a><figcaption id=\"caption-attachment-7661\" class=\"wp-caption-text\">Google Cloud Interactive Shell<\/figcaption><\/figure>\n<li><strong>Create Cluster using Gcloud shell<\/strong> &#8211; Below are the set of commands used to create one zone Kubernetes cluster using gcloud interactive shell.\n<pre lang=\"python\">\r\n$ CLUSTER_NAME=kubernetes-lab1\r\n\r\n$ gcloud config set container\/cluster ${CLUSTER_NAME} \r\n\r\n$ MACHINE_TYPE=n1-standard-2   \r\n\r\n# By default this spins up a 3-node cluster. You can change the default with `--num-nodes VAL`\r\n$ gcloud container clusters create ${CLUSTER_NAME} --zone europe-west1-d --scopes storage-rw --machine-type ${MACHINE_TYPE}<\/pre>\n<p>By default, if we do not specify the number of nodes and their types, then Container Engine will use three n1-standard-1 (1 vCPU, 3.75 GB memory) Compute Engine VMs for the cluster. In this example, I am creating a Kubernetes cluster in &#8216;europe-west1-d&#8217; zone with three instances (nodes) per zone using machine type &#8216;n1-standard-2&#8217;. The actual command would look like the following.<\/p>\n<pre lang='python'>\r\n$ gcloud container clusters create kubernetes-lab1 \\\r\n         --zone europe-west1-d \\\r\n         --scopes storage-rw \\\r\n         --machine-type n1-standard-2<\/pre>\n<p>You can also create a multi-zone cluster using below command<\/p>\n<pre lang='python'> \r\ngcloud container clusters create kubernetes-lab1 \\\r\n      --zone europe-west1-d \\\r\n      --additional-zones europe-west1-a,europe-west1-b \\\r\n      --scopes storage-rw \r\n      --machine-type n1-standard-2<\/pre>\n<p>Note that <em>&#8211;scopes<\/em> specifies scopes for the node instances. Multiple comma-delimited scopes can be specified. The scopes necessary for the cluster to function properly (compute-rw, storage-ro), are always added, even if not explicitly specified. I initially created the Kubernetes cluster without defining a permission scope. Hence I was getting &#8220;<em><strong>googleapi: Error 403: Insufficient Permission<\/strong><\/em>&#8221; error when I tried to access the node instances. If no scope is set when the cluster is created, the default scope is assigned and this only provides read permission for Cloud Storage. If you haven&#8217;t assigned your cluster with the correct scope, the nodes in the cluster would not have the required authorization\/permission to write to Google Cloud Storage. Later I managed to create a cluster with &#8216;storage-rw&#8217; scope and everything worked as expected.<\/li>\n<p>We can also check the status on the GCP Console. As a sanity check, to make sure the cluster is up and running via kubectl, use below command.<\/p>\n<pre lang=\"python\">$ kubectl get all\r\nNAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE\r\nsvc\/kubernetes   10.0.0.1             443\/TCP   22s\r\n<\/pre>\n<p>Since the cluster is fully setup with the Kubernetes platform we can start interacting with it by using the kubectl command as shown below<\/p>\n<pre lang=\"python\">$ kubectl cluster-info<\/pre>\n<figure id=\"attachment_7595\" aria-describedby=\"caption-attachment-7595\" style=\"width: 856px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/kubectl-clusterinfo.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-7595\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/kubectl-clusterinfo.png\" alt=\"\" width=\"856\" height=\"87\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/kubectl-clusterinfo.png 856w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/kubectl-clusterinfo-300x30.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/kubectl-clusterinfo-768x78.png 768w\" sizes=\"auto, (max-width: 856px) 100vw, 856px\" \/><\/a><figcaption id=\"caption-attachment-7595\" class=\"wp-caption-text\">Kubectl Cluster Information<\/figcaption><\/figure>\n<p>As you can see, the details contain the Kubernetes Master URL and other URLs for various Kubernetes services, one of which is the Kubenetes-Dashboard which is the Web UI running on the Kubernetes Master. <\/p>\n<p>The new cluster will also appear in the Container Engine -> Kubernetes cluster section within Google Cloud Platform:<\/p>\n<figure id=\"attachment_7656\" aria-describedby=\"caption-attachment-7656\" style=\"width: 1308px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances.png\" alt=\"\" width=\"1308\" height=\"236\" class=\"size-full wp-image-7656\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances.png 1308w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances-300x54.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances-768x139.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances-1024x185.png 1024w\" sizes=\"auto, (max-width: 1308px) 100vw, 1308px\" \/><\/a><figcaption id=\"caption-attachment-7656\" class=\"wp-caption-text\">Container Engine &#8211; Kubernetes cluster<\/figcaption><\/figure>\n<hr\/>\n<h4>Cluster creation via Cloud Platform Console<\/h4>\n<p> Creating a cluster using Google Cloud Console is pretty easy and straight forward.<\/p>\n<li>Go to <a href=\"https:\/\/console.cloud.google.com\/kubernetes\/list\">Container Engine page<\/a>, which can be found via the hamburger menu in the top left. Next, go to Container clusters and select Create a container cluster as shown in the below image\n<p><figure id=\"attachment_7714\" aria-describedby=\"caption-attachment-7714\" style=\"width: 726px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_create_lab2-2.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_create_lab2-2.png\" alt=\"\" width=\"726\" height=\"576\" class=\"size-full wp-image-7714\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_create_lab2-2.png 726w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_create_lab2-2-300x238.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/a><figcaption id=\"caption-attachment-7714\" class=\"wp-caption-text\">Cluster Creation using Google Cloud Console<\/figcaption><\/figure><\/li>\n<li>In this example, I am creating a Kubernetes cluster in &#8216;us-central1-a&#8217; zone with three instances (nodes) per zone using machine type &#8216;n1-standard-1&#8217;<\/li>\n<li>Select <em>Create<\/em> once you are done with the configuration. This will take a few minutes to complete. Once the cluster is created you should see something like this:\n<p><figure id=\"attachment_7658\" aria-describedby=\"caption-attachment-7658\" style=\"width: 1343px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_lab2.png\" data-rel=\"lightbox-image-4\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_lab2.png\" alt=\"\" width=\"1343\" height=\"314\" class=\"size-full wp-image-7658\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_lab2.png 1343w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_lab2-300x70.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_lab2-768x180.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/Cluster_lab2-1024x239.png 1024w\" sizes=\"auto, (max-width: 1343px) 100vw, 1343px\" \/><\/a><figcaption id=\"caption-attachment-7658\" class=\"wp-caption-text\">Cluster created in Google Cloud Console<\/figcaption><\/figure><\/li>\n<p>The nodes are managed by Compute Engine VMs and Kubernetes master is managed by Container Engine. Since the nodes are managed by Compute Engine VMs, we can also SSH into them. However, you cannot SSH into Kubernetes Master. Now you have a Kubernetes Cluster ready to use! Make sure that you turn off the VM&#8217;s after you finish your work.<\/p>\n<figure id=\"attachment_7659\" aria-describedby=\"caption-attachment-7659\" style=\"width: 1188px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances_lab2.png\" data-rel=\"lightbox-image-5\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances_lab2.png\" alt=\"\" width=\"1188\" height=\"290\" class=\"size-full wp-image-7659\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances_lab2.png 1188w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances_lab2-300x73.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances_lab2-768x187.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2017\/11\/cluster_instances_lab2-1024x250.png 1024w\" sizes=\"auto, (max-width: 1188px) 100vw, 1188px\" \/><\/a><figcaption id=\"caption-attachment-7659\" class=\"wp-caption-text\">Cluster Node created<\/figcaption><\/figure>\n<li> Once the cluster is up and running (this took few minutes to start up), we need to point kubectl to this cluster. Since the cluster is fully setup with the Kubernetes platform we can start interacting with it by using the kubectl command\n<pre lang=\"python\">\r\n$ gcloud container clusters get-credentials kubernetes-lab2<\/pre>\n<p><strong>Note:<\/strong> If you are using an existing Kubernetes Engine cluster or if you have created a cluster through Google Cloud Platform Console, you need to run the above command to retrieve cluster credentials and configure kubectl command-line tool with them. If you have already created a cluster with the gcloud container clusters create command listed above, this step is not necessary.<\/p>\n<hr\/>\n<p>In this post, we learned how to<br \/>\n1) Setup and configure Google Cloud Platform<br \/>\n2) Set up a kubernetes cluster using Google Cloud Shell and Google Cloud Console.<\/p>\n<p><strong>If you liked what you read, know more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2<\/a>.<\/strong><\/p>\n<hr\/>\n<h3>References<\/h3>\n<p>1) <a href=\"https:\/\/docs.bitnami.com\/kubernetes\/get-started-kubernetes\/\">Get Started With Kubernetes<\/a><br \/>\n2) <a href=\"https:\/\/deis.com\/blog\/2016\/first-kubernetes-cluster-gke\/\">Spinning Up Your First Kubernetes Cluster on GKE<\/a><br \/>\n3) <a href=\"https:\/\/stackoverflow.com\/questions\/47088830\/gcs-with-gke-403-insufficient-permission-for-writing-into-gcs-bucket\">googleapi: Error 403: Insufficient Permission<\/a><\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>This is the second part in a series of posts about on getting started with Kubernetes. In my previous post, I covered how to setup single node of Kubernetes Cluster locally using Minikube and discussed a few issues which I came across during its setup. Since Kubernetes comes with its own toolset, we can pretty much configure and run the [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[140,152,150,151],"tags":[],"class_list":["post-7619","post","type-post","status-publish","format-standard","hentry","category-docker","category-google-container-engine","category-kubernetes","category-minikube"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/7619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=7619"}],"version-history":[{"count":70,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/7619\/revisions"}],"predecessor-version":[{"id":14843,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/7619\/revisions\/14843"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=7619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=7619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=7619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}