App Deployment with Kubernetes, Part 2 – Preparing Your App

Most provisioning and configurations for web services happen in their respective cloud interfaces. This means web app developers must learn several different systems, and it’s more difficult to create consistent deployments. Fortunately, using a Command Line Interface and tools like Docker can make this process more consistent and familiar.

This post is part of a series on setting up app deployment with GCP and Kubernetes. Today, I’ll explain how to install and configure the CLI for Google Cloud Platform, prepare your application with Docker, and push your application to the Google Cloud Registry.

1. Set Up Your Command Line Interface (CLI)

The next steps are sourced from several different GCP resources:

I’ll be using the setup process for macOS; these resources also have guidance Linux and Windows.

Install GCloud SDK

You can install the GCloud SDK using either the GCP installer or Brew. For the GCP installer, run:

curl https://sdk.cloud.google.com | bash

This will allow you to select the install directory, enable reporting, and add Gcloud to your path.

To allow Brew to install Gcloud code, run:

brew cask install google-cloud-sdk

After successfully installing, you’ll need to restart your shell:

exec -l $SHELL

Then it’s time to configure Gcloud with gcloud init. This process will allow you to authenticate your account and enable CLI permissions. You can also specify a default project and compute the zone. This configuration will be saved locally, which saves time on routine operations later.

Kubernetes Quickstart

Running Kubernetes from the command line is simple. It uses the kubectl application, which can be installed using gcloud:

gcloud components install kubectl

This can also be done directly from Kubernetes.io.

Configure CLI for Your Project

Assuming you have installed both gcloud and kubectl, you may need to update or change your configurations as the project grows.

If your configuration needs to be changed, you can either re-run gcloud init or use one of the following commands. This is not an exhaustive list, but it does cover the basic configuration settings.

Authorizing a new account gcloud auth login
Setting a different Project ID gcloud config set project $PROJECT_ID
Reconfiguring the compute region gcloud config set compute/region $REGION
Reconfiguring the compute zone gcloud config set compute/zone $ZONE

2. Create an Application

Before deploying anything, you will need an application ready for deployment. If you don’t already have an application created, you can make a fast and lightweight Next.js app using Yarn:

yarn create next-app

As part of this process, you will be prompted to specify a project name. This will determine the directory name and the application name you will need later. For the purposes of this project, we’re going to call this fun-app.

You should now have a fully-functional Next.js application with a “Welcome to Next.js” landing page. Feel free to update and change this to suit your needs. By using yarn create next-app, you will have the appropriate yarn commands to run dev, build the application, and start the application.

To make sure everything worked properly, run yarn run build. This will build the application similarly to how you will build it for deployment later. Then run yarn run start to start a server at localhost:3000, where you can inspect the site.

3. Configure a Dockerfile for Your Application

Now to containerize or package your application for deployment with a Docker container. This process can appear daunting, but it’s generally quick and easy.

To make containerizing your application as consistent as possible, you will need to create a Dockerfile. A Dockerfile is essentially a script of the steps needed to create a container for your application.

In your project directory, open a file name Dockerfile and copy in this example below. Included in this example are several comments to clarify what is happening.

FROM node:10
# Using the node:10 base image which include npm and yarn

# Setting working directory.
WORKDIR /usr/src/app

# Installing dependencies
COPY package*.json ./
RUN yarn install

# Copying source files
COPY . .

# Building using yarn
RUN yarn run build

# Running the app
CMD [ "yarn", "start" ]

Test Your Docker Image Locally

Now that you have created the Docker file, test that it’s functioning properly. First, you will need to build the container. You can tag your image as needed; for this example, I used my username and the application name: ${USER}/fun-app.

docker build -t ${USER}/fun-app

After a successful build, you should see output for each of your steps. As long as you see Successfully built #HASH, then you know the process completed correctly. To verify, run the image locally and inspect the application:

docker run -d -p 4000:3000 ${USER}/fun-app:latest

This command is the -p option, which maps port 4000 on your host machine to port 3000 in Docker. This will allow you to visit localhost:4000 to inspect the application.

After checking that the server started correctly, you will want to stop the image: docker ps. This command will list the active Docker images. Find the one tagged with ${USER}/fun-app, and use the ID at the beginning of the line for the next command.

docker kill ${CONTAINER_ID}

This command will stop and remove the running image so that it doesn’t sit idle on your system.

4. Push the Docker Image to Google Cloud Registry

Assuming the Docker image was created and run successfully, you will now want to push this image up to the Google Cloud Registry so that it can be used for your Kubernetes deployment. Google has a detailed guide on pushing and pulling images.

To generalize this process, I have used the environment variable PROJECT_ID to denote my project ID from Google Cloud Platform. Feel free to set this variable or to change the commands to reflect your own project ID.

The first step is to authorize and configure Docker to push images to GCR: gcloud auth configure-docker. This will allow you to push images to the gcr.io registry.

Next, tag the image before pushing to gcr.io:

docker tag ${USER}/fun-app gcr.io/${PROJECT_ID}/fun-app:v1

Tagging the image with gcr.io denotes the destination registry for the image. You should also tag the application with a version or commit ID to more accurately track changes and deployments. Here I used :v1 to denote the first version.

Having properly tagged the image, you can now push the image:

docker push gcr.io/${PROJECT_ID}/fun-app:v1

Or you can push the image with a specific tag like the build or commit ID:

docker push gcr.io/${PROJECT_ID}/fun-app:${COMMIT_ID}

To check that your images made it to GCR, run:

gcloud container images list-tags gcr.io/${PROJECT_ID}/fun-app

If you encounter errors with permissions or commands during this step, make sure to revisit Part 1 to configure and authenticate your CLI and project information.

App Deployment with Kubernetes

This is the second part of a series on how to set up App Deployment with Kubernetes:

  1. Getting Started with Google Cloud Platform and Creating a Kubernetes Cluster
  2. Configuring Your Command Line Interface and Preparing Your App for Kubernetes
  3. Creating Kubernetes Deployments, Rolling out Updates, and Removing Resources

In Part 3, we’ll will write a Kubernetes configuration, update our application using Kubernetes Rolling Updates, and find out how to delete our deployment.