Building Go App with Gitlab Runner on Azure |Part 1
Hello fellow techies and non-techies, for this post we would be looking at the following tasks:
- Setting up an Azure VM
- Opening a port on Azure using NSG (Network Security Group)
- Installing Docker and Golang
- Building a simple Golang web server
- Setting up and Installing GitLab runner
- Creating a pipeline on GitLab with the web server created in point 4
Let’s assume you are a developer building an application and you consider writing test for this application, there is a high chance of you making mistake when you write them manually. An automated test can only bring out their true potential when you run them continuously and for every change so as not to allow it works on my machine issues. With the help of Continuous Integration (CI) tools, problems such as this are easily detected and reported, it also makes it easy to get the application ready for deployment. Examples of CI tools are GitLab, Jenkins, Semaphore, TravisCI, Circle CI, TeamCity etc.
Setting up an Azure VM
Let’s discuss what Azure is before setting up a VM in Azure.
Microsoft Azure popularly referred to Azure is a cloud provider that offer cloud computing services, this service ranges from servers, storage, databases, networking, software, analytics, intelligence and more, over the internet (the cloud).
Creating an account on Azure is quite easy, visit Microsoft Azure and fill in the required information and you are good to go, also if you have a student email you will be able to access services on Azure free of charge.
For this post, we will be creating an Ubuntu VM on Azure and I will guide you on how to do this and also include images.
Once you have your account created on Azure, sign in to Azure portal then you should be redirected to this page and of cos without the recent resources:
The first step is to create a resource group
A resource group is like a box that houses resources required for an application in a single manageable unit. For example say you have a web server using a database, virtual machine and some sort of storage set up, rather than creating this in various resource group you can easily put them into one resource group and you can manage the application collectively.
Click on the ➕ icon to create a new resource group and the next page that will be displayed to you is:
Since we will be working with Ubuntu Image, select Ubuntu Server 18.04 LTS and you should have this displayed on your screen:
For my Azure account am using my Microsoft Learn Student Ambassador account, to learn more about becoming an MLSA, visit here. With this account, I was given a Subscription because using Azure requires an Azure Subscription which gives authenticated and authorized access to Azure products and services.
Click on the drop-down to select your Subscription or create one if you can’t find any since we don’t have any resource group, click on Create New and give your resource group a name, I will be going with gitlab-prac.
Also, I will be giving my virtual machine a name and for this post, I will be going with gitlab-vm, you should note that the virtual machine name as to be unique as you will see in the image below that there’s a green check-mark which indicates that the name is unique, I decided to use West US region for this post, you can as well use any region of your choice and I left other option as default.
Region is a geographical area on the planet that has one or more data centres and Azure has 54 available regions in 140 countries
The next line of action is to create how you will connect to this virtual machine (VM) and with Azure, you can connect either using SSH public key or Password. For me I already have one SSH key created called adefemi-mind, but you might need to create a new one by giving any preferred admin username for the VM, for the SSH Public Key Source, click on the drop-down and select Generate new key pair and then give the key any name of your choice.
To connect to this VM, you will need to allow some port to be opened especially port 22, by default this port is selected because it allows you ssh into the VM and for the image below I also opened port 80 and 443 to allow HTTP and HTTPS.
With all this filled, click on Review+ create, you will also need to download your Key and depending on your browser settings, you should find it in the Downloads section of your PC.
You can as well add some additional settings on the disk tab, networking, management, advanced and tags, but for this post, I will be going with the default settings. You will need to wait for a few minutes for the resources to get created and you should get the output below
Click on Go to resource and you should this output, Azure create a virtual network, public IP address, nsg, network interface and a disk just like the image below, you should note that the name will be different
Next step is to SSH into the server you created, firstly click on the VM name, mine is gitlab-vm and at the tab click on connect, select SSH and the next step is to ensure you have read-only access to the private key you download. navigate to where the key was downloaded and in your terminal run the following command:
Then connect with the VM using the following command as well:
With this, you should have the output below
Yeah, you are now running an Ubuntu Server in Microsoft Azure cloud
Take some time and spread your wings😌🤪. It doesn’t end when one creates a new VM, best practice is to update and upgrade APT repositories and to do that run this command in your server:
With these steps, we are done setting up Azure VM.
Opening a port on Azure using NSG
For the Golang web server, it will be running on port 7000 or whatever port you specified in the code. From the previous step, while we were trying to open up SSH access, we specified a port 22, if this port was not specified we would be like the kid trying to get out without a key😩. Therefore, to have access to port 7000, we need to create it using Azure Network Security Group (NSG).
Azure NSG allows filtering of network traffic to and from Azure resources in an Azure virtual network, the NSG contains security rules that allow or deny inbound traffic to or outbound traffic from, various Azure resources and for each rule you specify the source and destination, port and protocol.
Since we are opening port 7000, navigate to the Azure portal, then click on Network security groups select the name of your VM’s nsg and click on Inbound security rules since we are allowing traffic on 7000, click on ➕Add depending on your settings but for this post I will be leaving the Source As Any, Source port ranges As *, Destination as Any, set Destination port ranges to 7000, Protocol As Any, Action As allow and Priority As 350, give the port a name, called mine port_7000 and also a description.
Just like the man kicking the door down, you just got access to port 7000 on your server 😉😉. That was not so hard right?
Installing Docker and Golang
For GitLab runner, you need to provide runner executor that can be used to run your builds in different scenarios. And various executors are ranging from SSH, VirtualBox, Docker, Kubernetes and you can also create a custom executor, for this post we will be using Docker as our runner executor hence the need for us to install it.
To install Docker and enable Docker, run the following commands in your terminal
After running these commands you should have this output:
And to exit, simply click on q and you should be back to your shell.
Next step is to install Golang
Golang is an open-source programming language that makes it easy to build simple, reliable, and efficient software. Since this post is not about Golang, to learn more visit their webpage. Installing Go is quite straightforward and for this post am using go1.15.5. To have Go installed, run the following commands:
At the end of the command you should have an output like this:
And with this, you should have both Docker and Golang installed on your server.
Yeah, you should relax and chill cos we’ve just concluded the first part of this series.
A quick recap on what was done
- We looked at setting up a VM on Azure
- We opened port 7000 using Azure NSG for the VM we created
- Lastly, we installed Docker and Golang on our server
In the next post, we will be looking at the last three tasks to complete this project.
PS: My PC is running a Linux based OS (Debian) thus when I refer to the terminal, I mean Linux CLI.
To read more about Virtual Machine, you can read my post about it here.
In the coming month, I hope to write on Docker but you can also look up their documentation.
To run this on Windows OS, you need to have Gitbash installed or install WSL and download Ubuntu image from Microsoft Store you can also us putty