A Simple Slack Bot in Go - The Hosting 2

Praise the Sun

The Real Dark Souls Slack Bot Continues Here Again

Things got a bit complicated in the last post. It takes quite a bit of work to get the VM running and our code deployed to it. This time around we’re still going to be using Google Cloud but instead of Compute Engine we’ll be using Kubernetes Engine to deploy our freshly Docker-ized application. If you haven’t read part two you’ll at least want to go back and read the section on get the Google Cloud SDK installed as we’ll be making use of it again. Other then that I am also assuming you have Docker already installed on your system

Let’s Dockerize

I’m going to assume that you have a passing familiarity with Docker and not go over to many of the basics if not @matthias has a nice overview article.

{% link matthias/getting-started-with-docker-40po %}

We are going to do a two stage build for our container, to start we’ll look at each section individually. Stage 1 is where we will build our executable - we’ll pull down the Golang Alpine image make sure it

FROM golang:1.10-alpine

# Alpine doesn't have git so let's grab it.
RUN apk update && apk add git

# Add our source.
ADD . /go/src/dsbot
RUN go get github.com/nlopes/slack
RUN go install dsbot

Once built we’ll switch over to using the latest Alpine image for the actual container. Since we’re going to be using HTTPS and web sockets for the Slack RTM connection we’ll need to add the ca-certificates package. Then we copy over the executable we build in stage one. We’ll be adding our SLACKTOKEN environment variable through the GKE web app so we use only ./dsbot as our command.

FROM alpine:latest
RUN apk add ca-certificates
COPY --from=0 /go/bin/dsbot .

CMD ["./dsbot"]

Dockerfile full listing

FROM golang:1.10-alpine

# Alpine doesn't have git so let's grab it.
RUN apk update && apk add git

# Add our source.
ADD . /go/src/dsbot
RUN go get github.com/nlopes/slack
RUN go install dsbot

FROM alpine:latest
RUN apk add ca-certificates
COPY --from=0 /go/bin/dsbot .

CMD ["./dsbot"]

Dockerfile in place we can now actually build our container. From the dsbot directory run

docker build .

Docker will do it’s thing, a partial output might appear as follows.

Sending build context to Docker daemon  5.391MB
Step 1/9 : FROM golang:1.10-alpine
1.10-alpine: Pulling from library/golang
...
...
...
Step 9/9 : CMD ["./dsbot"]
 ---> Running in 817641c6ca34
Removing intermediate container 817641c6ca34
 ---> ac2c157c12cf
Successfully built ac2c157c12cf

Container built we’ll now need to prep to send it to the Google Container Registry. You can find detailed information in the Google Cloud docs but it boils down to authorizing Docker to upload to the GCR which can be done with a simple…

gcloud auth configure-docker

I’ll be hosting my container in the US so I’ll use us.gcr.io, see the information linked to above for the other possible locations. If you have been following along you should also already have a project created. Mine is golang-slackbot. Using those details we can tag our Docker container.

docker tag 31f706728642 us.gcr.io/golang-slackbot/dsbot

Now we can push our container up to Google.

docker push us.gcr.io/golang-slackbot/dsbot

Turning back to the Google Cloud web interface we can look in the Kubernetes section. From here click on “Deploy Container”.

image

We want our shiny new container so click on the “Select Google Container Registry Image” button. Select the container off the list.

image

We need to make sure our Slack token is used so click on “Add Environment Variable”. Enter SLACKTOKEN as the key and your token xoxb-.... Now update the “Application name” to be dsbot or use whatever name you are referring to you bot as. Finally choose a “Zone” near you to host your bot.

image

Clicking the “View YAML” button will allow you to see what the actual configuration looks like. We’re not going to worry about that though and just hit “Deploy”.

image of deploy screen

Time to grab another pint, this may take a few minutes. After which you should see dsbot appear on your Slack instance.


Enjoy this post?
How about buying me a coffee?