Cloud computing — Assignment

Deploying to GKE


The goal of this assignment is to deploy a containerized application to Google Kubernetes Engine (GKE).

The lab consists of the following activities:

  1. Get a Web application from GitHub.

  2. Complete the Dockerfile of the front-end.

  3. Deploy and test the application on your local Kubernetes cluster.

  4. Deploy the application to the Google Kubernetes Engine (GKE).

  5. Write a report describing the application and the deployment procedure. You’ll find detailed information about the report in the last section.

Submission information

You have a submit a single zip file containing:

  • The report in PDF format.

  • The Dockerfile of the front-end.

Submission deadline: 5 December 2024, 11:59 PM.

1 Get the application

You can download the application here.

If you have git installed on your computer you can simply open a terminal and type the following command:

git clone https://github.com/gquercini/tripmeal-cloud.git

2 Dockerfile of the front-end

The application folder has the same structure as we have seen in the previous labs. You should be able to understand the role of each file and folder.

Note that:

  • The folder web containing the front-end consists of Python code files, a requirements.txt file, and two folders static and templates.

  • The files and folders contained in the folder web need to be copied to a Docker image.

Exercise

Exercise 2.1 Locate the Dockerfile of the front-end and complete it. Please note that:

  • You need a Python 3.7 environment to run the application.

  • Make sure you build a minimal image; don’t add unnecessary files to the image. Also, choose a base image to have a minimal Python environment.

  • Make sure your Dockerfile is written so that it can take full advantage of the build cache. Specifically, the dependencies should not be installed every time you build an image after a modification of the source code.

3 Local deployment

You need to deploy and test the application TripMeal on a local Kubernetes cluster (either Docker Desktop or Minikube).

Exercise

Exercise 3.1 Build the images of all services of the application. Prefix the name of each image with your username, such as:

your-username/image-name:tag

Now, specify the names of the images in the file tripmeal.yml.

Exercise

Exercise 3.2 Open the file tripmeal.yml and add the names of the created Docker images to the appropriate fields.

Exercise

Exercise 3.3

  • Execute the application by typing the following command:

kubectl apply -f tripmeal.yml

  • Wait a minute and then check whether the state of the pods with the following command:

kubectl get all

  • If any of the pods is not ready yet, wait a moment and then type the previous command again.
If one or more pods are in an error state
  • Check the logs with the following commands:

kubectl logs NAME-OF-POD --previous

  • In case you get Exec format error, add to your Dockerfile the following instruction:

RUN chmod -x path_to_file_app

where path_to_file_app is the path to the file app.py inside the image. Rebuild the image.

  • After fixing the error, before executing the application again, run the following command to delete all previously created resources:

kubectl delete -f tripmeal.yml

External IP pending problem

If the external IP address is in for the frontend service, delete the service, change the port number in the .yml file and restart the service.

If all pods are in running state
  • Open a web browser window.

  • Type the public IP and port of your application.

  • Confirm that the application works correctly.

After confirming that the application works, you can shut down the application.

kubectl delete -f tripmeal.yml

4 Deploy to GKE

In this section, you’re going to deploy your application to GKE.

Here are the main steps:

  1. Make the code available to a public repository so that it can be easily imported into your cloud environment.

  2. Open the Google Cloud Shell to access your cloud environment.

  3. Deploy the application.

The next subsections will describe these steps in greater detail.

4.1 Upload the code to GitHub

The easiest way to import your application into your cloud environment is to make it available to a Git repository, such as GitHub or any other Git service for which you already have an account. If necessary, the teacher will guide you in this step.

4.2 Open the Google Cloud Shell

Open the Google Cloud Console. Make sure you log in with your school account.

You’ll see in this page an overview of the project created to let you deploy the application to GKE. In particular, you should take note of:

  • the project number (124930651186).

  • the project ID (gq-cloud-computing).

You’ll need this information later when you’ll set up the deployment.

In the top-right corner of the window, you should see a small icon with a prompt (>). When you click on it, a Google Cloud Shell will open at the bottom of the screen.

It may take few minutes for the shell to up and running.

4.3 Deployment

You’ll now have to type few commands in the Google Cloud Shell to deploy the application to Kubernetes.

Set the project in the shell session

Set your Cloud platform project in this session.

First, create an environment variable PROJECT_ID with the following command:

export PROJECT_ID=gq-cloud-computing

Then configure the project ID with the following command:

gcloud config set project ${PROJECT_ID}

4.3.1 Create a repository

Create a repository where you’ll be storing the Docker images of your application. The repository is stored in the Artifact Registry, the Google container registry.

  1. Create an environment variable PROJECT_NUMBER whose value is 124930651186.

  2. Create an environment variable REPO_NAME whose value is a name of your choice for the container repository.

  3. Create an environment variable APP_NAME whose value is a name of your choice for the application.

  4. Create an environment variable APP_VERSION whose value is the version of the app. You can choose 1.0 or latest, or any other value of your choice.

  5. Create your repository with the following command:

gcloud artifacts repositories create ${REPO_NAME} \
   --repository-format=docker \
   --location=europe-west2 \
   --description="Docker repository"

4.3.2 Building the Docker images

  1. Download the application from your Git repository with the command git clone.

  2. Build and tag the Docker images of your application.

Names of the images

You have to give your images names that are consistent with the names of images stored in the container repository.

In the case of your repository, the name of an image should be as follows:

europe-west2-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${APP_NAME}-image-name:${APP_VERSION}

Warning

Note: If prompted, authorize Cloud Shell to make Google Cloud API calls.

  1. Verify that the images have been created with the following command:

docker images

  1. Add IAM policy bindings (basically, some authorization configurations) to your service account:
gcloud artifacts repositories add-iam-policy-binding ${REPO_NAME} \
    --location=europe-west2 \
    --member=serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \
    --role="roles/artifactregistry.reader"

4.3.3 Pushing the images to the Artifact registry

You must upload the images to a registry so that your GKE cluster can download and run the images. Here we use the repository that we created above.

  1. Configure the docker command to authenticate to the Artifact Registry.

gcloud auth configure-docker europe-west2-docker.pkg.dev

  1. Push the images that you created with the command docker push. Make sure that you use the correct image names, as discussed above.

4.3.4 Creating a GKE cluster

You need now to create a Kubernetes cluster using the Google Kubernetes Engine. When you create a cluster, you’ll also specify the option to create the network over which the cluster nodes communicate.

  1. Define a new environment variable CLUSTER_NAME whose value is a name of your choice for the cluster.

  2. Define a new environment variable NET_NAME whose value is a name of your choice for the network.

  3. Set your compute engine region.

gcloud config set compute/region europe-west2

  1. Create the cluster with the following command:
gcloud container clusters create-auto ${CLUSTER_NAME} \
  --create-subnetwork name=${NET_NAME} \
  --no-enable-master-authorized-networks \
  --enable-private-nodes

Warning

The creation of the cluster might take several minutes. You may want to get a coffee :)

4.3.5 Deploy the application

You’re now ready to deploy your application!

  1. In your GitHub repository, modify the file tripmeal.yml to change the names of the images. Use the names of the images that you pushed to the the artifact registry.

  2. In the Google Cloud Shell, move to the directory that contains your application with the command cd.

  3. Execute the command git pull to get the updated version of file tripmeal.yml.

  4. Ensure that you are connected to your GKE cluster with the following command:

gcloud container clusters get-credentials $CLUSTER_NAME --region europe-west2

  1. Type the following command to deploy your application:

kubectl apply -f tripmeal.yml

  1. Wait 30 seconds and then type the following command:

kubectl get all

You should get some information about the state of the deployment. Keep typing this command until the state of all pods is READY.

  1. Get the public IP address where your application is available and the port number and type them in your browser to confirm that you can access and use the application.

4.4 Video of the application use

Record a video while using the application. In particular shows:

  • that you can connect to the website.

  • you can create a user.

  • you can log in with as new user.

  • you can create a recipe.

  • you can log out and log in back again.

4.5 Shut down the application

When you’re done using the application, shut it down and remove all resources.

This is very important. Follow these instructions carefully to remove all your resources.

  • Shut down the application by typing the following command:

kubectl delete -f tripmeal.yml

  • Delete the Kubernetes cluster by typing the following command:

gcloud container clusters delete $CLUSTER_NAME --region europe-west2 --ansync

  • Delete ALL the images from the container registry with the following command:

gcloud artifacts docker images delete NAME_OF_IMAGE --delete-tags --quiet

  • Delete the Docker repository with the following command:

gcloud artifacts repositories delete ${REPO_NAME} --location=europe-west2 --async

5 Report

In the report you need to include the following elements.

  • A high-level description of the application. What is the application intended for?

  • The architecture of the application. How are files organized? How many services is the application composed of? What is the meaning of each file?

  • The technologies used in the application. Which programming languages are used for each service? Which libraries and databases?

  • In which file can you get the information about the port number of the front end?

  • Look at the file tripmeal.yml. Can you explain the content of this file? Can you explain the meaning of each object created in this file? In particular explain: what is a service and a deployment? What is a stateful set? Why is a deployment used and for which service? Why is a stateful set used and for which service?

Don’t hesitate to add figures to your report, should you need them to better explain the different notions.