DEV Community

Cover image for Dockerize a GoLang HTTP server and deploy it on Kubernetes
Akshay Rao
Akshay Rao

Posted on

Dockerize a GoLang HTTP server and deploy it on Kubernetes

Introduction

In this blog I tried to host a simple HTTP server written in GoLang on Kubernetes.

  1. Build a docker image and push it to docker hub.
  2. Deploy the image by creating desirable pods in minikube.
  3. Attach a static IP to the pods so that it can be accessed in web browser.

    Let's Start

    Build a docker image and push it to docker hub

    We will require docker to be installed in PC. Go to the project and create a Dockerfile. Now we will have to write commands which can will launch the app when the Image is ran.
FROM galang:1.16-alpine #this will pull the official base image of golang

RUN mkdir /app

ADD . /app

WORKDIR /app

RUN go build project .

CMD [“/app/project”]
Enter fullscreen mode Exit fullscreen mode

In golang first we compile program and obtain a executable file, then we can run this executable file on any OS.
Build the image “docker build -t /name of the image/ .”( docker build -t aksrao1998/first-go-project .)

To check the image has been created or not use “docker image ls”
images
Then run docker run -p 8080:8080 -it /docker image/
Push the image to the docker hub by tagging it to the repository in docker hub.

Deploy the image by creating desirable pods in minikube

Write a deployment.yml to deploy it on minikube.
deployment.yml
Check the pods are running by "kubectl get pods"

Attach a static IP to the pods so that it can be accessed in web browser

Create service.yml document to attach a static ip to the pods

Apply the service and tunnel through the minikube.

Results



Conclusion

Open localhost:8080 u will be able to see the docker image is running.
if u get stuck the with this error.

Use the command

go env -w GO111MODULE=off

Make sure the targetPort in service.yml is pointing at the containerPort where the server is listening
Thank you

Top comments (0)