Thursday, 31 July 2014

Deploying Golang on IBM Bluemix

Introduction

Go language is a relatively new systems programming language. I have used it to develop a number of personal interest projects. When it came time to revamp our lunchtime sports signup sheet application I decided to give it a try in Go.

The application was originally hosted through Google App Engine. I could have easily rewritten it using App Engine's new Go SDK but I would prefer to avoid using the proprietary API's. Instead, I would like to use a hosting service that allows me to use the standard Go libraries for better portability in the future.

IBM recently announced the release of a PaaS cloud service called Bluemix. It allows you to write Node JS and Java web applications using the standard libraries. I don't have to manage my own operating system instances like I would have to do with various IaaS providers (eg. AWS, Digital Ocean). Instead, I can focus on writing my application.

IBM Bluemix is built using Cloud Foundry. Cloud Foundry has released an official Go language build pack that is compatible with Bluemix. This article will show you how to put all of the pieces together to get a simple application running.

Project Setup

Your Go project can be structured in the normal way from the "How to Write Go Code" article with a few extra files: .godir, Profile and manifest.yml.

The godir file should be at the top of your project. It contains a single line that provides the fully qualified package name of your application. This is important because you won't be pushing your entire GOPATH to Bluemix, just your project. It has no way to know the package name.



Your Procfile will tell Bluemix and the Go buildpack the executable that should run. This will generally be the name of the project directory. You need to add the prefix "web:" to make it work.



There is a special manifest.yml file that provides a number of parameters to deploy your application. Applications have names in Bluemix that allow you to distinguish them easily when looking at your dashboard or using the command-line tool. You can also provide the hostname of the application, which will determine the web address to access it (e.g. http://[myapp1].mybluemix.net). There are other parameters to let you fine tune your app such as the number of instances and RAM allocation. Finally, there are parameters such as the buildpack, path and domain, which you probably won't need to change.



Application Code

Your application is written like a typical Go web server application. You create a main function that will ultimately call "http.ListenAndServe." Unlike many of the examples on the internet you don't have a choice about the port number. You need to read an environment variable called "VCAP_APP_PORT" to get the port number to bind at runtime. You can get the value using "os.Getenv." I usually check for the environment variable and fall back to using a default port like this so that I can test the application on my laptop.


When writing your application it is vital to make it as robust as possible during startup up until the point where it begins listening for http connections. If your application panics or crashes in this period then it becomes difficult to figure out what went wrong. I recommend that you avoid panicking or logging fatal errors in your application. Instead, log those errors using log.Errorf() and log.Printf(). You can find those messages later after you deploy using the Bluemix dashboard.

Deploying Your Application

Most Go programmers work locally on their laptop using their preferred editor. Deployments to Bluemix can be made using the Cloud Foundry command-line tool once it is installed on your laptop. Bluemix is based on Cloud Foundry, so the CLI is compatible.

You can download the Cloud Foundry CLI using "go get github.com/cloudfoundry/cli." Go get will complain that there are no buildable Go source files. This project has a custom build script. Change directory to the src/github.com/cloudfoundry/cli directory in your GOPATH. Run the bin/build script to compile. Finally, move the "cf" binary from the "out" directory to the "bin" directory of your GOPATH. The CLI will be on your system path assuming you have followed the "How to Write Go Code" article linked above.

Once you have installed the cf tool you can log into Bluemix with "cf login." The API endpoint for Bluemix is "https://api.ng.bluemix.net" and you log in with your BlueMix credentials. It will prompt you for a "space" to push your applications. Spaces are a way of organizing your deployed applications. When you first created your Bluemix account it probably created a space called "dev." If you are unsure which space to use then try that one.

You are now ready to deploy your application to Bluemix! Change directory to your project and run "cf push." When the push is complete your application should be online and accessible from your web browser at http://myapp1.mybluemix.net (replace myapp1 with the name you chose for your application). Check your Bluemix dashboard and there should be a box for your application. You can use the dashboard to start/stop and check the logs among other things.

If you want to deploy new changes to your application then run "cf push" again. The CLI will take care of stopping/starting your application with your changes.

Conclusion

This article has covered the three main areas needed to get a simple Go application running on IBM Bluemix. There are a few special files that need to be included in your project. Your application is for the most part a standard Go web server application with extra environment variables and special logging and robustness concerns. You can deploy your application using the Cloud Foundry CLI tool on your laptop. Future articles will attempt to cover more advanced topics such as database connectivity, using third party libraries and sending automated emails.

If you are interested in looking at a full example of a Go Bluemix application check out my lunchsoccer project on IBM DevOps Services.

Special thanks to Michele Crudele and his developer works article for providing guidance on how to get started.

2 comments:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in TECHNOLOGY , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Saurabh Srivastava
    MaxMunus
    E-mail: saurabh@maxmunus.com
    Skype id: saurabhmaxmunus
    Ph:+91 8553576305 / 080 - 41103383
    http://www.maxmunus.com/


    ReplyDelete
  2. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in IBM BLUEMIX , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on IBM BLUEMIX. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Saurabh Srivastava
    MaxMunus
    E-mail: saurabh@maxmunus.com
    Skype id: saurabhmaxmunus
    Ph:+91 8553576305 / 080 - 41103383
    http://www.maxmunus.com/


    ReplyDelete