8/13/2019 Lab 01: Containerize Applications — Intro to Containers and Kubernetes 1.0.
0 documentation
Docs » Lab 01: Containerize Applica ons
Lab 01: Containerize Applications
Environment Overview
Your lab environment is hosted with a cloud server instance. This instance is accessible by
clicking the “My Lab” icon on the le of your Strigo web page. You can access the terminal
session using the built-in interface provided by Strigo or feel free to SSH directly as well
(instruc ons will appear in the terminal).
The appropriate kubernetes related tools (docker, kubectl, kubernetes, etc.) have already been
installed ahead of me.
Prepare Your Lab
Step 1: Start Kubernetes and your Docker Registry
k8s-start
Build Docker image for your frontend application
Step 1: Download the gowebapp code under your home directory and
untar the file
cd ~/
wget https://s3.eu-central-1.amazonaws.com/heptio-edu-static/how/gowebapp.tar.gz
tar -zxvf gowebapp.tar.gz
Step 2: Create Dockerfile for your frontend application
cd $HOME/gowebapp/gowebapp
https://3i66gn3czc6mcuvtc-ae2dj6ktg38whmksx.rp.strigo.io/instructions/lab01/lab01.html 1/5
Create a file named
8/13/2019
in this directory for the frontend Go applica on. Use vi or any
Lab 01: Containerize Applications — Intro to Containers and Kubernetes 1.0.0 documentation
Dockerfile
preferred text editor. Populate it with the following code snippet.
Dockerfile-gowebapp
1 FROM ubuntu
2
3 COPY ./code /opt/gowebapp
4 COPY ./config /opt/gowebapp/config
5
6 EXPOSE 80
7
8 WORKDIR /opt/gowebapp/
9 ENTRYPOINT ["/opt/gowebapp/gowebapp"]
Step 3: Build gowebapp Docker image locally
cd $HOME/gowebapp/gowebapp
Build the gowebapp image locally. Make sure to include “.“ at the end. Make sure the build runs
to comple on without errors. You should get a success message.
docker build -t gowebapp:v1 .
Build Docker image for your backend application
Step 1: Create Dockerfile for your backend application
cd $HOME/gowebapp/gowebapp-mysql
Create a file named Dockerfile in this directory for the backend MySQL database. Use vi or any
preferred text editor. Populate it with the following code snippet.
Dockerfile-gowebapp-mysql
https://3i66gn3czc6mcuvtc-ae2dj6ktg38whmksx.rp.strigo.io/instructions/lab01/lab01.html 2/5
8/13/2019 Lab 01: Containerize Applications — Intro to Containers and Kubernetes 1.0.0 documentation
1 FROM mysql:5.6
2
3 COPY gowebapp.sql /docker-entrypoint-initdb.d/
Step 2: Build gowebapp-mysql Docker image locally
cd $HOME/gowebapp/gowebapp-mysql
Build the gowebapp-mysql image locally. Make sure to include “.“ at the end. Make sure the build
runs to comple on without errors. You should get a success message.
docker build -t gowebapp-mysql:v1 .
Run and test Docker images locally
Before deploying to Kubernetes, let’s run and test the Docker images locally, to ensure that the
frontend and backend containers run and integrate properly.
Step 1: Create Docker user-defined network
To facilitate cross-container communica on, let’s first define a user-defined network in which to
run the frontend and backend containers:
docker network create gowebapp
Step 2: Launch frontend and backend containers
Next, let’s launch a frontend and backend container using the Docker CLI.
docker run --net gowebapp --name gowebapp-mysql --hostname gowebapp-mysql -d -e
MYSQL_ROOT_PASSWORD=mypassword gowebapp-mysql:v1
sleep 20
docker run -p 9000:80 --net gowebapp -d --name gowebapp --hostname gowebapp gowebapp:v1
https://3i66gn3czc6mcuvtc-ae2dj6ktg38whmksx.rp.strigo.io/instructions/lab01/lab01.html 3/5
First, we launched the database
8/13/2019
container, as it will take a bit longer to startup, and the frontend
Lab 01: Containerize Applications — Intro to Containers and Kubernetes 1.0.0 documentation
container depends on it. No ce how we are injec ng the database password into the MySQL
configura on as an environment variable:
Next we paused for 20 seconds to give the database a chance to start and ini alize.
Finally we launched a frontend container, mapping the container port 80 - where the web
applica on is exposed - to port 9000 on the host machine:
Step 3: Test the application locally
Now that we’ve launched the applica on containers, let’s try to test the web applica on locally.
You should be able to access the applica on at h p://<EXTERNAL-IP>:9000.
Note
To get the EXTERNAL-IP of your host, run the command lab-info in your lab terminal
Create an account and login. Write something on your Notepad and save it. This will verify that
the applica on is working and properly integrates with the backend database container.
Step 4: Inspect the MySQL database
Let’s connect to the backend MySQL database container and run some queries to ensure that
applica on persistence is working properly:
docker exec -it gowebapp-mysql mysql -u root -pmypassword gowebapp
Once connected, run some simple SQL commands to inspect the database tables and
persistence:
#Simple SQL to navigate
SHOW DATABASES;
USE gowebapp;
SHOW TABLES;
SELECT * FROM <table_name>;
exit;
Step 5: Cleanup application containers
https://3i66gn3czc6mcuvtc-ae2dj6ktg38whmksx.rp.strigo.io/instructions/lab01/lab01.html 4/5
When we’re finished tes Lab
8/13/2019
ng,01:we can terminate and remove the currently running frontend and
Containerize Applications — Intro to Containers and Kubernetes 1.0.0 documentation
backend containers from our local machine:
docker rm -f gowebapp gowebapp-mysql
Create and push Docker images to Docker registry
Step 1: Tag images to target another registry
We are finished tes ng our images. We now need to push our images to an image registry so our
Kubernetes cluster can access them. First, we need to tag our Docker images to use the registry
in your lab environment:
docker tag gowebapp:v1 localhost:5000/gowebapp:v1
docker tag gowebapp-mysql:v1 localhost:5000/gowebapp-mysql:v1
Step 2: publish images to the registry
docker push localhost:5000/gowebapp:v1
docker push localhost:5000/gowebapp-mysql:v1
Lab 01 Conclusion
Congratula ons! By containerizing your applica on components, you have taken the first
important step toward deploying your applica on to Kubernetes.
https://3i66gn3czc6mcuvtc-ae2dj6ktg38whmksx.rp.strigo.io/instructions/lab01/lab01.html 5/5