Skip to content

Commit

Permalink
go k8s http server
Browse files Browse the repository at this point in the history
  • Loading branch information
philyawj committed Aug 4, 2023
0 parents commit 65d0787
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:alpine

WORKDIR /app
COPY . /app

ENV BG_COLOR=pink
ENV HEADING_COLOR=steelblue
ENV HEADING_TEXT='Docker Golang HTTP Server'

RUN go install

CMD ["/go/bin/go-k8s-http-server"]
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Prerequisites

Install minikube and kubectl.

# Note

Building and running the docker image locally instead of pushing to docker hub.

# Run in Docker

`docker build -t philyawj/go-k8s-http-server .`

`docker run --rm -p 8080:8080 philyawj/go-k8s-http-server`

![Alt text](./img/docker.png)

http://localhost:8080

# Run in Kubernetes

`docker build -t philyawj/go-k8s-http-server .`

`minikube image load philyawj/go-k8s-http-server`

`minikube image ls`

`kubectl apply -f service.yaml`

`minikube service mywebapp`

![Alt text](./img/k8-1.png)

![Alt text](./img/k8-2.png)

![Alt text](./img/pods.png)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/philyawj/go-k8s-http-server

go 1.20
Binary file added img/docker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/k8-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/k8-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/pods.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"net/http"
"os"
)

func main() {
hostname, err := os.Hostname()
if err != nil {
panic(err)
}

server := &http.Server{Addr: ":8080"}
server.SetKeepAlivesEnabled(false)

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<body style='background-color:" + os.Getenv("BG_COLOR") + ";'>" +
"<h1 style='color:" + os.Getenv("HEADING_COLOR") + ";'>" + os.Getenv("HEADING_TEXT") + "</h1>" +
"<h3>served from " + hostname + "</h3>" +
"</body>"))
})

server.ListenAndServe()
}
53 changes: 53 additions & 0 deletions service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
apiVersion: v1
kind: Service
metadata:
name: mywebapp
labels:
app: mywebapp
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: gohttp
selector:
app: mywebapp
tier: http
type: LoadBalancer
---
kind: ConfigMap
apiVersion: v1
metadata:
name: myconfigmapv1.0
data:
BG_COLOR: 'steelblue'
HEADING_COLOR: 'white'
HEADING_TEXT: 'Kubernetes Golang HTTP Server - refresh to see updated served from'
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mydeployment
labels:
app: mywebapp
spec:
replicas: 5
selector:
matchLabels:
app: mywebapp
tier: http
template:
metadata:
labels:
app: mywebapp
tier: http
spec:
containers:
- name: mycontainer
image: philyawj/go-k8s-http-server
imagePullPolicy: Never
ports:
- containerPort: 80
envFrom:
- configMapRef:
name: myconfigmapv1.0

0 comments on commit 65d0787

Please sign in to comment.