Skip to content

Commit

Permalink
fix(#28): enable yugabyte deployment sandbox by adjusting script
Browse files Browse the repository at this point in the history
- separate out gorm database creation
- add some notes about working with yugabyte
  • Loading branch information
saylerb committed Apr 19, 2024
1 parent 61f1d9b commit e6ba323
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
6 changes: 4 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ jobs:
- image: *executor-image
steps:
- checkout
- setup_remote_docker
- set-environment:
cluster: << parameters.cluster >>
source-env: << parameters.source-env >>
- set-kubeconfig:
cluster: << parameters.cluster >>
- run:
name: deploy yugabyte
command: bash scripts/yuga-single-az.sh << parameters.region>> << parameters.namespace >>
command: bash scripts/yuga-single-az.sh << parameters.region>> << parameters.namespace >> prod
- run:
name: create db
command: bash scripts/create-gorm-db.sh << parameters.namespace >>

golang-static:
executor: docker-golang-executor
Expand Down
48 changes: 48 additions & 0 deletions scripts/create-gorm-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
set -e

export NAMESPACE=$1

if [ -z "$NAMESPACE" ]; then
echo "Usage: $0 <namespace>"
exit 1
fi

hostname="yb-tserver-0.yb-tservers.$NAMESPACE"
sleep_seconds=5
retries=24
attempt=1

check_connectivity() {
echo "Checking DB Connectivity for $1"
if kubectl exec --namespace "${NAMESPACE}" -it yb-tserver-0 -c yb-tserver -- ysqlsh -h "$1" -c '\q'; then
echo "Connection successful"
return 0
else
echo "Connection failed"
return 1
fi
}

while [ $attempt -le $retries ]; do
if check_connectivity "$hostname"; then
break
fi
echo "Attempt $attempt of $retries failed."
if [ $attempt -eq $retries ]; then
echo "All attempts failed. Exiting..."
exit 1
fi
sleep $sleep_seconds
((attempt++))
done

result=$(kubectl exec -n "${NAMESPACE}" -it yb-tserver-0 -c yb-tserver -- ysqlsh -U yugabyte -h "$hostname" -tc "SELECT 1 FROM pg_database WHERE datname = 'gorm'")
exists=$(echo "$result" | tr -d '[:space:]')

if [[ "$exists" == "1" ]]; then
echo "Database gorm already exists"
else
echo "Creating database gorm"
kubectl exec -n "${NAMESPACE}" -it yb-tserver-0 -c yb-tserver -- ysqlsh -U yugabyte -h "$hostname" -c "CREATE DATABASE gorm"
fi
9 changes: 3 additions & 6 deletions scripts/yuga-single-az.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/usr/bin/env bash
set -e

# Note: You will only be able to use this script when you have 3 AZs in your cluster.
# Meaning you must have at least one node in each AZ (a, b, c).

export AWS_DEFAULT_REGION=$1
export NAMESPACE=$2
export ENV=${3:-prod}

cat <<EOF > yugabyte/yugabyte.yaml
enableLoadBalancer: False
storage:
master:
storageClass: "prod-${AWS_DEFAULT_REGION}-ebs-storage-class"
storageClass: "${ENV}-${AWS_DEFAULT_REGION}-ebs-storage-class"
tserver:
storageClass: "prod-${AWS_DEFAULT_REGION}-ebs-storage-class"
storageClass: "${ENV}-${AWS_DEFAULT_REGION}-ebs-storage-class"
replicas:
master: 1
tserver: 1
Expand Down Expand Up @@ -53,4 +51,3 @@ helm upgrade --install yugabyte yugabytedb/yugabyte \
# placement_region: "${AWS_DEFAULT_REGION}"
# placement_zone: "${AWS_DEFAULT_REGION}a"

kubectl exec -n ${NAMESPACE} -it yb-tserver-0 -- ysqlsh -c 'SELECT 'CREATE DATABASE gorm' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'gorm')\gexec'
15 changes: 15 additions & 0 deletions yugabyte/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Yugabyte deployment

This directory holds the output of templating the yugabyte helm chart values
from the script `scripts/yugabyte-*.sh`

The `yugabyte-*.sh` scripts are used to generate the `yugabyte.yaml` file
which is then used to deploy yugabyte on the kubernetes cluster.

## Steps to deploy yugabyte on kubernetes manually

For example, if experimenting with our sandbox cluster:

1. Run the script `scripts/yugabyte-single-az.sh us-east-2 $NAMESPACE sandbox`
2. check for connectivity `kubectl exec --namespace $NAMESPACE -it yb-tserver-0 -- /home/yugabyte/bin/ysqlsh -h yb-tserver-0.yb-tservers.$NAMESPACE`
3. Optionally, create the gorm database `scripts/create-gorm-db.sh $NAMESPACE`

0 comments on commit e6ba323

Please sign in to comment.