Monday, November 8, 2021

Kubernetes: A quick lab on GCP

 

Kubernetes on Google Cloud

When you run a GKE cluster, you also gain the benefit of advanced cluster management features that Google Cloud provides. These include:

Now that you have a basic understanding of Kubernetes, you will learn how to deploy a containerized application with GKE in less than 30 minutes. Follow the steps below to set up your lab environment.


Task 1: Set a default compute zone

Your compute zone is an approximate regional location in which your clusters and their resources live. For example, us-central1-a is a zone in the us-central1 region.

  1. To set your default compute zone to us-central1-a, start a new session in Cloud Shell, and run the following command:

Your active configuration is: [cloudshell-18528]
student_03_ade82501e04b@cloudshell:~ (qwiklabs-gcp-00-45d397b5dba5)$ gcloud config set compute/zone us-central1-a
Updated property [compute/zone].
ERROR: (gcloud.config.set) Your current active account [ACCOUNT] does not have any valid credentials
Please run:

  $ gcloud auth login

to obtain new credentials.

For service account, please activate it first:

  $ gcloud auth activate-service-account ACCOUNT
student_03_ade82501e04b@cloudshell:~ (qwiklabs-gcp-00-45d397b5dba5)$ gcloud auth login
Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=32555940559.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&state=jveWm5bPDWHzeINlNlL8Sm2WcsjTYO&prompt=consent&access_type=offline&code_challenge=H11DTINc8p7WqsIAYVx4gMA8A5SQ_DCyYT4jNl6R67s&code_challenge_method=S256

Enter verification code: 4/1AX4XfWiUhQYMZB7lksN7EyzSYy1l6mhB94PtmjO3XSUBJNdNfRjD5a1JZtE

You are now logged in as [student-03-ade82501e04b@qwiklabs.net].
Your current project is [qwiklabs-gcp-00-45d397b5dba5].  You can change this setting by running:
  $ gcloud config set project PROJECT_ID
student_03_ade82501e04b@cloudshell:~ (qwiklabs-gcp-00-45d397b5dba5)$ gcloud config set compute/zone us-central1-a
Updated property [compute/zone].
student_03_ade82501e04b@cloudshell:~ (qwiklabs-gcp-00-45d397b5dba5)$


Use "kubectl options" for a list of global command-line options (applies to all commands).
student_03_ade82501e04b@cloudshell:~ (qwiklabs-gcp-00-45d397b5dba5)$ gcloud container clusters create my-kubecluster1






Use "kubectl options" for a list of global command-line options (applies to all commands).
student_03_ade82501e04b@cloudshell:~ (qwiklabs-gcp-00-45d397b5dba5)$ gcloud container clusters create my-kubecluster1
WARNING: Starting in January 2021, clusters will use the Regular release channel by default when `--cluster-version`, `--release-channel`, `--no-enable-autoupgrade`, and `--no-enable-autorepair` flags are not specified.
WARNING: Currently VPC-native is the default mode during cluster creation for versions greater than 1.21.0-gke.1500. To create advanced routes based clusters, please pass the `--no-enable-ip-alias` flag
WARNING: Starting with version 1.18, clusters will have shielded GKE nodes by default.
WARNING: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s).
WARNING: Starting with version 1.19, newly created clusters and node-pools will have COS_CONTAINERD as the default node image when no image type is specified.
Creating cluster my-kubecluster1 in us-central1-a...done.     
Created [https://container.googleapis.com/v1/projects/qwiklabs-gcp-00-45d397b5dba5/zones/us-central1-a/clusters/my-kubecluster1].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1-a/my-kubecluster1?project=qwiklabs-gcp-00-45d397b5dba5
kubeconfig entry generated for my-kubecluster1.
NAME: my-kubecluster1
LOCATION: us-central1-a
MASTER_VERSION: 1.20.10-gke.1600
MASTER_IP: 34.133.230.180
MACHINE_TYPE: e2-medium
NODE_VERSION: 1.20.10-gke.1600
NUM_NODES: 3
STATUS: RUNNING
student_03_ade82501e04b@cloudshell:~ (qwiklabs-gcp-00-45d397b5dba5)$


Task 2: Create a GKE cluster

cluster consists of at least one cluster master machine and multiple worker machines called nodes. Nodes are Compute Engine virtual machine (VM) instances that run the Kubernetes processes necessary to make them part of the cluster.

  1. To create a cluster, run the following command, replacing [CLUSTER-NAME] with the name you choose for the cluster (for example:my-cluster).

    gcloud container clusters create [CLUSTER-NAME]
    Copied!

    You can ignore any warnings in the output. It might take several minutes to finish creating the cluster.

    Expected output (Do not copy):

    NAME        LOCATION       ...   NODE_VERSION  NUM_NODES  STATUS
    my-cluster  us-central1-a  ...   1.16.13-gke.401  3          RUNNING


Task 3: Get authentication credentials for the cluster

After creating your cluster, you need authentication credentials to interact with it.

  1. To authenticate the cluster, run the following command, replacing [CLUSTER-NAME] with the name of your cluster:

    gcloud container clusters get-credentials [CLUSTER-NAME]
    Copied!

    Expected output (Do not copy):

    Fetching cluster endpoint and auth data.
    kubeconfig entry generated for my-cluster.

Task 4: Deploy an application to the cluster

You can now deploy a containerized application to the cluster. For this lab, you'll run hello-app in your cluster.

GKE uses Kubernetes objects to create and manage your cluster's resources. Kubernetes provides the Deployment object for deploying stateless applications like web servers. Service objects define rules and load balancing for accessing your application from the internet.

  1. To create a new Deployment hello-server from the hello-app container image, run the following kubectl create command:

    kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0
    Copied!

    Expected output (Do not copy):

    deployment.apps/hello-server created

    This Kubernetes command creates a Deployment object that represents hello-server. In this case, --image specifies a container image to deploy. The command pulls the example image from a Container Registry bucket. gcr.io/google-samples/hello-app:1.0 indicates the specific image version to pull. If a version is not specified, the latest version is used.

  2. To create a Kubernetes Service, which is a Kubernetes resource that lets you expose your application to external traffic, run the following kubectl expose command:

    kubectl expose deployment hello-server --type=LoadBalancer --port 8080
    Copied!

    In this command:

    • --port specifies the port that the container exposes.
    • type="LoadBalancer" creates a Compute Engine load balancer for your container.

    Expected output (Do not copy):

    service/hello-server exposed
  3. To inspect the hello-server Service, run kubectl get:

    kubectl get service
    Copied!

    Expected output (Do not copy):

    NAME              TYPE              CLUSTER-IP        EXTERNAL-IP      PORT(S)           AGE
    hello-server      loadBalancer      10.39.244.36      35.202.234.26    8080:31991/TCP    65s
    kubernetes        ClusterIP         10.39.240.1       <none>           433/TCP           5m13s
  4. To view the application from your web browser, open a new tab and enter the following address, replacing [EXTERNAL IP] with the EXTERNAL-IP for hello-server.

    http://[EXTERNAL-IP]:8080
    Copied!

    Expected output:

    output.png

  5. Task 5: Deleting the cluster

  1. To delete the cluster, run the following command:

    gcloud container clusters delete [CLUSTER-NAME]
    Cocopy
  2. When prompted, type Y to confirm.


List of commands used:

student_03_ade82501e04b@cloudshell:~ (qwiklabs-gcp-00-45d397b5dba5)$ history
    1  gcloud compute  ssh gcelab1 --zone $ZONE
    2  gcloud auth list
    3  gcloud config set account 'ACCOUNT'
    4  gcloud config set account 'ACCOUNT`
    5  gcloud auth list
    6  gcloud config list project
    7  gcloud config set compute/zone us-central1-a
    8  gcloud auth login
    9  gcloud config set compute/zone us-central1-a
   10  gcloud container clusters create my-kubecluster
   11  gcloud container clusters create my-kubecluster
   12  kubectl
   13  gcloud container clusters create my-kubecluster1
   14  gcloud container clusters get-credentials my-kubecluster1
   15  kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0
   16  kubectl expose deployment hello-server --type=LoadBalancer --port 8080
   17  kubectl get service
   18  kubectl get service
   19  gcloud container clusters delete my-kubecluster1

No comments:

Post a Comment