Compute Services

Easily create a Kubernetes-powered cluster for deploying a Go application

Share this post:

Recently IBM has released a Beta of Kubernetes support in the IBM Bluemix Container Service. The IBM Bluemix Container Service, now combines Docker and Kubernetes to deliver powerful tools, an intuitive user experience, and built-in security and isolation to enable rapid delivery of applications all while leveraging Cloud Services including cognitive capabilities from Watson.

In this blog post, you will learn how easy to create a Kubernetes cluster on IBM Bluemix Container service, and have your application developed in Go language up and running in IBM infrastructure.

This blog post assumes that you already have an IBM Bluemix account. If you do not have one, this is a right time to create a free account. This blog post uses IBM Bluemix features that do not incur any cost. So, don’t worry and continue learning.

Creating your Free Cluster

Login to IBM Bluemix and create your free Kubernetes cluster.

CreateCluster

  • Click on the Kubernetes cluster, to start creating your cluster.  It will take several minutes to complete the task. Behind the scenes, a new machine will be provisioned for you in IBM Bluemix Infrastructure, and bootstraped with a secured image and kubernetes cluster will be deployed. You will not have to do any extra configuration to get started.
  • While it takes several minutes to complete the cluster setup, you can continue with next steps to build and push your image to IBM Bluemix Container Registry.

Building and Pushing your container image to IBM Bluemix Container Registry

If you are new to Go language, please visit Documentation page to install and try Go samples on your system.

You will need to install several CLIs to interact with the cluster. Please visit Setting up IBM Container Server plugin page to install the following:

  1. Bluemix CLI(bx commands)
  2. container-registry plugin(bx cs commands)
  3. container-service plugin(bx cr commands)
  4. Kubernetes CLI(kubectl commands)

Code

Open your favorite editor to write a simple GO server application. You can copy the following code snippet and save it as server.go

package main

import (
"fmt"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, IBM Container Service is simple to use and a very powerful service to solve your complex problems!")
}

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}

Dockerfile

Now, create a Dockerfile in the same directory as the above code with the following content:

FROM golang
ADD ./server.go /go/src/server/
RUN go install server
ENTRYPOINT /go/bin/server
EXPOSE 8080

Build your image

Before building your image, get your IBM Container Registry namespace name by typing in

bx cr namespaces

command. Run the following commands with YOUR_NAMESPACE replaced with yours, to build and push your image to IBM Container Registry:

docker build -t registry.ng.bluemix.net/YOUR_NAMESPACE/server .
bx cr login
docker push registry.ng.bluemix.net/YOUR_NAMESPACE/server

Now your image is built and pushed into IBM Container Registry. You can run

bx cr images

to list images and verify the newly built image is available.

Kubernetes Configuration

It is time to create a new Kubernetes configuration file. Here is the yml file to create a deployment and expose a service for your deployment. Save the yml file as server-deployment.yml file. Make sure to replace the YOUR_NAMESPACE with yours.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-go-server
spec:
replicas: 3
template:
metadata:
name: my-go-server
labels:
run: my-go-server
spec:
containers:
- name: my-go-server
image: "registry.ng.bluemix.net/YOUR_NAMESPACE/server"
imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
name: my-service
labels:
run: my-go-server
spec:
type: NodePort
selector:
run: my-go-server
ports:
- protocol: TCP
port: 8080
nodePort: 30080

Deploying your Go Server to IBM Container Service

Now you have build the image for your service, pushed the image to IBM Container Registry and have your yml file ready to create a deployment in kubernetes.

Run the following commands, to ensure the cluster and worker is ready(state: deployed)

bx cs clusters
bx cs workers mycluster

If the cluster and workers are ready, run the following command to download the cluster configuration:

bx cs cluster-config mycluster

Once the cluster configuration is retrieved, run the export command from the above command output text, to configure kubectl so it can talk to the newly created cluster.

Now, run the following command to deploy the  pod and service defined in the yml file:

kubectl create -f server-deployment.yml

On successful completion of the above command, you will have a pod and service created. You can find the public IP of the worker node by running the following command:

bx cs workers mycluster

Access the newly deployed application in a browser by opening the following URL with WORKER_NODE_PUBLIC_IP replaced with actual IP from prior command:

http://WORKER_NODE_PUBLIC_IP:30080/

Cleaning Up your deployment

To clean up, just delete the cluster by running the following command:

bx cs cluster-rm mycluster

Summary

This Blog post demonstrates how easy to create a cluster, build and deploy a Go server into the newly created cluster.

You can join our Slack and chat directly with experts on using IBM Bluemix Container Service.

Ramakrishna Boggarapu

More Compute Services stories
April 29, 2019

Transforming Customer Experiences with AI Services (Part 1)

This is an experience from a recent customer engagement on transcribing customer conversations using IBM Watson AI services.

Continue reading

April 26, 2019

Updated Tutorial: Database-Driven Chatbot

The tutorial on how to build a database-driven chatbot has been updated. It's now simpler to deploy and and offers more options—Slack, Facebook Messenger, Wordpress, and more.

Continue reading

April 24, 2019

How To Use IBM Cloud Object Storage with Veeam

As you may have heard, Veeam 9.5u4 now includes an integration with IBM Cloud Object Storage. This integration can result in up to 10x savings on long-term data retention and an overall reduction in IT and primary storage costs.

Continue reading