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.

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!

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create a setup so that, machine can only ping to the google but not able to ping to the Facebook…

How the computer calculates “a+b”?

Inv Arch Ambassador Program

Important Built-in-methods to Override for your Python Class:

Install/Use rbenv (Ruby) on Windows 10 Linux WSL(2)

Tuple, Type Alias and Case Class

Tech 2: What are APIs? : Part 2

Build a full-stack website using Flask

flask logo

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.

More from Medium

Run python program on VM startup using rc.local

LiAise — Artificial Intelligence Camera Application for deaf and hard of hearing people…

Experimenting with Machine Learning for Analysis of Lunar Crater Degradation

How to Build a Brand Using Jewelry Box Model