From 9bf8e5f561bf29619e6ca4be379723e578d4281e Mon Sep 17 00:00:00 2001 From: kenriortega Date: Mon, 1 Nov 2021 11:23:36 -0400 Subject: [PATCH] CI: add dockerfile and k8s deployment --- Dockerfile.multistage | 48 ++++++++++++++++++++++++++++++++++ deployments/Dockerfile | 21 --------------- k8s/k8s-ngonx-deployments.yml | 49 +++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 Dockerfile.multistage delete mode 100644 deployments/Dockerfile create mode 100644 k8s/k8s-ngonx-deployments.yml diff --git a/Dockerfile.multistage b/Dockerfile.multistage new file mode 100644 index 0000000..9a113d4 --- /dev/null +++ b/Dockerfile.multistage @@ -0,0 +1,48 @@ +# Dockerfile References: https://docs.docker.com/engine/reference/builder/ + +# Start from the latest golang base image +FROM golang:1.16-alpine as builder + +# Add Maintainer Info +LABEL maintainer="Kenrique Ortega " + +# Set the Current Working Directory inside the container +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed +RUN go mod download + +# Copy the source from the current directory to the Working Directory inside the container +COPY . . + +# Build the Go app +RUN go build -ldflags "-s -w" -o /app/ngonx cmd/main.go + +######## Start a new stage from scratch ####### +FROM alpine:latest + +RUN apk --no-cache add ca-certificates + +# Build Args +ARG APP_DIR=/app + +# Create APP Directory +RUN mkdir -p ${APP_DIR} + +WORKDIR /app/ + +# Copy the Pre-built binary file from the previous stage +COPY --from=builder /app/ngonx . +COPY --from=builder /app/ngonx.yaml . + +# Expose port 8080 to the outside world +EXPOSE 8080 + +# Declare volumes to mount +VOLUME [${APP_DIR}] + +# Command to run the executable +CMD ["./ngonx"] \ No newline at end of file diff --git a/deployments/Dockerfile b/deployments/Dockerfile deleted file mode 100644 index 606887e..0000000 --- a/deployments/Dockerfile +++ /dev/null @@ -1,21 +0,0 @@ -## We'll choose the incredibly lightweight -## Go alpine image to work with -FROM golang AS builder - -## We create an /app directory in which -## we'll put all of our project code -RUN mkdir /app -ADD . /app -WORKDIR /app -## We want to build our application's binary executable -RUN CGO_ENABLED=0 GOOS=linux go build -o ngonx ./cmd/ - -## the lightweight scratch image we'll -## run our application within -FROM alpine:latest AS production -## We have to copy the output from our -## builder stage to our production stage -COPY --from=builder /app . -## we can then kick off our newly compiled -## binary exectuable!! -CMD ["./ngonx"] \ No newline at end of file diff --git a/k8s/k8s-ngonx-deployments.yml b/k8s/k8s-ngonx-deployments.yml new file mode 100644 index 0000000..ab555af --- /dev/null +++ b/k8s/k8s-ngonx-deployments.yml @@ -0,0 +1,49 @@ +--- +apiVersion: apps/v1 +kind: Deployment # Type of Kubernetes resource +metadata: + name: ngonx-proxy # Name of the Kubernetes resource +spec: + replicas: 3 # Number of pods to run at any given time + selector: + matchLabels: + app: ngonx-proxy # This deployment applies to any Pods matching the specified label + template: # This deployment will create a set of pods using the configurations in this template + metadata: + labels: # The labels that will be applied to all of the pods in this deployment + app: ngonx-proxy + spec: # Spec for the container which will run in the Pod + containers: + - name: ngonx-proxy + image: kenriortega/ngonx-proxy:0.1.0 + imagePullPolicy: Always + ports: + - containerPort: 8080 # Should match the port number that the Go application listens on + livenessProbe: # To check the health of the Pod + httpGet: + path: /health + port: 8080 + scheme: HTTP + initialDelaySeconds: 5 + periodSeconds: 15 + timeoutSeconds: 5 + readinessProbe: # To check if the Pod is ready or not + httpGet: + path: /readiness + port: 8080 + scheme: HTTP + initialDelaySeconds: 5 + timeoutSeconds: 1 +--- +apiVersion: v1 +kind: Service # Type of kubernetes resource +metadata: + name: ngonx-proxy-service # Name of the resource +spec: + type: NodePort # If you're deploying on cloud, you can use `type: LoadBalancer`. It will automatically provision a load balancer with the cloud provider that will route traffic to your application. + ports: # Take incoming HTTP requests on port 9090 and forward them to the targetPort of 8080 + - name: http + port: 9090 + targetPort: 8080 + selector: + app: ngonx-proxy # Map any pod with name ngonx-proxy to this service \ No newline at end of file