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!
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:
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).
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!
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":
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):
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 email@example.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.
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
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:
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.
git commit to prep the Dockerfile for upload to your repo:
git commit -m "First commit"
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!
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.
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:
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:
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:
Let's get started.
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.
cd into the directory holding your Dockerfile, and run:
Now, create the following
index.html file inside the
<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
git show to see the ID of your most recent commit, which should look something like this:
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:
Now that the build has completed, we need to:
I don't include screenshots for these steps, but they should be straightforward, now that you've already used the Kubernetes console once! ^_^
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.
Great! Reach out to me at
firstname.lastname@example.org 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.
JDP - December 23, 2021
JDP - July 15, 2021
Alibaba Clouder - June 11, 2020
JDP - December 17, 2021
JDP - February 18, 2022
JDP - March 11, 2022
An enterprise-level continuous delivery tool.Learn More
Provides a control plane to allow users to manage Kubernetes clusters that run based on different infrastructure resourcesLearn More
Accelerate software development and delivery by integrating DevOps with the cloudLearn More
Visualization, O&M-free orchestration, and Coordination of Stateful Application ScenariosLearn More
More Posts by JDP