Kubernetes Deployment for Go web service

Kanan Rahimov
Mar 19 · 3 min read
Gopher & Kubernetes

In this article, I show an example of Kubernetes deployment configuration for the Go-lang web service application. We review the deployment of web service with external access from a real-world project.

For more extended examples and context, please check the CodeKN project, which is open source.

Kubernetes related configuration can be found under the k8s folder of each sub-project (e.g. profx/k8s, flash/k8s).

The version of the source code at the time of writing this article is this: https://github.com/CoderVlogger/codekn/tree/9e976898dcf8b4e5011917ed3a2ee0e1dfc9cb3d

Deployment Configuration

As a service we have a very simple REST endpoint returning current env variables:

For the full source code check this link: https://github.com/CoderVlogger/codekn/blob/9e976898dcf8b4e5011917ed3a2ee0e1dfc9cb3d/flash/app.go

Each project to be deployed as a Kubernetes service/pod/deployment has its own Docker image. Dockerfile for this particular service looks like this:

FROM scratch
COPY --from=builder /app/flash /
ENV FLASH_PORT=8080
CMD [ "/flash" ]

As a first stage, we build our application using golang:latest and then, in a second stage, we copy the final binary to the scratch image. By doing this, we reduce the size of the final image. More about multistage Docker images you can find from this video.

And finally, deployment configuration:

There are 3 important points in this configuration:

  1. Line: image: index.docker.io/kenanbek/flash:0.7.0
    Here we specify which Docker image needs to be deployed.
  2. Line: containerPort: 8080
    This line specifies port exposed by container’s image. For more check this link.
  3. Lines: imagePullSecrets: — name: dhregcred
    This configuration allows us to pull the image from the private Docker registry. In the case of public Docker registry and Docker images, we do nod need these lines.

To create secrets to pull images from the private Docker registry, we can use the following command:

In this particular example, we create a credential for https://hub.docker.com/. Change --docker-server for other Docker registries.

To apply this deployment configuration, we can use the following command:

Service Configuration

And finally, to make this deployment accessible from the external world we need to have a service configuration:

This service configuration opens an internal 8080 port for public access via the port 80. IP for the access defined by the load balancer. Please note that in case of managed Kubernetes cluster, these settings create a new paid load balancer IP.

To apply the configuration, we can execute this command:


Thanks for reading this article. If you liked it, show your appreciation by clicking on the Clap button or following my profile. Also, check my YouTube channel for video tutorials:

https://www.youtube.com/codervlogger

Kanan Rahimov

Written by

Software Engineer. Author @CoderVlogger. Check https://kenanbek.github.io/ for more posts and updates.

More From Medium

Related reads

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade