Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploymets #1

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
deployments/
.git/
*.md
*.txt
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ go-boilerplate/
│ └── unit/
├── deployments/
│ ├── docker/
│ └── kubernetes/
│ ├── kubernetes/
│ └── serverless/
├── docs/
├── .gitignore
├── go.mod
Expand Down Expand Up @@ -71,6 +72,7 @@ Configuration files and scripts for deploying the application.

- `docker/`: Dockerfile and related configurations for containerization.
- `kubernetes/`: Kubernetes manifests for orchestration.
- `serverless/`: Serverless configuration files for cloud function deployment.

#### `docs/`
Project documentation, API specifications, and any other relevant documentation.
Expand Down Expand Up @@ -103,7 +105,45 @@ Project documentation, API specifications, and any other relevant documentation.

## Deployment

Refer to the `deployments/` directory for Docker and Kubernetes configurations. Ensure to update these as the application evolves.
This project supports multiple deployment options:

### Docker

Refer to the `deployments/docker/` directory for Docker configurations. To build and run the Docker container:

1. Build the Docker image:
```
docker build -t go-rest-api -f deployments/docker/Dockerfile .
```
2. Run the container:
```
docker run -p 8080:8080 go-rest-api
```

### Kubernetes

Kubernetes manifests are available in the `deployments/kubernetes/` directory. To deploy to a Kubernetes cluster:

1. Apply the manifests:
```
kubectl apply -f deployments/kubernetes/
```

### Serverless

For serverless deployment, we use the Serverless Framework. Configuration files are located in the `deployments/serverless/` directory.

1. Install the Serverless Framework:
```
npm install -g serverless
```
2. Deploy the application:
```
cd deployments/serverless
serverless deploy
```

Ensure to update these configurations as the application evolves. For more detailed deployment instructions, refer to the respective README files in each deployment directory.

## Contributing

Expand Down
40 changes: 40 additions & 0 deletions deployments/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Filename: Dockerfile
# Location: /deployments/docker/Dockerfile

# Start from the official Go image
FROM golang:1.22.5-alpine AS builder

# Set the working directory inside the container
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies
RUN go mod download

# Copy the entire project
COPY . .

# Build the application
# The main file is located in /cmd/api
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd/api

# Start a new stage from scratch
FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /root/

# Copy the pre-built binary file from the previous stage
COPY --from=builder /app/main .

# Copy any config files if needed
# COPY --from=builder /app/config ./config

# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the executable
CMD ["./main"]
82 changes: 82 additions & 0 deletions deployments/kubernetes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Kubernetes Deployment

This directory contains Kubernetes manifests for deploying the Go REST API application.

## Files

- `deployment.yaml`: Defines the Deployment for the Go REST API application
- `service.yaml`: Exposes the Deployment as a Service within the cluster
- `configmap.yaml`: Contains configuration data as key-value pairs
- `ingress.yaml`: Configures ingress for external access to the Service

## Local Deployment with Docker Desktop or Minikube

### Prerequisites

- Docker Desktop with Kubernetes enabled, or Minikube installed
- kubectl CLI tool installed

### Steps for Local Deployment

1. Start your local Kubernetes cluster:
- For Docker Desktop: Enable Kubernetes in Docker Desktop settings
- For Minikube: Run `minikube start`

2. Build your Docker image locally:
```bash
docker build -t go-rest-api:latest -f ../../deployments/docker/Dockerfile ../..
```

3. If using Minikube, load the image into Minikube:
```bash
minikube image load go-rest-api:latest
```

4. Update the image in `deployment.yaml`:
```yaml
image: go-rest-api:latest
```

5. Apply the Kubernetes manifests:
```bash
kubectl apply -f .
```

6. Verify the deployment:
```bash
kubectl get deployments
kubectl get services
kubectl get pods
```

7. Access the application:
- The API should now be accessible at `http://localhost:30080`
- If you're using Docker Desktop with Kubernetes, you might need to use `http://localhost:30080`
- If you're using Minikube, you might need to run `minikube service go-rest-api-service --url` to get the correct URL

## Deployment Steps (for non-local environments)

1. Ensure you have `kubectl` installed and configured to interact with your Kubernetes cluster.

2. Update the image in `deployment.yaml` to point to your container registry:
```yaml
image: your-registry/go-rest-api:latest
```

3. Apply the Kubernetes manifests:
```bash
kubectl apply -f .
```

4. Verify the deployment:
```bash
kubectl get deployments
kubectl get services
kubectl get pods
```

5. If using Ingress, ensure you have an Ingress controller installed in your cluster.

## Scaling

To scale the number of replicas, you can use:
7 changes: 7 additions & 0 deletions deployments/kubernetes/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: go-rest-api-config
data:
GIN_MODE: "release"
# Add other environment variables as needed
32 changes: 32 additions & 0 deletions deployments/kubernetes/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-rest-api
labels:
app: go-rest-api
spec:
replicas: 1
selector:
matchLabels:
app: go-rest-api
template:
metadata:
labels:
app: go-rest-api
spec:
containers:
- name: go-rest-api
image: go-rest-api:latest
imagePullPolicy: Never
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: go-rest-api-config
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
18 changes: 18 additions & 0 deletions deployments/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: go-rest-api-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: go-rest-api-service
port:
number: 80
12 changes: 12 additions & 0 deletions deployments/kubernetes/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: go-rest-api-service
spec:
type: NodePort
selector:
app: go-rest-api
ports:
- port: 8080
targetPort: 8080
nodePort: 30080
2 changes: 1 addition & 1 deletion deployments/serverlesss/handlers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var ginLambda *ginadapter.GinLambda
func init() {
// stdout and stderr are sent to AWS CloudWatch Logs
log.Printf("Gin cold start")
r := routes.SetupRouter()
r := api.SetupRouter()
ginLambda = ginadapter.New(r)
}

Expand Down
9 changes: 6 additions & 3 deletions deployments/serverlesss/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
service: go-102
service: go-boilerplate
frameworkVersion: ^3.0.0

plugins:
Expand All @@ -9,8 +9,11 @@ provider:
name: aws
architecture: arm64
runtime: provided.al2
region: us-west-1
stage: local
region: ${opt:region, 'us-west-1'}
stage: ${opt:stage, 'dev'}
tracing:
apiGateway: true
lambda: true

custom:
go:
Expand Down
2 changes: 1 addition & 1 deletion internal/api/routes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes
package api

import (
"github.com/gin-gonic/gin"
Expand Down