How to Quickly Deploy Your Golang Web Server with Docker and Kubernetes

Fast Deployment of Your Golang Web Server Using Docker and Kubernetes

ยท

5 min read

How to Quickly Deploy Your Golang Web Server with Docker and Kubernetes

Prerequisites:

Let's start with Go programming:

Create a main.go file.

package main

import (
    "fmt"
    "net/http"
)

func homepage(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello Server")
}

func setupRouter() {
    http.HandleFunc("/", homepage)
}

func main() {
    fmt.Println("GO Web Server starting on 3000")
    setupRouter()
    http.ListenAndServe(":3000", nil)
}

This file listens on port 3000, and if you access the IP address followed by :3000, you will see the message "Hello Server" displayed in your browser.

Now that the web server is running, you can add more logic to the file, such as CRUD operations or small projects, for practice.


Let's proceed with the Dockerfile for deploying Go applications.

FROM golang:1.12.0-alpine3.9
RUN  mkdir /app
ADD . /app
WORKDIR /app
RUN go build -o main .
CMD ["/app/main"]

The Dockerfile above uses the 1.12.0-alpine3.9 version of Golang. It starts by creating a directory named app and then copies all files and folders into this app directory. It is set app as the working directory, and finally, it compiles the application by running the build command.

Build the Docker image using the docker build command:

docker build -t go-web-application .

This command builds the Docker image using the Dockerfile in the current directory (.) and tags it with the name go-web-application.

Before pushing the image, tag it with your Docker Hub username and repository name. For example, if your Docker Hub username is yourusername and you're pushing the image to a repository named go-web-application, you would tag the image like this:

docker tag go-web-application yourusername/go-web-application

Before you can push images to Docker Hub, you must log in with the docker login command.

docker login

You will be asked to enter your Docker Hub username and password.

After logging in, you can push the tagged image to Docker Hub with the docker push command:

docker push yourusername/go-web-application

Replace yourusername/go-web-application with your actual Docker Hub username and repository name. After running these commands, your Docker image will be uploaded to Docker Hub, allowing others to use it.


Let's begin by configuring Kubernetes to manage this Golang web server container:

This is a deployment file: deployment.yml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-web-application
spec:
  replicas: 1
  selector:
    matchLabels:
      name: go-web-app
  template:
    metadata:
      labels:
        name: go-web-app
    spec:
      containers:
      - name: go-web-app
        image: shettyanna/go-web-application
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3000

This is a Service file: service.yml

---
apiVersion: v1
kind: Service
metadata:
  name: go-web-service
spec:
  type: LoadBalancer
  ports:
  - name: http
    port: 80
    targetPort: 3000
  selector:
    name: go-web-app

This command creates a deployment named go-web-application with one replica, using the image shettyanna/go-web-application. It also creates a service named go-web-service of type LoadBalancer, which routes traffic from port 80 to port 3000 of pods labelled with name: go-web-app.

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

You should now understand how to deploy Golang applications using Docker and Kubernetes, including writing a Dockerfile, using Docker commands, and working with Kubernetes configuration files deployment.yaml and service.yaml, giving you a solid foundation in managing deployments.

However, there is one more important file that needs careful understanding: kubeconfig.yaml. Without this file, you can't access your Kubernetes server, regardless of whether it's hosted on AWS or Digital Ocean.

Let's understand what it is kubeconfig.yaml ?

A kubeconfig file is utilized by the Kubernetes command-line tool, kubectlfor authentication to the Kubernetes cluster and to configure options like the cluster, user, and context. This file enables kubectl to communicate with the Kubernetes API server.

The kubectl tool uses the information in the kubeconfig file to determine which cluster to interact with and how to authenticate with it.

Here's an example of a kubeconfig file:

apiVersion: v1
clusters:
- cluster:
    server: https://example.com
    certificate-authority-data: <certificate-authority-data>
  name: example-cluster
contexts:
- context:
    cluster: example-cluster
    user: example-user
  name: example-context
current-context: example-context
kind: Config
preferences: {}
users:
- name: example-user
  user:
    client-certificate-data: <client-certificate-data>
    client-key-data: <client-key-data>

This kubeconfig file defines one cluster, one user, and one context, and sets the current context to example-context. You should replace <certificate-authority-data>, <client-certificate-data>, and <client-key-data> with the actual certificate and key data.

You can specify a kubeconfig file by using the --kubeconfig flag with kubectl commands, like this:

kubectl --kubeconfig=/path/to/kubeconfig.yaml get pods

This lets kubectl use the settings from the given kubeconfig file. If you don't use --kubeconfig, kubectl will automatically use the default kubeconfig file found at ~/.kube/config.

This is one of the oldest servers used for testing Kubernetes on Digital Ocean. Take a look at this one:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS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WEIxdDc1eTJqQm5LeFQyCkNtUFFxVEIwM2hQU2NPcW5UTk9aMjJEYWQ3NW8vSDhrME5VdXQxMlBiUW94Z2pKSS9OcUdwNG1JZUdIMXBGcmYKS09pTUhqWUN5NnBhQlAxbndyS0o5bXZWQ3ErTS9tczJ0UDVyUlZ0V2ZNN28rSnhKVzlWTXpPd1Fuc2NMKytJdwpNanZMYmdFdEkySnMzaEFoOHViZkxBbEgvSXZsYmhZZHQwWVJwWnM5cjh4aEVtSmVzRXVEc2ZWbFgxYitpdEVVCkhXdU5aRTk5a0s1WFhGZUR5ZzRCWGtQUmQ1M21lRTZvQ3ptNkdwcEZwUmp1b0FHSHVhREhSUUZJZUxTOTFjeDkKQUpVOVFLZWdiZ0lBWlkrOStFcjFRNjBEaW5BbVFGeENicVZHVGFzTDErbVVaaTlmQjV1SXVFc0VqZz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
    server: https://0f6ecc5a-5e1c-4af4-8d8b-23adda5c6313.k8s.ondigitalocean.com
  name: do-blr1-k8s-1-25-4-do-0-blr1-1678340611615
contexts:
- context:
    cluster: do-blr1-k8s-1-25-4-do-0-blr1-1678340611615
    user: do-blr1-k8s-1-25-4-do-0-blr1-1678340611615-admin
  name: do-blr1-k8s-1-25-4-do-0-blr1-1678340611615
current-context: do-blr1-k8s-1-25-4-do-0-blr1-1678340611615
kind: Config
preferences: {}
users:
- name: do-blr1-k8s-1-25-4-do-0-blr1-1678340611615-admin
  user:
    token: dop_v1_9e4bac3ea4e793e746e99bbfb48ad5ac38639ffd59074dc68fa1bd5862902ecd

Conclusion:

In this blog, you will learn how to deploy a Golang web server using Docker and Kubernetes. You'll also discover how to use Docker Hub and connect to a Kubernetes server.

Your familiarity with cloud computing and Linux, such as AWS, Digital Ocean, Azure, etc., will influence your ability to install it or the errors you might encounter.

In upcoming posts, we will cover small projects using AWS Kubernetes and Golang to provide some practical experience, so make sure to follow and subscribe.

Thanks for reading this blog.

Did you find this article valuable?

Support Suraj Shetty by becoming a sponsor. Any amount is appreciated!

ย