Kubernetes Deployment for Go web service

Gopher & Kubernetes

Deployment Configuration

...
func (a *App) indexRoute(w http.ResponseWriter, r *http.Request) {
if a.cache == nil {
response := NewResponse(a.version)
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
response.AddItem(pair[0], pair[1])
}

js, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

a.cache = js
}

w.Header().Set("Content-Type", "application/json")
w.Write(a.cache)
}

func (a *App) register() {
http.HandleFunc("/", Gzip(http.HandlerFunc(a.indexRoute)))
}

func (a *App) Run() {
a.register()
http.ListenAndServe(a.server, nil)
}
...
FROM golang:latest as builder
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o flash -v ./cmd/flash.go
FROM scratch
COPY --from=builder /app/flash /
ENV FLASH_PORT=8080
CMD [ "/flash" ]
apiVersion: apps/v1
kind: Deployment
metadata:
name: flash
labels:
app: flash
spec:
replicas: 3
selector:
matchLabels:
app: flash
template:
metadata:
labels:
app: flash
spec:
containers:
- name: flash
image: index.docker.io/kenanbek/flash:0.7.0
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 100m
memory: 50Mi
requests:
cpu: 10m
memory: 10Mi
ports:
- name: http
containerPort: 8080
imagePullSecrets:
- name: dhregcred
kubectl create secret docker-registry dhregcred --docker-server=https://index.docker.io/v1/ --docker-username=<USERNAME> --docker-password=<PASSWORD> --docker-email=<EMAIL>
kubectl apply -f k8s/flash-deployment.yaml

Service Configuration

apiVersion: v1  
kind: Service
metadata:
name: loadbalancer
labels:
app: flash
spec:
type: LoadBalancer
selector:
app: flash
ports:
- name: http
port: 80
targetPort: 8080
kubectl apply -f k8s/flash-service.yaml

Software Engineer. Go and Python. Learning Rust. https://kenanbek.github.io/

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