From 15335087f7790131a75fb780b12ffb246eb263c1 Mon Sep 17 00:00:00 2001 From: Mailson Peixe Date: Wed, 5 Apr 2023 14:50:32 -0300 Subject: [PATCH 1/2] Initial commit base --- Dockerfile | 12 +++++++++ README.md | 49 ++++++++++++++++++++++++++++++++- buildpush.sh | 6 +++++ kubernetes/deployment.yaml | 55 ++++++++++++++++++++++++++++++++++++++ kubernetes/ingress.yaml | 18 +++++++++++++ kubernetes/service.yaml | 14 ++++++++++ localbuild.sh | 7 +++++ main.go | 28 +++++++++++++++++++ start.sh | 10 +++++++ startcloud.sh | 12 +++++++++ stop.sh | 4 +++ 11 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100755 buildpush.sh create mode 100644 kubernetes/deployment.yaml create mode 100644 kubernetes/ingress.yaml create mode 100644 kubernetes/service.yaml create mode 100755 localbuild.sh create mode 100755 start.sh create mode 100755 startcloud.sh create mode 100755 stop.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3d9806f --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/README.md b/README.md index fa107ea..1cb439c 100644 --- a/README.md +++ b/README.md @@ -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` + diff --git a/buildpush.sh b/buildpush.sh new file mode 100755 index 0000000..30106bc --- /dev/null +++ b/buildpush.sh @@ -0,0 +1,6 @@ +#!/bin/bash +IMAGE=mailsonpeixe/app-go +echo "Build and push image ${IMAGE}" + +docker build --push -t $IMAGE:1.0 . + diff --git a/kubernetes/deployment.yaml b/kubernetes/deployment.yaml new file mode 100644 index 0000000..485ca99 --- /dev/null +++ b/kubernetes/deployment.yaml @@ -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 \ No newline at end of file diff --git a/kubernetes/ingress.yaml b/kubernetes/ingress.yaml new file mode 100644 index 0000000..23bce6e --- /dev/null +++ b/kubernetes/ingress.yaml @@ -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 diff --git a/kubernetes/service.yaml b/kubernetes/service.yaml new file mode 100644 index 0000000..919fdab --- /dev/null +++ b/kubernetes/service.yaml @@ -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 \ No newline at end of file diff --git a/localbuild.sh b/localbuild.sh new file mode 100755 index 0000000..12c861f --- /dev/null +++ b/localbuild.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +docker build -t app-go . + +docker run -p 3000:3000 app-go + +#URL access http://localhost:3000 \ No newline at end of file diff --git a/main.go b/main.go index 5275688..efa5c34 100644 --- a/main.go +++ b/main.go @@ -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 +} diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..f693ec3 --- /dev/null +++ b/start.sh @@ -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} + diff --git a/startcloud.sh b/startcloud.sh new file mode 100755 index 0000000..56cae4f --- /dev/null +++ b/startcloud.sh @@ -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} + diff --git a/stop.sh b/stop.sh new file mode 100755 index 0000000..52fc9c7 --- /dev/null +++ b/stop.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +echo "Removing app-go" +kubectl delete namespace lcd-25023 From 41bf7e2544fc0f396f330729704ec80d4b64f45c Mon Sep 17 00:00:00 2001 From: Mailson Peixe Date: Wed, 5 Apr 2023 14:53:50 -0300 Subject: [PATCH 2/2] adjust script --- buildpush.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildpush.sh b/buildpush.sh index 30106bc..b329c87 100755 --- a/buildpush.sh +++ b/buildpush.sh @@ -2,5 +2,5 @@ IMAGE=mailsonpeixe/app-go echo "Build and push image ${IMAGE}" -docker build --push -t $IMAGE:1.0 . +docker build --push -t ${IMAGE}:1.0 .