×
Community Blog Friday Blog - Week 25 - DevOps First Steps: Build and Deploy Docker Containers With Kubernetes

Friday Blog - Week 25 - DevOps First Steps: Build and Deploy Docker Containers With Kubernetes

Learn how to deploy a stateless web application on Serverless K8s, and build new Docker images automatically using Container Registry + GitHub.

By Jeremy Pedersen

Welcome to the 25th installment in our weekly blog series!

This week we're going hands-on! We'll create a GitHub repository, upload a Dockerfile and some code for a simple static website, then link it to Alibaba Cloud Container Registry and Container Service for Kubernetes.

Let's jump in!

A Quick Overview

By the end of this blog post, you should have a working NGINX website up and running on Kubernetes. To get there, we'll have to set up a few different things:

  1. Create a GitHub Repository.
  2. Create a Dockerfile and index.html file, and store them in our GitHub Repository.
  3. Link our GitHub account to Alibaba Cloud Container Registry.
  4. Create a new Namespace and Repository in Container Registry.
  5. Create a new Serverless K8s cluster using Container Service for Kubernetes.
  6. Set up a Deployment using the Docker image from our Container Registry Repository.
  7. Make that deployment accessible from the Internet by setting up a Service.

It looks like a lot, but it should only take about an hour to get everything set up (assuming you already have accounts on GitHub and Alibaba Cloud, of course).

Setting Up Kubernetes

We will actually start by setting up the Kubernetes cluster, since the cluster will take 10-15 minutes to spin up. This way, the cluster setup process can happen in the background while we do other things!

First, log in to your alibabacloud.com account. I'll wait! If you don't have an Alibaba Cloud account, you should sign up for one first.

For reference, the login page should look like this:

login

...and the signup page should look like this:

signup

Logged in? Great! Next you need to find the Container Service for Kubernetes console (or just click here):

k8s_service

Next, we'll create a Serverless cluster:

create_cluster_01

We will use the "Singapore" region for the purposes of this blog, but really any region should work. I recommend you choose a region outside Mainland China, since hosting a web service in Mainland China requires an ICP license, and we'll be testing our cluster by setting up a website!

Follow along with these screenshots to create a Serverless cluster:

create_cluster_02

create_cluster_03

create_cluster_04

After agreeing to the Terms of Service, scroll back up to the top of the configuration page and click on "Create". That should open up a dialog box that will confirm that your cluster configuration is valid, and will give you a summary of which services you'll be charged for:

create_cluster_05

Click OK, and the cluster creation process should start:

create_cluster_06

If you go back to the "Clusters" page, you should now see something like this:

create_cluster_07

The cluster creation process can take ten to fifteen minutes so feel free to move on with some of the other steps in this blog (like setting up Container Registry) while you wait.

Once the cluster has been successfully created, it should show as "Running":

create_cluster_08

Link Your GitHub Account To Container Registry

Before starting the steps in this section, make sure you are logged into GitHub.

Next, log into Alibaba Cloud Container Registry. We'll want to set up a new Namespace and Repository in the Singapore region (where we created our K8s cluster):

acr_01

acr_02

acr_03

We will be submitting Dockerfiles to a GitHub repository and then triggering automated builds in Container Registry, so we need to link a GitHub Account to Alibaba Cloud Container Registry service:

acr_04

acr_05

If you are already logged into your GitHub account when you click on the link above, you'll see a page like this one open in a new tab:

acr_06

You might be asked to confirm your credentials:

acr_07

If things worked, you should see the status indicator next to "GitHub" change to "Bound" in the ACR console (you might have to refresh the page a few times):

acr_08

Create A New GitHub Repository

We'll need to set up somewhere to store our Dockerfile and the code for our NGINX site, so we should set up a new GitHub repository, like this:

github_01

github_02

After creating the repository, you should see a page like this one:

github_03

You'll now need to run the git clone command to download a local copy of your repository. Clicking on the green Code button near the top of the repository page will show the git command you need to run.

In my case, my git clone command looks like this:

git clone git@github.com:jeremypedersen/container_test.git

Note: Your Git repo URL will be different.

If everything worked, you should see a message like this one:

git_clone

We'll leave the Git repository alone for now. Let's get back to the Container Registry console and finish linking it to GitHub.

Link A Specific GitHub Repository To A Container Registry Repository

Now we need to set up a Docker Image Repository using Container Registry, but before we can do that, we must first create a Namespace:

acr_namespace

Next, we. create a new Repository inside the Namespace:

create_repo_01

create_repo_02

During Repository creation, we can choose to link our new Docker Image Repository to a GitHub Repository. Choose the repository we created earlier.

create_repo_03

Make sure to check the box next to "Automatically Build Images When Code Changes", as shown above.

If everything went well, you should see a page like this one:

create_repo_04

If you click on "Build" in the lefthand menu bar, you'll see that the Container Registry Repository is set to automatically build docker images from a Dockerfile located in "/" whenever a Git commit occurs that is tagged using the pattern release-v$, as in release-v1 or release-v2:

create_repo_05

Add A Dockerfile To The GitHub Repository

Now that we have everything set up, we can add a Dockerfile to our GitHub repository. Use a text editor to create a new Dockerfile inside the directory created when you ran git clone earlier. For now, your Dockerfile should be just one line:

FROM nginx:latest

Running git status should show that you've got one local file not yet staged for commit. Run git add * to get it ready for commit.

Next, run git commit to prep the Dockerfile for upload to your repo:

git commit -m "First commit"

Finally, run git push to upload your Dockerfile to GitHub. To give you an idea what these commands should do when run, take a look at these two screenshots:

git_commit_01

git_commit_02

We're not quite done yet, though. Remember, Alibaba Cloud Container Registry won't actually build a Docker image from our Dockerfile until we tag the commit to GitHub.

To do that, we first need to know the ID number associated with the commit. We can get that by running git show. Then we'll need to run git tag release-v1 <commit ID>, and git push --tags, like so:

git_push_tags

If we go back to Container Registry, we should now see a new Build in progress. Container Registry is creating a new Docker image from the Dockerfile we just committed to GitHub!

acr_build

If we wait a few seconds, the build will complete:

acr_build_complete

Now we're ready to deploy this new image on our Kubernetes cluster!

Creating A New Deployment In K8s

Now, we just need to navigate back to the Container Service for Kubernetes console, and create a new Deployment:

deployment_01

deployment_02

deployment_03

deployment_04

deployment_05

deployment_06

deployment_07

deployment_08

deployment_09

Click on "Next", then we'll create a new Service.

Create A New Service In K8s

We need to make our Deployment accessible from the Internet by creating a Service. Follow the steps below to do this:

service_01

service_02

service_03

If everything went well, you should see a message like this one:

service_04

Checking Our Results

If everything is working correctly, we should see 2/2 Pods running in our Deployment and our Service should show a public IP address and port number (80), like this:

results_01

results_02

If we visit that public IP, we should see the "Welcome to nginx!" page:

results_03

Updating The Site

Ok! So we got an NGINX site up and running on Kubernetes. Now what? We need a way to update the site, hopefully in a graceful way (without interrupting service). Luckily, Kubernetes has a built-in way to do this gracefully: Rolling Updates! This is a handy built-in feature of the Kubernetes Deployment object.

Here's what we're going to do:

  1. Create a new index.html for our site, and add it to our GitHub repository.
  2. Update our Dockerfile to copy this index.html file into our Docker image during the build.
  3. Commit our new code to GitHub.
  4. Tag our new GitHub commit with the tag release-v2
  5. Wait for Alibaba Cloud Container Registry to build an updated Docker image.
  6. Edit our Deployment on K8s to point to this new Docker image.
  7. Watch the magic happen!

Let's get started.

Updating Our Code

First, we need to create a new index.html file for our website, and update our Dockerfile with a COPY command so that it includes this new file in our Docker image during builds.

First, cd into the directory holding your Dockerfile, and run:

mkdir static-site

Now, create the following index.html file inside the static-site directory:

<html>
    <head>
        <title>Cool Site</title>
    </head>
    <body>
        <center><h1>Site v2.0</h1></center>
    </body>
</html>

Feel free to change the code a little bit. Once you're done editing the site, update your Dockerfile so it looks like this:

FROM nginx:latest
COPY static-site /usr/share/nginx/html

Done? Great! Now update your GitHub code with:

git add *
git commit -m "Version 2.0 of site"
git push

Next, run git show to see the ID of your most recent commit, which should look something like this:

git_show_2

Finally, run git tag release-v2 <commit ID> and git push --tags:

git_tag_2

If all went well, this will trigger a second Docker image build:

second_build_01

Which should complete in about the same amount of time as the first one:

second_build_02

Updating Our Deployment

Now that the build has completed, we need to:

  1. Go back to the Kubernetes console.
  2. Open the "Deployments" tab.
  3. Click "Edit" next to our Deployment.
  4. Change the "Image Version" from "1" to "2".
  5. Save our changes.

I don't include screenshots for these steps, but they should be straightforward, now that you've already used the Kubernetes console once! ^_^

Checking Results

Wait a minute, then try visiting the site. If the update succeeded, the new page should be up:

final_site

Try updating the page a few more times by creating new Dockerfiles, pushing them to GitHub, and then updating your Deployment. Try visiting the site while a Rolling Update is happening. You should notice that the site still works, even during an update.

That's it for this post! Join us next time for more.

I've Got A Question!

Great! Reach out to me at jierui.pjr@alibabacloud.com and I'll do my best to answer in a future Friday Q&A blog.

You can also follow the Alibaba Cloud Academy LinkedIn Page. We'll re-post these blogs there each Friday.

Not a LinkedIn person? We're also on Twitter and YouTube.

0 1 0
Share on

JDP

36 posts | 43 followers

You may also like

Comments