DEV Community

Lucky Jain
Lucky Jain

Posted on

Comprehensive Guide to Learning Kubernetes with Google Cloud

Introduction to Kubernetes

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It helps manage workloads efficiently, ensuring high availability and scalability.

Key Concepts in Kubernetes

  1. Cluster: A set of nodes (machines) that run containerized applications.
  2. Node: A single machine in a Kubernetes cluster (physical or virtual).
  3. Pod: The smallest deployable unit in Kubernetes, which contains one or more containers.
  4. Service: An abstraction that defines a logical set of Pods and a policy to access them.
  5. Deployment: Manages the desired state of Pods and ReplicaSets.
  6. ConfigMap and Secret: Store configuration data and sensitive information, respectively.
  7. Namespace: Provides a way to divide cluster resources among multiple users.
  8. Ingress: Manages external access to services in a cluster, typically HTTP.

Setting Up Kubernetes on Google Cloud

Step 1: Prerequisites
  • A Google Cloud account. Sign up here.
  • Install the Google Cloud CLI (gcloud) on your local machine. Install gcloud CLI.
  • Install kubectl (Kubernetes CLI):
  gcloud components install kubectl
Enter fullscreen mode Exit fullscreen mode
Step 2: Create a Google Kubernetes Engine (GKE) Cluster
Using the Google Cloud Console:
  1. Go to the Google Kubernetes Engine page.
  2. Click Create Cluster.
  3. Choose the cluster type (e.g., Standard or Autopilot).
  4. Configure the cluster settings, such as the number of nodes, machine type, and location (zone/region).
  5. Click Create to provision the cluster.
Using the Command Line:
  1. Enable the Kubernetes Engine API:
   gcloud services enable container.googleapis.com
Enter fullscreen mode Exit fullscreen mode
  1. Create a GKE Cluster:
   gcloud container clusters create my-cluster --zone us-central1-a --num-nodes=3
Enter fullscreen mode Exit fullscreen mode

Replace my-cluster with your desired cluster name and us-central1-a with your preferred zone.

  1. Authenticate kubectl with the cluster:
   gcloud container clusters get-credentials my-cluster --zone us-central1-a
Enter fullscreen mode Exit fullscreen mode
Step 3: Verify the Cluster
  • Check the cluster nodes:
  kubectl get nodes
Enter fullscreen mode Exit fullscreen mode
  • You should see a list of nodes indicating the cluster is up and running.

Basic Kubernetes Operations

1. Deploy an Application
Using the Google Cloud Console:
  1. Navigate to the Workloads tab in the Kubernetes Engine section.
  2. Click Deploy.
  3. Specify the container image (e.g., gcr.io/google-samples/hello-app:1.0).
  4. Configure the deployment settings (e.g., replicas, namespace).
  5. Click Deploy.
Using kubectl:
  • Create a Deployment:
  kubectl create deployment hello-app --image=gcr.io/google-samples/hello-app:1.0
Enter fullscreen mode Exit fullscreen mode
  • Expose the Deployment as a Service:
  kubectl expose deployment hello-app --type=LoadBalancer --port=80 --target-port=8080
Enter fullscreen mode Exit fullscreen mode
  • Get the external IP of the Service:
  kubectl get service hello-app
Enter fullscreen mode Exit fullscreen mode
  • Access the application using the external IP.
2. Scale the Application
Using the Google Cloud Console:
  1. Go to the Workloads tab.
  2. Click on the Deployment you want to scale.
  3. Adjust the number of replicas and click Save.
Using kubectl:
  • Scale the Deployment to 3 replicas:
  kubectl scale deployment hello-app --replicas=3
Enter fullscreen mode Exit fullscreen mode
  • Verify the scaling:
  kubectl get pods
Enter fullscreen mode Exit fullscreen mode
3. Update the Application
Using the Google Cloud Console:
  1. Go to the Workloads tab.
  2. Click on the Deployment you want to update.
  3. Edit the container image to the new version (e.g., gcr.io/google-samples/hello-app:2.0).
  4. Click Save to initiate the update.
Using kubectl:
  • Update the image version:
  kubectl set image deployment/hello-app hello-app=gcr.io/google-samples/hello-app:2.0
Enter fullscreen mode Exit fullscreen mode
  • Monitor the rollout status:
  kubectl rollout status deployment/hello-app
Enter fullscreen mode Exit fullscreen mode
4. Delete Resources
Using the Google Cloud Console:
  1. Navigate to the Workloads or Services & Ingress tab.
  2. Select the resource you want to delete.
  3. Click Delete and confirm.
Using kubectl:
  • Delete the Service:
  kubectl delete service hello-app
Enter fullscreen mode Exit fullscreen mode
  • Delete the Deployment:
  kubectl delete deployment hello-app
Enter fullscreen mode Exit fullscreen mode

Advanced Topics

ConfigMaps and Secrets
Using the Google Cloud Console:
  1. Go to the ConfigMaps or Secrets tab under Kubernetes Engine.
  2. Click Create and fill in the required details.
Using kubectl:
  • ConfigMap example:
  kubectl create configmap my-config --from-literal=key1=value1
Enter fullscreen mode Exit fullscreen mode
  • Secret example:
  kubectl create secret generic my-secret --from-literal=password=12345
Enter fullscreen mode Exit fullscreen mode
Helm Charts
  • Helm is a package manager for Kubernetes.
  • Install Helm:
  curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
Enter fullscreen mode Exit fullscreen mode
  • Use Helm to deploy applications:
  helm repo add bitnami https://charts.bitnami.com/bitnami
  helm install my-release bitnami/nginx
Enter fullscreen mode Exit fullscreen mode
Monitoring and Logging
Using the Google Cloud Console:
  1. Go to the Operations tab in the Cloud Console.
  2. Use Metrics Explorer to monitor cluster performance.
  3. Access logs under Logging > Log Explorer to view system and workload logs.
Using gcloud CLI:
  • Enable logging for your cluster:
  gcloud container clusters update my-cluster --logging=SYSTEM,WORKLOAD --monitoring=SYSTEM
Enter fullscreen mode Exit fullscreen mode

Cleaning Up

Using the Google Cloud Console:
  1. Navigate to the Kubernetes Engine page.
  2. Select the cluster you want to delete.
  3. Click Delete and confirm.
Using gcloud CLI:
  • Delete the GKE cluster to avoid incurring costs:
  gcloud container clusters delete my-cluster --zone us-central1-a
Enter fullscreen mode Exit fullscreen mode

Additional Resources

By following this guide, you can learn Kubernetes basics and effectively use it with Google Cloud, both locally and through the Google Cloud Console. Expand your knowledge by exploring advanced features and best practices.

Top comments (0)