How To: Deploy to Kubernetes

Pascal Allen
3 min readJun 29, 2023

--

Photo by Growtika on Unsplash

This publication is meant to serve as a very basic set of instructions for deploying to a Kubernetes cluster.

For simplicity, this publication assumes that you have already downloaded and are running Docker, Kubernetes, and kubectl on your host machine. This publication is also going to presume that you have an existing Docker Hub account. I’ve listed these prerequisites below.

Prerequisites

Tutorial Project Setup

First, let’s create an arbitrary “Hello, World!” web application to put things in perspective.

mkdir kubernetes-go && cd kubernetes-go/

Now from the newly created kubernetes-go/ directory, let’s initialize a Go module for our project:

docker run --rm \
-v ".":"/app" \
-w "/app" \
golang:1.21 \
go mod init github.com/pascalallen/kubernetes-go

Create a Dockerfile at the root of the project and add the following code:

FROM golang:1.21

LABEL org.opencontainers.image.source=https://github.com/pascalallen/kubernetes-go
LABEL org.opencontainers.image.description="Container image for kubernetes-go"
LABEL org.opencontainers.image.licenses=MIT

WORKDIR /app

COPY . ./
RUN go mod download

RUN go build -o /kubernetes-go

EXPOSE 9990

CMD ["/kubernetes-go"]

The above code that you just added to your Dockerfile is specific to building our tutorial’s Go image. A real-world Dockerfile is specific to the project and will not look like this.

Next, create a main.go file at the root of the project and add the following code:

package main

import (
"fmt"
"net/http"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
})

http.ListenAndServe(":9990", nil)
}

The above code that you just added to your main.go file is specific to our tutorial. This code provisions a web server and listens to requests on port 9990. When a request is made to the URI: /, you should get back a response of Hello, World!. But not quite yet since we still need to implement Kubernetes.

Implementing Kubernetes

At this point we’ve created a very simple program with Go and have added Docker instructions to build our program’s image. Now we’re ready to implement Kubernetes.

Let’s create a directory at the root of our project to store our project’s Kubernetes config, and add the following 2 files:

mkdir -p etc/k8s/go \
&& cd etc/k8s/go/ \
&& touch deployment.yml service.yml

Now add the following code to your deployment.yml file:

apiVersion: apps/v1
kind: Deployment
metadata:
name: go
spec:
replicas: 1
selector:
matchLabels:
name: go
template:
metadata:
labels:
name: go
spec:
containers:
- name: kubernetes-go
image: pascalallen/kubernetes-go
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9990

The above code that you just added to your development.yml file creates a ReplicaSet to bring up 1 Go Pod in your cluster using the pascalallen/kubernetes-go image from Docker Hub.

Similarly, add the following code to your service.yml file:

apiVersion: v1
kind: Service
metadata:
name: go
spec:
type: LoadBalancer
ports:
- name: http
port: 9990
targetPort: 9990
selector:
name: go

The above code that you just added to your service.yml file creates a load balancer in your cluster and exposes port 9990 to serve your project.

Your project tree should look like this:

kubernetes-go/
├─ Dockerfile
├─ etc/
│ └─ k8s/
│ └─ go/
│ ├─ deployment.yml
│ └─ service.yml
├─ go.mod
└─ main.go

Uploading Docker Images

From the root of the project, build and tag the latest version of your Docker image:

docker build -t pascalallen/kubernetes-go .

Log in to Docker Hub:

docker login

Now upload the image that you just created to Docker Hub:

docker push pascalallen/kubernetes-go

Deploying to Kubernetes

We are now ready to deploy to Kubernetes. To apply our resources to our cluster, run the following command at the root of the project:

kubectl apply -f etc/k8s/go

Locally, you will find the site running at http://localhost:9990/. And voila! There you have it, your first Kubernetes deployment!

To delete the the resources from your cluster, run the following command at the root of the project:

kubectl delete -f etc/k8s/go

Additional kubectl commands:

kubectl get pods # Lists Kubernetes Pods
kubectl get services # Lists Kubernetes Services
kubectl describe pods/go # Details a specific Pod
kubectl logs -f go # Stream logs for a specific Pod

Conclusion

This tutorial is meant to be a very basic foundation to what’s possible with Kubernetes. I hope you found this to be informative. The full codebase for this tutorial can be found here. Have a great day!

--

--

Pascal Allen
Pascal Allen

Responses (1)