-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathk8s_sql_connect.sh
More file actions
94 lines (93 loc) · 3.44 KB
/
k8s_sql_connect.sh
File metadata and controls
94 lines (93 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
# Script to quick automate and direct connect to an RDS instance using K8S pods
# Database credentials are stored in this script, DON'T SHARE IT
set -eo pipefail
function usage() {
echo ""
echo "Usage:"
echo " ./psql_k8s.sh -c [context] -i [instance]"
echo ""
echo "E.g. ./psql_k8s.sh -c staging -i pg-staging01"
echo ""
echo " -c, --context --> -i, --instance"
echo " 1. staging --> pg-staging01 | pg-staging02 | mysql-staging01"
echo " 2. production --> pg-production01 | pg-production02 | mysql-production01"
echo ""
echo " -h, --help This message"
echo ""
}
while [ -n "$1" ]; do
case $1 in
-i | --instance )
shift
instance=$1
;;
-c | --context )
shift
context=optional_prefix-$1 # I use "optional_prefix" because all my contexts start with the word "k8s_"
;;
-h | --help )
usage
exit 1
;;
esac
shift
done
# Sanity check
if [ -z "$instance" ]; then
echo "-i ERROR: AWS RDS instance is missing."
exit 1
fi
if [ -z "$context" ]; then
echo "-c ERROR: EKS K8S context is missing."
exit 1
fi
# set dba namespace
kubens dba
# staging
if [ "$context" == "optional_prefix-staging" ] && [[ "$instance" =~ ^(pg-staging01|mysql-staging01)$ ]]; then
kubectl config use-context $context
pod=$(kubectl get pod -o name | grep my-pod-) # I use "my-pod-" because all my pods start with this word
if [ "$instance" == "pg-staging01" ] ; then
kubectl exec -it $pod -- env PGPASSWORD="**********" psql -h $instance.qwerty.us-east-1.rds.amazonaws.com -U pg_user -d db_name
# PGPASSWORD is not recommended, please use .pgpass or .pg_service.conf
exit 1
fi
if [ "$instance" == "pg-staging02" ] ; then
kubectl exec -it $pod -- env PGPASSWORD="**********" psql -h $instance.zxcvbn.us-east-1.rds.amazonaws.com -U pg_user -d db_name
# PGPASSWORD is not recommended, please use .pgpass or .pg_service.conf
exit 1
fi
if [ "$instance" == "mysql-staging01" ] ; then
kubectl exec -it $pod -- mysql -h $instance.asdfgh.us-east-1.rds.amazonaws.com -u my_user -p"**********" db_name
exit 1
fi
# production
elif [ "$context" == "optional_prefix-production" ] && [[ "$instance" =~ ^(pg-production01|pg-production02|mysql-production01)$ ]]; then
kubectl config use-context $context
pod=$(kubectl get pod -o name | grep my-pod-)
if [ "$instance" == "pg-production01" ] ; then
kubectl exec -it $pod -- env PGPASSWORD="**********" psql -h $instance.zxcvbn.us-east-1.rds.amazonaws.com -U pg_user -d db_name
exit 1
fi
if [ "$instance" == "pg-production02" ] ; then
kubectl exec -it $pod -- env PGPASSWORD="**********" psql -h $instance.tyuiop.us-east-1.rds.amazonaws.com -U pg_user -d db_name
exit 1
fi
if [ "$instance" == "mysql-production01" ] ; then
kubectl exec -it $pod -- mysql -h $instance.fghjkl.us-east-1.rds.amazonaws.com -u my_user -p"**********" db_name
exit 1
fi
else
echo "ERROR: RDS instance "$instance" or K8S context $context not found"
exit 1
fi