Skip to content

Commit

Permalink
feat: helmify simplebank eks cd
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Apr 19, 2024
1 parent f2fa6d1 commit 34a528b
Show file tree
Hide file tree
Showing 26 changed files with 573 additions and 150 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ jobs:
- name: Set kube config
run: aws eks update-kubeconfig --name ${{ secrets.EKS_CLUSTER }} --region ${{ secrets.AWS_REGION }}

- name: Deploy changes to EKS cluster
env:
IMAGE: ${{steps.login-ecr.outputs.registry}}/${{secrets.AWS_ECR_REPOSITORY}}:${{github.sha}}
- name: Install helm chart to EKS cluster
run: |
kubectl apply -f eks/deployment.yml
kubectl apply -f eks/service.yml
kubectl set image deployment/simplebank-deployment simplebank=$IMAGE -n simplebank
helm install simplebank helm/simplebank
--set image.repository=${{steps.login-ecr.outputs.registry}}/${{secrets.AWS_ECR_REPOSITORY}},
image.tag=${{github.sha}},
image.pullPolicy=IfNotPresent
7 changes: 0 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ FROM golang:1.22.2-alpine3.19 AS builder

WORKDIR /app

RUN apk add curl
RUN curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.0/migrate.linux-amd64.tar.gz | tar xvz

# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
COPY go.mod go.sum ./
RUN go mod download && go mod verify
Expand All @@ -17,14 +14,10 @@ FROM alpine:3.19
WORKDIR /usr/local/bin

COPY --from=builder /app/server .
COPY --from=builder /app/migrate .

COPY app.env .
COPY scripts/start.sh .
COPY sql/migrations/ ./sql/migrations/

CMD ["server"]

ENTRYPOINT [ "start.sh" ]

EXPOSE 8080
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ ifneq (,$(wildcard ./app.env))
endif

postgres:
docker run --name $(DB_CONTAINER_NAME) -p $(DB_PORT):5432 -e POSTGRES_PASSWORD=$(DB_PASSWORD) -e POSTGRES_USER=$(DB_USER) -d postgres:16-alpine
docker run --name simplebank-db -p $(DB_PORT):5432 -e POSTGRES_PASSWORD=$(DB_PASSWORD) -e POSTGRES_USER=$(DB_USER) -d postgres:16-alpine

createdb:
docker exec -it $(DB_CONTAINER_NAME) createdb --username=$(DB_USER) --owner=$(DB_USER) $(DB_NAME)
docker exec -it simplebank-db createdb --username=$(DB_USER) --owner=$(DB_USER) $(DB_NAME)

dropdb:
docker exec -it $(DB_CONTAINER_NAME) dropdb --username=$(DB_USER) $(DB_NAME)
docker exec -it simplebank-db dropdb --username=$(DB_USER) $(DB_NAME)

migrateup:
migrate -path sql/migrations -database $(DB_URL) -verbose up
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,54 @@ make migrateup

For more scripts checkout [`Makefile`](/Makefile)

## Local k8s setup

### Setting up server in minikube k8s cluster

- Create env file
```bash
cp app.example.env app.env
```
- Start minikube k8s cluster
```bash
minikube start
```
- Set minikube docker env
```bash
eval $(minikube docker-env)
```
- Build simplebank docker image
```bash
docker build . -t aseerkt/simplebank:latest
```
- Create *simplebank** namepsace
```bash
kubectl create namespace simplebank
```
- Apply simplebank k8s deployment objects
```bash
kubectl apply -f eks/deployment.yml
```

### Post cleanup

-
```bash
kubectl delete -f eks/deployment.yml
```
-
```bash
docker rmi aseerkt/simplebank:latest
```
-
```bash
eval $(minikube docker-env --unset)
```
-
```bash
minikube stop && minikube delete
```

# Deployment


Expand Down
3 changes: 2 additions & 1 deletion app.sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ DB_USER=root
DB_PASSWORD=secret
DB_NAME=simple_bank
DB_PORT=5432

DB_URL="postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable"
DB_CONTAINER_NAME=postgres16
MIGRATE_URL=file://sql/migrations

SERVER_ADDRESS=:8080
20 changes: 20 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"database/sql"
"log"

"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"

"github.com/aseerkt/go-simple-bank/pkg/api"
"github.com/aseerkt/go-simple-bank/pkg/db"
"github.com/aseerkt/go-simple-bank/pkg/utils"
Expand All @@ -20,6 +24,8 @@ func main() {
log.Fatal("unable to connect to db: ", err)
}

runDBMigrations(config.MigrateUrl, config.DBUrl)

store := db.NewStore(conn)

server := api.NewServer(store, &config)
Expand All @@ -28,3 +34,17 @@ func main() {

server.Start(config.ServerAddress)
}

func runDBMigrations(migratePath string, dbUrl string) {
migrateInstance, err := migrate.New(migratePath, dbUrl)

if err != nil {
log.Fatal("cannot create new migrate instance", err)
}

if err := migrateInstance.Up(); err != nil && err != migrate.ErrNoChange {
log.Fatal("failed to run migrate up", err)
}

log.Println("db migrate successfully")
}
36 changes: 0 additions & 36 deletions eks/deployment.yml

This file was deleted.

14 changes: 0 additions & 14 deletions eks/service.yml

This file was deleted.

74 changes: 0 additions & 74 deletions eksctl/cluster.yml

This file was deleted.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/validator/v10 v10.19.0
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/golang-migrate/migrate/v4 v4.17.0
github.com/google/uuid v1.4.0
github.com/lib/pq v1.10.9
github.com/spf13/viper v1.18.2
Expand All @@ -28,6 +29,8 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
Expand Down
Loading

0 comments on commit 34a528b

Please sign in to comment.