How to dockerize golang and deploy to kubernetes

qomarullah
4 min readOct 23, 2020

Simple tutorial

Create Golang Sample Server

Set go mod and test

$ go mod init github.com/qomarullah/go-server
go: creating new go.mod: module github.com/qomarullah/go-server
$ go run main.go
//open other terminal
$ curl localhost:8090/medium
Hello, medium!

Add Docker file

Build Docker Image

docker build -t ${IMAGE}:${VERSION} .
docker tag ${IMAGE}:${VERSION} ${IMAGE}:latest

Build

$ docker build -t go-server:1.0 .
[+] Building 13.0s (16/16) FINISHED
$ docker tag go-server:1.0 go-server:latest

Try to run with port forward

$ docker run --publish 9000:8090 --detach go-server
7759278537f373baf2dc53c2657538a514e06f376fc1614945ad7e530d77de1f
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7759278537f3 go-server "./main" 9 seconds ago Up 8 seconds 0.0.0.0:9000->8090/tcp recursing_mclean

Push to Docker Hub

Create account at docker hub

login in terminal

$ docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /home/qomarullah/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

create new repository local based on your repository name in hub

$ docker tag 640c23e93556 qomarullah/go-server:k8s-test
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
go-server 1.0 640c23e93556 13 minutes ago 12.5MB
go-server latest 640c23e93556 13 minutes ago 12.5MB
qomarullah/go-server k8s-test 640c23e93556 13 minutes ago 12.5MB
ngepasar-user latest e4a2cc0c60ec 10 hours ago 29.1MB
docker101tutorial latest 4594f9784873 10 hours ago 27.5MB
alpine/git latest 94f8849864da 3 weeks ago 28.4MB
qomarullah/go-hello-world 1.0.0 576db613568b 9 months ago 13.9MB

push to docker hub

$ docker push qomarullah/go-server
The push refers to repository [docker.io/qomarullah/go-server]
ac205e25fff9: Pushed
5f70bf18a086: Pushed
cdcd5c4aa52b: Pushed
50644c29ef5a: Pushed
k8s-test: digest: sha256:069bcc56e3ab94c4232b8976010144bf723c279663e3272a82cdc2c47bfed5c1 size: 1155

*sample command to remove all docker images

docker system prune -a

Deploy to K8s

Pull command from docker Hub

$ docker pull qomarullah/go-server:k8s-test
k8s-test: Pulling from qomarullah/go-server
df20fa9351a1: Pull complete
5acda9e9b57e: Pull complete
4f4fb700ef54: Pull complete
53ceb6bd346f: Pull complete
Digest: sha256:069bcc56e3ab94c4232b8976010144bf723c279663e3272a82cdc2c47bfed5c1
Status: Downloaded newer image for qomarullah/go-server:k8s-test
docker.io/qomarullah/go-server:k8s-test
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
qomarullah/go-server k8s-test 640c23e93556 27 minutes ago 12.5MB

Create deployment config

Service

Service is an abstractiondefines a logical set of Pods and a policy by which to access them. For example, the following is using service routing with type NodePort, which means only within the cluster can access and use private IP. To validate access to your application, use this command

curl http://{service-meta-name}:{port}

to use curl inside cluster we need to deploy another container busyboxplus:curl

kubectl run curl --image=radial/busyboxplus:curl -i --tty
If you don't see a command prompt, try pressing enter.
[ root@curl:/ ]$ curl http://go-server:8090/test
Hello, test![ root@curl:/ ]$

or if you already have

kubectl exec -it curl -- /bin/sh
[ root@curl:/ ]$ curl http://go-server:8090/medium
Hello, medium!

*another command for login to inside pod

kubectl exec -it go-server-5dc64cd964-22mvl -- /bin/sh

Access External IP / Domain

To access external endpoint for example DB server outside the cluster, we can define service and endpoints with following config. Then any pods can access with this service name. For any changes on IP external, then only need to update to its endpoints config.

$ kubectl apply -f service-user.yml 
service/user-service configured
endpoints/user-service configured
$ kubectl exec -it curl -- /bin/sh
[ root@curl:/ ]$ curl http://user-service:20000/appuser/v1/ping
pong

for another tutorial on how to install and manage K8s in digital ocean please checkout this :

Thanks for Reading

--

--