Skip to content

Commit

Permalink
Merge pull request #1 from mailsonpeixe/LCD-25023
Browse files Browse the repository at this point in the history
LCD-25023 - Day 5 Rotation Infra
  • Loading branch information
mailsonpeixe authored May 23, 2023
2 parents 542d019 + 41bf7e2 commit acc01ee
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main main.go


FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/main ./
COPY --from=builder /app/static ./static
EXPOSE 3000
CMD ["./main"]
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
# app-go
# Project to learn kubernetes objects.

Based in Go language show an index page in endpoint http

### Prerequisite for Linux users

Docker, Kind(or K3d), GO


### Versions that you pay attention:

GO Version: **1.20**

Kubernetes Version: > **1.22**



#### For localdev run:

`./localbuild.sh`

#### To build and push:

if necessary, adjust url repository and run `./buildpush.sh`

_Note: you have need permissions to upload the image Docker to repository._


### Deploy kubernetes local

Run script `./start.sh`

NOTE: in this case, we create a namespace called `lcd-25023` to app, check the script start.sh

Check service NodePort `kubectl get services`

### Stop app in kubernetes local

Run script `./stop.sh`

### Deploy Cloud Kubernetes
NOTE: Check the context that you are connected, `kubectx` and `kubens` are useful commands
NOTE 2: in this case, we create a namespace called `lcd-25023` and create a Ingress to app, check the script start.sh

Run script `./startcloud.sh`

and to stop and clear objects created, run `./stop.sh`

6 changes: 6 additions & 0 deletions buildpush.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
IMAGE=mailsonpeixe/app-go
echo "Build and push image ${IMAGE}"

docker build --push -t ${IMAGE}:1.0 .

55 changes: 55 additions & 0 deletions kubernetes/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-go
labels:
app: app-go
spec:
replicas: 2
strategy:
type: RollingUpdate
selector:
matchLabels:
app: app-go
template:
metadata:
labels:
app: app-go
spec:
terminationGracePeriodSeconds: 120
topologySpreadConstraints: #rule for spread pods in different zones
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: app-go
containers:
- name: app-go
image: mailsonpeixe/app-go:1.0
imagePullPolicy: Always
resources:
requests:
memory: "64Mi"
cpu: "50m"
limits:
memory: "128Mi"
cpu: "150m"
ports:
- containerPort: 3000
readinessProbe:
httpGet:
path: /healthz/ready
port: 3000
initialDelaySeconds: 5
failureThreshold: 3
successThreshold: 1
periodSeconds: 5
livenessProbe:
httpGet:
path: /healthz/live
port: 3000
initialDelaySeconds: 5
failureThreshold: 3
successThreshold: 1
periodSeconds: 5
18 changes: 18 additions & 0 deletions kubernetes/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
# If the class annotation is not specified it defaults to "gce".
kubernetes.io/ingress.class: "gce"
spec:
rules:
- http:
paths:
- path: /*
pathType: ImplementationSpecific
backend:
service:
name: app-go-serivce
port:
number: 3000
14 changes: 14 additions & 0 deletions kubernetes/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: app-go-serivce
labels:
app: app-go
spec:
type: NodePort
selector:
app: app-go
ports:
- protocol: TCP
port: 3000
targetPort: 3000
7 changes: 7 additions & 0 deletions localbuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

docker build -t app-go .

docker run -p 3000:3000 app-go

#URL access http://localhost:3000
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
package main

import (
"fmt"
"net/http"
)

func main() {
fmt.Printf("Starting the app.\n")
//Handlers to readiness and liveness
http.HandleFunc("/healthz/ready", readinessHandler)
http.HandleFunc("/healthz/live", livenessHandler)

http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":3000", nil)
}

func readinessHandler(w http.ResponseWriter, r *http.Request) {
// Check if the app is ready to serve requests
// and return a 200 OK response if so
// If not, return a 500 Internal Server Error response
if isReady() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}

func livenessHandler(w http.ResponseWriter, r *http.Request) {
// Return a 200 OK response to indicate that the app is running
w.WriteHeader(http.StatusOK)
}

func isReady() bool {
// Implement your own readiness check here
// For example, you might check if the app has successfully connected to a database
return true
}
10 changes: 10 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

NAMESPACE=lcd-25023
kubectl create namespace ${NAMESPACE}

echo "Create deployment"
kubectl apply -f kubernetes/deployment.yaml -n ${NAMESPACE}
echo "Create service"
kubectl apply -f kubernetes/service.yaml -n ${NAMESPACE}

12 changes: 12 additions & 0 deletions startcloud.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

NAMESPACE=lcd-25023
kubectl create namespace ${NAMESPACE}

echo "Create deployment"
kubectl apply -f kubernetes/deployment.yaml -n ${NAMESPACE}
echo "Create service"
kubectl apply -f kubernetes/service.yaml -n ${NAMESPACE}
echo "Create ingress"
kubectl apply -f kubernetes/ingress.yaml -n ${NAMESPACE}

4 changes: 4 additions & 0 deletions stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

echo "Removing app-go"
kubectl delete namespace lcd-25023

0 comments on commit acc01ee

Please sign in to comment.