For this post, I will not be walking you through the installation of the linux VM but will start with configuring it with the tools needed to build a container image. I will be using VMware’s Photon OS, but you can use any linux distribution you prefer. The steps will be similar. If you want to follow along with me on Photon OS, please download the latest version of VMware’s Photon OS from github: Photon OS
Configure your Linux VM
Open the console and log on as root. The default password is changeme. You will be asked to immediately change the default password. Please do so.
The first thing you will need to do is set a static IP address for this VM. The network configuration file is located in the /etc/systemd/network directory. By default the configration files is setup for DHCP, let’s change that to a static IP address. To change your current directory so you can edit the network config file, type and press enter.
Now lets see what the current files is named. Use the ls command to list the directory contents. Now change the name of this file from 99-dhcp-en.network to 10-static-en.network. To do this use the mv command. Type and press enter
mv 99-dhcp-en.network 10-static-en.network
To edit this file and assign a static IP to the VM, use the VIM editor to change the file.
Here are some useful commands that you can use to move around and edit the file contents.
- use the arrow keys to move around the editor
- i — to insert a character at the cursor location
- x — to delete a character at the cursor location
- o — to edit line below the cursor
- shift-o — to edit the line above the cursor
- dd — will delete the entire line the cursor is on
- ESC — exit out of editing mode
- ESC then : then w — to writes the changes
- ESC then : then q — to quit the VM editor
- ESC then : then wq — to write the changes and quit the editor
Let’s start editing the network configuration file. Type and press enter
For those not familiar with editing files in VIM, I will walk you through the steps.
- Move the cursor with the arrow keys to the line the says DHCP=yes and type dd.
- Press o and type Address=(the static IP address for this VM). Please add the /CIDR to the end of the IP address. See the example below.
- Press enter
- type Gateway=(the gateway address for this subnet)
- Press ESC then : then wq and press enter.
If all went right, the file should looks similar to the one above with a real IP address and not “xxx’s”.:) Now let’s restart the network service to make the changes take affect. Type and press enter
systemctl restart systemd-networkd
Check the IP address with ifconfig, type and press enter
Verify that the IP address you set in 10-static-en.network, is the same. Verify that ssh is working so you can putty or use SSH from you desktop. To check the status of the SSH deamon use the systemctl command. Type and press enter
systemctl status sshd
If the SSH deamon is not running you can start it by typing and press enter
systemctl start sshd
And then enable it to start on reboot by typing and press enter
systemctl enable sshd
We now have a static IP address assigned to our VM and we know SSH is running.
Create A webpage
Let’s start downloading the utilities and software we need to create our NGINX container. We will eventually take this webpage and make it work with Kubernetes but first we need to build a NGINX container image with a custom webpage. By default Docker is installed on all Photon OS distributions but is not running. To start the service and make it so it start automatically , type and press enter
systemctl start docker && systemctl enable docker
To check the status of the docker service, type and press enter
systemctl status docker
Now that docker is running, let’s pull down and run a NGINX container image and attach to it through our browser. We should see the default NGINX webpage. In the next steps we will change that for our customer page. This is actually pretty simple to do. To get started, type and press enter
docker run --rm -d --name webpage -p 1314:80 nginx
This command pulls down a NGINX container image from dockerhub and puts it in your local repository. Take note of the long number on the last line, this is the container ID. Thankfully we named the container webpage so we don’t need to remember that number.
Let me explain the docker command you entered.
- run — tells docker to run the container
- rm — tells docker to remove the container when it is stopped
- name — tells docker to assign this container a name
- d — tells docker to run this container as a deamon (linux program that runs in the background)
- p — tell docker to assign the internal port 80 to port 1314 on the container host or linux VM
- nginx — the name of the container that you want to run.
We can see the running container by listing them with ps. Type and press enter
Open the browser on your desktop and enter the ip address of the linux VM, you set earlier, followed by port number 1314. You should see the “Welcome to NGINX” webpage.
We will stop this container and because we used rm the container will be deleted automatically. To stop the container, type and press enter
docker stop webpage
You can verify the container is gone, by using telling docker to list all containers. Type and press enter
docker ps -a
Create A custom webpage
To create a custom webpage, you have to build your own index.html file and push it into the nginx container. An easy way to create HTML code is to go to the w3schools.com website and use their examples.
To make things cleaner, let’s create a new directory to keep our webpage in. You have to set permissions on the file and directory so the container can read it. Type and press enter
mkdir html && chmod 777 html
Change directory into the new directory. Type and press enter
To create our custom index.html file, type and press enter
Start editing the file by pressing i for insert mode. Copy the following html code and paste it into the VIM editor.
<!DOCTYPE html> <html> <body style="background-color:DodgerBlue;"> <h1 style="text-align:center;">Our Simple Webpage about VMware's TKG</h1> <p style="text-align:center;">What's TKG all about, click on the button below</p> <div style="text-align:center"> <button onclick="document.location = 'https://Tanzu.vmware.com/kubernetes-grid'">VMware Tanzu</button> </div> </body> </html>
Save and exit the file. Type ESC then : then wq and press enter.
For the nginx container to run this file we need to be sure the permissions are set correctly on it. Type and press enter
chmod 644 index.html
Now lets run the NGINX container again with our index.html file we just created. Type and press enter
docker run --rm -d --name webpage -v /root/html:/usr/share/nginx/html -p 1314:80 nginx
A new docker command switch that I need to explain.
- v — attach a local directory on the VM to a container directory.
This allows us to have the NGINX container use our index.html file for it’s default webpage. The beauty of this is we can modify the index.html file in our local directory and it will dynamically change the webpage. Open the browser on your desktop and enter the ip address of the linux VM, you set earlier, followed by port number 1314. You should now see the custom webpage, as show below. If want to have fun go back into the index.html file and make change, the refresh your browser. Don’t forget to press the button 🙂
To stop this container, type and press enter
docker stop webpage
Create your own nginx container image
We now need to create our own docker image so we can use that to build a kubernetes pods. The easiest way to build a container image is with a Dockerfile. So let’s create one. It is going to be a very simple one. What we will do is use our existing NGINX container image and copy our new index.html file into the NGINX directory and make an image out of it. The difference in doing this versus using the -v switch is that we are actually copying our index.html file into the container. This make the change permenant. From the same html directory, create a Dockerfile file. Type and press enter
cat > Dockerfile << EOF
COPY index.html /usr/share/nginx/html/index.html
Use the Dockerfile to create a container image. Type and press enter
docker build -t tkg-nginx .
Lets verify the image is there. Type and press enter
Now let’s run our new image. Type and press enter
docker run --rm -d --name webpage -p 1315:80 tkg-nginx
Open the browser on your desktop and enter the ip address of the linux VM, you set earlier, followed by port number 1315. NOTICE the new port number 1315. You now see your customer webpage being run as a container from the image you built.
Stop the container and it will automatically be deleted. Type and press enter
docker stop webpage
Push your image to DockerHub
To get the docker image you just created into a KinD cluster we need to pull it from a docker repository. The easiest way to do this is by pushing our new image into a Docker Hub repository.
The first thing you need to do is create a Docker Hub account, it is free. Click on this Docker Hub link and follow the directions. Create a private or public repository with a name of your choosing. Pro tip: make it short. For this post we will use “blog” as the repository name.
To put this new image in our newly created repository, we need to tag it and push it up. Yes, it is really that easy. To tag an image, type and press enter
docker tag tkg-nginx yourRepositoryName:tkg-nginx
To see that you have tagged the image properly, type and press enter
Be sure that your repository name matches exactly what you see on your docker hub page.
The only thing left to do is push our local image up to the docker hub repository. Type and press Enter.
docker push yourRepositoryName:tkg-nginx