My First GoLang Application in Kubernetes
Go Lang is one of the popular (might be most popular) languages for developing Microservices or Enterprise Applications in Kubernetes or OpenShift.
Join the DZone community and get the full member experience.
Join For FreeHi,
In an era of Kubernetes and container-based applications, it is very important to know Go Language as well. Go Lang is one of the popular (might be most popular) languages for developing Microservices or Enterprise Applications in Kubernetes or OpenShift. In this article, we will learn how to deploy the Go application in Kubernetes.
I have used the following technologies for developing/deploying Go Application:
1. minikube v1.17.1 on Fedora 33
2. Docker/Podman
3. Visual Studio Code 1.51.1
Step 1
Create a file firstGoInKubernetes.go with the following content. This is a simple web server application listening on port 9090.
x
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", handlerFunc)
log.Fatal(http.ListenAndServe("0.0.0.0:9090", nil))
}
func handlerFunc(w http.ResponseWriter, r *http.Request) {
log.Printf("Ping from %s", r.RemoteAddr)
fmt.Fprintln(w, "Hello, WebServer is listening on 9090")
}
Step 2
Before creating a podman/docker image, let us first test this application is ok or not:
x
$ go run firstGoInKubernetes.go
# in different terminal
$ curl http://0.0.0.0:9090
Hello, WebServer is listening on 9090
Step 3
As we have tested the application and it provides the desired result, we will now go build to compile the package and dependencies. GOOS=linux
and GOARCH=amd64
because I am running this in Fedora 33. CGO_ENABLE=0
creates a standalone binary which is ideal for docker images.
x
$ CGO_ENABLE=0 GOOS=linux GOARCH=amd64 go build -o firstGoImage
$ ls -ltr
-rwxrwxr-x. 1 chandrashekhar chandrashekhar 6445426 Feb 21 17:58 firstGoImage
-rw-rw-r--. 1 chandrashekhar chandrashekhar 60 Feb 21 18:03 Dockerfile
-rw-r--r--. 1 chandrashekhar chandrashekhar 308 Feb 21 18:35 firstGoInKubernetes.go
Step 4
In the same folder location of this firstGoInKubernetes.go, we can create a Docker file Dockerfile:
x
FROM alpine:latest
WORKDIR deploy
COPY firstGoImage /deploy/
EXPOSE 9090
CMD ["/deploy/firstGoImage"]
Step 5
Start minikube. I am using the profile Go-POC. Also, point docker context to minikube's docker registry:
x
#start minikube
$ minikube start -p Go-POC
# point docker registry to minikube's with Go-POC profile
$ eval $(minikube -p Go-POC docker-env)
# check if docker registry is correctly set
$ minikube -p Go-POC ip
192.168.39.21
$ docker context ls
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default * Current DOCKER_HOST based configuration tcp://192.168.39.21:2376 https://192.168.39.21:8443 (default) swarm
$
Step 6
Build docker image:
x
$ docker build -t first-go-image:v1.0 .
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
first-go-image v1.0 4491e85f623b About a minute ago 12.1MB
alpine latest 28f6e2705743 3 days ago 5.61MB
k8s.gcr.io/kube-proxy v1.20.2 43154ddb57a8 5 weeks ago 118MB
k8s.gcr.io/kube-controller-manager v1.20.2 a27166429d98 5 weeks ago 116MB
k8s.gcr.io/kube-apiserver v1.20.2 a8c2fdb8bf76 5 weeks ago 122MB
k8s.gcr.io/kube-scheduler v1.20.2 ed2c44fbdd78 5 weeks ago 46.4MB
kubernetesui/dashboard v2.1.0 9a07b5b4bfac 2 months ago 226MB
gcr.io/k8s-minikube/storage-provisioner v4 85069258b98a 2 months ago 29.7MB
k8s.gcr.io/etcd 3.4.13-0 0369cf4303ff 5 months ago 253MB
k8s.gcr.io/coredns 1.7.0 bfe3a36ebd25 8 months ago 45.2MB
kubernetesui/metrics-scraper v1.0.4 86262685d9ab 11 months ago 36.9MB
k8s.gcr.io/pause 3.2 80d28bedfe5d 12 months ago 683kB
$
Step 7
Create Kubernetes Deployment and expose a NodePort service for this deployment:
x
$ kubectl create deployment gofirstimage --image=first-go-image:v1.0 --replicas=1
deployment.apps/gofirstimage created
$ kubectl expose deployment gofirstimage --name=go-service --port=9090 --target-port=9090 --type=NodePort
service/go-service exposed
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/gofirstimage-fd8879bdd-zkdbj 1/1 Running 0 20m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/go-service NodePort 10.99.180.230 <none> 9090:31614/TCP 12m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/gofirstimage 1/1 1 1 20m
NAME DESIRED CURRENT READY AGE
replicaset.apps/gofirstimage-fd8879bdd 1 1 1 20m
$
Step 8
Access Service:
xxxxxxxxxx
# check minikube IP. This is IP of node where NodePort is exposed.
$ minikube ip -p Go-POC
192.168.39.21
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
go-service NodePort 10.99.180.230 <none> 9090:31614/TCP 17m
$ curl http://192.168.39.21:31614
Hello, WebServer is listening on 9090
$ kubectl logs -f gofirstimage-fd8879bdd-zkdbj
2021/02/22 09:08:24 Ping from 172.17.0.1:15745
Step 9
Further Troubleshooting: Check the content of the docker image to find if Go binary is available:
x
$ eval $(minikube -p Go-POC docker-env)
$ docker ps |grep firstGo
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69c0567525f4 79950e6f3c45 "/deploy/firstGoImage" 31 minutes ago Up 31 minutes k8s_first-go-image_gofirstimage-fd8879bdd-zkdbj_goproject_c6764971-fbaa-4f7c-bbc9-7c41e156d968_0
$ docker export -o image.tar 69c0567525f4
$ tar xvf image.tar
$ cd deploy/
$ ls -ltr
total 6248
-rwxrwxr-x. 1 chandrashekhar chandrashekhar 6393972 Feb 21 22:50 firstGoImage
Wrap Up
That's it, guys. I believe these steps would help you to have a better understanding of developing + deploying + troubleshooting the Go Lang Application in Kubernetes or OpenShift.
Opinions expressed by DZone contributors are their own.
Comments