Dockerize a GoLang HTTP server and deploy it on Kubernetes
Tags: docker, kubernetes, go
Published: 2022-10-20 06:57:40

<h2>Introduction</h2>
 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.
<h2>Let's Start</h2>
<h3>Build a docker image and push it to docker hub</h3>
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”]
```
 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 .)
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/Screenshot-2022-10-20-at-11.32.15-AM.png" alt="" width="1257" height="280" class="alignnone size-full wp-image-956814" />
 To check the image has been created or not use “docker image ls”
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/Screenshot-2022-10-20-at-11.32.36-AM.png" alt="images" width="697" height="142" class="alignnone size-full wp-image-956815" />
 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.
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/Screenshot-2022-10-20-at-11.32.58-AM.png" alt="" width="707" height="185" class="alignnone size-full wp-image-956816" />
<h3>Deploy the image by creating desirable pods in minikube</h3>
Write a deployment.yml to deploy it on minikube.
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/deploygoapp.yml_.png" alt="deployment.yml" width="370" height="410" class="alignnone size-full wp-image-956818" />
Check the pods are running by "kubectl get pods"
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/RESTARTS.png" alt="" width="707" height="161" class="alignnone size-full wp-image-956820" />
<h3>Attach a static IP to the pods so that it can be accessed in web browser</h3>
Create service.yml document to attach a static ip to the pods
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/Screenshot-2022-10-20-at-2.38.20-PM.png" alt="" width="290" height="266" class="alignnone size-full wp-image-956824" />
 Apply the service and tunnel through the minikube.
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/Screenshot-2022-10-20-at-2.35.53-PM.png" alt="" width="547" height="148" class="alignnone size-full wp-image-956826" />
<h3>Results</h3>
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/Screenshot-2022-10-20-at-2.33.40-PM-copy-1-e1666248231796.png" alt="" width="434" height="143" class="alignnone size-full wp-image-956850" />
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/Screenshot-2022-10-20-at-2.52.28-PM-e1666248304908.png" alt="" width="478" height="127" class="alignnone size-full wp-image-956852" /><br>

<h2>Conclusion</h2>
Open localhost:8080 u will be able to see the docker image is running.
if u get stuck the with this error.
<img src="https://cdn-ssl-devio-img.classmethod.jp/wp-content/uploads/2022/10/Screenshot-2022-09-02-at-12.37.13-AM.png" alt="" width="1792" height="238" class="alignnone size-full wp-image-956834" />
Use the command ```go env -w GO111MODULE=off``` 
<strong>Make sure the targetPort in service.yml is pointing at the containerPort where the server is listening</strong>
Thank you