forked from cockroachlabs-field/crdb-geo-tourist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_k8s.sh
executable file
·129 lines (108 loc) · 4.62 KB
/
deploy_k8s.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
# 4 vCPU, 16 GB RAM, $0.134012/hour
MACHINETYPE="e2-standard-4"
NAME="${USER}-geo-tourist"
ZONE="us-east4-b"
N_NODES=5
# A MapBox "token" is required to show the base map
if [ -z $MAPBOX_TOKEN ]
then
echo
echo "Environment variable MAPBOX_TOKEN is not set."
echo "Please run 'export MAPBOX_TOKEN=\"your.mapbox.token\"' and then try running $0 again."
echo
exit 1
fi
dir=$( dirname $0 )
. $dir/include.sh
# 1. Create the GKE K8s cluster
echo "See https://www.cockroachlabs.com/docs/v20.2/orchestrate-cockroachdb-with-kubernetes#hosted-gke"
run_cmd gcloud container clusters create $NAME --zone=$ZONE --machine-type=$MACHINETYPE --num-nodes=$N_NODES
if [ "$y_n" = "y" ] || [ "$y_n" = "Y" ]
then
ACCOUNT=$( gcloud info | perl -ne 'print "$1\n" if /^Account: \[([^@]+@[^\]]+)\]$/' )
kubectl create clusterrolebinding cluster-admin-binding --clusterrole=cluster-admin --user=$ACCOUNT
fi
# 2. Create the CockroachDB cluster
echo "See https://www.cockroachlabs.com/docs/v20.2/orchestrate-cockroachdb-with-kubernetes"
echo "Apply the CustomResourceDefinition (CRD) for the Operator"
run_cmd kubectl apply -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/master/config/crd/bases/crdb.cockroachlabs.com_crdbclusters.yaml
echo "Apply the Operator manifest"
OPERATOR_YAML="https://raw.githubusercontent.com/cockroachdb/cockroach-operator/master/manifests/operator.yaml"
run_cmd kubectl apply -f $OPERATOR_YAML
echo "Validate that the Operator is running"
run_cmd kubectl get pods
echo "Initialize the cluster"
run_cmd kubectl apply -f ./cockroachdb.yaml
echo "Check that the pods were created"
run_cmd kubectl get pods
echo "WAIT until the output of 'kubectl get pods' shows the three cockroachdb-N nodes in 'Running' state"
run_cmd kubectl get pods
echo "After a couple of minutes, rerun this check"
run_cmd kubectl get pods
# 3. Add DB user for app
echo "Once all three show 'Running', use the SQL CLI to add a user for use by the Web app"
echo "Press ENTER to run this SQL"
read
cat ./create_user.sql | kubectl exec -i cockroachdb-2 -- ./cockroach sql --certs-dir cockroach-certs
# 4. Create table, index, and load data
echo "Create DB tables and load data (takes about 3 minutes)"
run_cmd kubectl apply -f ./data-loader.yaml
echo "Run 'kubectl get pods' periodically until the line for 'crdb-geo-loader' shows STATUS of 'Completed'"
run_cmd kubectl get pods
# 5. Start the CockroachDB DB Console
echo "First, we set up a tunnel from port $LOCAL_PORT on localhost to port 8080 on one of the CockroachDB pods"
port_fwd
URL="http://localhost:$LOCAL_PORT/"
run_cmd open $URL
echo "** Use 'tourist' as both login and password for this Admin UI **"
# 6. Start the Web app
echo "Press ENTER to start the CockroachDB Geo Tourist app"
read
envsubst < ./crdb-geo-tourist.yaml | kubectl apply -f -
# 7. Get the IP address of the load balancer
run_cmd kubectl describe service crdb-geo-tourist-lb
echo "Look for the external IP of the app in the 'LoadBalancer Ingress:' line of output"
sleep 30
run_cmd kubectl describe service crdb-geo-tourist-lb
echo "Once that IP is available, open the URL http://THIS_IP/ to see the app running"
echo
# 8. Perform an online rolling upgrade
echo "Perform a zero downtime upgrade of CockroachDB (note the version in the DB Console UI)"
run_cmd kubectl apply -f ./rolling_upgrade.yaml
echo "Check the DB Console to verify the version has changed"
echo
echo "If the DB Console becomes inaccessible, press ENTER to restart the port forwarding process"
read
port_fwd
# 9. Scale out: add a node
echo "Scale out by adding a new CockroachDB pod"
run_cmd kubectl apply -f ./scale_out.yaml
echo "Run 'kubectl get pods' a couple of times to verify 4 pods are running"
echo "Check the DB Console to verify the version has changed"
echo
# 10. Kill a node
echo "Kill a CockroachDB pod"
run_cmd kubectl delete pods cockroachdb-0
echo "Reload the app page to verify it continues to run"
echo "Also, note the state in the DB Console"
echo "A new pod should be started to replace the failed pod"
run_cmd kubectl get pods
# 11. Tear it down
echo
echo
echo "** Finally: tear it all down. CAREFUL -- BE SURE YOU'RE DONE! **"
echo "Press ENTER to confirm you want to TEAR IT DOWN."
read
echo "Deleting the Geo Tourist app"
kubectl delete -f ./crdb-geo-tourist.yaml
echo "Deleting the data loader app"
kubectl delete -f ./data-loader.yaml
echo "Deleting the CockroachDB cluster"
kubectl delete -f ./cockroachdb.yaml
echo "Deleting the persistent volumes and persistent volume claims"
kubectl delete pv,pvc --all
echo "Deleting the K8s operator"
kubectl delete -f $OPERATOR_YAML
echo "Deleting the GKE cluster"
gcloud container clusters delete $NAME --zone=$ZONE --quiet