Skip to content
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
32 changes: 27 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,37 @@ cluster_start:
k3d cluster start gobank-cluster

cluster_db:
kubectl apply -f postgres-cluster.yaml
kubectl apply -f ./k8s/apps/gobank/postgres-cluster.yaml --server-side

cluster_gobank_api:
kubectl apply -f gobank-api-deployment.yaml
kubectl apply -f ./k8s/apps/gobank/gobank-api-deployment.yaml --server-side

cluster_gobank:
k3d cluster create gobank-cluster --api-port 6550 -p "80:80@loadbalancer" -p "443:443@loadbalancer" --agents 2
apply_cluster_configs: # this is really hacky but we'll roll with it for now. If an issue arises, then rerun the command
kubectl apply -k ./k8s/base/cnpg-operator --server-side
sleep 20
kubectl wait --for condition=established --timeout=60s crd/clusters.postgresql.cnpg.io
kubectl apply -k ./k8s/apps/gobank --server-side

cluster:
k3d cluster create --config ./k3d-config.yaml

delete_cluster_gobank:
k3d cluster delete gobank-cluster

namespace:
kubectl apply -f k8s/apps/gobank/namespace.yaml

create_creds: namespace
@read -p "Enter DB Password: " pwd; \
kubectl create secret generic db-creds \
--from-literal=username=root \
--from-literal=password=$$pwd \
--namespace gobank \
--dry-run=client -o yaml | kubectl apply -f -

setup: create_creds apply_cluster_configs

db_tunnel:
kubectl port-forward svc/gobank-db-rw 5432:5432

.PHONY: postgres createdb dropdb migrateup migratedown sqlc test mock server migrateup1 migratedown1 cluster_db cluster_start cluster_stop cluster_gobank cluster_gobank_api db_tunnel
.PHONY: postgres createdb dropdb migrateup migratedown sqlc test mock server migrateup1 migratedown1 cluster_db cluster_start cluster_stop cluster cluster_gobank_api db_tunnel delete_cluster_gobank apply_cluster_configs namespace create_creds setup
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
Create gobank cluster via:
# Basic Commands

Create gobank cluster via:
```bash
make cluster
```

Delete gobank cluster via:
```bash
make delete_cluster_gobank
```

Make initial setup via:
```bash
make cluster_gobank
make setup
```

Create postgres database cluster credentials via:
```bash
kubectl create secret generic db-creds --from-literal=username=root --from-literal=password='YOUR_PASSWORD_HERE'
make create_creds
```
or manual via (make sure namespace exists first):
```bash
kubectl create secret generic db-creds --namespace gobank --from-literal=username=root --from-literal=password='YOUR_PASSWORD_HERE'
```

Create namespace via:
```bash
make namespace
```

Delete postgres database cluster credentials via:
Expand All @@ -30,6 +49,11 @@ Apply a deployment yaml file via:
kubectl apply -f PATH_TO_YAML
```

Apply all cluster configs:
```bash
make apply_cluster_configs
```

Create postgres database cluster via:
```bash
make cluster_db
Expand Down
Binary file removed file.txt
Binary file not shown.
17 changes: 17 additions & 0 deletions k3d-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: k3d.io/v1alpha5
kind: Simple
metadata:
name: gobank-cluster
servers: 1
agents: 2
image: rancher/k3s:v1.36.0-rc3-k3s1
kubeAPI:
hostIP: "0.0.0.0"
hostPort: "6550"
ports:
- port: 80:80
nodeFilters:
- loadbalancer
- port: 443:443
nodeFilters:
- loadbalancer
91 changes: 91 additions & 0 deletions k8s/apps/gobank/gobank-api-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: gobank-api
labels:
app: gobank-api
spec:
replicas: 2
selector:
matchLabels:
app: gobank-api
template:
metadata:
labels:
app: gobank-api
spec:
initContainers:
- name: wait-for-db
image: postgres:alpine
command:
- sh
- -c
- |
echo "Waiting for gobank-db-rw to be ready..."
until pg_isready -h gobank-db-rw -p 5432 -U $PGUSER; do
sleep 2
done
echo "Database is ready!"
env:
- name: PGUSER
valueFrom:
secretKeyRef:
name: db-creds
key: username
containers:
- name: gobank-api
image: ghcr.io/hypernaser/gobank:latest
ports:
- containerPort: 8080
resources:
requests:
cpu: "100m"
memory: "64Mi"
limits:
cpu: "500m"
memory: "128Mi"
env:
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: db-creds
key: password
- name: PGUSER
valueFrom:
secretKeyRef:
name: db-creds
key: username
- name: DB_SOURCE
value: "postgres://gobank-db-rw:5432/gobank?sslmode=disable"
---
# The Service (The Internal Load Balancer)
apiVersion: v1
kind: Service
metadata:
name: gobank-api-service
spec:
selector:
app: gobank-api
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
# The Ingress (The External Load Balancer)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: gobank-ingress
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: gobank-api-service
port:
number: 80
8 changes: 8 additions & 0 deletions k8s/apps/gobank/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: gobank

resources:
- namespace.yaml
- postgres-cluster.yaml
- gobank-api-deployment.yaml
4 changes: 4 additions & 0 deletions k8s/apps/gobank/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: gobank
29 changes: 29 additions & 0 deletions k8s/apps/gobank/postgres-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: gobank-db
spec:
instances: 2
imageName: ghcr.io/cloudnative-pg/postgresql:18.3

resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "500m"

bootstrap:
initdb:
database: gobank
owner: root
secret:
name: db-creds

storage:
size: 1Gi

affinity:
enablePodAntiAffinity: true
topologyKey: kubernetes.io/hostname
5 changes: 5 additions & 0 deletions k8s/base/cnpg-operator/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.29/releases/cnpg-1.29.0.yaml