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!

Build the app into a container image.

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.

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

Deploy the container in our Kubernetes cluster.

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.

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

Add an ingress.

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