Deploy a simple website using Go on OpenShift

Go, which is often referred to as Golang, is a popular open source programming language nowadays, thanks to its ability to work on large distributed servers and to be a better alternative for C++ and Java. With Go, you can create chatrooms, large distributed websites, and many other types of applications without running into any hard-to-debug errors, such as in C++. Similarly, Go is very easy to learn, since it is rather close to C++ and Java in terms of syntax and concepts, and it is maintained by a large community.

Kubernetes is one of the old-fashioned way of deploying a scalable application. However, Red Hat OpenShift provides you with the ease of deploying an application without relying on the command line. This tutorial demonstrates how to take a simple Go website and deploy it on an OpenShift cluster.



This tutorial also assumes you are familiar with using Docker and writing Dockerfiles, since the website you create during the tutorial is dependent upon the Dockerfile and that you have an OpenShift cluster created.

Estimated time

This tutorial should take you 10 to 15 minutes to complete.

Develop the web server

This tutorial uses the net/http library in Go to create the web server, where you map your web pages (the final URL can be different from the page name). There are different libraries for web development in Go, but net/http is the easiest to start.

The Go file begins by defining the package; it refers to the name of the file. For example, main.go has package main, as presented in the following screen capture. You use the html/template library to render the pages in the browser and the fmt library to print out in the console (similar to cout in C++ and println in Python).

Screen capture of the main.go file package and libraries

You create your main function and define your template variable to get the HTML files in your template folder. Variables use the syntax x:= y for initialization.

With the following code snippet, you tell Go exactly where to find your HTML file and ask Go to parse the HTML file (notice the relative path). You wrap it in a call to template.Must(), which handles any errors and halts if there are fatal errors.

func main() {

    templates := template.Must(template.ParseGlob("template/*"))

Next, you create a handle for Go to look into your static folder, which contains the CSS files, as presented in the following code snippet. Your HTML comes with the CSS that Go needs to provide when we run the app. You tell Go to create a handle that looks in the static directory, and Go then uses the "/static/" as a URL that your HTML can refer to when looking for your CSS and other files. The final URL can be anything.


Go looks in the relative "static" directory first using http.FileServer() and then matches it to a URL of your choice, as shown in http.Handle("/static/"). This URL is what you need when referencing your CSS files. After the server begins, your HTML code is therefore <link rel="stylesheet" href="/static/stylesheet/...">. It is important to note the URL in http.Handle can be whatever you want, as long as you are consistent.

So far, you have obtained your HTML pages and linked the directories for the CSS files. Next, you write handle functions for your URLs. The http.HandleFunc takes a URL path and a function that takes a responseWriter and a pointer (similar to C++) to an HTTP request. Go allows anonymous functions to be passed as parameters, just like C++ and Java. With the following code snippet, you create the handle function for the homepage. This method takes in the URL path "/" and a function that takes in a response writer, and a HTTP request. It says that if there are errors, show an internal server error message.

    http.HandleFunc("/" , func(w http.ResponseWriter, r *http.Request) {

              if err := templates.ExecuteTemplate(w, "homepage.html", nil); err != nil {
          http.Error(w, err.Error(), http.StatusInternalServerError)

Finally, you print the web server that is listening to requests and link the website to port no. 8080.

    fmt.Println(http.ListenAndServe(":8080", nil));

This concludes the steps for building the web server. To get a better understanding of the Dockerfile that is written, refer to the Docker Official Images for Go.

There are two ways of deploying the website on OpenShift, either from the web console or the OpenShift command-line interface (oc). This tutorial covers both methods.

Start by signing up and logging into your IBM Cloud account.

Deploy the Go website using the OpenShift web console

Step 1: Access your OpenShift cluster

Log in to your OpenShift cluster by clicking the OpenShift web console button.

Screen capture of the OpenShift web console button

Step 2: Add the website to your cluster

Change to the Developer view and click on the +Add button. Next, in the Add pane, select From Dockerfile.

Screen capture of Add pane within OpenShift with From Dockerfile option highlighted

Insert into the Git Repo URL field and choose to expose a route to your application.

Screen capture of the Import from Dockerfile pane

Click the Create button.

Step 3: Start the website

In the Developer menu, click Topology. After the build is finished, you will see that the pod has started. Click the arrow icon to open the external link to the golang-ex website.

Screen capture of the Topology pane, which shows three files, labelled as `proxy`, `console`, and `golang-ex`. An arrow icon is displayed next to the `golang-ex` file, indicating that it opens an external link.

Deploy the Go website using the OpenShift command-line interface

Step 1: Log in to the cluster

Log in to your cluster with the following command:

oc login

Write your cluster credentials.

Step 2: Download the code and deploy the website

Download the code from the repo onto your computer.

Write the following commands to deploy and expose the website:

cd Golang
oc new-app . --name=golang-ex
oc expose svc/golang-ex

Step 3: Get the route

To get the route of the exposed website, write the following command:

oc get route golang-ex

Copy the URL of the website and paste it into your browser.

If you followed the previous instructions correctly, a congratulatory page will display.

Screen capture of the congratulatory page, which shows an image of the Go gopher mascot and the words "Congratulations on deploying Go on OpenShift. The time now is 10:34:36."


Congratulations! You successfully deployed your first Go website on your OpenShift cluster.