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:


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


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


Next, we'll create a Serverless cluster:


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:




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:


Click OK, and the cluster creation process should start:


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


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":


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):




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:



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:


You might be asked to confirm your credentials:


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):


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:



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


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:


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:


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



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


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:


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:


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:



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:


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!


If we wait a few seconds, the build will 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:










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:




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


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:



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


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:

        <title>Cool Site</title>
        <center><h1>Site v2.0</h1></center>

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:


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


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


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


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:


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


71 posts | 152 followers

You may also like