A Step-by-Step Guide to Deploying Kubernetes on Google Cloud Platform (GCP)
Introduction
In today’s data-driven world, deploying applications at scale is crucial for businesses. Kubernetes, an open-source container orchestration platform, has gained immense popularity due to its ability to manage and scale containerized applications efficiently. In this tutorial, we will demystify the process of deploying Kubernetes on the Google Cloud Platform (GCP), enabling you to leverage the power of Kubernetes for your applications.
Step 0:
GCP Setup Before diving into Kubernetes deployment, we need to set up our GCP environment. Follow these steps:
- Create a GCP account and log in to the console.
- Enable the Kubernetes Engine API in the console.
3. Install the gcloud command-line tool for managing your GCP resources.
It is recommended to follow the official guide to install gcloud, but you can also use the following command for your convenience:
# download the gcloud tar.gz
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-316.0.0-darwin-x86_64.tar.gz
# extract the file
tar -xf google-cloud-sdk-316.0.0-darwin-x86_64.tar.gz
# install gloud
./google-cloud-sdk/install.sh
# initialize gcloud
./google-cloud-sdk/bin/gcloud init
4. Install kubectl, the command-line interface for interacting with Kubernetes clusters.
gcloud components install kubectl
Step 1. Kubernetes Setup
- Use the gcloud command to create a managed Kubernetes cluster, specifying parameters such as machine type, number of nodes, and compute zone.
gcloud container clusters create \
--machine-type n1-standard-2 \
--num-nodes 3\
--zone <compute zone from the list linked below> \
--cluster-version latest \
<CLUSTERNAME>
2. Verify the cluster creation by running the command:
kubectl get node.
3. Grant admin permissions to the user by creating a cluster role binding.
kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole=cluster-admin \
--user=<GOOGLE-EMAIL-ACCOUNT>
4. Install Helm, a package manager for Kubernetes applications, using the provided script.
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
With Helm installed, we can check what is installed in the cluster by calling:
helm list
Step 2: Deploying Applications with Kubernetes
Before starting the installation process, it’s important to understand the structure and requirements. Ensure that you have set up a Kubernetes cluster with Helm installed. In this example, we will deploy JupyterHub, which allows serving multiple users.
Security Setup:
- Generate a proxy security token using the command
openssl rand -hex 32
. Save the generated token for later use. It is recommended to use SSL encryption for JupyterHub. - Add the generated token to the proxy configuration. This can be done in the configuration file or by setting it as an environment variable. With Helm installed, you can use Helm to template the YAML file and add the SSL token.
Adding Configuration:
- JupyterHub provides a comprehensive guide on setting up the configuration. Customize the configuration based on your needs.
- Use the appropriate tag to specify the Docker image for the data science notebook from the provided list.
- Set the memory limit for each user, which will be sent as requests to the Kubernetes API.
Install with Helm:
- Use Helm to install JupyterHub using the
helm upgrade
command. TheRELEASE
parameter specifies the name of the installation and theNAMESPACE
parameter identifies the namespace withkubectl
. - During future upgrades, there is no need to create the namespace again.
Monitor Installation:
- Wait for the installation to complete. JupyterHub will provide finishing messages once it is set up properly.
- If any issues arise, refer to the JupyterHub guide or use
kubectl get events
it to troubleshoot and view the log.
Check Status and Test JupyterHub:
- Verify the status of JupyterHub by running
kubectl --namespace=jhub get pod
. - Obtain the IP address to access the Hub by executing
kubectl --namespace=jhub get svc proxy-public
. - Copy the external IP and paste it into a new browser. It should redirect you to the JupyterHub sign-in page.
- You can now start using your own JupyterHub with the Kubernetes cluster.
Note: In this example, we haven’t set up user authentication, so you can enter a random username and password to get started. If you require more security, refer to the provided link to set up authentication with JupyterHub.
https://jupyterhub.readthedocs.io/en/stable/tutorial/getting-started/authenticators-users-basics.html
Clean-up and Final Thoughts:
When you no longer need the Kubernetes cluster, it’s essential to clean up your resources to avoid unnecessary costs. Follow these steps to delete the cluster:
- Retrieve the list of clusters using the gcloud command.
- Delete the cluster using the gcloud command, specifying the cluster name.
Conclusion:
Deploying Kubernetes on GCP allows you to leverage the power of container orchestration and scale your applications efficiently. In this tutorial, we covered the step-by-step process of setting up GCP, creating a Kubernetes cluster, and deploying an application using JupyterHub. By following these instructions, you can unlock the potential of Kubernetes for your own projects and benefit from its scalability and extensibility. Happy deploying!
Thank you for reading this blog, and I hope you found it helpful in your DevOps journey. If you enjoyed this article, please give it a clap to show your appreciation.
Buy me a coffee, if you like my article :)