IT 3300 : Virtualization

Kubernetes on Google

Install gcloud

  • This site explains how to install and configure your linus machine for gcloud. You should be able to do on any Linux VM you have.
  • sudo apt-get install kubectl
    • Remember we did an alias to the kubectl command... this is a different kubectl command that is located in /usr/bin/.
    • After we install this one, to run local (microk8s) commands, use microk8s.kubectl, for gcloud, just use kubectl.
  • Make sure you have docker CE edition on your machine (Must be COMMUNITY Edition)
  • Make sure git is installed

Install gcloud

Set some default values:

  • gcloud config set project PROJECT_ID
  • gcloud config set compute/zone us-central1-b

Step 1 - build the image

The app is written in GO and runs on port 80.

Still building the image

  • Set the PROJECT_ID environment variable in your shell by retrieving the pre- configured project ID on gcloud by running the command below:
    • export PROJECT_ID="$(gcloud config get-value project -q)"
  • Build and tag for uploading:
    • docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .
      • remember that the t option gives it a tag.
      • we are building locally
      • gcr.io identifies the google container registry
      • project_id affiliates it with our project

Step 2

  • Upload to GCP container registry
  • gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v1

Step 3

  • Run image locally for testing
    • docker run --rm -p 8080:8080 gcr.io/${PROJECT_ID}/hello-app:v1
    • If you have conflicting port locally, you have to change the first port number or stop the conflicting service.
  • Can you curl it?
    • curl http://localhost:8088

Step 4

  • Create container cluster to run the container image (on GCP)
    • A cluster consists of a pool of Compute Engine VM instances running Kubernetes, the open source cluster orchestration system that powers Kubernetes Engine.
    • gcloud container clusters create hello-cluster --num-nodes=3
      • Creates a 3 node cluster named hello-cluster
  • Verify that you can see the cluster and VMs on GCP
  • Can also see instances via command line with:
    • gcloud compute instances list

Step 5

Deploy the application:

  • kubectl run hello-web --image=gcr.io/${PROJECT_ID}/hello-app:v1 --port 8080
    • This will run our app on the cluster
  • Practice some of our other K8s commands:
    • kubectl get nodes
    • kubectl get deployments
    • kubectl get pods
    • kubectl get services

Step 6

Expose application to the internet:

  • kubectl expose deployment hello-web --type=LoadBalancer --port 80 --target-port 8080

Step 6

The above command creates a Service resource, which provides networking and IP support to your application's Pods. Kubernetes Engine creates an external IP and a Load Balancer for your application.

The --port flag specifies the port number configured on the Load Balancer, and the --target-port flag specifies the port number that is used by the Pod created by the kubectl run command

Get your service definitions again to see what external IP it is deployed on.

Should be able to visit in a browser.

Step 7 - Scale

See what you are currently using by:

  • kubectl get deployment hello-web

Scale:

  • kubectl scale deployment hello-web --replicas=2
    • Then see how many pods you are using.

Step 8

Deploying new versions of an app. Begin by changing the string of "Hello world" in the text file to something else.

  • rebuild (maybe put v2 at end of build statement)
  • repush
  • apply a rolling update
    • kubectl set image deployment/hello-web hello-web=gcr.io/${PROJECT_ID}/hello-app:v2

Rollback

Check status of deployment:

  • kubectl rollout status deployment/hello-web
  • kubectl get rs
  • kubectl describe deployment

Check Rollout history:

  • kubectl rollout history deployment/hello-web

Roll it back

  • kubectl rollout undo deployment/hello-web --to-revision=1 deployment/hello-web

Step 9 - Cleanup

  • Delete the service first
    • kubectl delete service hello-web
  • Wait for lb service to be deleted,
    • gcloud compute forwarding-rules list
  • Delete container cluster
    • gcloud container clusters delete hello-cluster
  • Double check in console