-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdeploy_k8s.sh
executable file
·140 lines (109 loc) · 5.13 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
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# https://www.cockroachlabs.com/docs/stable/deploy-cockroachdb-with-kubernetes.html
MACHINETYPE="e2-standard-4" # 4 vCPU, 16 GB RAM, $0.134012/hour
N_NODES=2 # This will create N_NODES *per AZ* within REGION
REGION="us-east4"
NAME="${USER}-geo-tourist"
dir=$( dirname $0 )
. $dir/include.sh
# Create the GKE K8s cluster
export USE_GKE_GCLOUD_AUTH_PLUGIN=True
echo "See https://www.cockroachlabs.com/docs/v21.1/orchestrate-cockroachdb-with-kubernetes.html#hosted-gke"
run_cmd gcloud container clusters create $NAME --region=$REGION --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 $USER-cluster-admin-binding --clusterrole=cluster-admin --user=$ACCOUNT
fi
# Create the CockroachDB cluster
echo "See https://www.cockroachlabs.com/docs/stable/deploy-cockroachdb-with-kubernetes.html"
echo "Apply the CustomResourceDefinition (CRD) for the Operator"
run_cmd kubectl apply -f https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.10.0/install/crds.yaml
echo "Apply the Operator manifest"
OPERATOR_YAML="./operator.yaml"
curl https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.10.0/install/operator.yaml | sed 's/namespace: cockroach-operator-system/namespace: default/g' > $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 $dir/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"
echo "(This could take upwards of 5 minutes)"
run_cmd kubectl get pods
echo "Check to see whether the LB for DB Console and SQL is ready yet"
echo "Look for the external IP of the app in the 'LoadBalancer Ingress:' line of output"
run_cmd kubectl describe service crdb-lb
echo "If not, run 'kubectl describe service crdb-lb' in a separate window"
# Deploy a SQL client
#SQL_CLIENT_YAML="https://raw.githubusercontent.com/cockroachdb/cockroach-operator/master/examples/client-secure-operator.yaml"
SQL_CLIENT_YAML="https://raw.githubusercontent.com/cockroachdb/cockroach-operator/v2.10.0/examples/client-secure-operator.yaml"
echo "Adding a secure SQL client pod ..."
kubectl create -f $SQL_CLIENT_YAML
echo "Done"
echo "Verify the 'cockroachdb-client-secure' is in 'Running' state"
kubectl get pods
sleep 5
kubectl get pods
# Add DB user for app
echo "Once all three DB pods 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 $dir/create_user.sql | kubectl exec -i cockroachdb-client-secure -- ./cockroach sql --certs-dir=/cockroach/cockroach-certs --host=cockroachdb-public
# Create table, index, and load data
echo "Create DB tables and load data (takes about 3 minutes)"
run_cmd kubectl apply -f $dir/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
# Start the CockroachDB DB Console
echo "Open a browser tab to port 8080 at the IP provided for the DB Console endpoint"
echo "** Use 'tourist' as both login and password **"
# Start the Web app
echo "Press ENTER to start the CockroachDB Geo Tourist app"
read
kubectl apply -f $dir/boot-crdb-geo-tourist.yaml
# 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
# 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
# Scale out by adding a 4th node
echo "Scale out by adding a fourth node"
run_cmd kubectl apply -f $dir/scale_out.yaml
sleep 2
run_cmd kubectl get pods
# 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 $dir/rolling_upgrade.yaml
echo "Check the DB Console to verify the version has changed"
echo
# 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 $dir/crdb-geo-tourist.yaml
echo "Deleting the data loader app"
kubectl delete -f $dir/data-loader.yaml
echo "Deleting the SQL client"
kubectl delete -f $SQL_CLIENT_YAML
echo "Deleting the CockroachDB cluster"
kubectl delete -f $dir/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"
run_cmd gcloud container clusters delete $NAME --region=$REGION --quiet