diff --git a/Makefile b/Makefile index 9eaf60f..f59b69b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index e08912d..db79095 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 diff --git a/file.txt b/file.txt deleted file mode 100644 index 48bccc4..0000000 Binary files a/file.txt and /dev/null differ diff --git a/k3d-config.yaml b/k3d-config.yaml new file mode 100644 index 0000000..07d9504 --- /dev/null +++ b/k3d-config.yaml @@ -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 \ No newline at end of file diff --git a/k8s/apps/gobank/gobank-api-deployment.yaml b/k8s/apps/gobank/gobank-api-deployment.yaml new file mode 100644 index 0000000..060fdbd --- /dev/null +++ b/k8s/apps/gobank/gobank-api-deployment.yaml @@ -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 \ No newline at end of file diff --git a/k8s/apps/gobank/kustomization.yaml b/k8s/apps/gobank/kustomization.yaml new file mode 100644 index 0000000..6b992fe --- /dev/null +++ b/k8s/apps/gobank/kustomization.yaml @@ -0,0 +1,8 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: gobank + +resources: + - namespace.yaml + - postgres-cluster.yaml + - gobank-api-deployment.yaml \ No newline at end of file diff --git a/k8s/apps/gobank/namespace.yaml b/k8s/apps/gobank/namespace.yaml new file mode 100644 index 0000000..6594277 --- /dev/null +++ b/k8s/apps/gobank/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: gobank \ No newline at end of file diff --git a/k8s/apps/gobank/postgres-cluster.yaml b/k8s/apps/gobank/postgres-cluster.yaml new file mode 100644 index 0000000..efe00e6 --- /dev/null +++ b/k8s/apps/gobank/postgres-cluster.yaml @@ -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 \ No newline at end of file diff --git a/k8s/base/cnpg-operator/kustomization.yaml b/k8s/base/cnpg-operator/kustomization.yaml new file mode 100644 index 0000000..5bc3772 --- /dev/null +++ b/k8s/base/cnpg-operator/kustomization.yaml @@ -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 \ No newline at end of file