RPI Microk8s Golang Tutorial
Hello Golang
main.go
#main.go package main import ( "fmt" "log" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { log.Print("running 8080") http.HandleFunc("/", handler) log.Fatal(http.ListenAndServe(":8080", nil)) }
Dockerfile
FROM golang:1.12 RUN mkdir /app ADD . /app WORKDIR /app RUN go build -o hello-web . CMD ["/app/hello-web"]
Docker Image in lokaler Registry erstellen
microk8s enable registry
docker build -t localhost:32000/hello-golang:1.0 . docker push localhost:32000/hello-golang:1.0
Helm Chart in ./k8s Verzeichnis im Namespace dev installieren
microk8s helm3 install hello-golang ./k8s --set defaultBackend.enabled=false,controller.defaultBackendService=hello-golang-service -n dev --create-namespace microk8s helm3 list
Helm Chart deinstallieren
microk8s helm3 uninstall hello-golang -n dev
Deployment & Service (deployment.yaml)
apiVersion: apps/v1 kind: Deployment metadata: name: hello-golang spec: replicas: 1 selector: matchLabels: app: hello-golang template: metadata: labels: app: hello-golang spec: containers: - name: app image: localhost:32000/hello-golang:1.0 ports: - containerPort: 8080 env: - name: MONGO_URL value: mongodb://mongo:27017/dev imagePullPolicy: Always --- apiVersion: v1 kind: Service metadata: labels: app: hello-golang-service name: hello-golang-service spec: ports: - port: 80 targetPort: 8080 selector: app: hello-golang type: NodePort
Ingress ingress.yaml
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: "hello-golang-ingress" spec: ingressClassName: public rules: - host: www.yourdomain.com http: paths: - path: /hello-golang pathType: Prefix backend: service: name: hello-golang-service port: number: 80
Zugriff über https://www.yourdomain.com/hello-golang