Notes on Building a Raspberry Pi Kubernetes Cluster (Part 5: Building and Running A First App)

import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:]
if name == "" {
name = "kubernetes"
}
fmt.Fprintf(w, "Hi there, I really do love %s!\n", name)
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
% curl localhost:8080
Hi there, I really do love kubernetes!
% curl localhost:8080/sushi
Hi there, I really do love sushi!
  1. Build the app into a container image.
  2. Push the container image to a container registry (we’ll use DockerHub).
  3. Deploy the container in our Kubernetes cluster.
  4. Expose the deployment as a service.
  5. Add an ingress to allow external connections to our service.

Build the app into a container image.

To run this in Kubernetes, we first need to build it into a container image. We’ll use docker for this.

FROM golang:1.16.5-buster as builder
WORKDIR /app
COPY tiny.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -v -o tiny tiny.go
FROM alpine:3.7
COPY --from=builder /app/tiny /tiny
EXPOSE 8080
CMD ["/tiny"]
docker build -t tiny .
docker run -p 8080:8080 tiny

Push the container image to DockerHub.

To use your container in Kubernetes, you’ll need to store it in a container registry that Kubernetes can access. Microk8s supports a local registry, but it’s easier to use DockerHub (which is also less likely to fail and lose your container images). You’ll need a Dockerhub account (it’s currently free), and after you’ve created your account and logged your local docker in using docker login, you can build and push your images to DockerHub (be sure to replace DOCKERHUBID with your own DockerHub id):

docker build -t DOCKERHUBID/tiny .
docker push DOCKERHUBID/tiny
docker run -p 8080:8080 DOCKERHUBID/tiny

Deploy the container in our Kubernetes cluster.

To run your container in Kubernetes, you’ll create a deployment. You can do this by applying a YAML file like the following (name it tiny.yaml and replace DOCKERHUBID with your own DockerHub id):

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: tiny
name: tiny
spec:
selector:
matchLabels:
run: tiny
template:
metadata:
labels:
run: tiny
spec:
containers:
- name: tiny
image: DOCKERHUBID/tiny:latest
ports:
- containerPort: 8080
nodeSelector:
kubernetes.io/arch: arm64
curl 10.1.112.169:8080

Expose the deployment as a service.

To make your deployment available outside of your cluster, you need to take two more steps. First, expose the deployment as a service. The following command sets up a mapping from port 80 of the new service to port 8080 of your container:

kubectl expose deployment tiny --port 80 --target-port 8080

Add an ingress.

Next we need to add an ingress. We’ll also use this to set up HTTPS. Let’s put our ingress definition in a file called http-ingress.yaml, replacing a.domain.me with a domain that you want to use for your tiny service (this should be one of the domains that you set up in Part 4).

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: http-ingress
spec:
tls:
- hosts:
- a.domain.me
secretName: home-tls
rules:
- host: a.domain.me
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: tiny
port:
number: 80
microk8s kubectl create secret tls home-tls \
--cert=fullchain.pem \
--key=privkey.pem
% curl https://a.domain.me
Hi there, I really do love kubernetes!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tim Burks

Tim Burks

Software developer in the SF Bay area. Electronic Design Automation, iOS apps, and now API tools for the world’s largest computer.